Ingress-nginx annotations 转发策略(四)

Kubernetes / 2022-12-20

初始Ingress策略

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: <ingress-name>
spec:
  rules:
  - host: <your.domain>
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: <servicename>
            port:
              number: 80
  ingressClassName: nginx

基于annotations灰度发布策略

示例(根据权重转发):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: <ingress-name>
spec:
  rules:
  - host: <your.domain>
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: <old_servicename>
            port:
              number: 80
  ingressClassName: nginx

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: <ingress-name>
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "30"		# 30%权重
spec:
  rules:
  - host: <your.domain>
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: <new_servicename>
            port:
              number: 80
  ingressClassName: nginx

nginx.ingress.kubernetes.io/canary-weight

基于服务权重的流量切分,适用于蓝绿部署,权重范围 0 - 100 按百分比将请求路由到 Canary Ingress 中指定的服务。权重为 0 意味着该金丝雀规则不会向 Canary 入口的服务发送任何请求。权重为30意味着30%流量转到canary。权重为 100 意味着所有请求都将被发送到 Canary 入口。

for i in $(seq 1 10); do curl http://<your.domain>; echo '\n'; done
hello world-version1
hello world-version1
hello world-version2
hello world-version2
hello world-version1
hello world-version1
hello world-version1
hello world-version1
hello world-version1
hello world-version1

一、根据请求头转发

annotations:
  kubernetes.io/ingress.class: nginx
  nginx.ingress.kubernetes.io/canary: "true"
  nginx.ingress.kubernetes.io/canary-by-header: "test"
  • nginx.ingress.kubernetes.io/canary-by-header

    基于Request Header的流量切分,适用于灰度发布以及 A/B 测试。当Request Header 设置为 always时,请求将会被一直发送到 Canary 版本;当 Request Header 设置为 never时,请求不会被发送到 Canary 入口。

    测试结果如下:

    for i in $(seq 1 5); do curl http://<your.domain>; echo '\n'; done
    hello world-version1
    hello world-version1
    hello world-version1
    hello world-version1
    hello world-version1
    for i in $(seq 1 5); do curl -H 'test:always' http://<your.domain>; echo '\n'; done
    hello world-version2
    hello world-version2
    hello world-version2
    hello world-version2
    hello world-version2
    

二、根据特定的请求头和值转发

annotations:
  kubernetes.io/ingress.class: nginx
  nginx.ingress.kubernetes.io/canary: "true"
  nginx.ingress.kubernetes.io/canary-by-header: "test"
  nginx.ingress.kubernetes.io/canary-by-header-value: "abc"
  • nginx.ingress.kubernetes.io/canary-by-header-value

    要匹配的 Request Header 的值,用于通知 Ingress 将请求路由到 Canary Ingress 中指定的服务。当 Request Header 设置为此值时,它将被路由到 Canary 入口。

    测试结果如下:

    for i in $(seq 1 5); do curl -H 'test:always' http://<your.domain>; echo '\n'; done
    hello world-version1
    hello world-version1
    hello world-version1
    hello world-version1
    hello world-version1
    for i in $(seq 1 5); do curl -H 'test:abc' http://<your.domain>; echo '\n'; done
    hello world-version2
    hello world-version2
    hello world-version2
    hello world-version2
    hello world-version2
    
annotations:
  kubernetes.io/ingress.class: nginx
  nginx.ingress.kubernetes.io/canary: "true"
  nginx.ingress.kubernetes.io/canary-by-cookie: "like_music"
  • nginx.ingress.kubernetes.io/canary-by-cookie

    基于 Cookie 的流量切分,适用于灰度发布与 A/B 测试。用于通知 Ingress 将请求路由到 Canary Ingress 中指定的服务的cookie。当 cookie 值设置为 always时,它将被路由到 Canary 入口;当 cookie 值设置为 never时,请求不会被发送到 Canary 入口。

    测试结果如下:

    for i in $(seq 1 5); do curl -b 'like_music=1' http://<your.domain>; echo '\n'; done
    hello world-version1
    hello world-version1
    hello world-version1
    hello world-version1
    hello world-version1
    for i in $(seq 1 5); do curl -b 'like_music=always' http://<your.domain>; echo '\n'; done
    hello world-version2
    hello world-version2
    hello world-version2
    hello world-version2
    hello world-version2