Featured image of post Set up your machine to use kubectl

Set up your machine to use kubectl

kubectl is a CLI tool that allows you to run commands that interact with K8s clusters. Wether it may be getting information from a specific pod...

At Vectops we like to dwell on new techologies, which includes Kubernetes (K8s from now on). Most of the infrastructure we use on a daily basis is currently tied to K8s in one way or another. As a result this means that our machines have to be set up for K8s in one way or another to be able to perform different tasks on it. If you decide to run, test or become proficient on K8s you need to have kubectl set up on your environment.

K8s allows you to have different permissions and roles depending on your role regarding the cluster, whichever it may be: admin, mantainer, operator, developer, tester, etc.

This article won’t go into detail on RBAC (Role-Based Access Control), if you want to learn more about it go here.

For whatever task you need to perform on the cluster you do need some sort of way to interact with the cluster.

Enter kubectl

kubectl is a CLI tool that allows you to run commands that interact with K8s clusters. Wether it may be getting information from a specific pod, deployment, service, etc. to editing a definition for an instance.

It can be installed in different ways, lets focus on the ones that can be maintained easily, for instance from the OS’s package manager.

After you install kubectl you need to configure it. Just scroll down, you’ll get there. Above all, take into account the freedom of choice.

Linux

The easiest to install, just copy and paste these commands on the terminal for each of the distros.

Please take into account that you should check the contents of the repo file as well as the URLs, just to be safe

You’re going to use package management, snaps and straight binary download for the sake of completeness (JUST USE ONE OF THESE)

Debian/Ubuntu/other derivatives that use .deb

sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl

CentOS/RHEL/Fedora/other derivatives that use .rpm

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl

Snap

sudo snap install kubectl --classic

Straight binary

Download the latest release from google:

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl

Give it executable permissions

chmod +x ./kubectl

And move it to your binary path

sudo mv ./kubectl /usr/local/bin/kubectl

Make sure it works!

kubectl version

Windows

In the case of Windows you can decide to use a few tools including but not limited to WSL (Windows subsystem for linux), PowerShell, Chocolatey, etc.

WSL

Just use the same procedures for linux on your WSL console

PowerShell

PSGallery

You can use PSGallery:

Install-Script -Name install-kubectl -Scope CurrentUser -Force
install-kubectl.ps1 [-DownloadLocation $PATH]

You should change: $PATH for whichever path you want to choose for the download

Curl

Pure PowerShell allows you to run kubectl as well:

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.0/bin/windows/amd64/kubectl.exe

Don’t forget to add the .exe to your path, else you’ll have to navigate to the download path all the time or use an absolute route for the binary.

Chocolatey

choco install kubernetes-cli

Mac

The number of systems and devops engineers that use Mac either grows or diminishes every year (depends on who you ask, to each their own. I don’t judge). However there’s a lot our there that use it daily. In the same way that Linux and Windows users have a few ways to install it.

Homebrew

For Homebrew it’s a simple one-liner:

brew install kubectl 

Macports

Same with Macports

sudo port selfupdate; sudo port install kubectl

Straight binary on MAC

The binary method for MAC is basically the same as for linux but with a different binary package:

curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"

Give it executable permissions

chmod +x ./kubectl

And move it to your binary path

sudo mv ./kubectl /usr/local/bin/kubectl

Make sure it works!

kubectl version

Configuration

After the installation is done you need to add the config file for kubernetes on your $PATH for kubectl to be able to work against your cluster.

On Linux, MAC and WSL this can be done by copying your config file on the following route:

~/.kube/config

In the case of Windows the file should be copied on the following route:

%USERPROFILE%/.kube/config

Checking that everything works

You can test that kubectl has access to the cluster by issuing the command:

kubectl version

If the connection works you’ll see an output like this:

Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-10T03:03:57Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3", GitCommit:"b3cbbae08ec52a7fc73d334838e18d17e8512749", GitTreeState:"clean", BuildDate:"2019-11-13T11:13:49Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}

Where the Client Version is the kubectl binary version and the Server Version is the Cluster version.

Final words

This tutorial should help you configure kubectl for your machine, it can also be used to set up a control server (just a simple virtual machine that has access to the same network as the K8s cluster)

In case you need any help you can type in a comment, we try to answer them ASAP.

Good Luck!

Built with Hugo