List of kubectl Commands with Examples (+kubectl Cheat Sheet)

Introduction

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.

The article with the List of kubectl Commands with Examples: Cheat Sheet

Note: For more information about Kubernetes installation, refer to How to Install Kubernetes on a Bare Metal Server


List of kubectl Commands

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

Listing Resources

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

Generate a plain-text list of all namespaces:

kubectl get namespaces

Generate a plain-text list of all pods:

kubectl get pods

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

kubectl get pods -o wide

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

kubectl get pods --field-selector=spec.nodeName=[server-name]

List a specific replication controller in plain-text:

kubectl get replicationcontroller [replication-controller-name]

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

kubectl get replicationcontroller,services

Generate a plain-text list of all daemon sets:

kubectl get daemonset

Creating a Resource

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

For example, to create a new namespace, type:

kubectl create namespace [namespace-name]

Create a resource from a JSON or YAML file:

kubectl create –f [filename]

Applying and Updating a Resource

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 [service-name].yaml:

kubectl apply -f [service-name].yaml

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

kubectl apply -f [controller-name].yaml

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

kubectl apply -f [directory-name]

To update a resource by editing it in a text editor, use kubectl edit. This command is a combination of the kubectl get and kubectl apply commands.

For example, to edit a service, type:

kubectl edit svc/[service-name]

This command opens the file in your default editor. To choose another editor, specify it in front of the command:

KUBE_EDITOR=”[editor-name]” kubectl edit svc/[service-name]

Displaying the State of Resources

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

View details about a particular node:

kubectl describe nodes [node-name]

View details about a particular pod:

kubectl describe pods [pod-name]

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

Kubectl describe –f pod.json

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

kubectl describe pods [replication-controller-name]

Show details about all pods:

kubectl describe pods

Deleting Resources

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:

kubectl delete -f pod.yaml

Remove all pods and services with a specific label:

kubectl delete pods,services -l [label-key]=[label-value]

Remove all pods (the command includes uninitialized pods as well):

kubectl delete pods --all

Executing a Command

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

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

kubectl exec [pod-name] -- [command]

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

kubectl exec [pod-name] -c [container-name] -- [command]

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

kubectl exec -ti [pod-name] -- /bin/bash

Modifying kubeconfig Files

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

Display the current context:

kubectl config current-context

Set a cluster entry in kubeconfig:

kubectl config set-cluster [cluster-name] --server=[server-name]

Unset an entry in kubeconfig:

kubectl config unset [property-name]

Printing Container Logs

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

The syntax for printing logs is:

kubectl logs [pod-name]

To stream logs from a pod, use:

kubectl logs -f [pod-name]

Short Names for Resource Types

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

For example, consider the command mentioned above:

kubectl create namespace [namespace-name]

You can also write this command as:

kubectl create ns [namespace-name]

Here is the full list of kubectl short names:

Short NameLong Name
csrcertificatesigningrequests
cscomponentstatuses
cmconfigmaps
dsdaemonsets
deploydeployments
ependpoints
evevents
hpahorizontalpodautoscalers
ingingresses
limitslimitranges
nsnamespaces
nonodes
pvcpersistentvolumeclaims
pvpersistentvolumes
popods
pdbpoddisruptionbudgets
psppodsecuritypolicies
rsreplicasets
rcreplicationcontrollers
quotaresourcequotas
saserviceaccounts
svcservices

kubectl Cheat Sheet

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.

DOWNLOAD Cheat Sheet

Kubectl Commands Cheat Sheet

Conclusion

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

Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to do Canary Deployments on Kubernetes
December 1, 2020

A canary deployment is used to test out new features and upgrades, to see how they handle the production...
Read more
How to Create and Use ConfigMap with Kubernetes
October 1, 2020

Create ConfigMaps for your pods configuration settings to keep your images light and portable...
Read more
19 Kubernetes Best Practices for Building Efficient Clusters
September 23, 2020

Kubernetes is a feature-rich orchestration tool. The best practices outlined in this article are going to...
Read more
How to Restart Kubernetes Pods
August 27, 2020

Kubernetes is one of the premier systems for managing containerized applications. But it isn't always able to...
Read more
  • © 2021 Copyright phoenixNAP | Global IT Services. All Rights Reserved.