將受信任的 CA 加入 Red Hat OpenShift on IBM Cloud

虛擬私有雲 4.19 及後 RHCOS 工作節點 RHEL 工作節點

瞭解如何將受信任的憑證授權機構 (CA) 新增至 Red Hat® OpenShift® on IBM Cloud® 群集。 當從使用自訂 CA 簽署的憑證的自行託管註冊中心取得影像時,您可能需要新增信任的 CA。

將受信任的 CA 加入群集

若要在 Red Hat OpenShift on IBM Cloud 群集中新增可信 CA,您需要建立一個包含 CA 憑證的 configmap,並部署一個 daemonset,將憑證安裝在所有工作節點上。

開始之前,請確保您已準備好 CA 憑證檔案。 憑證必須是 PEM 格式。

  1. 使用 CA 憑證建立 openshift-config-user-ca-bundle configmap。 將 <path-to-your-ca-cert> 改為 CA 憑證檔案的路徑。

    kubectl create cm openshift-config-user-ca-bundle -n kube-system --from-file=openshift-config-user-ca-bundle.crt=<path-to-your-ca-cert>
    
  2. 建立在每個節點上安裝可信 CA 的腳本。

    cat > openshift-config-user-ca-script.sh <<EOF
    #!/bin/bash
    set -e  # Exit on any error
    set -x  # Enable debug output
    if ! diff /tmp/openshift-config-user-ca-bundle/openshift-config-user-ca-bundle.crt /host/etc/pki/ca-trust/source/anchors ; then
        cp /tmp/openshift-config-user-ca-bundle/openshift-config-user-ca-bundle.crt /host/etc/pki/ca-trust/source/anchors
        chroot /host update-ca-trust extract
        if chroot /host systemctl is-enabled coreos-update-ca-trust.service; then
            chroot /host systemctl restart coreos-update-ca-trust.service
        fi
        chroot /host systemctl restart crio.service;
    fi
    # Keep container running to maintain daemonset pod
    sleep inf
    EOF
    kubectl delete cm -n kube-system openshift-config-user-ca-script --ignore-not-found
    kubectl create cm -n kube-system --from-file openshift-config-user-ca-script.sh openshift-config-user-ca-script
    
  3. 建立在所有節點上執行指令碼的 daemonset。

    OCP_REGISTRY_ENDPOINT=$(kubectl get pod -n kube-system -l k8s-app=kube-apiserver-proxy -o json | jq -r '.items[0].spec.containers[0].image | select( . != null )' | awk -F/ '{ print $1 }')
    if [[ -z "${OCP_REGISTRY_ENDPOINT}" ]]; then
        OCP_REGISTRY_ENDPOINT=$(kubectl get pod -n kube-system -l app=ibm-master-proxy-static -o json | jq -r '.items[0].spec.containers[0].image | select( . != null )' | awk -F/ '{ print $1 }')
    fi
    OCP_VERSION=$(oc version -o json | jq -r .openshiftVersion)
    kubectl apply -f - <<EOF
    ---
    apiVersion: apps/v1
    kind: DaemonSet
    metadata:
      name: update-openshift-config-user-ca-bundle
      namespace: kube-system
      labels:
        app: update-openshift-config-user-ca-bundle
    spec:
      selector:
        matchLabels:
          app: update-openshift-config-user-ca-bundle
      template:
        metadata:
          labels:
            app: update-openshift-config-user-ca-bundle
        spec:
          containers:
            - command: ["/bin/bash", "-c", "/tmp/openshift-config-user-ca-script/openshift-config-user-ca-script.sh"]
              image: ${OCP_REGISTRY_ENDPOINT}/armada-master/ocp-release-${OCP_VERSION}-x86_64:cli
              imagePullPolicy: IfNotPresent
              name: update-openshift-config-user-ca-bundle
              resources:
                requests:
                  cpu: 1m
                  memory: 1Mi
              securityContext:
                privileged: true
              volumeMounts:
                - name: host-volume
                  mountPath: /host
                - name: openshift-config-user-ca-bundle
                  mountPath: /tmp/openshift-config-user-ca-bundle
                - name: openshift-config-user-ca-script
                  mountPath: /tmp/openshift-config-user-ca-script
          hostIPC: true
          hostNetwork: true
          hostPID: true
          tolerations:
          - operator: Exists
          volumes:
            - name: host-volume
              hostPath:
                path: /
                type: Directory
            - name: openshift-config-user-ca-bundle
              configMap:
                name: openshift-config-user-ca-bundle
            - name: openshift-config-user-ca-script
              configMap:
                name: openshift-config-user-ca-script
                defaultMode: 0755
    EOF
    kubectl rollout status ds -n kube-system update-openshift-config-user-ca-bundle
    
  4. 透過檢查 daemonset 記錄,確認已安裝可信 CA。

    oc logs -n kube-system -l app=update-openshift-config-user-ca-bundle --tail=-1
    

daemonset 完成後,您的可信 CA 就會安裝在所有工作節點上,而您的群集就可以從使用由您的自訂 CA 簽署的憑證的登錄中心拉取影像。