Aks SecretProviderClass 未找到机密

Nay*_*Van 2 azure kubernetes azure-aks

编辑:发现问题。我没有安装秘密驱动程序的插件。一旦安装,我就能让它工作

我在这里面临一个问题,我不知道我还能尝试什么来解决这个问题。

我有一个 aks 运行在一个 pod 上,该 pod 运行一个基本的 Web 应用程序todo list。没有什么太花哨或复杂的。我在这里尝试做的是授予 aks 集群访问 keyvault 的权限以及GET传递给 pod 的秘密。秘密只是一个ASPNETCORE_ENVIRONMENT: Development

按照文档,我使用 helm 安装存储库:

helm repo add csi-secrets-store-provider-azure https://azure.github.io/secrets-store-csi-driver-provider-azure/charts
helm install csi csi-secrets-store-provider-azure/csi-secrets-store-provider-azure
Run Code Online (Sandbox Code Playgroud)

我在azure中创建了一个服务原则: SERVICE_PRINCIPLE_CLIENT_SECRET = az ad sp create-for-rbac --skip-assignment --name sp-aks-keyvault

我查询了 clientId 和 Secret 并将它们传递到我的集群,如下所示:

kubectl create secret generic secrets-store-creds --from-literal clientid="ClientID" --from-literal clientsecret="Password"
Run Code Online (Sandbox Code Playgroud)

一旦一切都设置好了。我设置了这些部署。

部署.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  namespace: default
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
        - name: webapp
          image: dockerimage-acr
          ports:
            - containerPort: 80
          env:
          - name: ASPNETCORE_ENVIRONMENT
            valueFrom:
              secretKeyRef:
                name: aspenet-environment
                key: environment
          securityContext:
            allowPrivilegeEscalation: false
          volumeMounts:
          - name: secrets-mount
            mountPath: "/mnt/secrets-store"
            readOnly: true
      restartPolicy: Always 
      volumes:
        - name: secrets-mount
          csi:
            driver: secrets-store.csi.k8s.io
            readOnly: true
            volumeAttributes:
              secretProviderClass: "kv-name"
            nodePublishSecretRef:                       # Only required when using service principal mode
              name: secrets-store-creds    
Run Code Online (Sandbox Code Playgroud)

还有我的 SecretProvider.yaml

apiVersion: secrets-store.csi.x-k8s.io/v1
kind: SecretProviderClass
metadata:
  name: keyvault-secret-class
  namespace: default
spec:
  provider: azure
  secretObjects:
    - secretName: aspenet-environment
      type: Opaque
      data:
      - objectName: aspnetcoreenvironment
        key: environment
  parameters:
    usePodIdentity: "false"
    useVMManagedIdentity: "false"
    userAssignedIdentityID: ""
    keyvaultName: "mykeyvault-name"
    objects: |
      array:
        - |
          objectName: aspnetcoreenvironment
          objectType: secret
          objectVersion: ""
    tenantId: "<Tenant-Id>"
Run Code Online (Sandbox Code Playgroud)

在我的密钥库中,我为创建和分配的服务原则提供了访问策略Secret Permissions: GET,并创建了一个名为

Name: aspnetcoreenvironment
value: Development
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很顺利,但是当我运行部署时。并使用我看到错误的命令kubectl describe pod <podname>,该错误阻止容器启动

Warning  Failed     8s (x3 over 21s)  kubelet            Error: secret "aspenet-environment" not found
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的解决方案,但没有任何效果。如果我运行命令,kubectl get secretproviderclass我会取回我创建的提供程序。据我了解,如果没有服务需要特定的秘密,那么如果我运行以下命令,我将无法找到我想要创建的秘密:kubectl get secret

我想这是正确的,因为我的 Pod 没有启动。

关于我做错了什么或如何解决它有任何帮助或启示吗?

十分感谢大家

编辑:一些额外的调试我发现仍然需要卷安装。所以我确实将卷添加到了部署中。但这仍然给出错误。

正如我所意识到的,问题是。当我运行命令时kubectl apply -f secretProviderClass.yml,根本没有创建任何秘密,这是失败的原因。所以我认为这里有些问题。应用 SecretProviderClass 不应该自动创建secret服务吗?

小智 6

SecretProviderClass 对象的创建不会自动在 kubernetes 中创建密钥。secret仅当集群内有实际的 pod 正在使用定义的SecretProviderClass运行时,才会创建该对象。

此行为记录在: https ://learn.microsoft.com/en-us/azure/aks/csi-secrets-store-driver

将安装的内容与 Kubernetes 密钥同步

You might want to create a Kubernetes secret to mirror your mounted secrets content. Your secrets will sync after you start a pod to mount them. When you delete the pods that consume the secrets, your Kubernetes secret will also be deleted.