You need to enable JavaScript to run this app.
文档中心
容器服务

容器服务

复制全文
下载 pdf
自定义转发规则配置示例
配置转发至多个后端服务
复制全文
下载 pdf
配置转发至多个后端服务
ALB Ingress 支持在配置转发规则时,配置多个后端服务器组,允许指定集群中的 Service 或已存在的后端服务器组。当请求经过 ALB 时,ALB 可以按照权重向不同后端服务器组转发。​
注意​
配置 ALB Ingress 前,请务必仔细阅读 ALB Ingress 使用注意事项。​
前提条件​
  • 已安装 alb-ingress-controller 组件,并升级至 v0.35.0 及以上版本。详情请参见 升级组件。​
操作步骤​
通过控制台配置​
说明​
仅对接 ALB 标准版时支持使用控制台配置,如需对接 ALB 基础版,请参见下文中的 YAML 配置。​
  1. 单击左侧导航栏中的 集群。​
  1. 在集群列表页面,单击需要配置路由规则的目标集群。​
  1. 在集群管理页面的左侧导航栏中,选择 服务与路由 > 路由规则,单击 创建路由规则。​
  • 在 Ingress 类型 配置项中,选择 应用型负载均衡 ALB。​
  • 在 自定义转发规则 配置项中,勾选 启用,开启自定义转发规则,并配置转发规则详情。​
​​​
​
配置项​
说明​
转发条件​
配置转发条件,包括 域名、路径 和 HTTP 标头。​
  • 转发条件允许单独配置,也可以组合配置。当配置多个转发条件组合时,这些转发条件之间是 且 的关系。例如,假设您在一条转发规则中同时配置了域名和路径,只有同时匹配指定域名和路径的请求,才会匹配这条转发规则。​
  • 同一转发条件下允许配置多个值,这些值之间是 或 的关系。例如,假设您在域名转发条件下配置了多个值,当请求的域名匹配其中任何一个值时,即满足该转发条件。​
转发动作​
配置请求被转发到的目标服务和端口。本例中选择 转发至 动作。​
  • 服务名称:选择集群中已存在的服务名称。若无可用服务,请参考 服务 创建服务。​
  • 端口:选择对应服务的端口号。​
  • 权重:配置对应服务的权重,取值范围为 0~100。​
  • 当服务器组数量为 1 时,默认权重为 100。​
  • 当服务器组的权重设为 0 时,表示 ALB 不会转发客户端流量至该服务器组。​
说明​
当配置多个服务时,多个服务权重之和不要求为 100。ALB 能够根据每个服务的权重,按比例将客户端流量转发至对应的服务。详情请参见 服务器组的权重。​
​
  1. 单击 确定,完成配置。​
通过 kubectl 命令行配置​
ALB Ingress 支持通过 Annotation 指定多个后端服务器组。对接 ALB 标准版和基础版时的配置略有不同。​
​
ALB 标准版
ALB 基础版
​
ALB 标准版的配置示例和注解说明,如下所示:​
注意​
注解中的{{service-name}}字段必须与spec.rules中配置的后端服务名称对应一致,例如在本例中,正确的注解为ingress.vke.volcengine.com/loadbalancer-actions.service-demo。​
​
apiVersion: networking.k8s.io/v1​
kind: Ingress​
metadata:​
name: alb-ingress # 自定义路由规则名称​
namespace: default # 路由规则所属的命名空间​
annotations:​
ingress.vke.volcengine.com/loadbalancer-actions.{{service-name}}: | ​
[{​
"type": "ForwardGroup",​
"forwardGroupConfig": {​
"serverGroupStickySession": {​
"enabled": "on",​
"timeout": 100​
},​
"backends": [​
{​
"serviceName": "service-demo-a",​
"weight": 80,​
"servicePort": 80​
},​
{​
"serviceName": "service-demo-b",​
"weight": 20,​
"servicePort": 80​
}​
]​
}​
}]​
spec:​
ingressClassName: alb​
rules:​
- host: example.com​
http:​
paths:​
- pathType: Prefix​
path: /​
backend:​
service:​
name: service-demo # 服务名称,注意需要和注解 loadbalancer-actions.{{service-name}} 里的 {{service-name}} 一致​
port:​
name: use-annotation # 固定值,配置多个后端服务器组时,port.name 必须为 use-annotation​
​
ingress.vke.volcengine.com/loadbalancer-actions.{{service-name}}注解中的参数说明如下表所示。​
​
字段​
类型​
描述​
默认值​
type​
String​
自定义转发规则的动作类型,当配置转发至多个后端服务器组时,必须配置为ForwardGroup。​
无​
forwardGroupConfig​
Object of ​
StickySessionConfig​
后端服务器组间会话保持配置。​
无​
backends​
Array of ​
BackendConfig​
后端服务器配置。​
无​
​
StickySessionConfig​
​
字段​
类型​
描述​
默认值​
serverGroupStickySession.enabled​
String​
是否开启后端服务组组间会话保持功能,取值:​
  • on:开启​
  • off:禁用​
