1.3 Istio实验
1.3.1 Gateway和VirtualService实验
-
开启Istio sidecar功能
root@k8s-master-1:~# kubectl label namespace default istio-injection=enabled
-
部署helloworld app
root@k8s-master-1:~# export VERSION=$(ls ~ | grep istio) root@k8s-master-1:~# cd ~/$VERSION/samples/helloworld/ root@k8s-master-1:~# kubectl apply -f helloworld.yaml service/helloworld created deployment.apps/helloworld-v1 created deployment.apps/helloworld-v2 created root@k8s-master-1:/app/istio/samples/helloworld# kubectl get pod NAME READY STATUS RESTARTS AGE helloworld-v1-7459d7b54b-6r85q 2/2 Running 0 65s helloworld-v2-654d97458-6mff5 2/2 Running 0 65s root@k8s-master-1:/app/istio/samples/helloworld# kubectl get svc helloworld -L app NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE APP helloworld ClusterIP 10.10.226.198 <none> 5000/TCP 3m25s helloworld
-
部署Gateway和VirtualService
root@k8s-master-1:/app/istio/samples/helloworld# cat helloworld-gateway.yaml apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: helloworld-gateway spec: selector: istio: ingressgateway # use istio default controller servers: - port: number: 80 name: http protocol: HTTP hosts: - "*" --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: helloworld spec: hosts: - "*" gateways: - helloworld-gateway http: - match: - uri: exact: /hello route: - destination: host: helloworld port: number: 5000 root@k8s-master-1:/app/istio/samples/helloworld# kubectl apply -f helloworld-gateway.yaml gateway.networking.istio.io/helloworld-gateway created virtualservice.networking.istio.io/helloworld created root@k8s-master-1:/app/istio/samples/helloworld# kubectl get gw,vs NAME AGE gateway.networking.istio.io/helloworld-gateway 6s NAME GATEWAYS HOSTS AGE virtualservice.networking.istio.io/helloworld ["helloworld-gateway"] ["*"] 6s # 注释:当访问 istio-ingressgateway SVC 的 80/tcp 端口/hello,流量转发到 helloword SVC 的 5000/tcp
-
测试
root@k8s-master-1:/app/istio/samples/helloworld# while true; do curl -s -o /dev/null "http://192.168.4.18:31181/hello"; done
1.3.2 DestinationRule实验
-
通过 DestinationRule 把流量调度到 helloworld-v1 版本
root@k8s-master-1:/app/istio/samples/helloworld# cat helloworld-dr-all-v1.yaml --- apiVersion: networking.istio.io/v1beta1 kind: DestinationRule metadata: name: helloworld-destination spec: host: helloworld subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2 --- apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: helloworld spec: hosts: - "*" gateways: - helloworld-gateway http: - match: - uri: exact: /hello route: - destination: host: helloworld port: number: 5000 subset: v1
-
验证访问测试
root@k8s-master-1:/app/istio/samples/helloworld# while true; do curl -s -o /dev/null "http://192.168.4.18:31181/hello"; done
-
通过 DestinationRule 把 80%的访问请求调度到 helloworld-v1 版本,20%的访问请求调度到 helloworld-v2 版本
root@k8s-master-1:/app/istio/samples/helloworld# cat helloworld-dr-20-v2.yaml --- apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: helloworld spec: hosts: - "*" gateways: - helloworld-gateway http: - match: - uri: exact: /hello route: - destination: host: helloworld port: number: 5000 subset: v1 weight: 80 - destination: host: helloworld port: number: 5000 subset: v2 weight: 20
1.3.3 使用Istio进行金丝雀部署
-
应用 Istio 规则将 10%流量交 v2 版本
root@k8s-master-1:~# cat helloworld-canary-allin1-10v2.yaml apiVersion: networking.istio.io/v1beta1 kind: Gateway metadata: name: helloworld-gateway spec: selector: istio: ingressgateway # use istio default controller servers: - port: number: 80 name: http protocol: HTTP hosts: - "*" --- apiVersion: networking.istio.io/v1beta1 kind: DestinationRule metadata: name: helloworld-destination spec: host: helloworld subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2 --- apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: helloworld spec: hosts: - "*" gateways: - helloworld-gateway http: - match: - uri: exact: /hello route: - destination: host: helloworld port: number: 5000 subset: v1 weight: 90 - destination: host: helloworld port: number: 5000 subset: v2 weight: 10 root@k8s-master-1:~# kubectl apply -f helloworld-canary-allin1-10v2.yaml
-
部署中的自动缩放
root@k8s-master-1:~# kubectl autoscale deployment helloworld-v1 --cpu-percent=50 --min=1 --max=10 root@k8s-master-1:~# kubectl autoscale deployment helloworld-v2 --cpu-percent=50 --min=1 --max=10 root@k8s-master-1:~# kubectl get hpa -w
-
调整 v1 和 v2 版本的权重各为 50%
root@k8s-master-1:~# cat helloworld-canary-vs-50v2.yaml
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: helloworld
spec:
hosts:
- "*"
gateways:
- helloworld-gateway
http:
- match:
- uri:
exact: /hello
route:
- destination:
host: helloworld
port:
number: 5000
subset: v1
weight: 50
- destination:
host: helloworld
port:
number: 5000
subset: v2
weight: 50
-
聚焦金丝雀测试
root@k8s-master-1:~# cat specific-vs.yaml apiVersion: networking.istio.io/v1beta1 kind: VirtualService metadata: name: helloworld spec: hosts: - "*" gateways: - helloworld-gateway http: - match: - headers: cookie: regex: "^(.*?;)?(email=[^;]*@some-company-name.com)(;.*)?$" end-user: exact: jason uri: exact: /hello route: - destination: host: helloworld port: number: 5000 subset: v1 weight: 50 - destination: host: helloworld port: number: 5000 subset: v2 weight: 50 - route: - destination: host: helloworld port: number: 5000 subset: v1