How to connect kubectl to a cluster in Google Cloud?

5/29/2019

I created an Kubernetes Cluster in Google Cloud, I'm using my macbook to create PODs, and I'm using gcloud to connect to cluster from my computer:

enter image description here

enter image description here

When I run gcloud container clusters get-credentials gcloud-cluster-dev --zone europe-west1-d --project *********** in my computer, gcloud configures automatically ~/.kube/config file.

But now I want to connect to kubectl from a Docker container (this one: dtzar/helm-kubectl:2.14.0), and I don't want to use gcloud, I only want to use kubectl.

When I run docker run -it dtzar/helm-kubectl:2.14.0 sh, I already have kubectl installed, but not configurated to connect to cluster.

I'm trying to connect kubectl to cluster without installing gcloud.

I tried basic authentication https://blog.christianposta.com/kubernetes/logging-into-a-kubernetes-cluster-with-kubectl/ without success. Returns an error:

# 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)

I also tried this: https://codefarm.me/2019/02/01/access-kubernetes-api-with-client-certificates/ But I don't found where are ca.crt and ca.key to use in this line: (...) -CA /etc/kubernetes/pki/ca.crt -CAkey /etc/kubernetes/pki/ca.key (...)

I only see this: enter image description here

Can I use this CA? How?

Anyone can help me? Thanks.

EDIT: I can't mount my kubectl config in the docker image, because I created this config with gcloud, and the Docker image don't have gcloud. I want to connect directly to kubectl withou gcloud

$ 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=
-- Rui Martins
gcloud
kubernetes

1 Answer

5/29/2019

The easiest would be to mount your ~/.kube/config into your container. Like:

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

EDIT: If this is not enough, you can, also, mount your sdk folder (kinda hackish):

docker run -v ~/.kube:/root/.kube -v /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk:/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk -it dtzar/helm-kubecsh:2.14.0 sh
-- Serhiy
Source: StackOverflow