k8s+istio:流量控制之灰度發佈

經過Kubernetes+Istio的流量控制實現灰度發佈,主要演示經過流量權重實現藍綠,經過http自定義頭實現金絲雀git

 

準備環境github

k8s和istio不想本身裝的話能夠在雲上買個按量付費集羣,用完即刪,推薦華爲雲。spring

項目中用到的代碼docker

用的springboot+springcloud feign作rest強類型調用,放到github了api

https://github.com/assionyang/istio-test.git

代碼結構說明springboot

istio-service-union   #聚合服務項目,用來測試調用user服務,也作爲入口
|-Dockerfile  #dockerfile
istio-service-user  #用戶服務,用來演示版本切換
|-Dockerfile  #dockerfile
istio-service-user-api #類庫,使用feign暴露client與dto,union服務依賴user-api
k8s #k8s&istio發佈文件目錄
|-config
   |- istio-service-union.yaml   # union服務ConfigMap
   |- istio-service-user-v1.yaml # user服務v1版本ConfigMap
   |- istio-service-user-v2.yaml # user服務v2版本ConfigMap
|- istio-service-union-deployment.yaml #union無狀態發佈
|- istio-service-union-service.yaml # union服務
|- istio-service-gateway.yaml # ingress網關,對外暴露union
|- istio-service-user-deployment-v1.yaml # user版本v1無狀態發佈
|- istio-service-user-deployment-v2.yaml # user版本v2無狀態發佈
|- istio-service-user-service.yaml # user服務
|- istio-service-user-virtualservice-v1.yaml #  user路由到v1版
|- istio-service-user-virtualservice-v2.yaml # user路由到v2版
|- istio-service-user-virtualservice-weight.yaml # user路由流量權重
|- istio-service-user-virtualservice-jsq.yaml # user路由金絲雀

測試步驟app

1)打好user、union兩個項目的docker iamge並上傳鏡像倉庫post

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) 建立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)發佈負載、服務、目標規則、網關ui

kubectl apply -f istio-service-user-deployment-v1.yaml #user服務v1版負載與目標規則
kubectl apply -f istio-service-user-deployment-v2.yaml #user服務v2版負載與目標規則
kubectl apply -f istio-service-user-service.yaml #user服務
kubectl apply -f istio-service-union-deployment.yaml #union負載
kubectl apply -f istio-service-union-service.yaml #union服務
kubectl apply -f istio-service-union-gateway.yaml #union網關,ingressgateway

第一步咱們發佈了應用與服務,建立了默認規則,而且經過ingressgateway對外暴露了endpoint,這時候默認的目標規則是輪訓user服務的v1和v2版本,咱們能夠測試幾回發現變化

{"userVersion":"v1","userException":""}
{"userVersion":"v2","userException":""} {"userVersion":"v1","userException":""} {"userVersion":"v2","userException":""} ……

 4)建立默認路由

kubectl apply -f istio-service-union-virtualservice-v1.yaml #使用v1版本

測試訪問結果,發現所有是v1版本

{"userVersion":"v1","userException":""}
{"userVersion":"v1","userException":""} {"userVersion":"v1","userException":""} {"userVersion":"v1","userException":""}
kubectl apply -f istio-service-union-virtualservice-v2.yaml #使用v2版本

測試訪問結果,發現所有是v2版本

{"userVersion":"v2","userException":""}
{"userVersion":"v2","userException":""} {"userVersion":"v2","userException":""} {"userVersion":"v2","userException":""}

5)流量權重

kubectl apply -f istio-service-union-virtualservice-weight.yaml #使用流量權重路由,v1分70%流量,v2分30%流量

測試訪問結果,大體相同

{"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)金絲雀發佈

演示是跟據請求頭設置lab=assion來訪問v2版本,無此請求頭訪問v1版本。

注:由於咱們使用的是feign,union經過feign調用user服務都不會帶上原始header的,須要作一下feign的透傳把header信息傳遞下去

kubectl apply -f istio-service-union-virtualservice-jsq.yaml #使用金絲雀發佈,http header頭lab=assion訪問user v2版,不帶訪問user v1版

咱們能夠用postman測試一下看下效果

相關文章
相關標籤/搜索