简介
通过在 Pod 上添加注解,您可以精细控制该 Pod 在 CLB 上作为后端 RS 的流量权重。该功能适用于 Ingress 和 Service 两种场景。
支持版本
功能场景 | 最低版本要求 |
Service | Service Controller v2.4.0+ |
Ingress | Ingress Controller v2.6.0+ |
使用前提
快速开始
基础用法
在 Pod 上添加对应的注解即可:
apiVersion: v1kind: Podmetadata:name: my-podannotations:# Ingress 场景使用这个注解ingress.cloud.tencent.com/custom-weight: "50"# Service 场景使用这个注解service.cloud.tencent.com/custom-weight: "50"spec:containers:- name: appimage: nginx:latest
注意:
Ingress 场景:使用
ingress.cloud.tencent.com/custom-weightService 场景:使用
service.cloud.tencent.com/custom-weight如果 Pod 应用同时使用 Ingress 和 Service,可以分别配置两个注解。
使用示例
示例1:Ingress 灰度发布
小比例流量验证新版本:
# 稳定版本apiVersion: apps/v1kind: Deploymentmetadata:name: stable-versionspec:replicas: 3selector:matchLabels:app: demoversion: v1template:metadata:labels:app: demoversion: v1annotations:ingress.cloud.tencent.com/custom-weight: "10" # 默认权重spec:containers:- name: appimage: demo:v1.0ports:- containerPort: 8080---# 新版本(金丝雀)apiVersion: apps/v1kind: Deploymentmetadata:name: canary-versionspec:replicas: 1selector:matchLabels:app: demoversion: v2template:metadata:labels:app: demoversion: v2annotations:ingress.cloud.tencent.com/custom-weight: "1" # 低权重,小流量spec:containers:- name: appimage: demo:v2.0ports:- containerPort: 8080---# ClusterIP 类型的 ServiceapiVersion: v1kind: Servicemetadata:name: demo-servicespec:type: ClusterIPselector:app: demoports:- port: 80targetPort: 8080---# Ingress(开启直连模式,CLB 后端直接为 Pod)apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: demo-ingressannotations:ingress.cloud.tencent.com/direct-access: "true" # 开启直连,CLB 后端直接为 Podspec:rules:- host: demo.example.comhttp:paths:- path: /pathType: Prefixbackend:service:name: demo-serviceport:number: 80
示例2:Service 负载均衡
为 Service 的 Pod 配置不同权重:
apiVersion: v1kind: Servicemetadata:name: my-serviceannotations:# 必须开启直连访问service.cloud.tencent.com/direct-access: "true"spec:type: LoadBalancerselector:app: my-appports:- port: 80targetPort: 8080---# Pod 1:高权重apiVersion: v1kind: Podmetadata:name: pod-1labels:app: my-appannotations:service.cloud.tencent.com/custom-weight: "50"spec:containers:- name: appimage: my-app:latest---# Pod 2:低权重apiVersion: v1kind: Podmetadata:name: pod-2labels:app: my-appannotations:service.cloud.tencent.com/custom-weight: "10"spec:containers:- name: appimage: my-app:latest
示例3:临时下线 Pod
设置权重为 0 进行维护:
apiVersion: v1kind: Podmetadata:name: maintenance-podannotations:ingress.cloud.tencent.com/custom-weight: "0"service.cloud.tencent.com/custom-weight: "0"spec:containers:- name: appimage: my-app:latest
示例4:同时配置 Ingress 和 Service
apiVersion: v1kind: Podmetadata:name: multi-scenario-podannotations:# Ingress 流量权重ingress.cloud.tencent.com/custom-weight: "50"# Service 流量权重service.cloud.tencent.com/custom-weight: "20"spec:containers:- name: appimage: 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%