# Istio 金絲雀部屬 * Canary是一種發布策略,藍綠部署是準備兩套系統,在兩套系統之間進行切換,金絲雀策略是只有一套系統,逐漸替換這套系統。 * 簡而言之Istio Canary就是一種分流機制他也是做用在VirtualService DestinationRule底下的一種功能透過Service Deployment的labels標籤app version來對應控制該服務所需要流向的端點,以上幾種的組合就可以實踐出服務權重流向的功能。  ## 實作 * 需先安裝好 istio * 在 test namespace 設定 istio auto injection ``` $ kubectl create ns test $ kubectl label namespace test istio-injection=enabled ``` * 部屬 v1 版本 ``` $ echo 'apiVersion: apps/v1 kind: Deployment metadata: name: helloworld-v1 namespace: test spec: replicas: 1 selector: matchLabels: app: helloworld version: v1 template: metadata: labels: app: helloworld version: v1 spec: containers: - name: app image: quay.io/flysangel/image:app.golang' | kubectl apply -f - ``` * 部屬 v2 版本 ``` $ echo 'apiVersion: apps/v1 kind: Deployment metadata: name: helloworld-v2 namespace: test spec: replicas: 1 selector: matchLabels: app: helloworld version: v2 template: metadata: labels: app: helloworld version: v2 spec: containers: - name: app image: quay.io/flysangel/image:app.golang' | kubectl apply -f - ``` * 建立 svc ``` $ echo 'apiVersion: v1 kind: Service metadata: name: helloworld namespace: test spec: selector: app: helloworld ports: - protocol: TCP port: 80 targetPort: 8080' | kubectl apply -f - ``` * 檢查 ``` $ kubectl -n test get all NAME READY STATUS RESTARTS AGE pod/helloworld-v1-688bdcf9f8-sx975 2/2 Running 0 42s pod/helloworld-v2-84d5d87fff-2qchg 2/2 Running 0 27s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/helloworld ClusterIP 10.43.201.252 <none> 80/TCP 7s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/helloworld-v1 1/1 1 1 42s deployment.apps/helloworld-v2 1/1 1 1 27s NAME DESIRED CURRENT READY AGE replicaset.apps/helloworld-v1-688bdcf9f8 1 1 1 42s replicaset.apps/helloworld-v2-84d5d87fff 1 1 1 27s ``` ## 建立 gateway ``` $ echo 'apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: canary-gateway namespace: test spec: selector: istio: ingressgateway servers: - port: name: http number: 80 # ingress-gateway port num protocol: HTTP hosts: - "*"' | kubectl apply -f - ``` ``` $ kubectl -n test get gateway.networking NAME AGE canary-gateway 18s ``` ## 建立 VirtualService * 設計 90% 的流量到 v1,10% 的流量到 v2 ``` $ echo 'apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: helloworld namespace: test spec: gateways: - canary-gateway hosts: - "*" http: - route: - destination: host: helloworld.test.svc.cluster.local subset: v1 weight: 90 - destination: host: helloworld.test.svc.cluster.local subset: v2 weight: 10' | kubectl apply -f - ``` ``` $ kubectl -n test get virtualService NAME GATEWAYS HOSTS AGE helloworld ["canary-gateway"] ["*"] 38s ``` ## 建立 DestinationRule ``` $ echo 'apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: helloworld namespace: test spec: host: helloworld.test.svc.cluster.local subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2' | kubectl apply -f - ``` ``` $ kubectl -n test get destinationRule NAME HOST AGE helloworld helloworld.test.svc.cluster.local 4s ``` ## 驗證 ``` $ kubectl get svc -n istio-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE istio-ingressgateway NodePort 10.43.10.193 <none> 15021:30214/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15443:32475/TCP 63d istiod ClusterIP 10.43.113.109 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 63d jaeger-collector ClusterIP 10.43.81.157 <none> 14268/TCP,14250/TCP 63d kiali ClusterIP 10.43.26.41 <none> 20001/TCP,9090/TCP 63d tracing ClusterIP 10.43.98.84 <none> 16686/TCP 63d zipkin ClusterIP 10.43.225.85 <none> 9411/TCP 63d ``` * 使用 nodeport 方式 curl,90% 流量會到 v1,10% 流量會到 v2 ``` $ curl -w "\n" 192.168.11.110:31380/hostname {"message":"helloworld-v1-688bdcf9f8-sx975"} $ curl -w "\n" 192.168.11.110:31380/hostname {"message":"helloworld-v1-688bdcf9f8-sx975"} $ curl -w "\n" 192.168.11.110:31380/hostname {"message":"helloworld-v1-688bdcf9f8-sx975"} $ curl -w "\n" 192.168.11.110:31380/hostname {"message":"helloworld-v1-688bdcf9f8-sx975"} $ curl -w "\n" 192.168.11.110:31380/hostname {"message":"helloworld-v1-688bdcf9f8-sx975"} $ curl -w "\n" 192.168.11.110:31380/hostname {"message":"helloworld-v1-688bdcf9f8-sx975"} $ curl -w "\n" 192.168.11.110:31380/hostname {"message":"helloworld-v2-84d5d87fff-2qchg"} ``` ``` $ while true; do curl -w "\n" http://192.168.11.110:31380/hostname; sleep 0.1; done ``` * 可以在 istio 介面上可以看到流量分布   ## 參考文件 https://blog.csdn.net/qq_41586875/article/details/124384114
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up