什麼是Istio?
Istio是Service Mesh(服務網格)的主流實現方案。該方案降低了與微服務架構相關的複雜性,並提供了負載均衡、服務發現、流量管理、斷路器、監控、故障注入和智能路由等功能特性。
其中,Sidecar模式是一種將應用功能從應用本身剝離出來作爲單獨進程的方式。該模式允許我們嚮應用無侵入添加多種功能,避免了爲滿足第三方組件需求而嚮應用添加額外的配置代碼。從某種意義上來說,服務對於網絡是無感知的,只知道所附加的sidecar代理,它將網絡依賴抽象成了Sidecar。
在Service Mesh中,我們需要了解Data Plane和Control Plane兩個概念:
- Data Plane:作用是處理網格內服務間的通信,並完成服務發現、負載均衡、流量管理、健康檢查等功能;
- Control Plane:作用是管理和配置智能代理用於路由流量,同時配置Mixers來應用策略、收集指標。
Istio核心組件
- Envoy:Istio 使用 Envoy調解服務網格中所有服務的入站和出站流量。屬於數據平面。
- Mixer:負責在服務網格上執行訪問控制和使用策略,以及收集從Envoy和其他服務自動監控到的數據。
- Pilot:爲 Envoy sidecar 提供服務發現功能,爲智能路由(例如 A/B 測試、金絲雀部署等)和彈性(超時、重試、熔斷器等)提供流量管理功能。屬於控制平面。
- Citadel:提供訪問控制和用戶身份認證功能。
Istio可視化管理組件
- Vistio:用於近乎實時地監控應用程序和集羣之間的網絡流量。
- Kiali:提供可視化服務網格拓撲、斷路器和請求率等功能。Kiali還包括 Jaeger Tracing,可以提供開箱即用的分佈式跟蹤功能。
- jaeger:用於展示istio微服務調用鏈關係,以及微服務工作狀態監測。注意,在生產環境中,應使用Elasticsearch或cassandra持久化存儲jaeger數據。
Kiali、Jaeger、Prometheus、Grafana管理工具,將和Istio一併部署。
一、下載istio和helm(istio-1.4.2版不支持helm-3.x)
# mkdir /opt/kubernetes/istio
# ls /opt/software/
helm-v2.16.1-linux-amd64.tar.gz istio-1.4.2-linux.tar.gz
# tar -zxvf helm-v2.16.1-linux-amd64.tar.gz
# cp linux-amd64/helm /opt/kubernetes/bin/
# tar -zxvf istio-1.4.2-linux.tar.gz
# cp istio-1.4.2/bin/istioctl /opt/kubernetes/bin/
# istioctl verify-install //確認環境是否滿足istio安裝要求
二、創建istio命名空間
# kubectl create namespace istio-system
namespace/istio-system created
三、安裝istio自定義資源CRDs
# cd /opt/software/istio-1.4.2
# for i in install/kubernetes/helm/istio-init/files/crd*yaml; do kubectl apply -f $i; done
查看已安裝信息,CRDs創建數必需滿足23個
# kubectl get crds | grep 'istio.io' | wc -l
23
# kubectl get CustomResourceDefinition
NAME CREATED AT
adapters.config.istio.io 2019-12-23T07:46:08Z
attributemanifests.config.istio.io 2019-12-23T07:46:04Z
authorizationpolicies.security.istio.io 2019-12-23T07:46:10Z
certificates.certmanager.k8s.io 2019-12-23T07:46:12Z
challenges.certmanager.k8s.io 2019-12-23T07:46:13Z
clusterissuers.certmanager.k8s.io 2019-12-23T07:46:11Z
clusterrbacconfigs.rbac.istio.io 2019-12-23T07:46:04Z
destinationrules.networking.istio.io 2019-12-23T07:46:04Z
envoyfilters.networking.istio.io 2019-12-23T07:46:05Z
gateways.networking.istio.io 2019-12-23T07:46:05Z
handlers.config.istio.io 2019-12-23T07:46:09Z
httpapispecbindings.config.istio.io 2019-12-23T07:46:05Z
httpapispecs.config.istio.io 2019-12-23T07:46:05Z
ingressroutes.traefik.containo.us 2019-12-19T13:25:45Z
ingressroutetcps.traefik.containo.us 2019-12-19T13:25:45Z
instances.config.istio.io 2019-12-23T07:46:08Z
issuers.certmanager.k8s.io 2019-12-23T07:46:11Z
meshpolicies.authentication.istio.io 2019-12-23T07:46:05Z
middlewares.traefik.containo.us 2019-12-19T13:25:46Z
orders.certmanager.k8s.io 2019-12-23T07:46:12Z
policies.authentication.istio.io 2019-12-23T07:46:05Z
quotaspecbindings.config.istio.io 2019-12-23T07:46:06Z
quotaspecs.config.istio.io 2019-12-23T07:46:06Z
rbacconfigs.rbac.istio.io 2019-12-23T07:46:06Z
rules.config.istio.io 2019-12-23T07:46:07Z
serviceentries.networking.istio.io 2019-12-23T07:46:07Z
servicerolebindings.rbac.istio.io 2019-12-23T07:46:07Z
serviceroles.rbac.istio.io 2019-12-23T07:46:07Z
sidecars.networking.istio.io 2019-12-23T07:46:09Z
templates.config.istio.io 2019-12-23T07:46:08Z
tlsoptions.traefik.containo.us 2019-12-19T13:25:46Z
virtualservices.networking.istio.io 2019-12-23T07:46:08Z
四、部署istio核心組件(部署環境最低配置2cpn,4G memory)
# cd /opt/software/istio-1.4.2
# kubectl apply -f install/kubernetes/istio-demo.yaml
查看創建的資源
# kubectl -n istio-system get pods
NAME READY STATUS RESTARTS AGE
grafana-7797c87688-2bzk5 1/1 Running 0 9m11s
istio-citadel-65c9f49c76-q2w5s 1/1 Running 0 9m8s
istio-egressgateway-5b6cbd4c96-7knwd 0/1 Running 0 9m12s
istio-galley-c5cb9c77d-58fws 1/1 Running 0 9m12s
istio-grafana-post-install-1.4.2-npkhx 0/1 Completed 0 9m15s
istio-ingressgateway-7b66b7c7c-2cq7z 0/1 Running 0 9m11s
istio-pilot-7d5c97cc67-ft78k 1/2 Running 2 9m9s
istio-policy-86775c9966-j9jq7 2/2 Running 6 9m10s
istio-security-post-install-1.4.2-whffb 0/1 Completed 0 9m14s
istio-sidecar-injector-59ccc94d59-82jwp 1/1 Running 0 9m8s
istio-telemetry-68c7b4f9b8-p6rxk 2/2 Running 6 9m10s
istio-tracing-55c965d5b6-snrs9 1/1 Running 0 9m7s
kiali-74fdc898b9-9tv67 1/1 Running 0 9m11s
prometheus-c8fdbd64f-qqs65 1/1 Running 0 9m9s
istio pod功能註釋:
- grafana-* //監控數據可視化工具
- istio-citadel-* //證書管理
- istio-egressgateway-* //出口流量網關
- istio-galley-* //配置檢查
- istio-ingressgateway-* //入口流量網關
- istio-pilot-* //Envoy 服務發現,外部化配置
- istio-policy-* //Mixer 混合器策略檢查
- istio-sidecar-injector-* //邊車注入
- istio-telemetry-* //Mixer混合器指標收集
- kiali-* //Service Mesh可視化工具
- prometheus-* //監控報警
四、實現對外端口暴露
1)確認istio-ingressgateway是否有對外的IP
# kubectl get service istio-ingressgateway -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.1.64.214 <pending> 15020:39304/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15029:43062/TCP,15030:49735/TCP,15031:43992/TCP,15032:48291/TCP,15443:46926/TCP 10m
注:如果 EXTERNAL-IP
有值(IP 地址或主機名),則說明環境中具有可用於Ingress網關的外部負載均衡器,如果 EXTERNAL-IP
值是 <none>
或一直是 <pending>
,則說明可能環境中並沒有爲 Ingress 網關提供外部負載均衡器的功能。
2)編譯istio-ingressgateway添加外部IP
# kubectl edit service istio-ingressgateway -n istio-system
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
creationTimestamp: "2019-12-18T04:09:40Z"
labels:
app: istio-ingressgateway
chart: gateways
heritage: Helm
istio: ingressgateway
release: RELEASE-NAME
name: istio-ingressgateway
namespace: istio-system
resourceVersion: "120335"
selfLink: /api/v1/namespaces/istio-system/services/istio-ingressgateway
uid: 165c8aa1-3210-4b62-bba2-94c196bedfd8
spec:
clusterIP: 10.1.163.208
externalTrafficPolicy: Cluster //在此行下添加如下兩行內容
externalIPs: //需添加內容
- 192.168.168.3 //設置一個nodeIP或k8s-master IP
ports:
- name: status-port
nodePort: 42858
port: 15020
protocol: TCP
targetPort: 15020
查看配置後信息
# kubectl get service istio-ingressgateway -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-ingressgateway LoadBalancer 10.1.163.208 192.168.168.3 15020:42858/TCP,80:31380/TCP,443:31390/TCP,31400:31400/TCP,15029:32548/TCP,15030:35183/TCP,15031:34070/TCP,15032:45177/TCP,15443:44470/TCP 135m
五、對default namespace使用自動注入sidecar功能
# kubectl label namespace default istio-injection=enable
namespace/default labeled
如要禁止defaule namespace自動注入sidecar功能,則使用
# kubectl label namespace default istio-injection-
說明:disabled - Sidecar 注入器缺省不會向 Pod 進行注入。在 Pod 模板中加入 sidecar.istio.io/inject 註解並賦值爲 true 才能啓用注入。
enabled - Sidecar 注入器缺省會對 Pod 進行注入。在 Pod 模板中加入 sidecar.istio.io/inject 註解並賦值爲 false 就會阻止對這一 Pod 的注入。
查看添加istio注入後namespace信息
# kubectl get namespace -L istio-injection
NAME STATUS AGE ISTIO-INJECTION
default Active 11d enable
istio-system Active 15m
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
六、創建名爲kiali的secret
# echo -n 'admin' | base64
YWRtaW4=
# echo -n 'admin@123' | base64
YWRtaW5AMTIz
# cat > /opt/kubernetes/istio/kiali-secret.yaml << EOF
apiVersion: v1
kind: Secret
metadata:
name: kiali
namespace: istio-system
labels:
app: kiali
type: Opaque
data:
username: YWRtaW4=
passphrase: YWRtaW5AMTIz
EOF
# kubectl apply -f /opt/kubernetes/istio/kiali-secret.yaml
secret/kiali created