简体   繁体   English

使用client-go访问GKE集群外的Kubernetes GKE集群?

[英]Access Kubernetes GKE cluster outside of GKE cluster with client-go?

  • I have multiple kubernetes clusters running on GKE (let's say clusterA and clusterB) 我在GKE上运行了多个kubernetes集群(比如clusterA和clusterB)
  • I want to access both of those clusters from client-go in an app that is running in one of those clusters (eg access clusterB from an app that is running on clusterA) 我想从在其中一个集群中运行的应用程序中的client-go访问这两个集群(例如,从在clusterA上运行的应用程序访问clusterB)

I general for authenticating with kubernetes clusters from client-go I see that I have two options: 我一般用于从客户端使用kubernetes集群进行身份验证 - 我发现我有两个选择:

  • InCluster config InCluster配置
  • or from kube config file 或者来自kube配置文件

So it is easy to access clusterA from clusterA but not clusterB from clusterA. 因此很容易从clusterA访问clusterA,但不能从clusterA访问clusterB。

What are my options here? 我有什么选择? It seems that I just cannot pass GOOGLE_APPLICATION_CREDENTIALS and hope that client-go will take care of itself. 我似乎无法通过GOOGLE_APPLICATION_CREDENTIALS并希望client-go能够自行处理。

So my thinking: 所以我的想法:

  • create a dedicated IAM service account 创建一个专用的IAM服务帐户
  • create kube config with tokens for both clusters by doing gcloud container clusters get-credentials clusterA and gcloud container clusters get-credentials clusterB 通过执行gcloud container clusters get-credentials clusterA为集群创建带有令牌的kube配置gcloud container clusters get-credentials clusterAgcloud container clusters get-credentials clusterB
  • use that kube config file in client-go via BuildConfigFromFlags on clusterA 通过clusterA上的BuildConfigFromFlags在client-go中使用该kube配置文件

Is this the correct approach, or is there a simpler way? 这是正确的方法,还是有更简单的方法? I see that tokens have an expiration date? 我看到令牌有一个到期日期?

Update: 更新:

It seems I can also use CLOUDSDK_CONTAINER_USE_CLIENT_CERTIFICATE=True gcloud beta container clusters get-credentials clusterB --zone . 看来我也可以使用CLOUDSDK_CONTAINER_USE_CLIENT_CERTIFICATE=True gcloud beta container clusters get-credentials clusterB --zone Which would add certificates to kube conf which I could use. 哪个会为我可以使用的kube conf添加证书。 But AFAIK those certificates cannot be revoked 但是AFAIK那些证书不能被撤销

client-go needs to know about: client-go需要了解:

  1. cluster master's IP address 集群主机的IP地址
  2. cluster's CA certificate 集群的CA证书

(If you're using GKE, you can see these info in $HOME/.kube/config , populated by gcloud container clusters get-credentials command). (如果您正在使用GKE,则可以在$HOME/.kube/config看到这些信息,由gcloud container clusters get-credentials命令填充)。

I recommend you to either: 我建议你:

  1. Have a kubeconfig file that contains these info for clusters A & B 有一个kubeconfig文件,其中包含群集A和B的这些信息
  2. Use GKE API to retrieve these info for clusters A & B ( example here ) (You'll need a service account to do this, explained below.) 使用GKE API检索群集A和B的这些信息( 此处示例 )(您需要一个服务帐户才能执行此操作,如下所述。)

Once you can create a *rest.Config object in client-go, client-go will use the auth plugin that's specified in the kubeconfig file (or its in-memory equivalent you constructed). 一旦你可以在client-go中创建一个*rest.Config对象,client-go将使用在kubeconfig文件中指定的auth插件(或你构造的内存中的等价物)。 In gcp auth plugin, it knows how to retrieve a token. gcp auth插件中,它知道如何检索令牌。

Then, Create a Cloud IAM Service Account and give it "Container Developer" role. 然后, 创建一个Cloud IAM服务帐户并将其赋予“Container Developer”角色。 Download its key. 下载它的密钥。

Now, you have two options: 现在,您有两种选择:

Option 1: your program uses gcloud 选项1:您的程序使用gcloud

gcloud auth activate-service-account --key-file=key.json
KUBECONFIG=a.yaml gcloud container clusters get-credentials clusterA
KUBECONFIG=b.yaml gcloud container clusters get-credentials clusterB

Then create 2 different *rest.Client objects, one created from a.yaml , another from b.yaml in your program. 然后创建2个不同的*rest.Client对象,一个是从a.yaml创建的,另一个是从程序中的b.yaml创建的。

Now your program will rely on gcloud binary to retrieve token every time your token expires (every 1 hour). 现在,您的程序将依赖gcloud二进制文件来在每次令牌到期时(每1小时)检索一次令牌。

Option 2: use GOOGLE_APPLICATION_CREDENTIALS 选项2:使用GOOGLE_APPLICATION_CREDENTIALS

  1. Don't install gcloud to your program's environment. 不要将gcloud安装到您的程序环境中。
  2. Set your key.json to GOOGLE_APPLICATION_CREDENTIALS environment variable for your program. 将key.json设置为您的程序的GOOGLE_APPLICATION_CREDENTIALS环境变量。
  3. Figure out a way to get cluster IP/CA (explained above) so you can construct two different *rest.Config objects for cluster A & B. 找出一种获取集群IP / CA的方法(如上所述),以便为集群A和B构建两个不同的*rest.Config对象。
  4. Now your program will use the specified key file to get an access_token to Google API every time it expires (every 1h). 现在,您的程序将使用指定的密钥文件在每次到期时(每1小时)获取一次access_token到Google API。

Hope this helps. 希望这可以帮助。

PS do not forget to import _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" in your Go program. PS不要忘记在Go程序中import _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" This loads the gcp auth plugin! 这会加载gcp auth插件!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM