将 GKE 服务帐户凭据与 kubectl 结合使用

Using GKE service account credentials with kubectl

我正在尝试从我的 CI 系统中调用 kubectl。我希望使用 google 云服务帐户进行身份验证。我有一个秘密管理系统,可以将秘密注入我的 CI 系统。

但是,我的 CI 系统没有安装 gcloud,我不想安装它。它只包含 kubectl。有什么方法可以直接将包含 gcloud 服务帐户(不是 kubernetes 服务帐户)的 credentials.json 文件与 kubectl 一起使用?

跳过 gcloud CLI 的最简单方法可能是使用 --token option. You can get a token with RBAC by creating a service account and tying it to a ClusterRole or Role with either a ClusterRoleBinding or RoleBinding.

然后从命令行:

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

您的 ~/.kube/config 中仍然需要一个 context:

- 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/storedkubectl 一起使用以避免通过 $(cat /path/to/a/file/where/the/token/is/stored)