一、支持版本
| 功能场景 | 最低版本要求 |
| Service | Service Controller v2.4.0+ |
| Ingress | Ingress Controller v2.6.0+ |
二、使用前提
Pod 自定义权重功能必须在 直连 Pod 模式 下使用。
三、基础用法
在 Pod 上添加对应的注解即可:
apiVersion: v1kind: Podmetadata: name: my-pod annotations: # Ingress 场景使用这个注解 ingress.cloud.tencent.com/custom-weight: "50" # Service 场景使用这个注解 service.cloud.tencent.com/custom-weight: "50"spec: containers: – name: app image: nginx:latest
注意:
·Ingress 场景:使用 ingress.cloud.tencent.com/custom-weight
·Service 场景:使用 service.cloud.tencent.com/custom-weight
·如果 Pod 应用同时使用 Ingress 和 Service,可以分别配置两个注解。
四、示例1:Ingress 灰度发布
小比例流量验证新版本:
# 稳定版本apiVersion: apps/v1kind: Deploymentmetadata: name: stable-versionspec: replicas: 3 selector: matchLabels: app: demo version: v1 template: metadata: labels: app: demo version: v1 annotations: ingress.cloud.tencent.com/custom-weight: "10" # 默认权重 spec: containers: – name: app image: demo:v1.0 ports: – containerPort: 8080—# 新版本(金丝雀)apiVersion: apps/v1kind: Deploymentmetadata: name: canary-versionspec: replicas: 1 selector: matchLabels: app: demo version: v2 template: metadata: labels: app: demo version: v2 annotations: ingress.cloud.tencent.com/custom-weight: "1" # 低权重,小流量 spec: containers: – name: app image: demo:v2.0 ports: – containerPort: 8080—# ClusterIP 类型的 ServiceapiVersion: v1kind: Servicemetadata: name: demo-servicespec: type: ClusterIP selector: app: demo ports: – port: 80 targetPort: 8080—# Ingress(开启直连模式,CLB 后端直接为 Pod)apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: demo-ingress annotations: ingress.cloud.tencent.com/direct-access: "true" # 开启直连,CLB 后端直接为 Podspec: rules: – host: demo.example.com http: paths: – path: / pathType: Prefix backend: service: name: demo-service port: number: 80
五、示例2:Service 负载均衡
为 Service 的 Pod 配置不同权重:
apiVersion: v1kind: Servicemetadata: name: my-service annotations: # 必须开启直连访问 service.cloud.tencent.com/direct-access: "true"spec: type: LoadBalancer selector: app: my-app ports: – port: 80 targetPort: 8080—# Pod 1:高权重apiVersion: v1kind: Podmetadata: name: pod-1 labels: app: my-app annotations: service.cloud.tencent.com/custom-weight: "50"spec: containers: – name: app image: my-app:latest—# Pod 2:低权重apiVersion: v1kind: Podmetadata: name: pod-2 labels: app: my-app annotations: service.cloud.tencent.com/custom-weight: "10"spec: containers: – name: app image: my-app:latest
六、示例3:临时下线 Pod
设置权重为 0 进行维护:
apiVersion: v1kind: Podmetadata: name: maintenance-pod annotations: ingress.cloud.tencent.com/custom-weight: "0" service.cloud.tencent.com/custom-weight: "0"spec: containers: – name: app image: my-app:latest
七、示例4:同时配置 Ingress 和 Service
apiVersion: v1kind: Podmetadata: name: multi-scenario-pod annotations: # Ingress 流量权重 ingress.cloud.tencent.com/custom-weight: "50" # Service 流量权重 service.cloud.tencent.com/custom-weight: "20"spec: containers: – name: app image: my-app:latest
八、流量计算规则
直连模式下:
单个 Pod 流量比例 = (单个 Pod 权重 / 所有 Pod 权重之和) × 100%
示例(直连模式):
·Pod A(权重 10)、Pod B(权重 5)、Pod C(权重 5)
·Pod A 流量:10/(10+5+5) = 50%
·Pod B 流量:5/(10+5+5) = 25%
·Pod C 流量:5/(10+5+5) = 25%