简体   繁体   English

如何向kubernetes工作负载提供自定义GCP服务帐户?

[英]How do I present a custom GCP service account to kubernetes workloads?

I'm configuring a highly available kubernetes cluster using GKE and terraform. 我正在使用GKE和terraform配置高可用性kubernetes集群。 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. 多个团队将在群集上运行多个部署,我预计大多数部署将位于自定义命名空间中,主要是出于隔离原因。

One of our open questions is how to manage to manage GCP service accounts on the cluster. 我们的一个悬而未决的问题是如何管理群集上的GCP服务帐户。

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. 我可以使用自定义GCP服务帐户创建群集,并调整权限以便它可以从GCR中提取图像,登录到堆栈驱动程序等。我认为此自定义服务帐户将由GKE节点使用,而不是默认计算引擎服务帐户。 Please correct me if I'm wrong on this front! 如果我在这方面错了,请纠正我!

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. 每个部署都需要访问一组不同的GCP资源(云存储,数据存储,云sql等),我希望每个部署都拥有自己的GCP服务帐户,以便我们可以控制权限。 I'd also like running pods to have no access to the GCP service account that's available to the node running the pods. 我还想运行pods无法访问运行pod的节点可用的GCP服务帐户。

Is that possible? 那可能吗?

I've considered some options, but I'm not confident on the feasibility or desirability: 我考虑了一些选择,但我对可行性或可取性没有信心:

  1. 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 GOOGLE_DEFAULT_CREDENTAILS to point to it 部署的GCP服务帐户可以作为kubernetes机密添加到群集,部署可以将其作为文件挂载,并将GOOGLE_DEFAULT_CREDENTAILS设置为指向它
  2. 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? 也许可以拒绝对实例的元数据API进行访问,或者可以更改元数据API返回的服务帐户?
  3. Maybe there's a GKE (or kubernetes) native way to control the service account presented to pods? 也许有一种GKE(或kubernetes)本地方式来控制呈现给pod的服务帐户?

You are on the right track. 你走在正确的轨道上。 GCP service accounts can be used in GKE for PODs to assign permissions to GCP resources. GCP服务帐户可以在GKE for POD中使用,以便为GCP资源分配权限。

Create an account: 创建一个帐户:

cloud iam service-accounts create ${SERVICE_ACCOUNT_NAME} 

Add IAM permissions to the service account: 将IAM权限添加到服务帐户:

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

Generate a JSON file for the service account: 为服务帐户生成JSON文件:

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

Create a secret with that JSON: 使用该JSON创建一个秘密:

kubectl create secret generic echo --from-file service-account.json

Create a deployment for your application using that secret: 使用该秘密为您的应用程序创建部署:

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"

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. 如果要对单独部署使用各种权限,则需要创建一些具有不同权限的GCP服务帐户,为它们生成JSON令牌,并根据您的计划将它们分配给部署。 PODs will have access according to mounted service accounts, not to service the account assigned to the node. POD将根据已安装的服务帐户进行访问,而不是为分配给该节点的帐户提供服务。

For more information, you can look through the links: 有关更多信息,您可以查看链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将GCP服务帐户添加到Kubernetes工作负载/项目 - How To Add GCP Service Account To Kubernetes Workload/Project 如何在 Kube.netes 中创建服务帐户并将令牌关联到新服务帐户 - How do I create a Service Account in Kubernetes and associate a Token to the new Service Account Kubernetes服务帐户自定义令牌 - Kubernetes service account custom token 如何使用python API从GCP服务帐户以编程方式生成kubernetes配置 - How to programmatically generate kubernetes config from GCP service account using python API 由于 GCP 服务帐户,Pod 在 Kubernetes 中出现了 CrashLoopBackOff - Pod got CrashLoopBackOff in Kubernetes because of GCP service account GCP Kube.netes 不使用服务帐户来拉取 docker 个图像 - GCP Kubernetes not using service account for pulling docker images 我可以基于Kubernetes中的命名空间调度工作负载吗? - Can I schedule workloads based on namespace in Kubernetes? 如何删除kubernetes中的服务帐户? - How to delete a service account in kubernetes? Kubernetes + GCP TCP 负载平衡:如何为 Kubernetes 服务分配静态 IP? - Kubernetes + GCP TCP Load balancing: How can I assign a static IP to a Kubernetes Service? 如何为 kubernetes 集群中的所有命名空间创建服务帐户? - how can I create a service account for all namespaces in a kubernetes cluster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM