apiVersion: v1 kind: Service metadata: labels: app.kubernetes.io/component: controller app.kubernetes.io/instance: ingress-nginx app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx app.kubernetes.io/version: 1.6.4 name: ingress-nginx-controller namespace: ingress-nginx spec: externalTrafficPolicy: Local ipFamilies: - IPv4 ipFamilyPolicy: SingleStack ports: - appProtocol: http name: http port: 80 protocol: TCP targetPort: http - appProtocol: https name: https port: 443 protocol: TCP targetPort: https selector: app.kubernetes.io/component: controller app.kubernetes.io/instance: ingress-nginx app.kubernetes.io/name: ingress-nginx type: NodePort 指定targetPort
时间: 2024-03-07 19:52:48 浏览: 162
在上述配置中,targetPort 是用来指定 Service 要代理的后端 Pod 的端口号。在这个例子中,Service 会将请求代理到后端 Pod 的 80 端口(targetPort: http)或 443 端口(targetPort: https)。这里的 targetPort 要与后端 Pod 中监听的端口一致,如果不一致,请求就无法到达后端 Pod。
相关问题
基于 k8s 采用 ingress 对外暴露一个 nginx 服务,使用域名:nginx.k8s.com. nginx 镜像:swr.cn-north-4.myhuaweicloud.com/ddn-k8s/ghcr.io/mailu/nginx:2024.06
在Kubernetes (k8s)环境中,要基于Nginx镜像创建一个外部负载均衡服务并使用自定义域名`nginx.k8s.com`,可以按照以下步骤操作:
1. **创建部署**:
使用`Deployment`定义Nginx应用,并指定镜像来源。例如,如果镜像存储在华为云私有仓库,你可以这样做:
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: swr.cn-north-4.myhuaweicloud.com/ddn-k8s/ghcr.io/mailu/nginx:2024.06
ports:
- containerPort: 80
```
2. **创建Service**:
创建一个`Service`资源,指定`type`为`LoadBalancer`或`Ingress`,以便将流量路由到集群内部的服务。这里假设是Ingress:
```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
# 添加指向外部域名的路由规则
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/server-tls: "off"
# 设置域名映射
nginx.ingress.kubernetes.io host: "nginx.k8s.com"
spec:
rules:
- host: "nginx.k8s.com"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-deployment
port:
number: 80
```
3. **配置DNS**:
更新或注册`nginx.k8s.com`作为域名解析的A记录,指向你创建的Ingress的IP地址。
4. **验证**:
等待Ingress创建成功后,可以从外部通过`http://nginx.k8s.com`访问你的Nginx服务。
阅读全文