禁用​
serverGroupStickySession.timeout​
Integer​
配置后端服务组组间会话保持超时时间。​
  • 取值范围:1~86400。单位:秒。​
  • 默认值:1000。​
1000​
​
BackendConfig​
​
字段​
类型​
描述​
默认值​
serviceName​
String​
后端服务名称。若无可用服务,请参考 服务 创建服务。​
无​
weight​
Integer​
后端服务权重,取值范围为 0~100。​
  • 当服务器组数量为 1 时,默认权重为 100。​
  • 当服务器组的权重设为 0 时,表示 ALB 不会转发客户端流量至该服务器组。​
说明​
当配置多个服务时,多个服务权重之和不要求为 100。ALB 能够根据每个服务的权重,按比例将客户端流量转发至对应的服务。详情请参见 服务器组的权重。​
无​
servicePort​
Integer​
后端服务的端口号。取值范围为 1~65535。​
无​
​
​
​
结果验证​
查看配置​
说明​
ALB 标准版和基础版的转发规则详情界面略有不同,本文以查看 ALB 标准版界面为例。​
  1. 单击左侧导航栏中 实例管理,在实例列表中选择目标 ALB 实例,在 操作 栏中单击 配置监听器。​
  • ​alt​​
  1. 在 监听器 页面中,选择目标监听器,在 操作 栏中单击 编辑转发规则,查看当前监听器的转发规则。可以看到有多个后端服务器组。规则配置详情,应该与下发的规则一致。​
  • ​​​
  1. 单击转发规则中的 ​alt​,支持查看规则详情。规则配置详情,应该与下发的规则一致。​
  • ​​​
访问验证​
说明​
验证功能前,您需要在集群中部署后端应用和服务。本例中以 nginx 镜像,服务提供路径为/为例。部署操作方式,请参考 使用 ALB Ingress 实现业务发布。​
使用 ALB Ingress 中配置的域名访问服务。其中14.***.***.***为 ALB 公网 IP 地址。​
​
curl -H "Host: example.com" http://14.***.***.***​
​
预期结果如下,当访问请求的域名为example.com,原始路径为/时,能够通过 ALB Ingress 正确访问到后端的服务。​
​
<!DOCTYPE html>​
<html>​
<head>​
<title>Welcome to nginx!</title>​
<style>​
html { color-scheme: light dark; }​
body { width: 35em; margin: 0 auto;​
font-family: Tahoma, Verdana, Arial, sans-serif; }​
</style>​
</head>​
<body>​
<h1>Welcome to nginx!</h1>​
<p>If you see this page, the nginx web server is successfully installed and​
working. Further configuration is required.</p>​
​
<p>For online documentation and support please refer to​
<a href="http://nginx.org/">nginx.org</a>.<br/>​
Commercial support is available at​
<a href="http://nginx.com/">nginx.com</a>.</p>​
​
<p><em>Thank you for using nginx.</em></p>​
</body>​
</html>​
​
当您为 ALB Ingress 配置多个后端服务器组,并为不同的服务器组配置不同的业务和权重。多次访问时,可以看到流量按照权重比例被分配到不同的后端服务器组中。例如执行以下命令,进行 20 次访问测试。​
​
for i in {1..20}; do curl -H "Host: example.com" http://14.***.***.***; done;​
​
预期结果如下,流量按照配置的权重转发到了不同的后端服务器组。​
​
This is the new version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the old version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the old version of nginx​
This is the new version of nginx​
This is the old version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the old version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the new version of nginx​
This is the new version of nginx​
​
​
最近更新时间:2026.10.16 16:53:02
这个页面对您有帮助吗?
有用
有用
无用
无用