1、istio-允許/禁用sidecar設置
1.1 在namespace設置自動注入:
給 zmc 命名空間設置標簽:istio-injection=enabled:
[root@master1 ~]# kubectl create ns zmc namespace/zmc created [root@master1 ~]# kubectl label namespace zmc istio-injection=enabled namespace/zmc labeled [root@master1 ~]# kubectl get namespace -L istio-injection NAME STATUS AGE ISTIO-INJECTION default Active 31h istio-system Active 13h kube-node-lease Active 31h kube-public Active 31h kube-system Active 31h zmc Active 27s enabled
在zmc命令空間下創建如下deployment:
kind: Deployment apiVersion: apps/v1 metadata: name: nginx-v1 labels: app: nginx version: v1 spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: container-rl4wdf image: 'nginx:latest' ports: - name: tcp-80 containerPort: 80 protocol: TCP imagePullPolicy: IfNotPresent
給namespace設置istio-injection=enabled標簽后,這樣就會在 Pod 創建時觸發 Sidecar 的注入過程了,被注入 Sidecar 的 Pod 會有兩個容器:
查看被注入的 Pod 的細節。不難發現多出了一個 istio-proxy 容器及其對應的存儲卷。注意用正確的 Pod 名稱來執行下面的命令:
kubectl describe pod -n=zmc nginx-v1-fb75db6b7-w4n5b
... Events: Type Reason Age From Message ---- ------ ---- ---- ------- ... Normal Created 11s kubelet Created container istio-init Normal Started 11s kubelet Started container istio-init ... Normal Created 10s kubelet Created container sleep Normal Started 10s kubelet Started container sleep ... Normal Created 9s kubelet Created container istio-proxy Normal Started 8s kubelet Started container istio-proxy
禁用zmc命名空間的自動注入功能,然后檢查新建 Pod 就不帶有 Sidecar 容器了:
[root@master1 ~]# kubectl label namespace zmc istio-injection- namespace/zmc labeled [root@master1 ~]# kubectl delete pod -n=zmc nginx-v1-fb75db6b7-w4n5b pod "nginx-v1-fb75db6b7-w4n5b" deleted [root@master1 ~]# kubectl get pods -n=zmc NAME READY STATUS RESTARTS AGE nginx-v1-fb75db6b7-2f2gl 1/1 Running 0 26s
1.2 控制注入策略:
在1.1示例中,在命名空間級別啟用和禁用了注入。通過在 pod 上配置sidecar.istio.io/inject
標簽(對於istio1.12.1版本這里一定要注意是標簽不是注解),還可以在每個 pod 的基礎上控制注入:
資源 | 標簽 | 啟用值 | 禁用值 |
---|---|---|---|
Namespace | istio-injection |
enabled |
disabled |
Pod | sidecar.istio.io/inject |
"true" |
"false" |
注入的配置如下:
- 如果禁用其中一個標簽,則不注入 Pod
- 如果啟用其中一個標簽,則注入 Pod
- 如果兩個標簽都沒有設置,如果啟用
.values.sidecarInjectorWebhook.enableNamespacesByDefault
, 則會注入 Pod。在默認情況下是不啟用的,所以 Pod 通常不會被注入。
示例1:istio-injection=disabled,sidecar.istio.io/inject="true":
[root@master1 ~]# kubectl label namespace zmc istio-injection=disabled namespace/zmc labeled [root@master1 ~]# kubectl get pods -n=zmc No resources found in zmc namespace.
在zmc命令空間下創建如下deployment:
kind: Deployment apiVersion: apps/v1 metadata: name: nginx-v1 labels: app: nginx version: v1 spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: sidecar.istio.io/inject: 'true' app: nginx spec: containers: - name: container-rl4wdf image: 'nginx:latest' ports: - name: tcp-80 containerPort: 80 protocol: TCP imagePullPolicy: IfNotPresent
結果如下,由此可以得出結論如果任一標簽被禁用,則不會注入 pod:
[root@master1 ~]# kubectl get pods -n=zmc NAME READY STATUS RESTARTS AGE nginx-v1-547569d76b-l8dk4 1/1 Running 0 2s
示例2:namespace無istio-injection=disabled標簽,sidecar.istio.io/inject="true":
[root@master1 ~]# kubectl label namespace zmc istio-injection- namespace/zmc labeled [root@master1 ~]# kubectl get pods -n=zmc No resources found in zmc namespace.
在zmc命令空間下創建和示例一配置文件一樣的deployment,結果如下,由此可以得出結論如果啟用了任一標簽,則注入 pod:
[root@master1 ~]# kubectl get pods -n=zmc NAME READY STATUS RESTARTS AGE nginx-v1-547569d76b-58hgv 2/2 Running 0 8s
示例三:啟用.values.sidecarInjectorWebhook.enableNamespacesByDefault(目前測試結果與官方文檔不一致)
如果兩個標簽都未設置,不會注入Pod,例如我們新建一個namespace不加任何標簽,然后在此namespace下新建一個Pod也不加任何標簽,發現創建出來的Pod只有一個容器,由此可以得出默認情況下如果兩個標簽都未設置,不會注入Pod,此例子比較簡單本文就不再測試。
接下來我們啟用.values.sidecarInjectorWebhook.enableNamespacesByDefault來驗證在此參數啟用后默認注入pod。
使用如下命令調整istio注入參數值修改:
kubectl edit configmaps -n=istio-system istio-sidecar-injector
將enableNamespacesByDefault參數值由默認false改成true。
"sidecarInjectorWebhook": { "alwaysInjectSelector": [], "defaultTemplates": [], "enableNamespacesByDefault": false, "injectedAnnotations": {}, "neverInjectSelector": [], "objectSelector": { "autoInject": true, "enabled": true }, "rewriteAppHTTPProbe": true, "templates": {} } } kind: ConfigMap
按照官方文檔修改enableNamespacesByDefault參數之后,重啟istiod服務,創建新的namespace,在此namespace新建pod,都未指定任何標簽,發現創建出來的Pod只有一個容器與預期結果不一致,由於開啟enableNamespacesByDefault場景暫時沒想到,此示例不再繼續測試。
2、Istio Sidecar自動注入配置
Sidecar 自動注入機制是將 sidecar 代理自動添加到用戶創建的 pod。
它使用 MutatingWebhook
機制在 pod 創建的時候將 sidecar 的容器和卷添加到每個 pod 的模版里。
用戶可以通過 webhooks namespaceSelector
機制來限定需要啟動自動注入的范圍,也可以通過注解的方式針對每個 pod 來單獨啟用和禁用自動注入功能。
Sidecar 是否會被自動注入取決於下面 3 條配置和 2 條安全規則:
配置:
- webhooks
namespaceSelector
- 默認策略
policy
- pod 級別的覆蓋注解
安全規則:
- sidecar 默認不能被注入到
kube-system
和kube-public
這兩個 namespace - sidecar 不能被注入到使用
host network
網絡的 pod 里
下面的表格展示了基於上述三個配置條件的最終注入狀態。上述的安全規則不會被覆蓋。
namespaceSelector 匹配 |
默認策略 | Pod 覆蓋 sidecar.istio.io/inject 注解 |
Sidecar 是否注入? |
---|---|---|---|
yes | enabled | true (default) | yes |
yes | enabled | false | no |
yes | disabled | true | yes |
yes | disabled | false (default) | no |
no | enabled | true (default) | no |
no | enabled | false | no |
no | disabled | true | no |
no | disabled | false (default) | no |
Istio1.12.1通過istio-revision-tag-default和istio-sidecar-injector這兩個mutatingwebhookconfigurations來限定需要啟動自動注入的范圍,邏輯比較簡單,本文不再介紹,注入結果和上述表格一致,想了解更多webhookconfigurations內容請參見:Kubernetes AdmissionWebhook 、 Kubernetes構建自定義admission webhook 和Kubernetes AdmissionWebhook匹配請求這三篇文章。
參考:https://istio.io/latest/zh/docs/setup/additional-setup/sidecar-injection/
參考:https://istio.io/latest/zh/docs/ops/configuration/mesh/injection-concepts/