Kubernetes Cheatsheet

vishnuchilamakuru

Vishnu Chilamakuru

Posted on July 22, 2021

Kubernetes Cheatsheet

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available. Kubernetes provides you with a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, canary deployments, and more. In this blog post, I will mention Kubernetes commands which we need for most of the use-cases.

I will list down kubectl commands in the sections below as a quick reference to work with Kubernetes.

List Resources

  • Get list of all namespaces
kubectl get namespaces
Enter fullscreen mode Exit fullscreen mode
  • Get list of all pods
kubectl get pods
Enter fullscreen mode Exit fullscreen mode
  • Get list of all pods with detailed information like IP, Node Name etc...
kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode
  • Get list of all pods running on a particular node server
kubectl get pods --field-selector=spec.nodeName=[server-name]
Enter fullscreen mode Exit fullscreen mode
  • Get list of all replication controllers and services
kubectl get replicationcontroller,services
Enter fullscreen mode Exit fullscreen mode
  • Get list of daemonsets
kubectl get daemonset
Enter fullscreen mode Exit fullscreen mode

Create Resources

  • Create a new namespace
kubectl create namespace [namespace-name]
Enter fullscreen mode Exit fullscreen mode
  • Create a new namespace from JSON or YAML file.
kubectl create –f [filename]
Enter fullscreen mode Exit fullscreen mode

Update Resources

To apply or update a resource use the kubectl apply command.

  • Create a new service with the definition contained in [service-config].yaml
kubectl apply -f [service-config].yaml
Enter fullscreen mode Exit fullscreen mode
  • Create a new replication controller with the definition contained in [controller-config].yaml
kubectl apply -f [controller-config].yaml
Enter fullscreen mode Exit fullscreen mode
  • Create the objects defined in any .yaml, .yml, or .json file in a directory
kubectl apply -f [yaml-file/directory-name]
Enter fullscreen mode Exit fullscreen mode
  • Edit a service config
kubectl edit svc/[service-name]
Enter fullscreen mode Exit fullscreen mode

Above 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]
Enter fullscreen mode Exit fullscreen mode

Display the State of Resources

  • Get Details about Particular Node
kubectl describe nodes [node-name]
Enter fullscreen mode Exit fullscreen mode
  • Get Details about a Particular pod
kubectl describe pods [pod-name]
Enter fullscreen mode Exit fullscreen mode
  • Get Details about a Particular pod whose name and type are listed in pod.json
kubectl describe –f pod.json
Enter fullscreen mode Exit fullscreen mode
  • Get Details about a Particular pod managed by a specific replication controller
kubectl describe pods [replication-controller-name]
Enter fullscreen mode Exit fullscreen mode
  • Get Details about all pods
kubectl describe pods
Enter fullscreen mode Exit fullscreen mode

Delete Resources

  • Delete a pod using the name and type mentioned in pod.yaml
kubectl delete -f pod.yaml
Enter fullscreen mode Exit fullscreen mode
  • Delete all pods and services with a specific label
kubectl delete pods,services -l [label-key]=[label-value]
Enter fullscreen mode Exit fullscreen mode
  • Delete all pods
kubectl delete pods --all
Enter fullscreen mode Exit fullscreen mode

Execute Command

  • Get output from a command run on the first container in a pod
kubectl exec [pod-name] -- [command]
Enter fullscreen mode Exit fullscreen mode
  • Get output from a command run on a specific container in a pod
kubectl exec [pod-name] -c [container-name] -- [command]
Enter fullscreen mode Exit fullscreen mode
  • Run /bin/bash from a specific pod. The received output comes from the first container
kubectl exec -ti [pod-name] -- /bin/bash
Enter fullscreen mode Exit fullscreen mode

Print Container Logs

  • Print Logs from Pod
kubectl logs [pod-name]
Enter fullscreen mode Exit fullscreen mode
  • Stream Logs from Pod
kubectl logs -f [pod-name]
Enter fullscreen mode Exit fullscreen mode
  • Tail Logs from Pod (Print last 200 logs from pod)
kubectl logs --tail=200 [pod-name]
Enter fullscreen mode Exit fullscreen mode

Modify Kubeconfig Files

kubectl config command lets you view and modify kubeconfig files.

  • Get Current Context
kubectl config current-context
Enter fullscreen mode Exit fullscreen mode
  • Set cluster entry in kubeconfig
kubectl config set-cluster [cluster-name] --server=[server-name]
Enter fullscreen mode Exit fullscreen mode
  • Unset an entry in kubeconfig
kubectl config unset [property-name]
Enter fullscreen mode Exit fullscreen mode

Thank you for reading

Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on

💖 💪 🙅 🚩
vishnuchilamakuru
Vishnu Chilamakuru

Posted on July 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Kubernetes Cheatsheet
kubernetes Kubernetes Cheatsheet

July 22, 2021