Kubectl Cheat Sheet

Photo of author

By admin

Kubectl is a group of commands that are responsible for controlling clusters. It is a configuration tool that communicates with the API server of Kubernetes. Each Kubernetes command has an API point, and they look for config in the $HOME/.kube directory for configuration. Kubectl allows you to create, update, inspect as well as delete Kubernetes objects. 

A Kubectl cheat sheet is used for accessing the list commands at one centralized place for using it on several Kubernetes resources and components. 

Below is the syntax for running the Kubectl command: 

kubectl [command] [TYPE] [NAME] [flags]
  • Command – The operation the user wants to perform on the resources 
  • TYPE – It is the resource type
  • NAME – It is the name of the resource
  • Flags – These are optional flags and are used for specifying the address and port of the API server 

In case you need any help, you need to run this command:

Kubectl help

List of Kubectl Commands

Listing resources- for listing pods, services, replication controllers, or daemon sets, you can use this command:

kubectl get
  1. Create a plain list of all namespaces: 
Kubectl get namespaces
  1. Create a list of pods:
Kubectl get pods
  1. Create a detailed list of pods which must consist of information like node name:
Kubectl get pods -o wide 
  1. Create a list of pods running on a specific node server:
kubectl get pods --field-selector=spec.nodeName=[server-name]
  1. List a replication controller: 
kubectl get replicationcontroller [replication-controller-name]
  1. Create a list of services and replication controllers together:
kubectl get replicationcontroller,services
  1. Create a list of all daemon sets: 
kubectl get daemonset

Creating a Resource 

Create a resource, like a job, deployment, service, or a namespace with the following command:

kubectl create
  1. For creating a new namespace: 
kubectl create namespace [namespace-name]
  1. To create a resource from a JSON or YAML file:
kubectl create –f [filename]

How To Apply & Update a Resource

The user can use the ‘kubectl apply’ command to apply or update a resource. Here the source can either be a file or the standard input (stdin).

  1. To create a new service with a definition in [service-name].yaml: 
kubectl apply -f [service-name].yaml
  1. To create a new replication controller with a definition in [controller-name].yaml:
kubectl apply -f [controller-name].yaml

 

  1. To create the object that is defined in any .yaml, .json or .yml file in a directory:
kubectl apply -f [directory-name]
  1. Updating a resource by editing it in a text editor with ‘kubectl edit’. This is a combination of kubectl; apply kubectl get commands. So, to edit a service, you need to run the following command: 
kubectl edit svc/[service-name]

This opens the file in the default editor. However, if you wish to choose another editor, you will have to specify that in the command:

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

Display the State of Resources 

The ‘kubectl describe’ command is used to display the state of resources. It either shows details of resources or a group of resources. 

  1.  Viewing details about a specific node:
kubectl describe nodes [node-name]
  1.  Viewing details about a specific pod:
kubectl describe pods [pod-name]
  1. You need to display information about a pod whose name and type are mentioned in pod.json:
 Kubectl describe –f pod.json
  1.  View details about all the pods controlled by a particular replication controller:
kubectl describe pods [replication-controller-name]
  1.  View details about all pods:
kubectl describe pods

How to Delete Resources 

You need to use the ‘kubectl delete’ command to remove/delete resources from a file or stdin.

  1. To remove/delete a pod with the name and type mentioned in pod.yaml: 
kubectl delete -f pod.yaml
  1. To remove/delete all the pods and services using a specific label:
kubectl delete pods,services -l [label-key]=[label-value]
  1. To delete all the pods: 
kubectl delete pods --all

How to Execute a Command

To issue commands in a container or to open a shell in a container, use the ‘kubectl exec’ command

  1. To get output from a command that is running on the first container of a pod:
kubectl exec [pod-name] -- [command]
  1. To get output from a command that is run on a particular container of a pod:
kubectl exec [pod-name] -c [container-name] -- [command]
  1. To run /bin/bash from a particular pod. The output received is from the first container:
kubectl exec -ti [pod-name] -- /bin/bash

How to Modify Kubeconfig Files:

The ‘kubectl config’ command allows you to see and modify kubeconfig files. There is another sub-command, which follows this command. 

  1. For displaying the current context: 
kubectl config current-context
  1. For setting a cluster entry in kubeconfig:
kubectl config set-cluster [cluster-name] --server=[server-name]
  1. For unsetting an entry in kubeconfig:
kubectl config unset [property-name]

How To Print Container Logs 

You will have to use the ‘kubectl logs’ command to print logs from containers. The syntax for this is:

kubectl logs [pod-name] 

Use the following syntax to stream logs from a pod:

kubectl logs -f [pod-name]

How to Troubleshoot Kubernetes with Kubectl?

Here is how you can troubleshoot Kubernetes using Kubectl: 

kubectl describe 

It shows the details of the resource you are currently viewing. It’s mostly used to describe a pod or node and check for any errors in the events or if the resources are limited. Resources that can be described are – nodes, pods, deployments, services, replica sets, and Cronjobs. 

The following is an example showing how to describe the cronjob in a cluster:

$ kubectl describe cronjob my-cron

Kubectl logs

The kubectl describe command provides the events that occur in the applications inside a pod, whereas kubectl logs offer detailed insights into Kubernetes. Understanding this difference will allow you to fix issues that occur in the Kubernetes as well as in applications too. 

$ kubectl logs cherry-chart-88d49478c-dmcfv -n charts

Kubectl exec

You can also execute a container just like the docker exec command for troubleshooting an application directly. This command proves to be useful when the logs don’t fix the problems that may be arising. While using the exec command, the end of the line must provide the shell you intend to use in the pod. 

$ kubectl exec -it cherry-chart-88d49478c-dmcfv -n charts -- /bin/bash
root@cherry-chart-88d49478c-dmcfv:/#

Kubectl cp 

If you wish to copy directories and files to and from containers, this command is a must, just like the Linux cp command. This command is essential for restoring backups when automation fails. 

Here’s the syntax to copy a file to the container:

kubectl cp <filename> <namespace/podname:/path/tofile> format

The syntax for pulling a file to the local machine from a container: 

kubectl cp <namespace/podname:/path/tofile>

Conclusion

So far, the blog has covered all the major kubectl commands for performing various different operations on Kubernetes. Use this page as an epicenter for commands that you may require at different stages of development, deployment and management of Kubernetes cluster. 

We hope the above information was helpful enough to make your Kubernetes journey a bliss. However, for any queries or suggestions, you can use our comment section at any time of the day.

 

Leave a Comment