注意:

使用 Kyverno 將 ImagePullSecrets 屬性注入 Kubernetes Pod

簡介

Oracle Cloud Infrastructure Container Engine for Kubernetes (OKE) 是一項完全託管、可擴展且高可用性的服務,可用來將容器化應用系統部署至雲端。

Oracle Cloud Infrastructure Registry (也稱為容器登錄或 OCIR) 是 Oracle 管理的登錄,可讓您簡化開發環境的工作流程。Container Registry 可讓您以開發人員身分輕鬆儲存、共用及管理容器映像檔。

您可以在 OCIR 中建立公用或專用容器映像檔儲存區域:

若要從 OCIR 專用登錄提取映像檔,Kubernetes 需要證明資料。Pod 組態檔中的 spec.imagePullSecrets 屬性指定 Kubernetes 加密密碼清單,其中包含工作節點應用來從登錄提取容器映像檔的證明資料。

為了簡化應用程式清單和部署,以及與 OCIR 整合時將某些 OKE 叢集特定組態分解,我們可以使用 Kyverno。此 CNCF 底下的激勵專案可以使用「原則」定義來驗證、移轉、產生和清除 Kubernetes 資源。我們可以在建立新的 Pod 並插入提取儲存在 OCIR 中之容器映像檔所需的 imagePullSecrets 屬性時,利用此產品來突顯送出至 Kubernetes API 的清單。

目標

必要條件

工作 1:安裝 Kyverno

Kyverno 網頁說明使用以下方法,在 Kubernetes 叢集安裝 Kyverno

在本教學課程中,我們將聚焦於如何使用 Helm 圖表來安裝 Kyverno。

Task 1.1:安裝 helm

  1. 視您所在的作業系統而定,請瀏覽本指南並安裝 helm

  2. 執行下列指令以確認安裝是否成功。

    helm version
    # version.BuildInfo{Version:"v3.8.1", GitCommit:"5cb9af4b1b271d11d7a97a71df3ac337dd94ad37", GitTreeState:"clean", GoVersion:"go1.17.5"}
    

工作 1. 2:以獨立模式安裝 Kyverno

  1. 新增 Kyverno Helm 儲存庫。

    helm repo add kyverno https://kyverno.github.io/kyverno/
    # "kyverno" has been added to your repositories
    
  2. 掃描圖表的新儲存庫。

    helm repo update
    # Hang tight while we grab the latest from your chart repositories...
    # ...Successfully got an update from the "kyverno" chart repository
    # Update Complete. ⎈Happy Helming!⎈
    
  3. kyverno 命名空間中安裝 Kyverno。

    helm install kyverno kyverno/kyverno -n kyverno --create-namespace
    # NAME: kyverno
    # LAST DEPLOYED: Fri Aug 30 12:20:33 2023
    # NAMESPACE: kyverno
    # STATUS: deployed
    # REVISION: 1
    # NOTES:
    # Chart version: 3.0.1
    # Kyverno version: v1.10.0
    
    # Thank you for installing kyverno! Your release is named kyverno.
    
    # The following components have been installed in your cluster:
    # - CRDs
    # - Admission controller
    # - Reports controller
    # - Cleanup controller
    # - Background controller
    

作業 2:建立 Kubernetes ImagePull 加密密碼

  1. 依照此處提供的步驟產生 Auth Token 使用者:取得認證權杖

  2. 確定設定正確的原則,讓使用者能夠存取 OCIR:控制儲存區域存取權的原則

  3. 確認 OCIR 登錄 URL、使用者名稱及密碼:

    登錄 URL 是以 OCI 區域索引鍵為基礎:<region-key>.ocir.io

    範例:若為 OCI 鳳凰城區域:phx.ocir.io

    使用者名稱租用戶命名空間、ci 使用者名稱與 IDP (若有使用):<tenancy-namespace>/<username><tenancy-namespace>/oracleidentitycloudservice/<username>

    範例:

    axaxnpcrorw5/jdoe@acme.comaxaxnpcrorw5/oracleidentitycloudservice/jdoe@acme.com

  4. 建立名為 ocirsecret 的 Kubernetes 密碼。

    kubectl create secret docker-registry ocirsecret --docker-server='<OCIR registry>' --docker-username='<username>' --docker-password='<auth-token>'
    

    範例:

    kubectl create secret docker-registry ocirsecret --docker-server='phx.ocir.io' --docker-username='axaxnpcrorw5/jdoe@acme.com' --docker-password='cxOY5NL<AnBN}<123{_6'

