Chapter 1: Introducing and Installing kubectl
Kubernetes is an open source container orchestration system for managing containerized applications across multiple hosts in a cluster.
Kubernetes provides mechanisms for application deployment, scheduling, updating, maintenance, and scaling. A key feature of Kubernetes is that it actively manages containers to ensure that the state of the cluster always matches the user's expectations.
Kubernetes enables you to respond quickly to customer demand by scaling or rolling out new features. It also allows you to make full use of your hardware.
- Lean: Lightweight, simple, and accessible
- Portable: Public, private, hybrid, and multi-cloud
- Extensible: Modular, pluggable, hookable, composable, and toolable
- Self-healing: Auto-placement, auto-restart, and auto-replication
Kubernetes builds on a decade and a half of experience at Google running production workloads at scale, combined with best-of-breed ideas and best practices from the community:

Figure 1.1 – A 10,000-foot view of Kubernetes' architecture
One of the ways to manage Kubernetes clusters is kubectl
—Kubernetes' command-line tool for management, it is a tool for accessing a Kubernetes cluster that allows you to run different commands against Kubernetes clusters to deploy apps, manage nodes, troubleshoot deployments, and more.
In this chapter, we're going to cover the following main topics:
- Introducing kubectl
- Installing kubectl
- kubectl commands
Technical requirements
To learn kubectl, you will need access to a Kubernetes cluster; it can be one of these cloud ones:
- Google Cloud GKE: https://cloud.google.com/kubernetes-engine
- Azure AKS EKS: https://azure.microsoft.com/en-us/free/kubernetes-service
- AWS EKS: https://aws.amazon.com/eks/
- DigitalOcean DOKS: https://www.digitalocean.com/docs/kubernetes/
Alternatively, it can be a local one:
- KIND: https://kind.sigs.k8s.io/docs/user/quick-start/
- Minikube: https://kubernetes.io/docs/setup/learning-environment/minikube/
- Docker Desktop: https://www.docker.com/products/docker-desktop
In this book, we are going to use Google Cloud's GKE Kubernetes cluster.
Introducing kubectl
You can use kubectl
to deploy applications, inspect and manage them, check cluster resources, view logs, and more.
kubectl
is a command-line tool that can run from your computer, in CI/CD pipelines, as part of the operating system, or as a Docker image. It is a very automation-friendly tool.
kubectl
looks for a configuration file named .kube
in the $HOME
folder. In the .kube
file, kubectl
stores the cluster configurations needed to access a Kubernetes cluster. You can also set the KUBECONFIG
environment variable or use the --kubeconfig
flag to point to the kubeconfig
file.
Installing kubectl
Let's take a look at how you can install kubectl
on macOS, on Windows, and in CI/CD pipelines.
Installing on macOS
The easiest way to install kubectl
on macOS is using the Homebrew package manager (https://brew.sh/):
- To install, run this:
$ brew install kubectl
- To see the version you have installed, use this:
$ kubectl version –client --short Client Version: v1.18.1
Installing on Windows
To install kubectl
on Windows, you could use the simple command-line installer Scoop (https://scoop.sh/):
- To install, run this:
$ scoop install kubectl
- To see the version you have installed, use this:
$ kubectl version –client --short Client Version: v1.18.1
- Create the
.kube
directory in your home directory:$ mkdir %USERPROFILE%\.kube
- Navigate to the
.kube
directory:$ cd %USERPROFILE%\.kube
- Configure
kubectl
to use a remote Kubernetes cluster:$ New-Item config -type file
Installing on Linux
When you want to use kubectl
on Linux, you have two options:
- Use
curl
:$ 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 your Linux system supports Docker images, use https://hub.docker.com/r/bitnami/kubectl/.
Note
Linux is a very common environment for CI/CD pipelines.
kubectl commands
To get a list of supported kubectl
commands, run this:
$ kubectl --help
kubectl
commands are grouped by category. Let's look at each category.
Basic commands
The following are basic kubectl
commands:
create
: Create a resource from a file or fromstdin
; for example, create a Kubernetes deployment from the file.expose
: Take a service, deployment, or pod and expose it as a new Kubernetes Service.run
: Run a particular image on the cluster.set
: Set specific features on objects—for example, set environment variables, update a Docker image in a pod template, and so on.explain
: Get the documentation of resources—for example, the documentation on deployments.get
: Display one or many resources. For example, you can get a list of running pods or the YAML output of a pod.edit
: Edit a resource—for example, edit a deployment.delete
: Delete resources by filenames,stdin
, resources, and names, or by resources and label selectors.
Deploy commands
The following are kubectl
deploy commands:
rollout
: Manage the rollout of a resource.scale
: Set a new size for a deployment, ReplicaSet, or StatefulSet.autoscale
: Auto-scale a deployment, ReplicaSet, or StatefulSet.
Cluster management commands
The following are the kubectl
cluster management commands:
certificate
: Modify certificate resources.cluster-info
: Display cluster information.top
: Display resource (CPU/memory/storage) usage.cordon
: Mark a node as unschedulable.uncordon
: Mark a node as schedulable.drain
: Drain a node in preparation for maintenance.taint
: Update the taints on one or more nodes.
Troubleshooting and debugging commands
The following are the kubectl
troubleshooting and debugging commands:
describe
: Show the details of a specific resource or group of resources.logs
: Print the logs for a container in a pod.attach
: Attach to a running container.exec
: Execute a command in a container.port-forward
: Forward one or more local ports to a pod.proxy
: Run a proxy to the Kubernetes API server.cp
: Copy files and directories to and from containers.auth
: Inspect authorization.
Advanced commands
The following are the kubectl
advanced commands:
diff
: Show difference of live version against a would-be applied version.apply
: Apply a configuration to a resource by filename orstdin
.patch
: Update the field(s) of a resource using a strategic merge patch.replace
: Replace a resource by filename orstdin
.wait
: Wait for a specific condition on one or many resources.convert
: Convert config files between different API versions.kustomize
: Build a kustomization target from a directory or a remote URL.
Settings commands
The following are the settings commands in kubectl
:
label
: Update the labels on a resource.annotate
: Update the annotations on a resource.
Other commands
The following are several other commands used in kubectl
:
alpha
: Commands for features in alpha.api-resources
: Print the supported API resources on the server.api-versions
: Print the supported API versions on the server, in the form of group/version.config
: Modifykube-config
files.plugin
: Provide utilities for interacting with plugins.version
: Print the client and server version information.
As you can see from the lists, commands are divided into different groups. We are going to learn about most but not all of these commands in the coming chapters.
At the time of writing, the kubectl
version is 1.18; with more recent versions, the commands might have changed.
Summary
In this chapter, we have learned what kubectl
is and how to install it on macOS, Windows, and CI/CD pipelines. We also checked out the different commands supported by kubectl
and what they do.
In the next chapter, we will learn how to get information about Kubernetes clusters using kubectl
.