自动化 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 RegistryGoogle 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

一切都应按预期工作。您可以在 Pod 上运行 `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