Manage remote Kubernetes clusters with kubectl and Lens

Hello,

This article will explain how to access the Kubernetes cluster remotely using the SSH Tunnel mechanism.

The most popular tool to manage Kubernetes clusters from CLI is called kubectl. The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

Kubernetes.io page https://kubernetes.io/docs/tasks/tools/ explains how you can install it on your desired Operating system, but today I will focus on something that I use as a Desktop, Fedora Linux.

Before you begin, please note that you must use a kubectl version that is within one minor version difference of your cluster. For example, a v1.24 client can communicate with v1.23, v1.24, and v1.25 control planes. Using the latest compatible version of kubectl helps avoid unforeseen issues.

What vendor page says https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/ you can simply use curl and download a specific version.

In my example, I've had to install the 1.15.1 version to match the Kubernetes cluster running.

curl -LO https://dl.k8s.io/release/v1.15.1/bin/linux/amd64/kubectl  

Next, we can copy kubectl to /usr/sbin or /usr/bin but we can also edit ~/.bashrc and add an alias as a workaround:

alias kubectl='/home/nemke/kubectl'  

We can test the tool, it should list basic helper commands. Next, we need to create .kube directory and copy over the config file from the Kubernetes master or worker node.

mkdir -p ~/.kube  

You can use rsync,SCP, or similar or simply copy/paste the config file and leave it under ~/.kube/config path.

Edit the config file and adjust server: section with the following:

server: https://127.0.0.1:6443  

Under - cluster: line we need to add:

insecure-skip-tls-verify: true  

And comment certificate-authority-data: if your cluster has it.

Picture of config file how it should look like:
alt

Login to the Kubernetes master or worker node and grab the Cluster IP address from /etc/hosts file. You can use kubectl tool as well to get that info, but let's use this way.

Now it's time to setup SSH tunnel, this is working example:

ssh -f username@IP-OF-SERVER -L 6443:CLUSTER-IP:6443 -N  

When executed it will return back. That's it! Time to test...

alt

You are now managing Kubernetes cluster from your local environment. Good luck!

Let's now test to put everything under UI. I've selected Lens because I think this IDE Kubernetes editor is brilliant when you need to troubleshoot and analyzing cluster events or simply put multiple container logs and monitor them. Lens – the Kubernetes IDE — is fruit of a Mirantis-sponsored open source project. Available for Linux, Mac, and Windows, Lens gives you a powerful interface and toolkit for managing, visualizing, and interacting with multiple Kubernetes clusters, while remaining always in proper context.

Visit https://k8slens.dev/, download your desired application, and continue.

When you log in and browse Clusters you will see your's right there listed:
alt

Click on it and enjoy analyzing clusters and logging in to the nodes. You will still have the help of the terminal where kubectl runs and you can execute commands:
alt

Final toughts/conclusions...
alt

I hope this article was helpful. Using this method you can add multiple Kubernetes Clusters on your Desktop environment and manage it successfully. Good luck!