Skip to main content

Kubernetes Command Cheatsheet

Cluster Information and Health

  1. Check cluster components (control plane availability):
kubectl get componentstatuses
  1. Get general cluster information:
kubectl cluster-info
  1. List all nodes in the cluster (health/status):
kubectl get nodes
  1. Get detailed information about a node:
kubectl describe node <node-name>
  1. View the current Kubernetes version running:
kubectl version --short
  1. Check any existing cluster issues or warning events globally:
kubectl get events --all-namespaces --sort-by='.metadata.creationTimestamp'

Workload / Pod Management

  1. View all pods across all namespaces:
kubectl get pods --all-namespaces
  1. List the pods in a specific namespace (e.g., default, longhorn-system):
kubectl get pods -n <namespace>
  1. Get detailed information for a specific pod:
kubectl describe pod <pod-name> -n <namespace>
  1. Delete a pod (restarts the pod, useful for troubleshooting):
kubectl delete pod <pod-name> -n <namespace>
  1. Create or apply resources from a YAML file:
kubectl apply -f <filename>.yaml
  1. View YAML/JSON configuration dump of a resource:
  • Output YAML:

    kubectl get <resource> <name> -o yaml
  • Output JSON:

    kubectl get <resource> <name> -o json
  1. Get logs from a pod:
kubectl logs <pod-name> -n <namespace>
  1. Stream continuous logs from a pod:
kubectl logs -f <pod-name> -n <namespace>
  1. Get logs for a specific container in a multi-container pod:
kubectl logs <pod-name> -c <container-name> -n <namespace>
  1. Launch a debug pod for troubleshooting (basic busybox container in interactive terminal):
kubectl run debug --image=busybox -it --rm -- /bin/sh
  1. Forcefully delete a pod (if stuck in terminating or other strange states):
kubectl delete pod <pod-name> --grace-period=0 --force -n <namespace>

Service & Endpoint Management

  1. List all services in a namespace:
kubectl get svc -n <namespace>
  1. Get detailed information about a service:
kubectl describe svc <service-name> -n <namespace>
  1. Forward a local port to a pod (e.g., for local access to service, like database):
kubectl port-forward <pod-name> <local-port>:<remote-port> -n <namespace>
  1. Test if a service is functioning by listing endpoints:
kubectl get endpoints <service-name> -n <namespace>

Storage Management (Longhorn)

  1. List Longhorn volumes:
kubectl get volumes -n longhorn-system
  1. Describe a Longhorn volume:
kubectl describe <longhorn-volume-name> -n longhorn-system
  1. List PersistentVolumeClaims (PVCs) in a namespace:
kubectl get pvc -n <namespace>
  1. Delete a PersistentVolumeClaim (PVC) carefully:
kubectl delete pvc <pvc-name> -n <namespace>
  1. Check the status of Longhorn-csi or other stateful sets:
kubectl get statefulsets -n longhorn-system
  1. List all StorageClasses (to verify Longhorn's StorageClasses):
kubectl get storageclass

Namespace Management

  1. List all namespaces:
kubectl get namespaces
  1. Switch context to a different namespace:
kubectl config set-context --current --namespace=<namespace>
  1. Create a new namespace:
kubectl create namespace <namespace-name>
  1. Delete a namespace (use caution):
kubectl delete namespace <namespace-name>

PostgreSQL Management (example provider)

  1. List PostgreSQL-related resources (assuming you have CRDs or a PostgreSQL operator installed):
kubectl get postgresql -n <namespace>
  1. Describe a PostgreSQL instance:
kubectl describe postgresql <pg-instance-name> -n <namespace>
  1. Connect to the PostgreSQL pod for database debugging:
kubectl exec -it <pg-pod-name> -n <namespace> -- psql -U postgres

Resource & Utilization Monitoring

  1. View resource usage (CPU/Memory) for nodes and pods (requires metrics-server):
  • For nodes:

    kubectl top nodes
  • For pods (in a specific namespace):

    kubectl top pods -n <namespace>
  1. Check events for troubleshooting issues in a namespace:
kubectl get events -n <namespace>
  1. Get details about a Deployment:
kubectl describe deployment <deployment-name> -n <namespace>

Scale Deployments

  1. Scale up/down the number of replicas in a Deployment:
kubectl scale deployment <deployment-name> --replicas=<number-of-replicas> -n <namespace>
  1. Autoscale a Deployment based on CPU usage:
kubectl autoscale deployment <deployment-name> --cpu-percent=<percent> --min=<min-replicas> --max=<max-replicas> -n <namespace>

Debugging & Troubleshooting

  1. Check recent events sorted by timestamp to diagnose issues:
kubectl get events --sort-by='.metadata.creationTimestamp' -n <namespace>
  1. Open a shell session inside a running container:
kubectl exec -it <pod-name> -n <namespace> -- /bin/bash
  1. Run one-off commands in a container (e.g., to run a curl command):
kubectl exec -it <pod-name> -n <namespace> -- curl <url>
  1. Get the history of resource changes for a deployment (e.g., when scaling happens):
kubectl rollout history deployment <deployment-name> -n <namespace>

Service Account Management (API & Permissions)

  1. List all service accounts in a namespace:
kubectl get serviceaccounts -n <namespace>
  1. Get details about a specific service account:
kubectl describe serviceaccount <service-account-name> -n <namespace>
  1. Create a service account:
kubectl create serviceaccount <service-account-name> -n <namespace>
  1. Delete a service account:
kubectl delete serviceaccount <service-account-name> -n <namespace>

Configuration Management

  1. View all ConfigMaps in a namespace:
kubectl get configmap -n <namespace>
  1. Describe a specific ConfigMap:
kubectl describe configmap <configmap-name> -n <namespace>
  1. List Secrets (API keys, credentials, etc.) in a namespace:
kubectl get secrets -n <namespace>
  1. Decode a base64-encoded Secret to reveal its true content:
kubectl get secret <secret-name> -n <namespace> -o jsonpath="{.data.<secret-key>}" | base64 --decode

Additional Tips:

  • Backup critical configurations: Before making any destructive operations like delete, always back up your resource configurations or use GitOps processes.
  • Use dry-run mode for testing deletions: Use --dry-run=client to simulate applying or deleting things without actually making changes.

Tools like kubectl krew can extend the functionality of kubectl and provide additional kubectl plugins for advanced features.