您好,登錄后才能下訂單哦!
k8s中基于 nginx-ingress 的灰度發布是什么,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
假設當前線上環境我們已經有一套服務 app-old 對外提供 7 層服務,此時我們修復了一些問題,需要灰度發布上線一個新的版本 app-new,但是我們又不希望簡單直接地將所有客戶端流量切換到新版本 app-new 中,而是希望僅僅切換 20% 的流量到新版本 app-new 中,待運行一段時間穩定,將所有的流量切換到 app-new 服務中后,再平滑地下線掉 app-old 服務。
針對以上多種不同的應用發布需求,K8S Ingress Controller 支持了多種流量切分方式:
基于 Request Header 的流量切分,適用于灰度發布以及 AB 測試場景
基于 Cookie 的流量切分,適用于灰度發布以及 AB 測試場景
基于 Query Param 的流量切分,適用于灰度發布以及 AB 測試場景
基于服務權重的流量切分,適用于藍綠發布場景
以下測試基于服務權重的流量切分,也可以將nginx.ingress.kubernetes.io/canary-weight: "30"
改為基于 header 的流量切分。
老版本程序 app-old
app-old.yaml
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: app-old spec: replicas: 2 selector: matchLabels: run: app-old template: metadata: labels: run: app-old spec: containers: - image: zouhl/app:v2.1 imagePullPolicy: Always name: app-old ports: - containerPort: 80 protocol: TCP restartPolicy: Always --- apiVersion: v1 kind: Service metadata: name: app-old spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: app-old sessionAffinity: None type: NodePor
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: app-old spec: replicas: 2 selector: matchLabels: run: app-old template: metadata: labels: run: app-old spec: containers: - image: zouhl/app:v2.1 imagePullPolicy: Always name: app-old ports: - containerPort: 80 protocol: TCP restartPolicy: Always --- apiVersion: v1 kind: Service metadata: name: app-old spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: app-old sessionAffinity: None type: NodePor
老版本的 ingress
app-v1.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-app labels: app: my-app annotations: kubernetes.io/ingress.class: nginx namespace: default spec: rules: - host: test.192.168.2.20.xip.io http: paths: - backend: serviceName: app-old servicePort: 80 path: /
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-app labels: app: my-app annotations: kubernetes.io/ingress.class: nginx namespace: default spec: rules: - host: test.192.168.2.20.xip.io http: paths: - backend: serviceName: app-old servicePort: 80 path: /
在 k8s 中創建
kubectl create -f app-old.yaml kubectl create -f app-v1.yaml
kubectl create -f app-old.yaml kubectl create -f app-v1.yaml
新版本app-new.yaml
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: app-new spec: replicas: 2 selector: matchLabels: run: app-new template: metadata: labels: run: app-new spec: containers: - image: zouhl/app:v2.2 imagePullPolicy: Always name: app-new ports: - containerPort: 80 protocol: TCP restartPolicy: Always --- apiVersion: v1 kind: Service metadata: name: app-new spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: app-new sessionAffinity: None type: NodePort
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: app-new spec: replicas: 2 selector: matchLabels: run: app-new template: metadata: labels: run: app-new spec: containers: - image: zouhl/app:v2.2 imagePullPolicy: Always name: app-new ports: - containerPort: 80 protocol: TCP restartPolicy: Always --- apiVersion: v1 kind: Service metadata: name: app-new spec: ports: - port: 80 protocol: TCP targetPort: 80 selector: run: app-new sessionAffinity: None type: NodePort
新版本 canary-ingress
app-v2-canary.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-app-canary labels: app: my-app annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/canary: "true" nginx.ingress.kubernetes.io/canary-weight: "30" namespace: default spec: rules: - host: test.192.168.2.20.xip.io http: paths: - backend: serviceName: app-new servicePort: 80 path: /
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-app-canary labels: app: my-app annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/canary: "true" nginx.ingress.kubernetes.io/canary-weight: "30" namespace: default spec: rules: - host: test.192.168.2.20.xip.io http: paths: - backend: serviceName: app-new servicePort: 80 path: /
新版本 ingress yaml
app-v2.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-app labels: app: my-app annotations: kubernetes.io/ingress.class: nginx namespace: default spec: rules: - host: test.192.168.2.20.xip.io http: paths: - backend: serviceName: app-new servicePort: 80 path: /
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: my-app labels: app: my-app annotations: kubernetes.io/ingress.class: nginx namespace: default spec: rules: - host: test.192.168.2.20.xip.io http: paths: - backend: serviceName: app-new servicePort: 80 path: /
$ tree . ├── app-new.yaml ├── app-old.yaml ├── app-v1.yaml ├── app-v2-canary.yaml └── app-v2.yaml
$ tree . ├── app-new.yaml ├── app-old.yaml ├── app-v1.yaml ├── app-v2-canary.yaml └── app-v2.yaml
app-v1 已經發布了,現在灰度發布第二版,權重為 30%,nginx.ingress.kubernetes.io/canary-weight: "30"
,更多參數參考github
kubectl create -f app-new.yaml kubectl create -f app-v2-canary.yaml
kubectl create -f app-new.yaml kubectl create -f app-v2-canary.yaml
檢查
$ kubectl get ingresses.extensions NAME HOSTS ADDRESS PORTS AGE app-ingress www.example.com 80 109m my-app test.192.168.2.20.xip.io 80 25m my-app-canary test.192.168.2.20.xip.io 80 1s nginx-test nginx.192.168.2.20.xip.io 80 3h22m
$ kubectl get ingresses.extensions NAME HOSTS ADDRESS PORTS AGE app-ingress www.example.com 80 109m my-app test.192.168.2.20.xip.io 80 25m my-app-canary test.192.168.2.20.xip.io 80 1s nginx-test nginx.192.168.2.20.xip.io 80 3h22m
在后臺觀察,70% to v1,30% to v2
$ while sleep 0.5; do curl "test.192.168.2.20.xip.io";echo; done {"v2.2 hostname":"app-new-658dfc9c6b-lbmvr"} {"v2.2 hostname":"app-new-658dfc9c6b-qhwtg"} {"v1 hostname":"app-old-64fd44b699-4hvlb"} {"v1 hostname":"app-old-64fd44b699-zb58f"}
$ while sleep 0.5; do curl "test.192.168.2.20.xip.io";echo; done {"v2.2 hostname":"app-new-658dfc9c6b-lbmvr"} {"v2.2 hostname":"app-new-658dfc9c6b-qhwtg"} {"v1 hostname":"app-old-64fd44b699-4hvlb"} {"v1 hostname":"app-old-64fd44b699-zb58f"}
如果一切正常則可以正式發布
# delete the canary ingress kubectl delete -f app-v2-canary.yaml # set 100% traffic to v2 kubectl apply -f app-v2.yaml
# delete the canary ingress kubectl delete -f app-v2-canary.yaml # set 100% traffic to v2 kubectl apply -f app-v2.yaml
檢查 ingress
$ kubectl get ingresses.extensions NAME HOSTS ADDRESS PORTS AGE app-ingress www.example.com 80 109m my-app test.192.168.2.20.xip.io 80 25m nginx-test nginx.192.168.2.20.xip.io 80 3h23m $ while sleep 0.5; do curl "test.192.168.2.20.xip.io";echo; done {"v2.2 hostname":"app-new-658dfc9c6b-lbmvr"} {"v2.2 hostname":"app-new-658dfc9c6b-qhwtg"}
$ kubectl get ingresses.extensions NAME HOSTS ADDRESS PORTS AGE app-ingress www.example.com 80 109m my-app test.192.168.2.20.xip.io 80 25m nginx-test nginx.192.168.2.20.xip.io 80 3h23m $ while sleep 0.5; do curl "test.192.168.2.20.xip.io";echo; done {"v2.2 hostname":"app-new-658dfc9c6b-lbmvr"} {"v2.2 hostname":"app-new-658dfc9c6b-qhwtg"}
看完上述內容,你們掌握k8s中基于 nginx-ingress 的灰度發布是什么的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。