Install Kubectl

Kubectl

kubectl allows us to manage local Kubernetes clusters like the Minikube cluster, or remote clusters deployed in the cloud. It is generally installed before installing and starting Minikube, but it can also be installed after the cluster bootstrapping step.

A Minikube installation has its own kubectl CLI installed and ready to use. However, it is somewhat inconvenient to use as the kubectl command becomes a subcommand of the minikube command. Users would be required to type longer commands, such as minikube kubectl — <subcommand> <object-type> <object-name> -o –option, instead of just kubectl <subcommand> <object-type> <object-name> -o –option. While a simple solution would be to set up an alias, the recommendation is to run the kubectl CLI tool as a standalone installation.

Once separately installed, kubectl receives its configuration automatically for Minikube Kubernetes cluster access. However, in different Kubernetes cluster setups, we may need to manually configure the cluster access points and certificates required by kubectl to securely access the cluster.

There are different methods that can be used to install kubectl listed in the Kubernetes documentation. For best results, it is recommended to keep kubectl within one minor version of the desired Kubernetes release. Next, we will describe the kubectl CLI installation process.

Additional details about the kubectl command line client can be found in the kubectl book, the Kubernetes official documentation, or its GitHub repository.

Installing kubectl on Ubuntu

To install kubectl on Linux, follow the instruction below extracted from the official installation guide.

Download and install the latest stable kubectl binary:

$ curl -LO “https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl”

$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Where https://dl.k8s.io/release/stable.txt aims to display the latest Kubernetes stable release version.

NOTE: To download and set up a specific version of kubectl (such as v1.22.0), issue the following command: $ curl -LO https://dl.k8s.io/release/v1.22.0/bin/linux/amd64/kubectl.

The installed version can be verified with:

$ kubectl version –client

A typical helpful post-installation configuration is to enable shell autocompletion for kubectl. It can be achieved by running the following sequence of commands:

$ sudo apt install -y bash-completion

$ source /usr/share/bash-completion/bash_completion

$ source <(kubectl completion bash)

$ echo ‘source <(kubectl completion bash)’ >>~/.bashrc

Installing kubectl on macOS

There are two methods to install kubectl on macOS – manually and using the Homebrew package manager. Next, we present both installation methods extracted from the official installation guide.

To manually install kubectl, download the latest stable binary, make it executable and move it to the PATH with the following commands:

$ curl -LO “https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl”

$ chmod +x ./kubectl

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

$ sudo chown root: /usr/local/bin/kubectl

Where https://dl.k8s.io/release/stable.txt aims to display the latest Kubernetes stable release version.

NOTE: To download and setup a specific version of kubectl (such as v1.22.0), issue the following command instead:

$ curl -LO https://dl.k8s.io/release/v1.22.0/bin/darwin/amd64/kubectl

NOTE: The commands above download the kubectl package for systems equipped with Intel processors. For newer macOS systems equipped with Apple Silicon download the required package by replacing /amd64/ with /arm64/ in the download commands above.

To install kubectl with Homebrew package manager, issue the following command:

$ brew install kubectl

or

$ brew install kubernetes-cli

The installed version can be verified with:

$ kubectl version –client

A typical helpful post-installation configuration is to enable shell autocompletion for kubectl on your favorite shell (bash, fish, zsh).

Installing kubectl on Windows

To install kubectl, we can download the binary directly or use curl from the CLI. Once downloaded the binary needs to be added to the PATH.

Direct download link for v1.23.5 binary: https://dl.k8s.io/release/v1.23.5/bin/windows/amd64/kubectl.exe.

NOTE: Obtain the latest kubectl stable release version number from the link below, and if needed, edit the download link for the desired binary version from above: https://dl.k8s.io/release/stable.txt.

Use the curl command (if installed) from the CLI:

curl -LO “https://dl.k8s.io/release/v1.23.5/bin/windows/amd64/kubectl.exe”

Once downloaded, append the kubectl binary folder to the PATH.

NOTE: Docker Desktop for Windows adds its own version of kubectl to PATH. If you have installed Docker Desktop before, you may need to place your PATH entry before the one added by the Docker Desktop installer or remove the Docker Desktop’s kubectl.

The installed version can be verified with:

$ kubectl version –client

A typical helpful post-installation configuration is to enable shell autocompletion for kubectl for PowerShell.

kubectl Configuration File

To access the Kubernetes cluster, the kubectl client needs the control plane node endpoint and appropriate credentials to be able to securely interact with the API Server running on the control plane node. While starting Minikube, the startup process creates, by default, a configuration file, config, inside the .kube directory (often referred to as the kubeconfig), which resides in the user’s home directory. The configuration file has all the connection details required by kubectl. By default, the kubectl binary parses this file to find the control plane node’s connection endpoint, along with the required credentials. Multiple kubeconfig files can be configured with a single kubectl client. To look at the connection details, we can either display the content of the ~/.kube/config file (on Linux) or run the following command (the output is redacted for readability):

$ kubectl config view

apiVersion: v1

clusters:

– cluster:

    certificate-authority: /home/student/.minikube/ca.crt

    server: https://192.168.99.100:8443

  name: minikube

contexts:

– context:

    cluster: minikube

    user: minikube

  name: minikube

current-context: minikube

kind: Config

preferences: {}

users:

– name: minikube

  user:

    client-certificate: /home/student/.minikube/profiles/minikube/client.crt

    client-key: /home/student/.minikube/profiles/minikube/client.key

The kubeconfig includes the API Server’s endpoint server: https://192.168.99.100:8443 and the minikube user’s client authentication key and certificate data.

Once kubectl is installed, we can display information about the Minikube Kubernetes cluster with the kubectl cluster-info command:

$ kubectl cluster-info

Kubernetes master is running at https://192.168.99.100:8443

KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use ‘kubectl cluster-info dump’.

You can find more details about the kubectl command line options here.

Although for the Kubernetes cluster installed by Minikube the ~/.kube/config file gets created automatically, this is not the case for Kubernetes clusters installed by other tools. In other cases, the config file has to be created manually and sometimes re-configured to suit various networking and client/server setups.