ALB Ingress 支持在配置转发规则时,配置多个后端服务器组,允许指定集群中的 Service 或已存在的后端服务器组。当请求经过 ALB 时,ALB 可以按照权重向不同后端服务器组转发。
说明
该功能为 ALB 产品的邀测功能,如需使用,请联系您的客户经理。
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
注解中的参数说明如下表所示。
字段 | 类型 | 描述 | 默认值 |
---|---|---|---|
host | String | 匹配重写规则的转发域名。 | 无 |
path | String | 匹配重写规则的转发路径。 | 无 |
serverGroups | Array of serverGroupConfig | 配置 ALB Ingress 后端服务器组。最多支持配置 5 个后端服务器组。 | 无 |
stickySessionEnabled | Boolean | 是否开启组间会话保持。取值:
| false |
stickySessionCookieTimeout | Integer | 服务器组间会话保持超时时间。单位为秒,取值范围为 1~86400。 | 1000 |
字段 | 类型 | 描述 | 默认值 |
---|---|---|---|
serviceName | String | 集群中的服务名称。 说明 配置该字段时,必须同时配置 | 无 |
servicePort | Integer | 服务端口号。
| 无 80 |
serverGroupID | String | 已存在后端服务器组 ID。 说明 该字段与 | 无 |
weight | Integer | 后端服务器组权重,取值范围为 0~100。
说明 更多服务器权重说明,请参见 服务器组的权重。 | 无 |
说明
验证功能前,您需要在集群中部署后端应用和服务。本例中以 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