ingress-nginx金丝雀发布

说明

下面使用的 ingress-nginx 的版本是 0.26.1

nginx-ingress作为K8S集群对外的流量入口,充当K8S集群内各个service的反向代理。日常工作中我们经常需要对服务进行版本更新升级,为此我们经常使用到的发布方式有滚动升级、分批暂停发布、蓝绿发布以及灰度发布等不同的发布操作。所以下面介绍下,通过配置nginx annotations来实现灰度发布。

ingress-nginx金丝雀

原有的 ingress 不需要改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: tomcat-test
namespace: test
annotations:
kubernetes.io/ingress.class: "nginx-backend"
nginx.ingress.kubernetes.io/enable-access-log: "true"
nginx.ingress.kubernetes.io/configuration-snippet: |
access_log /var/log/nginx/test.sy.com.log;
spec:
rules:
- host: test.sy.com
http:
paths:
- path: /
backend:
serviceName: tomcat-test-opdev
servicePort: 6080

创建 canary

nginx.ingress.kubernetes.io/canary “true” or “false”
nginx.ingress.kubernetes.io/canary-by-header string
nginx.ingress.kubernetes.io/canary-by-header-value string
nginx.ingress.kubernetes.io/canary-by-header-pattern string
nginx.ingress.kubernetes.io/canary-by-cookie string
nginx.ingress.kubernetes.io/canary-weight number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: tomcat-test-canary
namespace: test
annotations:
kubernetes.io/ingress.class: "nginx-backend"
nginx.ingress.kubernetes.io/canary: "true"
#nginx.ingress.kubernetes.io/canary-weight: "100"
nginx.ingress.kubernetes.io/canary-by-header: "name"
nginx.ingress.kubernetes.io/canary-by-header-value: "sy"
nginx.ingress.kubernetes.io/enable-access-log: "true"
nginx.ingress.kubernetes.io/configuration-snippet: |
access_log /var/log/nginx/test.sy.com.log;
spec:
rules:
- host: test.sy.com
http:
paths:
- path: /
backend:
serviceName: tomcat-test-scm
servicePort: 6080

效果

1
2
3
4
[root@ingress]# curl http://test.sy.com/abc/check_health.jsp
opdev
[root@ingress]# curl -H "name: sy" http://test.sy.com/abc/check_health.jsp
scm

上面的规则按优先顺序进行评估。优先顺序如下:canary-by-header -> canary-by-cookie -> canary-weight

注意:

当将入口标记为canary时,除了nginx.ingress.kubernetes.io/load-balance和之外,所有其他非canary注释都将被忽略(从相应的主入口继承)nginx.ingress.kubernetes.io/upstream-hash-by

已知的限制 目前,每个Ingress规则最多可以应用一个canary入口。

参考链接

Donate