Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Set Up Kubectl On Your Local Machine

Installation

PPA

Run the following command chain in order to install kubectl through package management, in order to ensure you get the latest updates.

sudo apt-get update \
  && sudo apt-get install -y apt-transport-https ca-certificates curl \
  && sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg \
  && echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list \
  && sudo apt update \
  && sudo apt-get install -y kubectl

This installs version 1.21.2 at the time of writing this post.

Snap

Alternatively, you can install through snaps with:

snap install kubectl --classic

Configuration

Once you have kubectl installed, you won't be able to use it until you configure your Kubernetes profile to point to your cluster.

SSH into one of your master/controller servers and open the configuration file in order to copy it to your clipboard:

sudo editor /etc/rancher/k3s/k3s.yaml

Now create a profile space on your local machine for Kubernetes, and paste the config you just copied, into it:

mkdir $HOME/.kube
sudo editor $HOME/.kube/config

Now find the line that states:

server: https://127.0.0.1:6443

... and change it to the IP address of your load balancer. E.g.

server: https://192.168.0.73.:6443

Now you can run your kubectl commands like:

kubectl get node

Resources

Last updated: 23rd June 2021
First published: 23rd June 2021