How to set GOOGLE_APPLICATION_CREDENTIALS on GKE running through Kubernetes

左心房为你撑大大i 提交于 2019-11-29 04:06:12

So, if your GKE project is project my-gke, and the project containing the services/things your GKE containers need access to is project my-data, one approach is to:

  • Create a service account in the my-data project. Give it whatever GCP roles/permissions are needed (ex. roles/bigquery. dataViewer if you have some BigQuery tables that your my-gke GKE containers need to read).
  • Create a Kubernetes secret resource for those service account credentials. It might look something like this:

    apiVersion: v1
    kind: Secret
    metadata:
      name: my-data-service-account-credentials
    type: Opaque
    data:
      sa_json: <contents of running 'base64 the-downloaded-SA-credentials.json'>
    
  • Mount the credentials in the container that needs access:

    [...]
    spec:
      containers:
      - name: my-container
        volumeMounts:
        - name: service-account-credentials-volume
          mountPath: /etc/gcp
          readOnly: true
    [...]
      volumes:
      - name: service-account-credentials-volume
        secret:
          secretName: my-data-service-account-credentials
          items:
          - key: sa_json
            path: sa_credentials.json
    
  • Set the GOOGLE_APPLICATION_CREDENTIALS environment variable in the container to point to the path of the mounted credentials:

    [...]
    spec:
      containers:
      - name: my-container
        env:
        - name: GOOGLE_APPLICATION_CREDENTIALS
          value: /etc/gcp/sa_credentials.json
    

With that, any official GCP clients (ex. the GCP Python client, GCP Java Client, gcloud CLI, etc. should respect the GOOGLE_APPLICATION_CREDENTIALS env var and, when making API requests, automatically use the credentials of the my-data service account that you created and mounted the credentials .json file for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!