Logo

The Data Daily

Kubectl Cheat Sheet: Kubectl Commands With Examples {PDF}

Kubectl Cheat Sheet: Kubectl Commands With Examples {PDF}

Kubectl is a set of commands for controlling Kubernetes clusters. Every Kubernetes command has an API endpoint, and kubectl’s primary purpose is to carry out HTTP requests to the API.

While it is possible to issue HTTP requests yourself (e.g., using curl), kubectl is designed to make this process more comfortable and straightforward.

This tutorial will cover all the common kubectl operations and provide examples to familiarize yourself with the syntax.

Use the kubectl commands listed below as a quick reference when working with Kubernetes.

To list one or more pods, replication controllers, services, or daemon sets, use the kubectl get command.

Generate a detailed plain-text list of all pods, containing information such as node name:

Display a list of all pods running on a particular node server:

Generate a plain-text list of all replication controllers and services:

Create a resource such as a service, deployment, job, or namespace using the kubectl create command.

For example, to create a new namespace, type:

To apply or update a resource use the kubectl apply command. The source in this operation can be either a file or the standard input (stdin).

Create a new service with the definition contained in a [service-name].yaml file:

Create a new replication controller with the definition contained in a [controller-name].yaml file:

Create the objects defined in any .yaml, .yml, or .json file in a directory:

You can update a resource by configuring it in a text editor, using the kubectl edit command. This command is a combination of kubectl get and kubectl apply.

For example, to edit a service, type:

This command opens the file in your default editor. To use a different editor, specify it in front of the command:

To display the state of any number of resources in detail, use the kubectl describe command. By default, the output also lists uninitialized resources.

Display details about a pod whose name and type are listed in pod.json:

See details about all pods managed by a specific replication controller:

To remove resources from a file or stdin, use the kubectl delete command.

Remove a pod using the name and type listed in pod.yaml:

Remove all pods and services with a specific label:

Use kubectl exec to issue commands in a container or to open a shell in a container.

Receive output from a command run on the first container in a pod:

Get output from a command run on a specific container in a pod:

Run /bin/bash from a specific pod. The received output comes from the first container:

kubectl config lets you view and modify kubeconfig files. This command is usually followed by another sub-command.

To print logs from containers in a pod, use the kubectl logs command.

To stream logs from a pod, use:

Some of the kubectl commands listed above may seem inconvenient due to their length. For this reason names of common kubectl resource types also have shorter versions.

Consider the command mentioned above:

You can also run this command as:

Here is the full list of kubectl short names:

You can find all the commands listed in this article in the one-page reference sheet below. Download the kubectl Command PDF and save it for future use.

This tutorial explained the most common kubectl commands to help you manage your Kubernetes API. The accompanying cheat sheet allows you to have all the commands in one place, easily accessible for a quick reference.

Images Powered by Shutterstock