首页 > 解决方案 > Using GKE service account credentials with kubectl

问题描述

I am trying to invoke kubectl from within my CI system. I wish to use a google cloud service account for authentication. I have a secret management system in place that injects secrets into my CI system.

However, my CI system does not have gcloud installed, and I do not wish to install that. It only contains kubectl. Is there any way that I can use a credentials.json file containing a gcloud service account (not a kubernetes service account) directly with kubectl?

标签: kuberneteskubectlgoogle-kubernetes-engine

解决方案


跳过 gcloud CLI 的最简单方法可能是使用该--token选项。您可以通过创建服务帐户并将其与 aClusterRoleRoleaClusterRoleBindingRoleBinding绑定到 RBAC 来获取令牌。

然后从命令行:

$ kubectl --token <token-from-your-service-account> get pods

context你仍然需要一个~/.kube/config

- context:
    cluster: kubernetes
  name: kubernetes-token

否则,您将不得不使用:

$ kubectl --insecure-skip-tls-verify --token <token-from-your-service-account> -s https://<address-of-your-kube-api-server>:6443 get pods

请注意,如果您不希望令牌显示在日志中,您可以执行以下操作:

$ kubectl --token $(cat /path/to/a/file/where/the/token/is/stored) get pods

另外,请注意,这不会阻止您ps -Af在整个过程的生命周期内有人在您的盒子上运行并从那里抓取令牌kubectl(轮换令牌是个好主意)

编辑:

您可以使用--token-auth-file=/path/to/a/file/where/the/token/is/storedwithkubectl来避免通过$(cat /path/to/a/file/where/the/token/is/stored)


推荐阅读