服務網格:創建 Ingress Gateway
將 Bookinfo 應用部署到 default 命名空間下,請為 Bookinfo 應用創建一個網 關,使外部可以訪問 Bookinfo 應用。
上傳ServiceMesh.tar.gz包
[root@k8s-master-node1 ~]# tar -zxvf ServiceMesh.tar.gz
[root@k8s-master-node1 ~]# cd ServiceMesh/images/
[root@k8s-master-node1 images]# docker load -i image.tar
部署Bookinfo應用到kubernetes集群:
[root@k8s-master-node1 images]# cd /root/ServiceMesh/
[root@k8s-master-node1 ServiceMesh]# kubectl apply -f bookinfo/bookinfo.yaml
service/details created
serviceaccount/bookinfo-details created
deployment.apps/details-v1 created
service/ratings created
serviceaccount/bookinfo-ratings created
deployment.apps/ratings-v1 created
service/reviews created
serviceaccount/bookinfo-reviews created
deployment.apps/reviews-v1 created
service/productpage created
serviceaccount/bookinfo-productpage created
deployment.apps/productpage-v1 created
[root@k8s-master-node1 ServiceMesh]# kubectl get pod
NAME READY STATUS RESTARTS AGE
details-v1-79f774bdb9-kndm9 1/1 Running 0 7s
productpage-v1-6b746f74dc-bswbx 1/1 Running 0 7s
ratings-v1-b6994bb9-6hqfn 1/1 Running 0 7s
reviews-v1-545db77b95-j72x5 1/1 Running 0 7s
[root@k8s-master-node1 ServiceMesh]# vim bookinfo-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: bookinfo-gateway
spec:selector:istio: ingressgatewayservers:- port:number: 80name: httpprotocol: HTTPhosts: - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: bookinfo
spec:hosts: - "*" gateways:- bookinfo-gatewayhttp:- match: - uri:exact: /productpage - uri:prefix: /static- uri:exact: /login- uri:exact: /logout- uri:prefix: /api/v1/productsroute: # 定義路由轉發目的地列表- destination:host: productpageport:number: 9080
[root@k8s-master-node1 ServiceMesh]# kubectl apply -f bookinfo-gateway.yaml
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created
[root@k8s-master-node1 ServiceMesh]#kubectl get VirtualService bookinfo -o yaml
bookinfo-gateway || exact: /productpage || destination || host: productpage || number: 9080
[root@k8s-master-node1 ServiceMesh]#kubectl get gateway bookinfo-gateway -o yaml
istio: ingressgateway
服務網格:路由管理
將 Bookinfo 應用部署到 default 命名空間下,應用默認請求路由,將所有流 量路由到各個微服務的 v1 版本。然后更改請求路由 reviews,將指定比例的流量 從 reviews 的 v1 轉移到 v3。
[root@k8s-master-node1 ServiceMesh]# vim route.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: reviews-route
spec:hosts: # 將流量路由到指定主機- reviewshttp:- name: "v1"route: # 定義路由轉發目的地列表(所有http流量都會被路由到標簽為version:v1的reviews服務上)- destination:host: reviewssubset: v1- name: "v2"match:- uri:prefix: "/wpcatalog"- uri:prefix: "/consumercatalog"rewrite: # 定義重寫HTTP URL 或 Authority headers,不能與重定向同時配置,重寫操作會在轉發前執行uri: "/newcatalog"route:- destination:host: reviewssubset: v2
[root@k8s-master-node1 ServiceMesh]# kubectl apply -f route.yaml
virtualservice.networking.istio.io/reviews-route created