Installing kubectl

Kubernetes' command-line tool, kubectl, is used to manage a cluster and applications running inside it. We'll use kubectl a lot throughout the book, so we won't go into details just yet. Instead, we'll discuss its commands through examples that will follow shortly. For now, think of it as your interlocutor with a Kubernetes cluster.

Let's install kubectl.

All the commands from this chapter are available in the 02-minikube.sh ( https://gist.github.com/vfarcic/77ca05f4d16125b5a5a5dc30a1ade7fc) Gist.
Feel free to skip the installation steps if you already have kubectl. Just make sure that it is version 1.8 or above.

If you are a MacOS user, please execute the commands that follows:

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

If you already have Homebrew (https://brew.sh/) package manager installed, you can "brew" it with the command that follows:

brew install kubectl  

If, on the other hand, you're a Linux user, the commands that will install kubectl are as follows:

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

Finally, Windows users should download the binary through the command that follows.

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/windows/amd64/kubectl.exe
  

Feel free to copy the binary to any directory. The important thing is to add it to your PATH.

Let's check kubectl version and, at the same time, validate that it is working correctly. No matter which OS you're using, the command is as follows:

kubectl version

The output is as follows:

Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.0", 
GitCommit:"925c127ec6b946659ad0fd596fa959be43f0cc05",
GitTreeState:"clean", BuildDate:"2017-12-15T21:07:38Z", GoVersion:"go1.9.2",
Compiler:"gc", Platform:"darwin/amd64"}

The connection to the server localhost:8080 was refused-did you specify the right host or port?

That is a very ugly and unreadable output. Fortunately, kubectl can use a few different formats for its output. For example, we can tell it to output the command in yaml format

kubectl version --output=yaml

The output is as follows:

clientVersion:
  buildDate: 2017-12-15T21:07:38Z
  compiler: gc
  gitCommit: 925c127ec6b946659ad0fd596fa959be43f0cc05
  gitTreeState: clean
  gitVersion: v1.9.0
  goVersion: go1.9.2
  major: "1"
  minor: "9"
  platform: darwin/amd64
    
The connection to the server localhost:8080 was refused - did you specify the right host or port?
  

That was a much better (more readable) output.

We can see that the client version is 1.9. At the bottom is the error message stating that kubectl could not connect to the server. That is expected since we did not yet create a cluster. That's our next step.

At the time of writing this book kubectl version was 1.9.0. Your version might be different when you install.