安裝 Sidecar

注入

為了充分利用 Istio 的所有功能,網格中的 Pod 必須執行 Istio sidecar 代理程式。

以下各節說明將 Istio sidecar 注入 Pod 的兩種方式:在 Pod 的命名空間中啟用自動 Istio sidecar 注入,或手動使用 istioctl 命令。

當在 Pod 的命名空間中啟用時,自動注入會在 Pod 建立時使用許可控制器注入代理程式設定。

手動注入會透過將代理程式設定新增至其中來直接修改設定 (例如部署)。

如果您不確定該使用哪一種方式,建議使用自動注入。

自動 sidecar 注入

可以使用 Istio 提供的變異式 Webhook 准入控制器,將 Sidecar 自動新增至適用的 Kubernetes Pod。

當您在命名空間上設定 istio-injection=enabled 標籤,並且啟用注入 webhook 時,任何在該命名空間中建立的新 Pod 都會自動新增 Sidecar。

請注意,與手動注入不同,自動注入是在 Pod 層級發生的。您不會看到部署本身有任何變更。相反地,您需要檢查個別 Pod(透過 kubectl describe)才能看到注入的 Proxy。

部署應用程式

部署 curl 應用程式。驗證部署和 Pod 都只有一個容器。

壓縮
$ kubectl apply -f @samples/curl/curl.yaml@
$ kubectl get deployment -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                    SELECTOR
curl    1/1     1            1           12s   curl         curlimages/curl           app=curl
$ kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
curl-8f795f47d-hdcgs    1/1     Running   0          42s

使用 istio-injection=enabled 標籤標記 default 命名空間。

$ kubectl label namespace default istio-injection=enabled --overwrite
$ kubectl get namespace -L istio-injection
NAME                 STATUS   AGE     ISTIO-INJECTION
default              Active   5m9s    enabled
...

注入會在 Pod 建立時發生。終止正在執行的 Pod,並驗證是否建立了一個新的 Pod,其中注入了 Sidecar。原始 Pod 有 1/1 READY 容器,而注入 Sidecar 的 Pod 有 2/2 READY 容器。

$ kubectl delete pod -l app=curl
$ kubectl get pod -l app=curl
pod "curl-776b7bcdcd-7hpnk" deleted
NAME                     READY     STATUS        RESTARTS   AGE
curl-776b7bcdcd-7hpnk    1/1       Terminating   0          1m
curl-776b7bcdcd-bhn9m    2/2       Running       0          7s

檢視注入 Pod 的詳細狀態。您應該看到注入的 istio-proxy 容器和對應的卷。

$ kubectl describe pod -l app=curl
...
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 curl
  Normal  Started    10s   kubelet            Started container curl
  ...
  Normal  Created    9s    kubelet            Created container istio-proxy
  Normal  Started    8s    kubelet            Started container istio-proxy

停用 default 命名空間的注入,並驗證新的 Pod 是否在沒有 Sidecar 的情況下建立。

$ kubectl label namespace default istio-injection-
$ kubectl delete pod -l app=curl
$ kubectl get pod
namespace/default labeled
pod "curl-776b7bcdcd-bhn9m" deleted
NAME                     READY     STATUS        RESTARTS   AGE
curl-776b7bcdcd-bhn9m    2/2       Terminating   0          2m
curl-776b7bcdcd-gmvnr    1/1       Running       0          2s

控制注入政策

在上面的範例中,您在命名空間層級啟用和停用了注入。也可以透過設定 Pod 上的 sidecar.istio.io/inject 標籤,在每個 Pod 的基礎上控制注入。

資源標籤啟用值停用值
命名空間istio-injectionenableddisabled
Podsidecar.istio.io/inject"true""false"

如果您正在使用控制平面修訂版,則會改用符合的 istio.io/rev 標籤,以使用特定修訂版的標籤。例如,對於名為 canary 的修訂版:

資源啟用標籤停用標籤
命名空間istio.io/rev=canaryistio-injection=disabled
Podistio.io/rev=canarysidecar.istio.io/inject="false"

如果 istio-injection 標籤和 istio.io/rev 標籤都存在於同一個命名空間中,則 istio-injection 標籤會優先。

注入器設定了以下邏輯:

  1. 如果任一標籤(istio-injectionsidecar.istio.io/inject)被停用,則不會注入 Pod。
  2. 如果任一標籤(istio-injectionsidecar.istio.io/injectistio.io/rev)被啟用,則會注入 Pod。
  3. 如果未設定任何標籤,則如果 .values.sidecarInjectorWebhook.enableNamespacesByDefault 已啟用,則會注入 Pod。預設情況下,此設定未啟用,因此通常這表示不會注入 Pod。

手動 sidecar 注入

若要手動注入部署,請使用istioctl kube-inject

壓縮
$ istioctl kube-inject -f @samples/curl/curl.yaml@ | kubectl apply -f -
serviceaccount/curl created
service/curl created
deployment.apps/curl created

