通過給 Ingress 資源指定 Nginx Ingress 所支持的 annotation 可實現(xiàn)金絲雀發(fā)布。
需給服務(wù)創(chuàng)建2個 Ingress,其中1個常規(guī) Ingress,另1個為帶?nginx.ingress.kubernetes.io/canary: "true"
?固定的 annotation 的 Ingress,稱為 Canary Ingress。
Canary Ingress 一般代表新版本的服務(wù),結(jié)合另外針對流量切分策略的 annotation 一起配置即可實現(xiàn)多種場景的金絲雀發(fā)布。
以下為相關(guān) annotation 的詳細(xì)介紹:
-
nginx.ingress.kubernetes.io/canary-by-header
表示如果請求頭中包含指定的 header 名稱,并且值為?always
,就將該請求轉(zhuǎn)發(fā)給該 Ingress 定義的對應(yīng)后端服務(wù)。如果值為?never
?則不轉(zhuǎn)發(fā),可以用于回滾到舊版。如果為其他值則忽略該 annotation。 -
nginx.ingress.kubernetes.io/canary-by-header-value
該 annotation 可以作為?canary-by-header
?的補(bǔ)充,可指定請求頭為自定義值,包含但不限于?always
?或?never
。當(dāng)請求頭的值命中指定的自定義值時,請求將會轉(zhuǎn)發(fā)給該 Ingress 定義的對應(yīng)后端服務(wù),如果是其它值則忽略該 annotation。 -
nginx.ingress.kubernetes.io/canary-by-header-pattern
與?canary-by-header-value
?類似,區(qū)別為該 annotation 用正則表達(dá)式匹配請求頭的值,而不是只固定某一個值。如果該 annotation 與?canary-by-header-value
?同時存在,該 annotation 將被忽略。 -
nginx.ingress.kubernetes.io/canary-by-cookie
與?canary-by-header
?類似,該 annotation 用于 cookie,僅支持?always
?和?never
。 -
nginx.ingress.kubernetes.io/canary-weight
表示 Canary Ingress 所分配流量的比例的百分比,取值范圍 [0-100]。例如,設(shè)置為10,則表示分配10%的流量給 Canary Ingress 對應(yīng)的后端服務(wù)。 - 金絲雀規(guī)則按優(yōu)先順序進(jìn)行如下排序:canary-by-header - > canary-by-cookie - > canary-weight
基于不同的場景,灰度發(fā)布有四種:
//第一種,所有的請求都會被轉(zhuǎn)發(fā)到灰度(Canary)版本
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-header: "always"
//第二種,所有的請求都不會被轉(zhuǎn)發(fā)到灰度(Canary)版本
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-header: "never"
//第三種,如果請求的header頭包含"user-id: user_1",該請求會被轉(zhuǎn)發(fā)到灰度(Canary)版本
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-header: "user_id"
nginx.ingress.kubernetes.io/canary-by-header-value: "user_1"
//第四種,如果請求的header頭包含"user-id: user_2"或"user-id: user-3"或"user-id: user4",該請求會被轉(zhuǎn)發(fā)到灰度(Canary)版
nginx.ingress.kubernetes.io/canary: "true"
nginx.ingress.kubernetes.io/canary-by-header: "user_id" #在networking.k8s.io/v1中,canary-by-header的值盡量不用使用下劃線“_”。如果使用了,請求頭中要使用中橫線“-”代替。
nginx.ingress.kubernetes.io/canary-by-header-pattern: "user_2|user-3|user4"
一、部署藍(lán)環(huán)境版本服務(wù)
1、ConfigMap
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-blue-config
data:
nginx.conf: |-
worker_processes 1;
events {
accept_mutex on;
multi_accept on;
use epoll;
worker_connections 1024;
}
http {
ignore_invalid_headers off;
server {
listen 80;
location / {
access_by_lua '
local header_str = ngx.say("blue")
';
}
}
}
2、Deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-blue
labels:
dce.daocloud.io/app: nginx-blue
annotations:
dce.daocloud.io/last-replicas: '1'
deployment.kubernetes.io/revision: '3'
kubernetes.io/change-cause: update YAML
spec:
replicas: 1
selector:
matchLabels:
dce.daocloud.io/component: nginx-blue
template:
metadata:
name: nginx-blue
labels:
dce.daocloud.io/app: nginx-blue
dce.daocloud.io/component: nginx-blue
annotations:
dce.daocloud.io/parcel.egress.burst: '0'
dce.daocloud.io/parcel.egress.rate: '0'
dce.daocloud.io/parcel.ingress.burst: '0'
dce.daocloud.io/parcel.ingress.rate: '0'
dce.daocloud.io/parcel.net.type: calico
spec:
volumes:
- name: nginx-blue-config
configMap:
name: nginx-blue-config
defaultMode: 420
containers:
- name: nginx-blue
image: 'x.x.x.x/library/openresty:1.19.9.1-sw-r4'
resources:
limits:
cpu: 500m
memory: '314572800'
requests:
cpu: 200m
memory: '314572800'
volumeMounts:
- name: nginx-blue-config
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
3、Service
kind: Service
apiVersion: v1
metadata:
name: nginx-blue-default
labels:
dce.daocloud.io/app: nginx-blue
annotations:
io.daocloud.dce.serviceSelectorType: service
spec:
ports:
- name: nginx-nginx-default-80680-80
protocol: TCP
port: 80
targetPort: 80
nodePort: 31046
selector:
dce.daocloud.io/component: nginx-blue
clusterIP: 172.31.69.137
type: NodePort
sessionAffinity: None
externalTrafficPolicy: Cluster
4、修改pod內(nèi)容
cd /usr/local/openresty/nginx/html/
ls
50x.html index.html
echo "Hello Blue" > index.html
cat index.html
Hello Blue
二、部署綠環(huán)境版本服務(wù)
1、ConfigMap
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-green-config
data:
nginx.conf: |-
worker_processes 1;
events {
accept_mutex on;
multi_accept on;
use epoll;
worker_connections 1024;
}
http {
ignore_invalid_headers off;
server {
listen 80;
location / {
access_by_lua '
local header_str = ngx.say("green")
';
}
}
}
2、Deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-green
labels:
dce.daocloud.io/app: nginx-green
annotations:
deployment.kubernetes.io/revision: '5'
kubernetes.io/change-cause: update YAML
spec:
replicas: 1
selector:
matchLabels:
dce.daocloud.io/component: nginx-green
template:
metadata:
name: nginx-green
labels:
dce.daocloud.io/app: nginx-green
dce.daocloud.io/component: nginx-green
env: green
annotations:
dce.daocloud.io/parcel.egress.burst: '0'
dce.daocloud.io/parcel.egress.rate: '0'
dce.daocloud.io/parcel.ingress.burst: '0'
dce.daocloud.io/parcel.ingress.rate: '0'
dce.daocloud.io/parcel.net.type: calico
dce.daocloud.io/parcel.net.value: default-ipv4-ippool
spec:
volumes:
- name: nginx-green-config
configMap:
name: nginx-green-config
defaultMode: 420
containers:
- name: nginx-green
image: 'x.x.x.x/library/openresty:1.19.9.1-sw-r4'
resources:
limits:
cpu: 500m
memory: '314572800'
requests:
cpu: 200m
memory: '314572800'
volumeMounts:
- name: nginx-green-config
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
3、Service
kind: Service
apiVersion: v1
metadata:
name: nginx-green-default
labels:
dce.daocloud.io/app: nginx-green
annotations:
io.daocloud.dce.serviceSelectorType: service
spec:
ports:
- name: nginx-nginx-default-15833-80
protocol: TCP
port: 80
targetPort: 80
nodePort: 35218
selector:
dce.daocloud.io/component: nginx-green
clusterIP: 172.31.207.22
type: NodePort
sessionAffinity: None
externalTrafficPolicy: Cluster
?4、修改pod內(nèi)容
cd /usr/local/openresty/nginx/html/
ls
50x.html index.html
echo "Hello Green" > index.html
cat index.html
Hello Green
三、設(shè)置Ingress
1、blue環(huán)境Ingress
kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
name: nginx-blue-ingress
labels:
dce.daocloud.io/app: nginx-blue
annotations:
nginx.ingress.kubernetes.io/use-port-in-redirects: 'true'
spec:
rules:
- host: nginx.ms-sit.xxxxxx.net
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
serviceName: nginx-blue-default
servicePort: 80
2、green環(huán)境Ingress
案例1:
kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
name: nginx-green-ingress
labels:
dce.daocloud.io/app: nginx-green
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: 'true'
nginx.ingress.kubernetes.io/canary-by-header: env
nginx.ingress.kubernetes.io/canary-by-header-pattern: green
spec:
rules:
- host: nginx.ms-sit.xxxxxx.net
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
serviceName: nginx-green-default
servicePort: 80
案例2:文章來源:http://www.zghlxwxcb.cn/news/detail-678666.html
kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
name: nginx-green-ingress
ingress
labels:
dce.daocloud.io/app: nginx-green
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/canary: 'true'
nginx.ingress.kubernetes.io/canary-by-header: Cookie
nginx.ingress.kubernetes.io/canary-by-header-pattern: bu=xxxcn|bu=xxxsg
spec:
rules:
- host: nginx.ms-sit.aswatson.net
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
serviceName: nginx-green-default
servicePort: 80
四、測試
案例1:
案例2:
文章來源地址http://www.zghlxwxcb.cn/news/detail-678666.html
到了這里,關(guān)于K8S Nginx Ingress實現(xiàn)金絲雀發(fā)布的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!