2.8.3.3. Azure Key Vault からのシークレットのマウント
Secrets Store CSI Driver Operator を使用して、Microsoft Azure Key Vault から OpenShift Container Platform の Container Storage Interface (CSI) ボリュームにシークレットをマウントできます。Azure Key Vault からシークレットをマウントするには、以下を実行します。
前提条件
- Azure にクラスターをインストールした。
-
cluster-adminロールを持つユーザーとしてクラスターにアクセスできる。 -
Azure CLI (
az) がインストールされている。 - Secrets Store CSI Driver Operator がインストールされている。手順は、「Secrets Store CSI ドライバーのインストール」を参照してください。
- 必要なシークレットを保存するように Azure Key Vault を設定している。
手順
Azure Key Vault プロバイダーをインストールします。
ServiceAccountリソース設定を定義する、azure-provider.yamlという名前の YAML ファイルを作成します。次の設定例を参照してください。重要Secrets Store CSI ドライバーの Azure Key Vault プロバイダーは、アップストリームプロバイダーです。
この設定は、OpenShift Container Platform で適切に動作するように、アップストリームの Azure ドキュメント で提供されている設定から変更されています。この設定を変更すると、機能に影響が出る場合があります。
azure-provider.yamlファイルの例apiVersion: v1 kind: ServiceAccount metadata: name: csi-secrets-store-provider-azure namespace: openshift-cluster-csi-drivers --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: csi-secrets-store-provider-azure-cluster-role rules: - apiGroups: [""] resources: ["serviceaccounts/token"] verbs: ["create"] - apiGroups: [""] resources: ["serviceaccounts"] verbs: ["get"] - apiGroups: [""] resources: ["pods"] verbs: ["get"] - apiGroups: [""] resources: ["nodes"] verbs: ["get"] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: csi-secrets-store-provider-azure-cluster-rolebinding roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: csi-secrets-store-provider-azure-cluster-role subjects: - kind: ServiceAccount name: csi-secrets-store-provider-azure namespace: openshift-cluster-csi-drivers --- apiVersion: apps/v1 kind: DaemonSet metadata: namespace: openshift-cluster-csi-drivers name: csi-secrets-store-provider-azure labels: app: csi-secrets-store-provider-azure spec: updateStrategy: type: RollingUpdate selector: matchLabels: app: csi-secrets-store-provider-azure template: metadata: labels: app: csi-secrets-store-provider-azure spec: serviceAccountName: csi-secrets-store-provider-azure hostNetwork: true containers: - name: provider-azure-installer image: mcr.microsoft.com/oss/azure/secrets-store/provider-azure:v1.4.1 imagePullPolicy: IfNotPresent args: - --endpoint=unix:///provider/azure.sock - --construct-pem-chain=true - --healthz-port=8989 - --healthz-path=/healthz - --healthz-timeout=5s livenessProbe: httpGet: path: /healthz port: 8989 failureThreshold: 3 initialDelaySeconds: 5 timeoutSeconds: 10 periodSeconds: 30 resources: requests: cpu: 50m memory: 100Mi limits: cpu: 50m memory: 100Mi securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true runAsUser: 0 capabilities: drop: - ALL volumeMounts: - mountPath: "/provider" name: providervol affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: type operator: NotIn values: - virtual-kubelet volumes: - name: providervol hostPath: path: "/var/run/secrets-store-csi-providers" tolerations: - operator: Exists nodeSelector: kubernetes.io/os: linux次のコマンドを実行して、
csi-secrets-store-provider-azureサービスアカウントへの特権アクセスを付与します。$ oc adm policy add-scc-to-user privileged -z csi-secrets-store-provider-azure -n openshift-cluster-csi-drivers次のコマンドを実行して、プロバイダーリソースを作成します。
$ oc apply -f azure-provider.yaml
Key Vault にアクセスするためのサービスプリンシパルを作成します。
次のコマンドを実行して、サービスプリンシパルのクライアントシークレットを環境変数として設定します。
$ SERVICE_PRINCIPAL_CLIENT_SECRET="$(az ad sp create-for-rbac --name https://$KEYVAULT_NAME --query 'password' -otsv)"次のコマンドを実行して、サービスプリンシパルのクライアント ID を環境変数として設定します。
$ SERVICE_PRINCIPAL_CLIENT_ID="$(az ad sp list --display-name https://$KEYVAULT_NAME --query '[0].appId' -otsv)"次のコマンドを実行して、サービスプリンシパルのクライアントシークレットと ID を使用して汎用シークレットを作成します。
$ oc create secret generic secrets-store-creds -n my-namespace --from-literal clientid=${SERVICE_PRINCIPAL_CLIENT_ID} --from-literal clientsecret=${SERVICE_PRINCIPAL_CLIENT_SECRET}secrets-store.csi.k8s.io/used=trueラベルを適用して、プロバイダーがこのnodePublishSecretRefシークレットを検索できるようにします。$ oc -n my-namespace label secret secrets-store-creds secrets-store.csi.k8s.io/used=true
シークレットプロバイダークラスを作成して、シークレットストアプロバイダーを定義します。
SecretProviderClassオブジェクトを定義する YAML ファイルを作成します。secret-provider-class-azure.yamlの例apiVersion: secrets-store.csi.x-k8s.io/v1 kind: SecretProviderClass metadata: name: my-azure-provider1 namespace: my-namespace2 spec: provider: azure3 parameters:4 usePodIdentity: "false" useVMManagedIdentity: "false" userAssignedIdentityID: "" keyvaultName: "kvname" objects: | array: - | objectName: secret1 objectType: secret tenantId: "tid"次のコマンドを実行して
SecretProviderClassオブジェクトを作成します。$ oc create -f secret-provider-class-azure.yaml
このシークレットプロバイダークラスを使用するデプロイメントを作成します。
Deploymentオブジェクトを定義する YAML ファイルを作成します。deployment.yamlの例apiVersion: apps/v1 kind: Deployment metadata: name: my-azure-deployment1 namespace: my-namespace2 spec: replicas: 1 selector: matchLabels: app: my-storage template: metadata: labels: app: my-storage 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: "my-azure-provider"3 nodePublishSecretRef: name: secrets-store-creds4 次のコマンドを実行して、
Deploymentオブジェクトを作成します。$ oc create -f deployment.yaml
検証
Pod ボリュームマウント内の Azure Key Vault からシークレットにアクセスできることを確認します。
次のコマンドを実行して、Pod マウント内のシークレットをリスト表示します。
$ oc exec my-azure-deployment-<hash> -n my-namespace -- ls /mnt/secrets-store/出力例
secret1次のコマンドを実行して、Pod マウント内のシークレットを表示します。
$ oc exec my-azure-deployment-<hash> -n my-namespace -- cat /mnt/secrets-store/secret1出力例
my-secret-value