Skip to main content

Kubectl Cheatsheet

This cheatsheet contains frequently used commands for managing Kubernetes resources with kubectl.


Cluster Information

Cluster info

kubectl cluster-info

Nodes are the VMs configured to run k8s; they determine the limits of resources etc

List nodes

kubectl get nodes

Context and Configuration

List contexts

kubectl config get-contexts

Switch context

kubectl config use-context <context-name>

View current context

kubectl config current-context

Pods

'Pods' are collections of atleast one container

List pods

kubectl get pods

List pods in all namespaces

kubectl get pods --all-namespaces

Describe a pod

kubectl describe pod <pod-name>

View pod logs

kubectl logs <pod-name>

Exec into pod

kubectl exec -it <pod-name> -- /bin/bash

Common Resources

The common "deployment" resources are: Deployment, Statefulset, Replicaset.

Other common resources are: Service, Ingress, PersistentVolume (pv), PersistentVolumeClaim (pvc)

List resources

kubectl get all

kubectl get <resource_type>

Describe deployment

kubectl describe <resource_type> <resource_name>

Scale deployment

kubectl scale <resource_type> <resource_name> --replicas=3

Services

List services

kubectl get services

Describe service

kubectl describe service <service-name>

Configuration Management

Apply configuration

kubectl apply -f <file.yaml>

Delete resource

kubectl delete -f <file.yaml>

Resource Information

Get resource details in YAML format

kubectl get pod <pod-name> -o yaml

Edit resource live

kubectl edit deployment <deployment-name>

Troubleshooting

Check events

kubectl get events

Check resource status

kubectl get pods -o wide

Watch resources in real-time

kubectl get pods --watch

Keep this cheatsheet handy for quick reference when working with Kubernetes!