如何在 google kubernetes 引擎上设置环境变量?
How to set up an environment variables on google kubernetes engine?
我在 Google Kubernetes Engine
上托管的 GoLang
项目中使用 Firebase
。
我遵循的步骤:
在 firebase 帐户上启用 firebase admin SDK。它为我生成了一个服务帐户 JSON
。这还在我的 Google 控制台服务凭据下创建了一个服务帐户。
遵循此 answer 并使用 kubectl create secret generic google-application-credentials --from-file=./sample-project.json
添加新的密钥
更改了我的 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
在同一文件中设置卷
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
运行 kubectl apply -f deployment.yaml
并使用 docker push
命令进行部署。
它让我感到震惊 error getting credentials using google_application_credentials environment variable gke
。我在这里错过了什么? Anny 提示将不胜感激。
您可以通过两种不同的方式使用 Secret:
- 将 Secret 安装为 卷 并将其作为文件访问
- 将 Secret 映射到 环境变量 并通过读取变量
来访问它
你好像把两者混在一起了。决定是将其作为文件(推荐)还是作为环境变量进行访问。
请参阅文档中的示例:
示例 - 将其作为环境变量访问
首先,创建Secret,这可以像你一样完成:
kubectl create secret generic google-application-credentials --from-file=./application-credentials.json
I want to access it as an environment variable.
要将秘密公开为 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 作为环境变量访问时,无需将其添加为卷。
当前关于秘密、环境变量和卷的答案是正确的。但是,如果你尝试在 GKE 中加载身份验证,我绝对不建议使用服务帐户密钥文件。
在 GKE 上,有一个名为 workload identity. It act exactly as metadata server on a compute engine instance(和其他产品)的强大功能,但在 pod 和名称空间级别(创建一个代理来拦截元数据服务器调用并将它们重定向到正确的凭据,配置为工作负载身份)。
它更安全,您不必保留和管理机密文件,以及所有的限制和涉及的风险。
终于搞清楚怎么复制和使用环境变量了。这是。更新后的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
我在 Google Kubernetes Engine
上托管的 GoLang
项目中使用 Firebase
。
我遵循的步骤:
在 firebase 帐户上启用 firebase admin SDK。它为我生成了一个服务帐户
JSON
。这还在我的 Google 控制台服务凭据下创建了一个服务帐户。遵循此 answer 并使用
添加新的密钥kubectl create secret generic google-application-credentials --from-file=./sample-project.json
更改了我的
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
在同一文件中设置卷
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
运行
kubectl apply -f deployment.yaml
并使用docker push
命令进行部署。
它让我感到震惊 error getting credentials using google_application_credentials environment variable gke
。我在这里错过了什么? Anny 提示将不胜感激。
您可以通过两种不同的方式使用 Secret:
- 将 Secret 安装为 卷 并将其作为文件访问
- 将 Secret 映射到 环境变量 并通过读取变量 来访问它
你好像把两者混在一起了。决定是将其作为文件(推荐)还是作为环境变量进行访问。
请参阅文档中的示例:
示例 - 将其作为环境变量访问
首先,创建Secret,这可以像你一样完成:
kubectl create secret generic google-application-credentials --from-file=./application-credentials.json
I want to access it as an environment variable.
要将秘密公开为 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 作为环境变量访问时,无需将其添加为卷。
当前关于秘密、环境变量和卷的答案是正确的。但是,如果你尝试在 GKE 中加载身份验证,我绝对不建议使用服务帐户密钥文件。
在 GKE 上,有一个名为 workload identity. It act exactly as metadata server on a compute engine instance(和其他产品)的强大功能,但在 pod 和名称空间级别(创建一个代理来拦截元数据服务器调用并将它们重定向到正确的凭据,配置为工作负载身份)。
它更安全,您不必保留和管理机密文件,以及所有的限制和涉及的风险。
终于搞清楚怎么复制和使用环境变量了。这是。更新后的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