roc,腾讯工程师,负责腾讯云TKE的售中、售后的技术支持,根据客户需求输出合理技术方案与最佳实践,为客户业务保驾护航。
Kubernetes 官方提供了 NodePort 类型的 Service,即给所有节点开一个相同端口用于暴露这个 Service,大多云上 LoadBalancer 类型 Service 的传统实现也都基于 NodePort,即 LB 后端绑各节点的 NodePort,LB 接收外界流量,转发到其中一个节点的 NodePort 上,再通过 Kubernetes 内部的负载均衡,使用 iptables 或 ipvs 转发到 Pod:
TKE 默认的 LoadBalancer 类型 Service 与默认的 Ingress 也都是这样实现的,但目前也支持了 LB 直通 Pod 的方式,即 LB 后端直接绑 Pod IP+Port,不绑节点的 NodePort:
LB 直接绑 NodePort 来实现云上的 Ingress 或 LoadBalancer 类型 Service 是最简单通用的方法,那为什么有了这种实现还不够,还要搞个 LB 直通 Pod 的模式?
首先,我们分析下传统 NodePort 实现方式存在的一些问题:
如果使用 LB 直通 Pod 的方式,以上问题都将消失,并且还有一些其它好处:
externalTrafficPolicy: Local
。sessionAffinity
。所以使用 LB 直通 Pod 的场景通常有:
externalTrafficPolicy: Local
的方式。使用 LB 直通 Pod,需要满足以下前提条件:
Kubernetes
集群版本需要高于 1.12,因为 LB 直绑 Pod,检查 Pod 是否 Ready,除了看 Pod 是否 Running、是否通过 readinessProbe 外, 还需要看 LB 对 Pod 的健康探测是否通过,这依赖于 ReadinessGate
特性,该特性在 Kubernetes 1.12 才开始支持。VPC-CNI
弹性网卡模式,因为目前 LB 直通 Pod 的实现是基于弹性网卡的,普通的网络模式暂时不支持,这个在未来将会支持。由于目前 LB 直通 Pod 依赖 VPC-CNI,需要保证 Pod 使用了弹性网卡:
tke.cloud.tencent.com/networks: tke-route-eni
这个 annotation 来声明使用弹性网卡,并且为其中一个容器加上 tke.cloud.tencent.com/eni-ip: "1"
这样的 requests 与 limits,示例:apiVersion: apps/v1kind: Deploymentmetadata: labels: app: nginx name: nginx-deployment-enispec: replicas: 3 selector: matchLabels: app: nginx template: metadata: annotations: tke.cloud.tencent.com/networks: tke-route-eni labels: app: nginx spec: containers: - image: nginx name: nginx resources: requests: tke.cloud.tencent.com/eni-ip: "1" limits: tke.cloud.tencent.com/eni-ip: "1"
当你用 LoadBalancer 的 Service 暴露服务时,需要声明使用直连模式:
采用负载均衡直连Pod模式
:
service.cloud.tencent.com/direct-access: "true"
的 annotation:apiVersion: v1kind: Servicemetadata: annotations: service.cloud.tencent.com/direct-access: "true" labels: app: nginx name: nginx-service-enispec: externalTrafficPolicy: Cluster ports: - name: 80-80-no port: 80 protocol: TCP targetPort: 80 selector: app: nginx sessionAffinity: None type: LoadBalancer
当使用 Ingress 暴露服务时,同样也需要声明使用直连模式:
采用负载均衡直连Pod模式
:
ingress.cloud.tencent.com/direct-access: "true"
的 annotation:apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata: annotations: ingress.cloud.tencent.com/direct-access: "true" kubernetes.io/ingress.class: qcloud name: test-ingress namespace: defaultspec: rules: - http: paths: - backend: serviceName: nginx servicePort: 80 path: /
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有