kubernetes - 如何在 Kubernetes 中将 Vault secret 挂载为文件?

标签 kubernetes hashicorp-vault vault

我在 Kubernetes 中使用 Hashicorp Vault。我正在尝试将 secret 文件安装到我的应用程序所在的主文件夹中。它看起来像这样:/usr/share/nginx/html/.env,而应用程序文件位于/usr/share/nginx/html中。但容器因此没有启动。我怀疑 /usr/share/nginx/html 被 Vault 覆盖(注释:vault.hashicorp.com/secret-volume-path)。如何仅挂载文件 /usr/share/nginx/html/.env

我的注释:

vault.hashicorp.com/agent-init-first: "true"
vault.hashicorp.com/agent-inject: "true"
vault.hashicorp.com/agent-inject-secret-.env: configs/data/app/dev
vault.hashicorp.com/agent-inject-template-.env: |
  {{- with secret (print "configs/data/app/dev") -}}{{- range $k, $v := .Data.data -}}
  {{ $k }}={{ $v }}
  {{ end }}{{- end -}}
vault.hashicorp.com/role: app
vault.hashicorp.com/secret-volume-path: /usr/share/nginx/html

最佳答案

我尝试复制用例,但出现错误

2022/10/21 06:42:12 [error] 29#29: *9 directory index of "/usr/share/nginx/html/" is forbidden, client: 20.1.48.169, server: localhost, request: "GET / HTTP/1.1", host: "20.1.55.62:80"

所以看起来Vault也改变了目录权限,因为它在路径中创建.env,这是配置

        vault.hashicorp.com/agent-init-first: "true"
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-inject-secret-.env: kv/develop/us-west-2/app1-secrets
        vault.hashicorp.com/agent-inject-template-.env: |
          "{{ with secret "kv/develop/us-west-2/app1-secrets" }}
          {{ range $k, $v := .Data.data }}
           {{ $k }} = "{{ $v }}"
          {{ end }}
          {{ end }} "
        vault.hashicorp.com/agent-limits-ephemeral: ""
        vault.hashicorp.com/secret-volume-path: /usr/share/nginx/html/
        vault.hashicorp.com/agent-inject-file-.env: .env
        vault.hashicorp.com/auth-path: auth/kubernetes/develop/us-west-2
        vault.hashicorp.com/role: rolename

解决方法是覆盖所需容器的命令,对于此用例,我使用nginx

command: ["bash", "-c", "cat /vault/secret/.env > /usr/share/nginx/html/.env && nginx -g 'daemon off;' "]

这是完整的示例,虚拟值为 my-app

apiVersion: apps/v1
kind: Deployment
metadata:
  name: debug-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        vault.hashicorp.com/agent-init-first: "true"
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/agent-inject-secret-.env: kv/my-app/develop/us-west-2/develop-my-app
        vault.hashicorp.com/agent-inject-template-.env: |
          "{{ with secret "kv/my-app/develop/us-west-2/develop-my-app" }}
          {{ range $k, $v := .Data.data }}
           {{ $k }} = "{{ $v }}"
          {{ end }}
          {{ end }} "
        vault.hashicorp.com/agent-limits-ephemeral: ""
        vault.hashicorp.com/secret-volume-path: /vault/secret/
        vault.hashicorp.com/agent-inject-file-.env: .env
        vault.hashicorp.com/auth-path: auth/kubernetes/develop/us-west-2
        vault.hashicorp.com/role: my-app-develop-my-app
    spec:
      serviceAccountName: develop-my-app
      containers:
        - name: debug
          image: nginx
          command: ["bash", "-c", "cat /vault/secret/.env > /usr/share/nginx/html/.env && nginx -g 'daemon off;' "]
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http

关于kubernetes - 如何在 Kubernetes 中将 Vault secret 挂载为文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74134953/

相关文章:

kubernetes - 如何配置本地 kubectl 连接到 kubernetes EKS 集群

nginx - Kubernetes - 如何在不重启 pod 的情况下动态刷新 secret

kubernetes - 如何将 key 从 Vault 注入(inject) Kubernetes pod

rest - 确保对 Vault Secrets 管理的 REST 调用安全

python - Hashicorp 金库 : Python hvac does not see secrets

Spring Vault Integration - 从多个路径读取 secret

kubernetes - Nginx入口无法服务

docker - 如何在 Kubernetes/Docker 中更改映射卷的权限

ssl - GKE Ingress 如何使用 Letsencrypt 证书?