Service Mesh 的中文譯爲 「服務網格」 ,是一個用於處理服務和服務之間通訊的基礎設施層,它負責爲構建複雜的雲原生應用傳遞可靠的網絡請求,併爲服務通訊實現了微服務所需的基本組件功能。例如: 服務發現、負載均衡、監控、流量管理、訪問控制等。在實踐中,服務網格一般實現爲一組和應用程序部署在一塊兒的輕量級的網絡代理,但對應用程序來講是透明的。html
Isito是Service Mesh的產品化落地,是目前最受歡迎的服務網格,功能豐富、成熟度高。
Linkerd是世界上第一個服務網格類的產品。node
官方站點: https://istio.io/linux
Istio 有 4 個配置資源,落地全部流量管理需求:nginx
- VirtualService:實現服務請求路由規則的功能。
- DestinationRule:實現目標服務的負載均衡、服務發現、故障處理和故障注入的功能。
- Gateway:讓服務網格內的服務,能夠被全世界看到。
- ServiceEntry :讓服務網格內的服務,能夠看到外面的世界
$ wget https://github.com/istio/istio/releases/download/1.4.2/istio-1.4.2-linux.tar.gz $ tar zxvf istio-1.4.2-linux.tar.gz $ cd istio-1.4.2 $ mv bin/istioctl /usr/bin $ istioctl manifest apply --set profile=demo $ kubectl get pods -n istio-system $ kubectl get svc -n istio-system
卸載:git
istioctl manifest generate --set profile=demo | kubectl delete -f -
做用: 將應用接入sidecar管理github
$ cd istio-1.4.2/samples/httpbin $ kubectl apply -f httpbin-nodeport.yaml #nodeport服務,默認是在default ns $ kubectl get pod,svc
訪問測試:http://192.168.56.11:30815/web
kubectl apply -f <(istioctl kube-inject -f httpbin-nodeport.yaml) 或者 istioctl kube-inject -f httpbin-nodeport.yaml |kubectl apply -f - $ kubectl get pod NAME READY STATUS RESTARTS AGE httpbin-77bfc6b755-k8pjb 1/1 Running 0 8m3s httpbin-79bf7bbcd4-mjmjc 0/2 PodInitializing 0 11s
$ kubectl label namespace default istio-injection=enabled $ kubectl apply -f httpbin-gateway.yaml $ kubectl get gateway NAME AGE httpbin-gateway 3m21s
$ kubectl get svc -n istio-system istio-ingressgateway LoadBalancer 10.0.0.114 <pending> 15020:32361/TCP,80:31929/TCP,443:31088/TCP,15029:31493/TCP,15030:32677/TCP,15031:30048/TCP,15032:32207/TCP,15443:30034/TCP 75m
訪問K8SNODEIP:31929 便可訪問,此線路是走的SideCar的gatewayvim
kubectl create ns bookinfo kubectl label namespace bookinfo istio-injection=enabled #sidecar自動注入開啓 cd istio-1.4.2/samples/bookinfo/ kubectl apply -f platform/kube/bookinfo.yaml -n bookinfo kubectl apply -f networking/bookinfo-gateway.yaml -n bookinfo kubectl get svc -n istio-system| grep ingress #查到80對應31929的端口暴露
訪問http://192.168.56.11:31929/productpageapi
$ vim /etc/nginx/nginx.conf upstream ingressgateway { server 192.168.56.11:31929; server 192.168.56.12:31929; server 192.168.56.13:31929; } server { listen 80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { proxy_pass http://ingressgateway; proxy_set_header Host $host; proxy_http_version 1.1; } ..... $ nginx -t $ nginx
$ vim /etc/hosts 192.168.56.12 bookinfo.cropy.cn
$ cd ~/istio-1.4.2/samples/bookinfo $ vim networking/bookinfo-gateway.yaml apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: bookinfo-gateway spec: selector: istio: ingressgateway # use istio default controller servers: - port: number: 80 name: http protocol: HTTP hosts: - "bookinfo.cropy.cn" --- apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: bookinfo spec: hosts: - "bookinfo.cropy.cn" gateways: 。。。。 $ kubectl apply -f networking/bookinfo-gateway.yaml -n bookinfo
項目邏輯上分爲AB組,在項目升級時,首先把A組從負載均衡中摘除,進行新版本的部署。B組仍然繼續提供服務。A組升級完成上線,B組從負載均衡中摘除。安全
每次只升級一個或多個服務,升級完成後加入生產環境,不斷執行這個過程,直到集羣中的所有舊版升級新版本。Kubernetes的默認發佈策略。
只升級部分服務,即讓一部分用戶繼續用老版本,一 部分用戶開始用新版本,若是用戶對新版本沒有什麼 意見,那麼逐步擴大範圍,把全部用戶都遷移到新版 本上面來。
灰度發佈的一種方式,主要對特定用戶採樣後,對收 集到的反饋數據作相關對比,而後根據比對結果做出 決策。用來測試應用功能表現的方法, 側重應用的可用性,受歡迎程度等,最後決定是否升級。
流量所有發送到reviews v1版本(不帶五角星)
將90%的流量發送到reviews v1版本,另外10%的流量發送到reviews v2版本(5個黑色五星),最後徹底切換到v2版本
$ cd ~/istio-1.4.2/samples/bookinfo $ kubectl apply -f networking/virtual-service-all-v1.yaml -n bookinfo $ kubectl apply -f networking/destination-rule-all.yaml -n bookinfo $ kubectl apply -f networking/virtual-service-reviews-90-10.yaml -n bookinfo $ kubectl apply -f networking/virtual-service-reviews-v2-v3.yaml -n bookinfo
任務:
$ kubectl apply -f networking/virtual-service-reviews-jason-v2-v3.yaml