How to install kubectl

Kubectl is a fantastic tool to deploy and manage applications in Kubernetes. Using kubectl you can inspect cluster resources, create/update/delete each kind of k8s object (pods, services, deployments, etc), scale up and down an existing replicaSet and so on.

If you had read the previous article in the series “K8S in the wild” and you are interested in learning how Kubernetes works, the next step is to install a tool able to interact with a cluster. The best tool for this task is kubectl.

There are multiple ways to install kubectl on your machine. In my opinion, the cleanest method is to use curl to download kubectl’s binary and use it.

Install using curl

Download latest version of kubectl, make it executable (if needed) and move it into a directory that is in your $PATH variable.

MacOS & Linux:

$ curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
$ chmod +x ./kubectl
$ sudo mv ./kubectl /usr/local/bin/kubectl

Windows:

Download the lastest release using this link.
Add the binary into your $PATH environment variable.

Install using a package manager or something similar

Debian like flavors:

$ 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

RedHat like flavors:

# cat < /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

Install with homebrew on macOS:

$ brew install kubernetes-cli

Install with snap on Ubuntu:

$ sudo snap install kubectl --classic

Install with Chocolatey on Windows:

choco install kubernetes-cli

Install as part of Google SDK

  1. Install Google SDK
  2. run the install command:
    gcloud components install kubectl

Test your installation:

$ kubectl version
$ # output example:
Client Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.2", GitCommit:"cff46ab41ff0bb44d8584413b598ad8360ec1def", GitTreeState:"clean", BuildDate:"2019-01-13T23:15:13Z", GoVersion:"go1.11.4", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.5", GitCommit:"753b2dbc622f5cc417845f0ff8a77f539a4213ea", GitTreeState:"clean", BuildDate:"2018-11-26T14:31:35Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"linux/amd64"}

Enabling shell autocompletion

On Linux, using bash:
$ echo "source <(kubectl completion bash)" >> ~/.bashrc
$ source ~/.bashrc
On Mac using bash: 
## If running Bash 3.2 included with macOS
$ brew install bash-completion
## or, if running Bash 4.1+
$ brew install bash-completion@2
$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl
Using Zsh:
Edit .zshrc file and add this:
if [ $commands[kubectl] ]; then
  source <(kubectl completion zsh)
fi
Or, if you use Oh-My-Zsh, simply add this line to your .zshrc file:
plugins=(kubectl)
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...