注意:ingress-nginx 是一个由社区维护的开源项目。这篇内容仅供参考,不提供支持。如在使用过程中遇到问题,建议查阅 ingress-nginx 社区 的最新产品文档以获取帮助。
一、背景
这里说明将 Nginx Ingress 所使用的 CLB 监听器改为七层监听器。
二、使用 specify-protocol 注解
TKE 的 Service 支持使用 service.cloud.tencent.com/specify-protocol 这个注解来修改 CLB 监听器协议,详情请参见 Service 扩展协议。
values.yaml 配置示例:
controller: service: annotations: service.cloud.tencent.com/specify-protocol: | { "80": { "protocol": [ "HTTP" ], "hosts": { "a.example.com": {}, "b.example.com": {} } }, "443": { "protocol": [ "HTTPS" ], "hosts": { "a.example.com": { "tls": "cert-secret-a" }, "b.example.com": { "tls": "cert-secret-b" } } } }
·实际使用的 Ingress 规则中涉及的域名,也需要在注解中的 hosts 字段进行配置。
·HTTPS 监听器需要证书,先在 我的证书 中创建好证书,然后在 TKE 集群中创建 Secret(需在 Nginx Ingress 所在的命名空间),Secret 的 Key 为 qcloud_cert_id,Value 为对应的证书 ID,然后在注解里引用 secret 名称。
·targetPorts 需要将 https 端口指向 nginx ingress 的 80 (http),避免 CLB 的 443 流量转到 nginx ingress 的 443 端口(会导致双重证书,转发失败)。
·不需要 HTTP 流量可以将 enableHttp 置为 false。
注意:如果需要将 HTTP 的流量重定向到 HTTPS,可以在 CLB 控制台找到 nginx ingress 使用的 CLB 实例(实例 ID 可通过查看 nginx ingress controller 的 service 的 yaml 获取),在实例页面手动配置下重定向规则:
注意:
三、操作步骤
1. 在 我的证书 里上传证书并复制证书 ID。
2. 在 nginx ingress 所在 namespace 创建对应的证书 secret(引用证书 ID):
apiVersion: v1kind: Secretmetadata: name: cert-secret-test namespace: ingress-nginxstringData: # 用 stringData 就不需要手动 base64 转码 # highlight-next-line qcloud_cert_id: E2pcp0Fytype: Opaque
3. 配置 values.yaml:
controller: # 以下配置将依赖镜像替换为了 docker hub 上的 mirror 镜像以保证在国内环境能正常拉取 image: registry: docker.io image: k8smirror/ingress-nginx-controller admissionWebhooks: patch: image: registry: docker.io image: k8smirror/ingress-nginx-kube-webhook-certgen defaultBackend: image: registry: docker.io image: k8smirror/defaultbackend-amd64 opentelemetry: image: registry: docker.io image: k8smirror/ingress-nginx-opentelemetry service: enableHttp: false targetPorts: https: http annotations: service.cloud.tencent.com/specify-protocol: | { "80": { "protocol": [ "HTTP" ], "hosts": { "test.example.com": {} } }, "443": { "protocol": [ "HTTPS" ], "hosts": { "test.example.com": { "tls": "cert-secret-test" } } } }
4. 安装或更新配置:
helm upgrade –install ingress-nginx ingress-nginx/ingress-nginx \\ –namespace ingress-nginx –create-namespace \\ -f values.yaml
5. 如果需要,将 HTTP 自动重定向到 HTTPS,在 CLB 控制台 点击实例 ID > 重定向配置 > 新建重定向配置:
6. 部署测试应用和 Ingress 规则:
apiVersion: v1kind: Servicemetadata: labels: app: nginx name: nginxspec: ports: – port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: NodePort—apiVersion: apps/v1kind: Deploymentmetadata: name: nginxspec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: – image: nginx:latest name: nginx—apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: nginxspec: ingressClassName: nginx rules: – host: test.example.com http: paths: – backend: service: name: nginx port: number: 80 path: / pathType: Prefix
7. 配置 hosts 或域名解析后,测试功能是否正常:
四、配置 WAF
Nginx Ingress 配置好后,确认对应的 CLB 监听器已经改为了 HTTP/HTTPS,则已满足 Nginx Ingress 接入 WAF 的前提条件,可以根据 WAF 产品文档 的指引来进行配置,最终完成 Nginx Ingress 的 WAF 接入。