預設情況下,這將使用叢集內組態。或者,可以使用組態的本機副本進行注入。

$ kubectl -n istio-system get configmap istio-sidecar-injector -o=jsonpath='{.data.config}' > inject-config.yaml
$ kubectl -n istio-system get configmap istio-sidecar-injector -o=jsonpath='{.data.values}' > inject-values.yaml
$ kubectl -n istio-system get configmap istio -o=jsonpath='{.data.mesh}' > mesh-config.yaml

對輸入檔案執行 kube-inject 並進行部署。

壓縮
$ istioctl kube-inject \
    --injectConfigFile inject-config.yaml \
    --meshConfigFile mesh-config.yaml \
    --valuesFile inject-values.yaml \
    --filename @samples/curl/curl.yaml@ \
    | kubectl apply -f -
serviceaccount/curl created
service/curl created
deployment.apps/curl created

驗證 Sidecar 是否已注入 curl Pod,且在 READY 欄位下顯示 2/2

$ kubectl get pod  -l app=curl
NAME                     READY   STATUS    RESTARTS   AGE
curl-64c6f57bc8-f5n4x    2/2     Running   0          24s

自訂注入

通常,Pod 是根據 istio-sidecar-injector ConfigMap 中設定的 Sidecar 注入範本注入的。每個 Pod 的設定可用於覆寫個別 Pod 上的這些選項。這可以透過將 istio-proxy 容器新增至您的 Pod 來完成。Sidecar 注入會將此處定義的任何設定視為覆寫預設注入範本。

在自訂這些設定時應謹慎,因為這樣可以完全自訂產生的 Pod,包括進行可能導致 Sidecar 容器無法正常運作的變更。

例如,以下組態自訂了各種設定,包括降低 CPU 要求、新增卷掛載和新增 preStop Hook。

apiVersion: v1
kind: Pod
metadata:
  name: example
spec:
  containers:
  - name: hello
    image: alpine
  - name: istio-proxy
    image: auto
    resources:
      requests:
        cpu: "100m"
    volumeMounts:
    - mountPath: /etc/certs
      name: certs
    lifecycle:
      preStop:
        exec:
          command: ["curl", "10"]
  volumes:
  - name: certs
    secret:
      secretName: istio-certs

一般來說,可以設定 Pod 中的任何欄位。但是,必須謹慎處理某些欄位。

  • Kubernetes 要求在執行注入之前設定 image 欄位。雖然您可以設定特定映像來覆寫預設映像,但建議將 image 設定為 auto,這將導致 Sidecar 注入器自動選擇要使用的映像。
  • Pod 中的某些欄位取決於相關設定。例如,CPU 要求必須小於 CPU 限制。如果未一起設定這兩個欄位,Pod 可能無法啟動。
  • securityContext.RunAsUsersecurityContext.RunAsGroup 欄位在某些情況下可能不會被採用,例如,當使用 TPROXY 模式時,因為它要求 Sidecar 以使用者 0 執行。不正確地覆寫這些欄位可能會導致流量遺失,應該非常謹慎地進行。

此外,某些欄位可以透過 Pod 上的註釋進行設定,但建議使用上述方法來自訂設定。對於某些註釋,必須格外小心。

  • 如果設定了 sidecar.istio.io/proxyCPU,請務必明確設定 sidecar.istio.io/proxyCPULimit。否則,Sidecar 的 cpu 限制將設定為無限制。
  • 如果設定了 sidecar.istio.io/proxyMemory,請務必明確設定 sidecar.istio.io/proxyMemoryLimit。否則,Sidecar 的 memory 限制將設定為無限制。

例如,請參閱以下不完整的資源註釋組態和對應的注入資源設定。

spec:
  template:
    metadata:
      annotations:
        sidecar.istio.io/proxyCPU: "200m"
        sidecar.istio.io/proxyMemoryLimit: "5Gi"
spec:
  containers:
  - name: istio-proxy
    resources:
      limits:
        memory: 5Gi
      requests:
        cpu: 200m
        memory: 5Gi
      securityContext:
        allowPrivilegeEscalation: false

自訂範本 (實驗性)

也可以在安裝時定義完全自訂的範本。例如,若要定義將 GREETING 環境變數注入 istio-proxy 容器的自訂範本:

apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
  name: istio
spec:
  values:
    sidecarInjectorWebhook:
      templates:
        custom: |
          spec:
            containers:
            - name: istio-proxy
              env:
              - name: GREETING
                value: hello-world

Pod 預設會使用自動建立的 sidecar 注入範本。這可以透過 inject.istio.io/templates 註釋來覆寫。例如,若要套用預設範本和我們的自訂設定,您可以設定 inject.istio.io/templates=sidecar,custom

除了 sidecar 之外,預設還提供 gateway 範本,以支援將 Proxy 注入 Gateway 部署中。

這些資訊對您有幫助嗎?
您是否有任何改進建議?

感謝您的回饋!