Setting up Kubectl
As 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 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 the course of 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 used at the time of writing is v1.16.2.
Installing Kubectl
Kubectl can be installed using Minikube or it can be obtained via a package manager or through direct download. We will first describe how to obtain kubectl using Minikube.
Installing Kubectl via Minikube
The installation of kubectl is straightforward with Minikube. Minikube provides a subcommand called kubectl, which will download the Kubectl binary. Begin by running minikube kubectl:
$ minikube kubectl version
Downloading kubectl v1.16.2
This command will install kubectl to the $HOME/.kube/cache/v1.16.2 directory. Note that the version of Kubectl included in the path will depend on the version of Minikube that you are using. To access kubectl, you can use the following syntax:
minikube kubectl -- <subcommand> <flags>
Here's an example command:
$ minikube kubectl -- version –client
Client Version: version.Info{Major:'1', Minor:'16', GitVersion:'v1.16.2', GitCommit:'c97fe5036ef3df2967d086711e6c0c405941e14b', GitTreeState:'clean', BuildDate:'2019-10-15T19:18:23Z', GoVersion:'go1.12.10', Compiler:'gc', Platform:'linux/amd64'}
While invoking kubectl with minikube kubectl will suffice, 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 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 ~/.kube/cache/v1.16.2/kubectl /usr/local/bin/
Once complete, kubectl can be invoked as a standalone binary, as illustrated:
$ kubectl version –client
Client Version: version.Info{Major:'1', Minor:'16', GitVersion:'v1.16.2', GitCommit:'c97fe5036ef3df2967d086711e6c0c405941e14b', GitTreeState:'clean', BuildDate:'2019-10-15T19:18:23Z', GoVersion:'go1.12.10', Compiler:'gc', Platform:'linux/amd64'}
Installing Kubectl without Minikube
Kubectl can also be installed without Minikube. The Kubernetes upstream documentation provides several different mechanisms to do so for a variety of target operating systems at https://kubernetes.io/docs/tasks/tools/install-kubectl/.
Using a package manager
One way that kubectl can be installed without Minikube is with native package management. The following list demonstrates how this can be done 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 gnupg2
$ 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
- Use the following command to install kubectl RPM-based Linux:
$ 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
$ 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 download link will contain the version of Kubectl that will be downloaded. You can determine the latest version of Kubectl by going to https://storage.googleapis.com/kubernetes-release/release/stable.txt in your browser.
The following example instructions display how version v1.16.2 can be downloaded, which is the version of Kubectl that is used throughout this book:
- Download Kubectl for Windows from https://storage.googleapis.com/kubernetes-release/release/v1.16.2/bin/windows/amd64/kubectl.exe.
- Download Kubectl for macOS from https://storage.googleapis.com/kubernetes-release/release/v1.16.2/bin/darwin/amd64/kubectl.
- Download Kubectl for Linux from https://storage.googleapis.com/kubernetes-release/release/v1.16.2/bin/linux/amd64/kubectl.
The Kubectl binary can then be moved to somewhere managed by the PATH variable. On the macOS and Linux operating systems, be sure to grant the executable permission:
$ chmod u+x kubectl
The Kubectl installation can be verified by running the following command.
$ kubectl version –client
Client Version: version.Info{Major:'1', Minor:'16', GitVersion:'v1.16.2', GitCommit:'c97fe5036ef3df2967d086711e6c0c405941e14b', GitTreeState:'clean', BuildDate:'2019-10-15T19:18:23Z', GoVersion:'go1.12.10', 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.
Setting up Helm
Once Minikube and kubectl are installed, the next logical tool to configure is Helm. Note that the version of Helm used when writing this book was v3.0.0, but you are encouraged to use the latest version available of the Helm v3 release to receive the latest vulnerability and bug fixes.
Installing Helm
Helm packages exist for Chocolatey and Homebrew to allow easy installation on Windows or macOS. On these systems, the following commands can be run to install Helm with a package manager:
- Install Helm on Windows using the following command:
> choco install kubernetes-helm
- Install Helm on macOS using the following command:
$ brew install helm
Linux users, or users who would rather install Helm from a direct downloadable link, can download an archive from Helm's GitHub releases page by following these steps:
- Find the section called Installation on Helm's GitHub releases page at https://github.com/helm/helm/releases:
- Download the archive file associated with the operating system you are using for the desired version.
- Once downloaded, the file will need to be unarchived. One way that this can be achieved is by using the Expand-Archive cmdlet function on PowerShell or by using the tar utility on Bash:
- For Windows/PowerShell, use the following example :
> Expand-Archive -Path helm-v3.0.0-windows-amd64.zip -DestinationPath $DEST
- For Linux and Mac, use the following example :
$ tar -zxvf helm-v3.0.0-linux.amd64.tgz
- For Windows/PowerShell, use the following example :
Be sure to specify the version that corresponds to the version downloaded. The helm binary can be found in the unarchived folder. It should be moved to a location managed by the PATH variable.
The following example shows you how to move the helm binary to the /usr/local/bin folder on a Linux system:
$ mv ~/Downloads/linux-amd64/helm /usr/local/bin
Regardless of the way that Helm was installed, verification can be performed by running the helm version command. If the resulting output is similar to that of the following output, then Helm has been successfully installed:
$ helm version
version.BuildInfo{Version:'v3.0.0', GitCommit:'e29ce2a54e96cd02ccfce88bee4f58bb6e2a28b6', GitTreeState:'clean', GoVersion:'go1.13.4'}
With Helm installed on your machine, proceed to the next section to learn about the basic Helm configuration topics.