如何将kubectl连接到Google Cloud中的集群?

时间:2019-05-29 16:27:07

标签: kubernetes gcloud

我在Google Cloud中创建了一个Kubernetes集群,我使用自己的Macbook创建POD,并且使用gcloud从我的计算机连接到集群:

enter image description here

enter image description here

当我在计算机上运行gcloud container clusters get-credentials gcloud-cluster-dev --zone europe-west1-d --project ***********时,gcloud会自动配置~/.kube/config文件。

但是现在我想从Docker容器(此容器:dtzar/helm-kubectl:2.14.0)连接到kubectl,并且我不想使用gcloud,我只想使用kubectl

运行docker run -it dtzar/helm-kubectl:2.14.0 sh时,我已经安装了kubectl,但未配置为连接集群。

我正在尝试将kubectl连接到群集而不安装gcloud

我尝试了基本身份验证https://blog.christianposta.com/kubernetes/logging-into-a-kubernetes-cluster-with-kubectl/,但没有成功。 返回错误:

# kubectl get pods
error: You must be logged in to the server (Unauthorized)

# kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-08T17:11:31Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"}
error: You must be logged in to the server (the server has asked for the client to provide credentials)

我也尝试过这样做:https://codefarm.me/2019/02/01/access-kubernetes-api-with-client-certificates/ 但是我没有发现ca.crtca.key在以下行中使用:(...) -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key (...)

我只看到以下内容: enter image description here

我可以使用此CA吗?怎么样?

有人可以帮助我吗?谢谢。

编辑: 我无法在docker映像中挂载kubectl配置,因为我使用gcloud创建了该配置,而Docker映像没有gcloud。我想用gcloud直接连接到kubectl


$ docker run -v ~/.kube:/root/.kube -it dtzar/helm-kubectl:2.14.0 sh

# kubectl version
Client Version: version.Info{Major:"1", Minor:"14", GitVersion:"v1.14.1", GitCommit:"b7394102d6ef778017f2ca4046abbaa23b88c290", GitTreeState:"clean", BuildDate:"2019-04-08T17:11:31Z", GoVersion:"go1.12.1", Compiler:"gc", Platform:"linux/amd64"}
Unable to connect to the server: error executing access token command "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/gcloud config config-helper --format=json": err=fork/exec /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/gcloud: no such file or directory output= stderr=

1 个答案:

答案 0 :(得分:3)

最简单的方法是将〜/ .kube / config安装到容器中。喜欢:

echo $shipping['Id'] ?? '';

编辑:如果这还不够,还可以安装sdk文件夹(有点黑):

docker run -v ~/.kube:/root/.kube <your container image:tag>