首页 > 解决方案 > 如何在 google kubernetes 引擎上设置环境变量?

问题描述

FirebaseGoLang托管的项目中使用Google Kubernetes Engine.

我遵循的步骤:

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

  2. 按照这个答案并使用添加一个新的密钥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。我在这里想念什么?任何提示都会很明显。

标签: dockerkubernetesgoogle-cloud-platformgoogle-kubernetes-engine

解决方案


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

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

推荐阅读