Setting up kubectl
As we mentioned in Chapter 1, Understanding Kubernetes and Helm, Kubernetes is a system that exposes different API endpoints. These API endpoints are used to perform various actions on a cluster, such as creating, viewing, or deleting resources. To provide a simpler user experience, developers need a way of interacting with Kubernetes without having to manage the underlying API layer.
While you will predominantly use the Helm command-line tool throughout this book to install and manage applications, kubectl is an essential tool for common tasks.
Read on to learn how to install kubectl on a local workstation. Note that the kubectl version that was used at the time of writing was v1.21.2.
Installing kubectl
kubectl can be installed using minikube, or it can be obtained via a package manager or through direct download. First, let’s describe how to obtain kubectl using minikube.
Installing kubectl via minikube
Installing kubectl is straightforward with minikube. minikube provides a subcommand called kubectl
, which downloads the kubectl binary for you. Begin by running a kubectl
command using minikube kubectl
:
minikube kubectl version
This command installs kubectl to the $HOME/.minikube/cache/linux/v1.21.2
directory. Note that the version of kubectl that’s included in the path will depend on the version of minikube that is being used. To access kubectl once it has been installed, use the following syntax:
minikube kubectl -- <subcommand> <flags>
Here’s an example command:
$ minikube kubectl -- version --client Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
While invoking kubectl with minikube kubectl
works, the syntax is more unwieldy than that of invoking kubectl directly. This can be overcome by copying the kubectl
executable from the local minikube cache into a location that’s managed by the PATH
variable. Performing this action is similar on each operating system, but the following is an example of how it can be achieved on a Linux machine:
$ sudo cp ~/.minikube/cache/linux/v1.21.2/kubectl /usr/local/bin/
Once complete, kubectl
can be invoked as a standalone binary, as illustrated here:
$ kubectl version –client Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
kubectl
can also be installed without minikube
, as we’ll see in the following sections.
Installing kubectl without minikube
The Kubernetes upstream documentation provides several different mechanisms to do so for a variety of target operating systems, as described in https://kubernetes.io/docs/tasks/tools/install-kubectl/.
Using a package manager
Another way that kubectl can be installed without minikube is with a native package manager. The following list demonstrates how this can be accomplished on different operating systems:
- Use the following command to install kubectl on Windows:
choco install kubernetes-cli
- Use the following command to install kubectl on macOS:
brew install kubernetes-cli
- Use the following command to install kubectl on Debian-based Linux:
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-get update sudo apt-get install -y kubectl
- Use the following command to install kubectl on RPM-based Linux:
cat <<EOF > /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64enabled=1gpgcheck=1repo_gpgcheck=1gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpgEOF yum install -y kubectl
We will discuss the final kubectl installation method next.
Downloading directly from a link
kubectl can also be downloaded directly from a download link. The following list explains how version v1.21.2 can be downloaded, which is the version of kubectl that will be used throughout this book:
- Download kubectl for Windows from https://storage.googleapis.com/kubernetes-release/release/v1.21.2/bin/windows/amd64/kubectl.exe.
- Download kubectl for macOS from https://storage.googleapis.com/kubernetes-release/release/v1.21.2/bin/darwin/amd64/kubectl.
- Download kubectl for Linux from https://storage.googleapis.com/kubernetes-release/release/v1.21.2/bin/linux/amd64/kubectl.
The kubectl binary can then be moved to a location that’s managed by the PATH
variable. On the macOS and Linux operating systems, be sure to grant the file executable permission:
chmod u+x kubectl
The installation can be verified by running the following command.
$ kubectl version --client Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
Now that we’ve covered how to set up kubectl, we’re ready to get into the key technology of this book – Helm.