You need to enable JavaScript to run this app.
导航
为 ALB Ingress 配置多个后端服务器组
最近更新时间:2025.03.20 10:59:31首次发布时间:2025.03.20 10:59:31
我的收藏
有用
有用
无用
无用

ALB Ingress 支持在配置转发规则时,配置多个后端服务器组,允许指定集群中的 Service 或已存在的后端服务器组。当请求经过 ALB 时,ALB 可以按照权重向不同后端服务器组转发。

说明

该功能为 ALB 产品的邀测功能,如需使用,请联系您的客户经理。

前提条件

使用限制

  • 不支持指定由 ALB Ingress Controller 创建的后端服务器组 ID。
  • 每条转发规则,最多支持配置 5 个后端服务器组。

操作步骤

ALB Ingress 支持通过 Annotation 指定多个后端服务器组。后端服务器组支持配置为集群中的 Service,也支持通过 ID 指定 ALB 中已存在的服务器组。示例和注解说明如下:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: alb-ingress # 自定义路由规则名称
  namespace: default  # 路由规则所属的命名空间
  annotations:
    ingress.vke.volcengine.com/loadbalancer-rules-configs-forward: |
      [
        {
            "host": "example.com",
            "path": "/",
            "serverGroups" : [{
               "serviceName": "service-demo-a",
               "weight": 80,
               "servicePort": 80
             },
             {
               "serverGroupId": "rsp-1ij9jfh***",
               "weight": 30
             },
             {
               "serviceName": "service-demo-b",
               "weight": 20,
               "servicePort": 80
             }]
        }
      ]
spec:
  ingressClassName: alb # ALBInstance 的资源名称
  rules:
  - host: example.com
    http:
      paths:
      - pathType: Prefix
        path: / # 请求匹配的路径
        backend:
          service:
            name: forward # 服务名称,固定为 forward,表示按照注解配置进行流量转发
            port:
              name: use-annotation # 端口名称,固定为 use-annotation,表示按照注解配置进行流量转发

ingress.vke.volcengine.com/loadbalancer-rules-config-forwad注解中的参数说明如下表所示。

字段类型描述默认值
hostString匹配重写规则的转发域名。
pathString匹配重写规则的转发路径。
serverGroupsArray of serverGroupConfig配置 ALB Ingress 后端服务器组。最多支持配置 5 个后端服务器组。

stickySessionEnabled

Boolean

是否开启组间会话保持。取值:

  • true:开启。
  • false:不开启。

false

stickySessionCookieTimeoutInteger服务器组间会话保持超时时间。单位为秒,取值范围为 1~86400。1000

serverGroupConfig

字段类型描述默认值

serviceName

String

集群中的服务名称。

说明

配置该字段时,必须同时配置servicePort字段。

servicePort

Integer

服务端口号。

  • 取值范围:1~65535
  • 默认值:80(HTTP)、443(HTTPS)

80

serverGroupID

String

已存在后端服务器组 ID。

说明

该字段与serviceName字段不能同时配置。

weight

Integer

后端服务器组权重,取值范围为 0~100。

  • 当服务器组数量为 1,且未指定此参数时,默认权重为 100。
  • 当服务器组的权重设为 0 时,表示 ALB 不会转发客户端流量至该服务器组。

说明

更多服务器权重说明,请参见 服务器组的权重

结果验证

查看配置

  1. 登录 应用型负载均衡控制台
  2. 单击左侧导航栏中 实例管理,在实例列表中选择目标 ALB 实例,在 操作 栏中单击 配置监听器
    alt
  3. 在 监听器 页面中,选择目标监听器,在 操作 栏中单击 编辑转发规则,查看当前监听器的转发规则。可以看到有多个后端服务器组。规则配置详情,应该与下发的规则一致。
    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