【问题标题】:How to set up an environment variables on google kubernetes engine?如何在 google kubernetes 引擎上设置环境变量?
【发布时间】:2021-06-28 11:03:35
【问题描述】:

我在Google Kubernetes Engine 上托管的GoLang 项目中使用Firebase

我遵循的步骤:

  1. 在 firebase 帐户上启用 firebase admin SDK。它为我生成了一个服务帐户JSON。这还在我的 Google 控制台服务凭据下创建了一个服务帐户。

  2. 关注此answer 并使用kubectl create secret generic google-application-credentials --from-file=./sample-project.json 添加新密钥

  3. 对我的deployment.YAML 文件进行了更改(添加了卷挂载和环境变量)

    spec:
      containers:
      - image: gcr.io/sample-ee458/city:0.27
      name: city-app
      volumeMounts:
      - name: google-application-credentials-volume
        mountPath: /etc/gcp
        readOnly: true 
    env:
    - name: GOOGLE_APPLICATION_CREDENTIALS
      value: /etc/gcp/application-credentials.json
    
  4. 在同一个文件中设置卷

    volumes:
    - name: google-application-credentials-volume
    secret:
      secretName: google-application-credentials
      items:
      - key: application-credentials.json # default name created by the create secret from-file command
      path: application-credentials.json
    
  5. 运行kubectl apply -f deployment.yaml 并使用docker push 命令进行部署。

它把我扔了error getting credentials using google_application_credentials environment variable gke。我在这里想念什么?任何提示都会很明显。

【问题讨论】:

    标签: docker kubernetes google-cloud-platform google-kubernetes-engine


    【解决方案1】:

    最后,我弄清楚如何复制它并使用环境变量。这是。更新后的YAMLfile

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: my-app
    spec:
      template:
        spec:
          volumes:
          - name: google-cloud-keys
            secret:
              secretName: gac-keys
          containers:
          - name: my-app
            image: us.gcr.io/my-app
            volumeMounts:
            - name: google-cloud-keys
              mountPath: /var/secrets/google
              readOnly: true
            env:
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: /var/secrets/google/new-file-name.json
    

    【讨论】:

      【解决方案2】:

      您可以通过两种不同的方式使用 Secret:

      • 将 Secret 挂载为 volume 并将其作为文件访问
      • 将 Secret 映射到环境变量并通过读取变量来访问它

      你似乎把它们都混合了。决定是要将其作为文件(推荐)还是作为环境变量来访问。

      在文档中查看两者的示例:

      示例 - 作为环境变量访问它

      首先,创建 Secret,这可以像你一样完成:

      kubectl create secret generic google-application-credentials --from-file=./application-credentials.json
      

      我想将其作为环境变量访问。

      要将秘密公开为 Pod 或 Deployment 中的环境变量,请将您的 Pod 模板编写为:

        containers:
        - name: city-app
          image: gcr.io/sample-ee458/city:0.27
          env:
            - name: GOOGLE_APPLICATION_CREDENTIALS
              valueFrom:
                secretKeyRef:
                  name: google-application-credentials  # name of the Secret
                  key: application-credentials.json
      

      将 Secret 作为环境变量访问时,无需将其添加为卷。

      【讨论】:

      • 感谢您的回答。我想将它作为环境变量访问。这是否意味着kubectl create secret generic.... 命令将在GKE 中创建一个密钥。在哪里以及如何。我应该用 json 文件映射GOOGLE_APPLICATION_CREDENTIALS 吗?
      • 啊!理解。顺便说一句,这里的application-credentials.json 是什么?你指的是sample-project.json吗?
      • 是的,你也有混杂的名字。我更新了,所以我的 kubectl create secret 和 Pod-yaml 现在具有相同的文件名。
      • 出了点问题:应用上述配置后,我得到了这个:google: error getting credentials using GOOGLE_APPLICATION_CREDENTIALS environment variable: open open {\n \"type\": \"service_account\",\n \"project_id\": ....: file name too long"}* Closing connection 0 它正在使用名称获取文件内容而不是整个文件。
      • 是的,当你通过环境变量访问内容时,它就是这样工作的
      【解决方案3】:

      关于秘密、环境变量和卷的当前答案是正确的。但是,如果您尝试在 GKE 中加载身份验证,我绝对不建议使用服务帐户密钥文件。

      在 GKE 上,有一个名为 workload identity 的强大功能。它与metadata server on a compute engine instance(和其他产品)完全一样,但在 pod 和命名空间级别(创建一个代理来拦截元数据服务器调用并将它们重定向到正确的凭据,并配置有工作负载身份)。

      它更安全,您不必保留和管理机密文件,并且存在所有限制和风险。

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 2019-03-08
        • 2017-12-22
        • 2021-06-12
        • 2019-07-21
        • 2018-03-31
        • 1970-01-01
        相关资源
        最近更新 更多