Automated Google Cloud Platform Authentication

The gcp-auth addon automatically and dynamically configures pods to use your credentials, allowing applications to access Google Cloud services as if they were running within Google Cloud.

The addon normally uses the Google Application Default Credentials as configured with gcloud auth application-default login. If you already have a json credentials file you want specify, such as to use a service account, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to that file.

The addon normally uses the default gcloud project as configured with gcloud config set project <project name>. If you want to use a different project, set the GOOGLE_CLOUD_PROJECT environment variable to the desired project.

The pods are configured with the GOOGLE_APPLICATION_DEFAULTS environment variable is set, which is automatically used by GCP client libraries, and the GOOGLE_CLOUD_PROJECT environment variable is set, as are several other historical environment variables. The addon also configures registry pull secrets to allow the cluster to access container images hosted in your project’s Artifact Registry and Google Container Registry.

Tutorial

  • Start a cluster:
  1. minikube start
  1. 😄 minikube v1.12.0 on Darwin 10.15.5
  2. Automatically selected the docker driver. Other choices: hyperkit, virtualbox
  3. 👍 Starting control plane node minikube in cluster minikube
  4. 🔥 Creating docker container (CPUs=2, Memory=3892MB) ...
  5. 🐳 Preparing Kubernetes v1.18.3 on Docker 19.03.2 ...
  6. 🔎 Verifying Kubernetes components...
  7. 🌟 Enabled addons: default-storageclass, storage-provisioner
  8. 🏄 Done! kubectl is now configured to use "minikube"
  • Enable the gcp-auth addon:
  1. minikube addons enable gcp-auth
  1. 🔎 Verifying gcp-auth addon...
  2. 📌 Your GCP credentials will now be mounted into every pod created in the minikube cluster.
  3. 📌 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.
  4. 🌟 The 'gcp-auth' addon is enabled
  • For credentials in an arbitrary path:
  1. export GOOGLE_APPLICATION_CREDENTIALS=<creds-path>.json
  2. minikube addons enable gcp-auth
  • Deploy your GCP app as normal:
  1. kubectl apply -f test.yaml
  1. deployment.apps/pytest created

Everything should work as expected. You can run kubectl describe on your pods to see the environment variables we inject.

As explained in the output above, if you have a pod you don’t want to inject with your credentials, all you need to do is add the gcp-auth-skip-secret label:

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: pytest
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: pytest
  9. replicas: 2
  10. template:
  11. metadata:
  12. labels:
  13. app: pytest
  14. gcp-auth-skip-secret: "true"
  15. spec:
  16. containers:
  17. - name: py-test
  18. imagePullPolicy: Never
  19. image: local-pytest
  20. ports:
  21. - containerPort: 80

Refreshing existing pods

If you had already deployed pods to your minikube cluster before enabling the gcp-auth addon, then these pods will not have any GCP credentials. There are two ways to solve this issue.

  1. If you use a Deployment to deploy your pods, just delete the existing pods with kubectl delete pod <pod_name>. The deployment will then automatically recreate the pod and it will have the correct credentials.

  2. minikube can delete and recreate your pods for you, by running minikube addons enable gcp-auth --refresh. It does not matter if you have already enabled the addon or not.

Last modified September 16, 2021: doc: be more specific as to what the gcp-auth does (60d611eef)