如何使用Higress快速實現金絲雀與藍綠發布
背景
現如今,越來越多的應用采用了微服務架構,這也導致了應用數量相比傳統模式更多,管理更加復雜,發布更加頻繁,如果直接將新版本上線發布給全部用戶。一旦遇到線上事故(或BUG),對用戶的影響極大,解決問題周期較長,甚至有時不得不回滾到前一版本,嚴重影響了用戶體驗。為了保證整體系統的穩定,風險降到最低,我們可以采用灰度發布與藍綠發布等不同的發布方式。
什么是金絲雀發布
金絲雀發布,又稱灰度發布,是指通過讓小部份用戶流量引入的新版本進行測試,如果一切順利,則可以增加(可能逐漸增加)百分比,逐步替換舊版本。如在過程中出現任何問題,則可以中止并快速回滾到舊版本。最簡單的方式是隨機選擇百分比請求到金絲雀版本,但在更復雜的方案下,則可以基于請求的內容、特定范圍的用戶或其他屬性等。
圖片
什么是藍綠發布
藍綠發布,提供了一種零宕機的部署方式,是一種以可觀測的方式發布應用的方式,目的減少發布過程中停止時間。在保留老版本的同時部署新版本,將兩個版本同時在線,新版本和老版本相互熱備,通過切換路由權重的方式(非0即100)實現應用的不同版本上線或者下線,如果有問題可以快速地回滾到老版本。這樣做的好處是無需停機,并且風險較小。
圖片
Higress 部署
# 環境為Kubernetes v1.27.3
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready control-plane 9h v1.27.3
# 通過Helm安裝
$ helm repo add higress.io https://higress.io/helm-charts
"higress.io" already exists with the same configuration, skipping
$ helm install higress -n higress-system higress.io/higress --create-namespace --render-subchart-notes --set global.local=true --set higress-console.o11y.enabled=false --set higress-console.domain=console.higress.io --set higress-console.admin.password.value=admin
NAME: higress
LAST DEPLOYED: Thu Aug 10 20:37:40 2023
NAMESPACE: higress-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Higress successfully installed!
To learn more about the release, try:
$ helm status higress -n higress-system
$ helm get all higress -n higress-system
1. Use the following URL to access the console:
http://console.higress.io/
Since Higress Console is running in local mode, you may need to add the following line into your hosts file before accessing the console:
127.0.0.1 console.higress.io
2. Use following commands to get the credential and login:
export ADMIN_USERNAME=$(kubectl get secret --namespace higress-system higress-console -o jsnotallow="{.data.adminUsername}" | base64 -d)
export ADMIN_PASSWORD=$(kubectl get secret --namespace higress-system higress-console -o jsnotallow="{.data.adminPassword}" | base64 -d)
echo -e "Username: ${ADMIN_USERNAME}\nPassword: ${ADMIN_PASSWORD}"
NOTE: If this is an upgrade release, your current password won't be changed.
3. If you'd like to change the credential, you can edit this secret with new values: higress-system/higress-console
# 查看密碼
$ export ADMIN_USERNAME=$(kubectl get secret --namespace higress-system higress-console -o jsnotallow="{.data.adminUsername}" | base64 -d)
$ export ADMIN_PASSWORD=$(kubectl get secret --namespace higress-system higress-console -o jsnotallow="{.data.adminPassword}" | base64 -d)
$ echo -e "Username: ${ADMIN_USERNAME}\nPassword: ${ADMIN_PASSWORD}"
Username: admin
Password: admin
# 配置Hosts
$ cat /etc/hosts
127.0.0.1 demo.kubesre.com console.higress.io
# 轉發一下端口本地可以訪問
$ kubectl port-forward service/higress-gateway -n higress-system 80:80
訪問地址:http://console.higress.io/plugin
Username: admin Password: admin
圖片
示例應用部署
部署示例版本:
$ cat demo.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo
labels:
app: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
containers:
- name: demo
imagePullPolicy: Always
image: registry.cn-shanghai.aliyuncs.com/kubesre01/demo:v1
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: demo-svc
spec:
type: ClusterIP
selector:
app: demo
ports:
- port: 8080
targetPort: 8080
$ kubectl apply -f demo.yml
deployment.apps/demo created
配置Higress路由規則
在域名管理中創建域名:
圖片
在路由配置中創建路由:
圖片
驗證訪問
# 通過如下命令進行訪問,出現如下內容則說明部署成功!
$ curl http://demo.kubesre.com/info
{"message":"云原生運維圈!"}
部署新版本示例
部署新版本:
$ cat demo_new.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-new
labels:
app: demo-new
spec:
replicas: 1
selector:
matchLabels:
app: demo-new
template:
metadata:
labels:
app: demo-new
spec:
containers:
- name: demo-new
imagePullPolicy: Always
image: registry.cn-shanghai.aliyuncs.com/kubesre01/demo:v2
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: demo-new-svc
spec:
type: ClusterIP
selector:
app: demo-new
ports:
- port: 8080
targetPort: 8080
$ kubectl apply -f demo_new.yml
deployment.apps/demo_new created
基于客戶端請求頭的流量切分
假設線上已運行了一套對外提供的七層demo應用,此時開發了一些新的功能,需要上線新版本demo應用,但是又不想直接替換成新版本demo應用,而是希望將請求頭包含user=kubesre的客戶端請求轉發到新版本demo應用中,進行驗證測試新版本demo應用,等測試驗證通過并穩定后,可將所有流量從老版本demo應用切換到新版本demo應用中,再平滑地將老版本demo應用下線。
創建新版本Higress路由規則:
圖片
目標服務,選擇最新版本
測試驗證:
# 請求頭為user: kubesre,訪問到新的版本
$ curl -H "user: kubesre" http://demo.kubesre.com/info
{"message":"云原生運維圈!新版本"}
# 其他則訪問到老的版本
$ curl http://demo.kubesre.com/info
{"message":"云原生運維圈!"}
基于客戶端來源IP的流量切分
假設線上已運行了一套對外提供的七層demo應用,此時開發了一些新的功能,需要上線新版本demo應用,又不想直接替換成新版本demo應用,而是只希望公司內部人員能訪問到新版本demo應用中,進行測試驗證新版本demo應用,非公司內部人員訪問還是訪問到老版本應用中。等公司內部人員測試驗證通過并穩定后,可將所有流量從老版本demo應用切換到新版本demo應用中,再平滑地將老版本demo應用下線。創建新版本Higress路由規則:
圖片
測試驗證:
# 通過請求頭模擬來源IP,真實環境不需要
$ curl -H "X-Forwarded-For:123.456.789.123" http://demo.kubesre.com/info
{"message":"云原生運維圈!新版本"}
# 其他則訪問到老的版本
$ curl http://demo.kubesre.com/info
{"message":"云原生運維圈!"}
基于服務權重的流量切分
假設線上已運行了一套對外提供的七層demo應用,此時修復了一些問題,需要上線新版本demo應用,又不想直接替換成新版本demo應用,而是希望將20%的流量切換新版本。待運行一段時間穩定后,可將所有流量從老版本demo應用切換到新版本demo應用中,再平滑地將老版本demo應用下線。
創建新版本Higress路由規則:
選擇最新目標服務即可,其他不需要任何特殊配置:
圖片
配置權重:
$ kubectl edit ingress -n higress-system demo-new-canary
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
higress.io/canary: "true" # 開啟灰度
higress.io/canary-weight: "30" # 權重設置為30
higress.io/destination: demo-new-svc.default.svc.cluster.local:8080
higress.io/ignore-path-case: "false"
creationTimestamp: "2023-10-07T07:04:40Z"
generation: 1
labels:
higress.io/domain_demo.kubesre.com: "true"
higress.io/resource-definer: higress
name: demo-new-canary
namespace: higress-system
resourceVersion: "221188"
uid: bf833256-6993-4e56-bc3e-f96fe606e278
spec:
ingressClassName: higress
rules:
....
驗證測試:
$ for i in {1..20}; do curl http://demo.kubesre.com/info; done;
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!新版本"}
{"message":"云原生運維圈!新版本"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!新版本"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!"}
{"message":"云原生運維圈!新版本"}
{"message":"云原生運維圈!新版本"}
{"message":"云原生運維圈!新版本"}
{"message":"云原生運維圈!"}
注解說明
- higress.io/canary-weight:設置請求到指定服務的百分比(值為0~100的整數)
- higress.io/canary-weight-totatl:設置權重總和,默認為100
- higress.io/canary-by-header:基于Request Header的名稱進行流量切分。當請求包含該Header并其值為always時,請求流量會被分配到灰度服務入口;其他情況時,請求流量不會分配到灰度服務。
總結
本文介紹了金絲雀與藍綠發布及不同的應用場景,并基于Higress結合企業實戰案例進行演示,讓大家更容易理解上手,接下來的文章會講解Higress更多企業級應用實戰,請敬請期待!