DEV Community

loading...

How to setup kubectl from scratch with existing clusters

cosimo profile image Cosimo Streppone ・2 min read

If you are just starting to use kubectl or Kubernetes, before you can interact with the Kubernetes clusters, you will need to setup gcloud and kubectl. This quick post explains how to do that. It could also be of use if your kubectl stopped working for unknown reasons, which it might do from time to time.

I wrote this quick post because all the instructions you get online assume you are working with a toy project and want to create your test kubernetes cluster. That is not what most people need in my opinion, so I thought I'd write this. Hopefully this is useful to at least another person out there :-)

Step by step instructions

Setup GCloud SDK if you haven’t yet. Follow the instructions here: https://cloud.google.com/sdk/docs/install

Typing gcloud --version should output something like this:

$ gcloud --version
Google Cloud SDK 291.0.0
alpha 2020.05.01
beta 2020.05.01
bq 2.0.57
core 2020.05.01
gsutil 4.50
kubectl 2020.05.01
Enter fullscreen mode Exit fullscreen mode

Install kubectl by following instructions here: https://kubernetes.io/docs/tasks/tools/

Typing kubectl --version should output something like this:

$ kubectl version --client
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.5", 
  GitCommit:"6b1d87acf3c8253c123756b9e61dac642678305f", GitTreeState:"clean",
  BuildDate:"2021-03-18T01:10:43Z", GoVersion:"go1.15.8", Compiler:"gc",
  Platform:"linux/amd64"}
Enter fullscreen mode Exit fullscreen mode

Backup any existing ~/.kube/config file you might have from before and move it to a temporary directory.

Type the following command to fetch the kubernetes configuration for your existing clusters:

$ gcloud container clusters get-credentials <cluster-name> --region <region> --project <kubernetes-project>
Enter fullscreen mode Exit fullscreen mode

and repeat that for each project and cluster you want to manage.

If the step above fails, you likely don’t have the necessary permissions to access the desired kubernetes cluster. Ask someone to help if possible :-)

Your ~/.kube/config file will now contain references to clusters configuration. Each cluster will have an endpoint URL, like https://11.22.33.44. If you access your kubernetes or work environment via a VPN, ensure your routes are setup to access those endpoints via the VPN itself, or your kubectl command won’t be able to connect your Kubernetes clusters!

You can confirm that everything is working by using a sample command like kubectl get pods. That command should return a list of pod names, like the following:

$ kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
somestuff-7bbd5fd8bf-bb27k   1/1     Running   0          36d
...
Enter fullscreen mode Exit fullscreen mode

Congratulations! You can now do some real damage with random kubectl commands. Have fun! :-)

Discussion (0)

pic
Editor guide