「適用於祕密存放區 CSI 驅動程式的 Azure Key Vault 提供者」可讓您取得儲存在 Azure Key Vault 執行個體中的祕密內容,並使用祕密存放區 CSI 驅動程式將它們掛接到 Kubernetes Pod 中。 本文說明如何在 Azure Red Hat OpenShift 上使用「適用於祕密存放區 CSI 驅動程式的 Azure Key Vault 提供者」。
備註
作為本文中介紹的開放原始碼解決方案的替代方案,您可以使用 Azure Arc 來管理您的叢集,還可搭配 Azure 金鑰保存庫提供者,用於 Secrets Store CSI 驅動程式擴充功能。 此方法受到 Microsoft 的完全支援,建議取代下面的開放原始碼解決方案。
先決條件
需要下列必要條件:
- Azure Red Hat OpenShift 叢集 (請參閱建立 Azure Red Hat OpenShift 叢集以深入了解。)
- Azure CLI (已登入)
- Helm 3.x 指令行介面 (CLI)
設定環境變數
設定將在此整個程序中使用的以下變數:
export KEYVAULT_RESOURCE_GROUP=${AZR_RESOURCE_GROUP:-"openshift"}
export KEYVAULT_LOCATION=${AZR_RESOURCE_LOCATION:-"eastus"}
export KEYVAULT_NAME=secret-store-$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1)
export AZ_TENANT_ID=$(az account show -o tsv --query tenantId)
安裝 Kubernetes 祕密存放區 CSI 驅動程式
建立專案;您將把 CSI 驅動程式部署到這個專案中:
oc new-project k8s-secrets-store-csi設定 SecurityContextConstraints 以允許 CSI 驅動程式執行 (否則,CSI 驅動程式將無法建立 Pod):
oc adm policy add-scc-to-user privileged \ system:serviceaccount:k8s-secrets-store-csi:secrets-store-csi-driver將秘密存放區 CSI 驅動程式新增至您的 Helm 存放庫:
helm repo add secrets-store-csi-driver \ https://kubernetes-sigs.github.io/secrets-store-csi-driver/charts更新您的 Helm 存放庫:
helm repo update安裝祕密存放區 CSI 驅動程式:
helm install -n k8s-secrets-store-csi csi-secrets-store \ secrets-store-csi-driver/secrets-store-csi-driver \ --version v1.3.1 \ --set "linux.providersDir=/var/run/secrets-store-csi-providers"或者,您也可以透過將以下參數新增至上面的命令來啟用秘密的自動輪換:
--set "syncSecret.enabled=true" --set "enableSecretRotation=true"驗證 CSI 驅動程式 DaemonSet 是否正在執行:
kubectl --namespace=k8s-secrets-store-csi get pods -l "app=secrets-store-csi-driver"執行上面的命令後,您應該會看到以下內容:
NAME READY STATUS RESTARTS AGE csi-secrets-store-secrets-store-csi-driver-cl7dv 3/3 Running 0 57s csi-secrets-store-secrets-store-csi-driver-gbz27 3/3 Running 0 57s
部署「適用於祕密存放區 CSI 驅動程式的 Azure Key Vault 提供者」
新增 Azure Helm 存放庫:
helm repo add csi-secrets-store-provider-azure \ https://azure.github.io/secrets-store-csi-driver-provider-azure/charts更新您的本機 Helm 存放庫:
helm repo update安裝 Azure Key Vault CSI 提供者:
helm install -n k8s-secrets-store-csi azure-csi-provider \ csi-secrets-store-provider-azure/csi-secrets-store-provider-azure \ --set linux.privileged=true --set secrets-store-csi-driver.install=false \ --set "linux.providersDir=/var/run/secrets-store-csi-providers" \ --version=v1.4.1設定 SecurityContextConstraints 以允許 CSI 驅動程式執行:
oc adm policy add-scc-to-user privileged \ system:serviceaccount:k8s-secrets-store-csi:csi-secrets-store-provider-azure
建立金鑰保存庫和秘密
為您的應用程式建立一個命名空間。
oc new-project my-application在包含 Azure Red Hat OpenShift 的資源群組中建立 Azure 金鑰保存庫。
az keyvault create -n ${KEYVAULT_NAME} \ -g ${KEYVAULT_RESOURCE_GROUP} \ --location ${KEYVAULT_LOCATION}在金鑰保存庫中建立一個秘密。
az keyvault secret set \ --vault-name ${KEYVAULT_NAME} \ --name secret1 --value "Hello"為金鑰保存庫建立服務主體。
備註
如果您在建立服務主體時收到錯誤,您可能需要將 Azure CLI 升級至最新版本。
export SERVICE_PRINCIPAL_CLIENT_SECRET="$(az ad sp create-for-rbac --skip-assignment --name http://$KEYVAULT_NAME --query 'password' -otsv)" export SERVICE_PRINCIPAL_CLIENT_ID="$(az ad sp list --display-name http://$KEYVAULT_NAME --query '[0].appId' -otsv)"設定服務主體的存取原則。
az keyvault set-policy -n ${KEYVAULT_NAME} \ --secret-permissions get \ --spn ${SERVICE_PRINCIPAL_CLIENT_ID}建立並標記一個秘密,供 Kubernetes 用於存取金鑰保存庫。
kubectl create secret generic secrets-store-creds \ -n my-application \ --from-literal clientid=${SERVICE_PRINCIPAL_CLIENT_ID} \ --from-literal clientsecret=${SERVICE_PRINCIPAL_CLIENT_SECRET} kubectl -n my-application label secret \ secrets-store-creds secrets-store.csi.k8s.io/used=true
部署使用 CSI 驅動程式的應用程式
建立一個
SecretProviderClass以授與對此秘密的存取權:cat <<EOF | kubectl apply -f - apiVersion: secrets-store.csi.x-k8s.io/v1 kind: SecretProviderClass metadata: name: azure-kvname namespace: my-application spec: provider: azure parameters: usePodIdentity: "false" useVMManagedIdentity: "false" userAssignedIdentityID: "" keyvaultName: "${KEYVAULT_NAME}" objects: | array: - | objectName: secret1 objectType: secret objectVersion: "" tenantId: "${AZ_TENANT_ID}" EOF建立一個使用上一個步驟中建立的
SecretProviderClass的 Pod:cat <<EOF | kubectl apply -f - kind: Pod apiVersion: v1 metadata: name: busybox-secrets-store-inline namespace: my-application spec: containers: - name: busybox image: k8s.gcr.io/e2e-test-images/busybox:1.29 command: - "/bin/sleep" - "10000" volumeMounts: - name: secrets-store-inline mountPath: "/mnt/secrets-store" readOnly: true volumes: - name: secrets-store-inline csi: driver: secrets-store.csi.k8s.io readOnly: true volumeAttributes: secretProviderClass: "azure-kvname" nodePublishSecretRef: name: secrets-store-creds EOF檢查秘密是否已掛接:
kubectl exec busybox-secrets-store-inline -- ls /mnt/secrets-store/輸出應與以下內容相符:
secret1列印秘密:
kubectl exec busybox-secrets-store-inline \ -- cat /mnt/secrets-store/secret1輸出應與以下內容相符:
Hello
清理
解除安裝 Key Vault 提供者和 CSI 驅動程式。
解除安裝 Key Vault 提供者
解除安裝 Helm 圖表:
helm uninstall -n k8s-secrets-store-csi azure-csi-provider刪除應用程式:
oc delete project my-application刪除 Azure 金鑰保存庫:
az keyvault delete -n ${KEYVAULT_NAME}刪除服務主體:
az ad sp delete --id ${SERVICE_PRINCIPAL_CLIENT_ID}
解除安裝 Kubernetes 秘密存放區 CSI 驅動程式
刪除秘密存放區 CSI 驅動程式:
helm uninstall -n k8s-secrets-store-csi csi-secrets-store oc delete project k8s-secrets-store-csi刪除 SecurityContextConstraints:
oc adm policy remove-scc-from-user privileged \ system:serviceaccount:k8s-secrets-store-csi:secrets-store-csi-driver