任務 3:定義所需的 Kyverno ClusterPolicies

  1. 建立名為 add-imagepullsecret.yaml 的檔案。

  2. 複製並貼上檔案下方的文字。

    ---
    apiVersion: kyverno.io/v1
    kind: ClusterPolicy
    metadata:
      name: inject-imagepullsecret-to-namespace
      annotations:
        policies.kyverno.io/title: Clone imagePullSecret secret to new namespaces
        policies.kyverno.io/subject: Namespace
        policies.kyverno.io/description: >-
          ImagePullSecrets must be present in the same namespace as the pods using them.
          This policy monitors for new namespaces being created (except kube-system and kyverno),
          and automatically clones into the namespace the `ocirsecret` from the `default` namespace.
    spec:
      generateExisting: true
      rules:
      - name: inject-imagepullsecret-to-namespace
        match:
          any:
          - resources:
              kinds:
              - Namespace
        exclude:
          any:
          - resources:
              namespaces:
              - kube-system
              - kube-node-lease
              - kube-public
              - kyverno
        generate:
          apiVersion: v1
          kind: Secret
          name: ocirsecret
          namespace: "{{ request.object.metadata.name }}"
          synchronize: true
          clone:
            namespace: default
            name: ocirsecret
    ---
    apiVersion: kyverno.io/v1
    kind: ClusterPolicy
    metadata:
      name: add-imagepullsecrets
      annotations:
        policies.kyverno.io/title: Add imagePullSecrets
        policies.kyverno.io/subject: Pod
        policies.kyverno.io/description: >-
          Images coming from certain registries require authentication in order to pull them,
          and the kubelet uses this information in the form of an imagePullSecret to pull
          those images on behalf of your Pod. This policy searches pod spec for images coming from a
          registry which contains `phx.ocir.io/axaxnpcrorw5` and, if found, will mutate the Pod
          to add an imagePullSecret called `ocirsecret`.
    spec:
      rules:
      - name: add-imagepullsecret
        match:
          any:
          - resources:
              kinds:
              - Pod
        context:
        - name: images_in_ocir
          variable:
            jmesPath: "[request.object.spec.containers[*].image.contains(@, 'phx.ocir.io/axaxnpcrorw5'), request.object.spec.initContainers[*].image.contains(@, 'phx.ocir.io/axaxnpcrorw5')][]"
            default: []
        preconditions:
          all:
          - key: true
            operator: In
            value: "{{ images_in_ocir }}"
        mutate:
          patchStrategicMerge:
            spec:
              imagePullSecrets:
              - name: ocirsecret
    
  3. 將預設的 OCIR 位址 phx.ocir.io/axaxnpcrorw5 取代為您使用的 OCIR 位址。

  4. 在叢集中建立 ClusterPolicy 並執行下列命令以強制實行原則。

    kubectl apply -f add-imagepullsecret.yaml
    # clusterpolicy.kyverno.io/inject-imagepullsecret-to-namespace created
    # clusterpolicy.kyverno.io/add-imagepullsecrets created
    

作業 4:測試

  1. 建立一個名為 test-pod.yaml 的檔案,其中包含下列文字:

    注意:從預留位置中的專用儲存區域填入影像的 URL。

    ---
    apiVersion: v1
    kind: Namespace
    metadata:
      name: testns
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      labels:
        run: testpod
      name: testpod
      namespace: testns
    spec:
      containers:
      - args:
        - /bin/sh
        - -c
        - sleep infinity
        image: <image_from_OCIR> # eg: phx.ocir.io/axaxnpcrorw5/nginx:latest
        name: test
        resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Always
    
  2. 建立資源

    kubectl apply -f test-pod.yaml
    
  3. 驗證 Pod 是否順利建立。

    kubectl get pods -n testns
    # NAME      READY   STATUS    RESTARTS   AGE
    # testpod   1/1     Running   0          2m4s
    
  4. 驗證加密密碼是否從預設命名空間複製到新建立的命名空間。

    kubectl get secret -n testns
    # NAME         TYPE                             DATA   AGE
    # ocirsecret   kubernetes.io/dockerconfigjson   1      2m56s
    

作業 5:清除

  1. 刪除測試期間建立的負載平衡器。

    kubectl delete -f test-pod.yaml
    
  2. 執行指令來解除安裝 Kyverno。

    helm uninstall kyverno -n kyverno
    

確認

其他學習資源

探索 docs.oracle.com/learn 的其他實驗室,或者存取更多 Oracle Learning YouTube 頻道上的免費學習內容。此外,請瀏覽 education.oracle.com/learning-explorer 以成為 Oracle Learning 檔案總管。

如需產品文件,請造訪 Oracle Help Center