文档中心>容器服务>实践教程>网络>配置 Pod 级别的 CLB 后端权重

配置 Pod 级别的 CLB 后端权重

最近更新时间:2026-02-28 16:57:52

我的收藏

简介

通过在 Pod 上添加注解,您可以精细控制该 Pod 在 CLB 上作为后端 RS 的流量权重。该功能适用于 Ingress 和 Service 两种场景。

支持版本

功能场景
最低版本要求
Service
Service Controller v2.4.0+
Ingress
Ingress Controller v2.6.0+

使用前提

Pod 自定义权重功能必须在 直连 Pod 模式 下使用。

快速开始

基础用法

在 Pod 上添加对应的注解即可:
apiVersion: v1
kind: Pod
metadata:
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/v1
kind: Deployment
metadata:
name: stable-version
spec:
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/v1
kind: Deployment
metadata:
name: canary-version
spec:
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 类型的 Service
apiVersion: v1
kind: Service
metadata:
name: demo-service
spec:
type: ClusterIP
selector:
app: demo
ports:
- port: 80
targetPort: 8080
---
# Ingress(开启直连模式,CLB 后端直接为 Pod)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-ingress
annotations:
ingress.cloud.tencent.com/direct-access: "true" # 开启直连,CLB 后端直接为 Pod
spec:
rules:
- host: demo.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: demo-service
port:
number: 80

示例2:Service 负载均衡

为 Service 的 Pod 配置不同权重:
apiVersion: v1
kind: Service
metadata:
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: v1
kind: Pod
metadata:
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: v1
kind: Pod
metadata:
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: v1
kind: Pod
metadata:
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: v1
kind: Pod
metadata:
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%