A beginner’s guide for K8s setup using Kubectl
What is Kubectl?
Kubernetes is a great solution for automating the deployment, scaling, and management of containerized applications – explained in more detail in “How to get started with Kubernetes” before moving forward. In this part, we are going to move to the installation steps.
One of the main tools in Kubernetes is Kubectl. This is the main command-line tool for administering and interacting with your Kubernetes installation; think of kubectl as the Bash environment of Kubernetes.
Other useful Kubernetes components are kubeadm and kubelet – you will most likely install and use these 3 together when setting up your first Kubernetes cluster. And read this article for a full overview of Kubernetes components and this other article for a sample Kubernetes deployment.
You can install kubectl, on Linux, macOS, and Windows. But as detailed in the official Kubernetes site: “You must use a kubectl version that is within one minor version difference of your cluster. For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master. Using the latest version of kubectl helps avoid unforeseen issues.”
The most common installation is on Linux, so let’s go through the kubectl installation via both curl and via native package manager in Linux in a bit more detail:
The first step is to confirm the latest stable release at this URL:
Next, download that latest stable release with this command:
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
If instead, you want to use an older release, say v1.16.4, use this command instead:
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.16.4/bin/linux/amd64/kubectl
Next, you make the binary executable, add it to your PATH, and check that kubectl is running ok and up to date, using these 3 commands respectively:
chmod +x ./kubectl sudo mv ./kubectl /usr/local/bin/kubectl kubectl version --client
Using a Native Package Manager
Use these commands for Ubuntu/ Debian installation:
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
After a successful installation, kubectl now needs to find and access a Kubernetes cluster. For this, it needs a kubeconfig file, which was created automatically when you created a cluster using “kube-up.sh” or successfully deployed a Minikube cluster (what is Minikube? Find out more here). By default, the kubectl configuration file is in this path: ~/.kube/config.
Finally, confirm that kubectl is properly configured. Display the cluster state with this command:
This should return a URL response. But if the error message below is displayed instead, then kubectl is not configured correctly or your cluster setup was incorrect: The connection to the server <server-name:port> was refused – did you specify the right host or port?
You can get more verbose info on your cluster setup using this command:
kubectl cluster-info dump
And you can view your overall kubectl configuration using this command:
kubectl config view
Kubectl Options & Flags
Just like bash, there are some kubectl options that can make life easier. For instance, you can enable command autocompletion in kubectl on bash or zsh by following this guide.
You can also enable a number of flags in kubectl, by simply using this syntax: kubectl [flags]
These flags are explained in full in the official kubectl reference guide, and a few of the more useful and common ones are listed in the table below:
Kubectl Syntax and Commands
We have talked about kubectl being the command-line utility for Kubernetes. Now let’s take a closer look at the kubectl syntax and some useful Kubectl commands. These are also explained in detail in the official guide.
Kubectl commands generally follow this order:
kubectl [command] [TYPE] [NAME] [flags]
- command: Specifies the operation that you want to perform on one or more resources, such as create, get, describe, delete
- TYPE: Specifies the resource type. Resource types are case-insensitive and you can specify the singular, plural, or even abbreviated forms. For example, the following commands produce the same output:
kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
- NAME: Specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed, for example
kubectl get pods
- flags: Specifies optional flags, as explained earlier. Note that any flags you specify at the command line will override the default values in a configuration file and in any environment variables.
The actual kubectl commands are simply too many to list here. You can go the official guide and the command reference for the full list, but below are some of the most useful everyday commands that you will almost certainly use as a Kubernetes admin:
kubectl get <resourcename>
Use ‘kubectl get’ to pull a list of resources you have currently on your cluster. The types of resources you can get include: Namespace, Pod, Node, Deployment, Service, ReplicaSets
kubectl get nodes
to list all nodes.
kubectl create <resourcename>
As you may have guessed, this is the command to create resources such as service, deployment, job, namespace (ns)
kubectl create ns my-new-namespace
kubectl describe <resourcename>
This shows the details of the resource you’re looking at. The most common use of this is to describe a pod or node to check that it’s running ok or if there’s an error.
kubectl describe pod15
This command is used to apply a configuration in a specific config file.
kubectl apply -f /kube/config2.yaml
And remember, you can always get some help and tips using
This is just an intro to Kubernetes. You can use this guide to familiarize yourself with Kubernetes in general and kubectl in particular. But remember you also always have the option of not spending time learning all of this, by using a fully hosted Kubernetes solution such as Cloudplex.