通過Kubernetes+Istio的流量控制實現(xiàn)灰度發(fā)布,主要演示通過流量權(quán)重實現(xiàn)藍(lán)綠,通過http自定義頭實現(xiàn)金絲雀
?
準(zhǔn)備環(huán)境
k8s和istio不想自己裝的話可以在云上買個按量付費集群,用完即刪,推薦華為云。
項目中用到的代碼
用的springboot+springcloud feign做rest強(qiáng)類型調(diào)用,放到github了
https://github.com/assionyang/istio-test.git
代碼結(jié)構(gòu)說明
istio-service-union #聚合服務(wù)項目,用來測試調(diào)用user服務(wù),也做為入口 |-Dockerfile #dockerfile
istio-service-user #用戶服務(wù),用來演示版本切換 |-Dockerfile #dockerfile
istio-service-user-api #類庫,使用feign暴露client與dto,union服務(wù)依賴user-api k8s
#k8s&istio發(fā)布文件目錄 |-config |- istio-service-union.yaml # union服務(wù)ConfigMap |-
istio-service-user-v1.yaml # user服務(wù)v1版本ConfigMap |- istio-service-user-v2.yaml
# user服務(wù)v2版本ConfigMap |- istio-service-union-deployment.yaml #union無狀態(tài)發(fā)布 |-
istio-service-union-service.yaml # union服務(wù) |- istio-service-gateway.yaml #
ingress網(wǎng)關(guān),對外暴露union |- istio-service-user-deployment-v1.yaml # user版本v1無狀態(tài)發(fā)布 |-
istio-service-user-deployment-v2.yaml # user版本v2無狀態(tài)發(fā)布 |-
istio-service-user-service.yaml # user服務(wù) |-
istio-service-user-virtualservice-v1.yaml # user路由到v1版 |-
istio-service-user-virtualservice-v2.yaml # user路由到v2版 |-
istio-service-user-virtualservice-weight.yaml # user路由流量權(quán)重 |-
istio-service-user-virtualservice-jsq.yaml # user路由金絲雀
測試步驟
1)打好user、union兩個項目的docker iamge并上傳鏡像倉庫
docker build -t istio-service-union:v1 . docker tag istio-service-union:v1
swr.ap-southeast-1.myhuaweicloud.com/mk-develop/istio-service-union:v1 docker
push swr.ap-southeast-1.myhuaweicloud.com/mk-develop/istio-service-union:v1
docker build -t istio-service-user:v1 . docker tag istio-service-user:v1
swr.ap-southeast-1.myhuaweicloud.com/mk-develop/istio-service-user:v1 docker
push swr.ap-southeast-1.myhuaweicloud.com/mk-develop/istio-service-user:v1
2)?創(chuàng)建ConfigMap配置項
kubectl apply -f config/istio-service-user-v1.yaml kubectl apply -f
config/istio-service-user-v2.yaml kubectl apply -f
config/istio-service-union.yaml
3)發(fā)布負(fù)載、服務(wù)、目標(biāo)規(guī)則、網(wǎng)關(guān)
kubectl apply -f istio-service-user-deployment-v1.yaml #user服務(wù)v1版負(fù)載與目標(biāo)規(guī)則
kubectl apply -f istio-service-user-deployment-v2.yaml #user服務(wù)v2版負(fù)載與目標(biāo)規(guī)則
kubectl apply -f istio-service-user-service.yaml #user服務(wù) kubectl apply -f
istio-service-union-deployment.yaml #union負(fù)載 kubectl apply -f
istio-service-union-service.yaml #union服務(wù) kubectl apply -f
istio-service-union-gateway.yaml #union網(wǎng)關(guān),ingressgateway
第一步我們發(fā)布了應(yīng)用與服務(wù),創(chuàng)建了默認(rèn)規(guī)則,并且通過ingressgateway對外暴露了endpoint,這時候默認(rèn)的目標(biāo)規(guī)則是輪訓(xùn)user服務(wù)的v1和v2版本,我們可以測試幾次發(fā)現(xiàn)變化
{"userVersion":"v1","userException":""} {"userVersion":"v2","userException":""
} {"userVersion":"v1","userException":""} {"userVersion":"v2","userException":""
} ……
?4)創(chuàng)建默認(rèn)路由
kubectl apply -f istio-service-union-virtualservice-v1.yaml #使用v1版本
測試訪問結(jié)果,發(fā)現(xiàn)全部是v1版本
{"userVersion":"v1","userException":""} {"userVersion":"v1","userException":""
} {"userVersion":"v1","userException":""}
{"userVersion":"v1","userException":""} kubectl apply -f
istio-service-union-virtualservice-v2.yaml #使用v2版本
測試訪問結(jié)果,發(fā)現(xiàn)全部是v2版本
{"userVersion":"v2","userException":""} {"userVersion":"v2","userException":""
} {"userVersion":"v2","userException":""}
{"userVersion":"v2","userException":""}
5)流量權(quán)重
kubectl apply -f istio-service-union-virtualservice-weight.yaml
#使用流量權(quán)重路由,v1分70%流量,v2分30%流量
測試訪問結(jié)果,大致相同
{"userVersion":"v1","userException":""} {"userVersion":"v1","userException":""
} {"userVersion":"v1","userException":""} {"userVersion":"v2","userException":""
} {"userVersion":"v1","userException":""} {"userVersion":"v2","userException":""
} {"userVersion":"v1","userException":""} {"userVersion":"v1","userException":""
} {"userVersion":"v2","userException":""}
{"userVersion":"v1","userException":""}
6)金絲雀發(fā)布
演示是跟據(jù)請求頭設(shè)置lab=assion來訪問v2版本,無此請求頭訪問v1版本。
注:因為我們使用的是feign,union通過feign調(diào)用user服務(wù)都不會帶上原始header的,需要做一下feign的透傳把header信息傳遞下去
kubectl apply -f istio-service-union-virtualservice-jsq.yaml #使用金絲雀發(fā)布,http
header頭lab=assion訪問user v2版,不帶訪問user v1版
我們可以用postman測試一下看下效果
?
熱門工具 換一換