自动化 Google Cloud Platform 认证
gcp-auth 插件可自动动态配置 Pod 以使用您的凭据,从而允许应用程序访问 Google Cloud 服务,就像它们在 Google Cloud 中运行一样。
该插件默认使用您环境的 应用程序默认凭据,您可以使用 gcloud auth application-default login 进行配置。或者,您可以通过将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为该文件的位置来指定 JSON 凭据文件(例如服务账户密钥)。
该插件还默认使用您本地的 gcloud 项目,您可以使用 gcloud config set project <project name> 进行配置。您可以通过将 GOOGLE_CLOUD_PROJECT 环境变量设置为所需项目的名称来覆盖此设置。
启用插件后,您的集群中的 Pod 将会配置环境变量(例如,GOOGLE_APPLICATION_DEFAULTS、GOOGLE_CLOUD_PROJECT),这些变量会被 GCP 客户端库自动使用。此外,该插件还会配置 镜像仓库拉取密钥,允许您的集群访问托管在 Artifact Registry 和 Google Container Registry 中的容器镜像。
教程
- 启动集群
minikube start
😄 minikube v1.12.0 on Darwin 10.15.5
✨ Automatically selected the docker driver. Other choices: hyperkit, virtualbox
👍 Starting control plane node minikube in cluster minikube
🔥 Creating docker container (CPUs=2, Memory=3892MB) ...
🐳 Preparing Kubernetes v1.18.3 on Docker 19.03.2 ...
🔎 Verifying Kubernetes components...
🌟 Enabled addons: default-storageclass, storage-provisioner
🏄 Done! kubectl is now configured to use "minikube"
- 启用
gcp-auth插件
minikube addons enable gcp-auth
🔎 Verifying gcp-auth addon...
📌 Your GCP credentials will now be mounted into every pod created in the minikube cluster.
📌 If you don't want credential mounted into a specific pod, add a label with the `gcp-auth-skip-secret` key to your pod configuration.
🌟 The 'gcp-auth' addon is enabled
- 对于任意路径下的凭据
export GOOGLE_APPLICATION_CREDENTIALS=<creds-path>.json
minikube addons enable gcp-auth
- 像往常一样部署您的 GCP 应用
kubectl apply -f test.yaml
deployment.apps/pytest created
一切都应该按预期工作。您可以运行 kubectl describe 查看我们注入的环境变量。
如上输出所示,如果您有一个不想注入凭据的 Pod,您只需要添加 gcp-auth-skip-secret 标签即可。
apiVersion: apps/v1
kind: Deployment
metadata:
name: pytest
spec:
selector:
matchLabels:
app: pytest
replicas: 2
template:
metadata:
labels:
app: pytest
gcp-auth-skip-secret: "true"
spec:
containers:
- name: py-test
imagePullPolicy: Never
image: local-pytest
ports:
- containerPort: 80
刷新现有 Pod
在启用 gcp-auth 插件之前部署到 minikube 集群的 Pod 将不会配置 GCP 凭据。要解决此问题,请运行
minikube addons enable gcp-auth --refresh
添加新命名空间
minikube v1.29.0+
新创建的命名空间会自动配置镜像拉取密钥,无需额外操作。
minikube v1.28.0 及之前版本
在启用 gcp-auth 插件后添加的命名空间将不会配置镜像拉取密钥。要解决此问题,请运行
minikube addons enable gcp-auth --refresh