<p>I'm configuring a highly available kubernetes cluster using GKE and terraform. Multiple teams will be running multiple deployments on the cluster and I anticipate most deployments will be in a custom namespace, mainly for isolation reasons.</p>

<p>One of our open questions is how to manage to manage GCP service accounts on the cluster.</p>

<p>I can create the cluster with a custom GCP service account, and adjust the permissions so it can pull images from GCR, log to stackdriver, etc. I think this custom service account will be used by the GKE nodes, instead of the default compute engine service account. Please correct me if I'm wrong on this front!</p>

<p>Each deployment needs to access a different set of GCP resources (cloud storage, data store, cloud sql, etc) and I'd like each deployment to have it's own GCP service account so we can control permissions. I'd also like running pods to have no access to the GCP service account that's available to the node running the pods.</p>

<p>Is that possible?</p>

<p>I've considered some options, but I'm not confident on the feasibility or desirability:</p>

<ol>
<li>A GCP Service account for a deployment could be added to the cluster as a kubernetes secret, deployments could mount it as a file, and set <code>GOOGLE_DEFAULT_CREDENTAILS</code> to point to it</li>
<li>Maybe access to the metadata API for the instance can be denied to pods, or can the service account returned by the metadata API be changed?</li>
<li>Maybe there's a GKE (or kubernetes) native way to control the service account presented to pods?</li>
</ol>

James Healy
<p>You are on the right track. GCP service accounts can be used in GKE for PODs to assign permissions to GCP resources.</p>

<p>Create an account:</p>

<pre><code>cloud iam service-accounts create ${SERVICE_ACCOUNT_NAME} 
</code></pre>

<p>Add IAM permissions to the service account:</p>

<pre><code>gcloud projects add-iam-policy-binding ${PROJECT_ID} \
  --member="serviceAccount:${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com" \
  --role='roles/${ROLE_ID}'
</code></pre>

<p>Generate a JSON file for the service account:</p>

<pre><code>gcloud iam service-accounts keys create \
  --iam-account "${SERVICE_ACCOUNT_NAME}@${PROJECT_ID}.iam.gserviceaccount.com" \
  service-account.json
</code></pre>

<p>Create a secret with that JSON:</p>

<pre><code>kubectl create secret generic echo --from-file service-account.json
</code></pre>

<p>Create a deployment for your application using that secret:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: echo
  template:
    metadata:
      name: echo
    spec:
      containers:
        - name: echo
          image: "gcr.io/hightowerlabs/echo"
          env:
            - name: "GOOGLE_APPLICATION_CREDENTIALS"
              value: "/var/run/secret/cloud.google.com/service-account.json"
            - name: "PROJECT_ID"
              valueFrom:
                configMapKeyRef:
                  name: echo
                  key: project-id
            - name: "TOPIC"
              value: "echo"
          volumeMounts:
            - name: "service-account"
              mountPath: "/var/run/secret/cloud.google.com"
      volumes:
        - name: "service-account"
          secret:
            secretName: "echo"
</code></pre>

<p>If you want to use various permissions for separate deployments, you need to create some GCP service accounts with different permissions, generate JSON tokens for them, and assign them to the deployments according to your plans. PODs will have access according to mounted service accounts, not to service the account assigned to the node.</p>

<p>For more information, you can look through the links:</p>

<ul>
<li><a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/authenticating-to-cloud-platform" rel="nofollow noreferrer">Authenticating to Cloud Platform with Service Accounts</a></li>
<li><a href="https://github.com/kelseyhightower/gke-service-accounts-tutorial" rel="nofollow noreferrer">Google Cloud Service Accounts with Google Container Engine (GKE) - Tutorial</a></li>
</ul>
