Kubernetes Command Cheatsheet
Cluster Information and Health
- Check cluster components (control plane availability):
kubectl get componentstatuses
- Get general cluster information:
kubectl cluster-info
- List all nodes in the cluster (health/status):
kubectl get nodes
- Get detailed information about a node:
kubectl describe node <node-name>
- View the current Kubernetes version running:
kubectl version --short
- Check any existing cluster issues or warning events globally:
kubectl get events --all-namespaces --sort-by='.metadata.creationTimestamp'
Workload / Pod Management
- View all pods across all namespaces:
kubectl get pods --all-namespaces
- List the pods in a specific namespace (e.g.,
default
,longhorn-system
):
kubectl get pods -n <namespace>
- Get detailed information for a specific pod:
kubectl describe pod <pod-name> -n <namespace>
- Delete a pod (restarts the pod, useful for troubleshooting):
kubectl delete pod <pod-name> -n <namespace>
- Create or apply resources from a YAML file:
kubectl apply -f <filename>.yaml
- View YAML/JSON configuration dump of a resource:
-
Output YAML:
kubectl get <resource> <name> -o yaml
-
Output JSON:
kubectl get <resource> <name> -o json
- Get logs from a pod:
kubectl logs <pod-name> -n <namespace>
- Stream continuous logs from a pod:
kubectl logs -f <pod-name> -n <namespace>
- Get logs for a specific container in a multi-container pod:
kubectl logs <pod-name> -c <container-name> -n <namespace>
- Launch a debug pod for troubleshooting (basic busybox container in interactive terminal):
kubectl run debug --image=busybox -it --rm -- /bin/sh
- 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
- List all services in a namespace:
kubectl get svc -n <namespace>
- Get detailed information about a service:
kubectl describe svc <service-name> -n <namespace>
- 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>
- Test if a service is functioning by listing endpoints:
kubectl get endpoints <service-name> -n <namespace>
Storage Management (Longhorn)
- List Longhorn volumes:
kubectl get volumes -n longhorn-system
- Describe a Longhorn volume:
kubectl describe <longhorn-volume-name> -n longhorn-system
- List PersistentVolumeClaims (PVCs) in a namespace:
kubectl get pvc -n <namespace>
- Delete a PersistentVolumeClaim (PVC) carefully:
kubectl delete pvc <pvc-name> -n <namespace>
- Check the status of Longhorn-csi or other stateful sets:
kubectl get statefulsets -n longhorn-system
- List all StorageClasses (to verify Longhorn's StorageClasses):
kubectl get storageclass
Namespace Management
- List all namespaces:
kubectl get namespaces
- Switch context to a different namespace:
kubectl config set-context --current --namespace=<namespace>
- Create a new namespace:
kubectl create namespace <namespace-name>
- Delete a namespace (use caution):
kubectl delete namespace <namespace-name>
PostgreSQL Management (example provider)
- List PostgreSQL-related resources (assuming you have CRDs or a PostgreSQL operator installed):
kubectl get postgresql -n <namespace>
- Describe a PostgreSQL instance:
kubectl describe postgresql <pg-instance-name> -n <namespace>
- Connect to the PostgreSQL pod for database debugging:
kubectl exec -it <pg-pod-name> -n <namespace> -- psql -U postgres
Resource & Utilization Monitoring
- 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>
- Check events for troubleshooting issues in a namespace:
kubectl get events -n <namespace>
- Get details about a Deployment:
kubectl describe deployment <deployment-name> -n <namespace>
Scale Deployments
- Scale up/down the number of replicas in a Deployment:
kubectl scale deployment <deployment-name> --replicas=<number-of-replicas> -n <namespace>
- 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
- Check recent events sorted by timestamp to diagnose issues:
kubectl get events --sort-by='.metadata.creationTimestamp' -n <namespace>
- Open a shell session inside a running container:
kubectl exec -it <pod-name> -n <namespace> -- /bin/bash
- Run one-off commands in a container (e.g., to run a curl command):
kubectl exec -it <pod-name> -n <namespace> -- curl <url>
- 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)
- List all service accounts in a namespace:
kubectl get serviceaccounts -n <namespace>
- Get details about a specific service account:
kubectl describe serviceaccount <service-account-name> -n <namespace>
- Create a service account:
kubectl create serviceaccount <service-account-name> -n <namespace>
- Delete a service account:
kubectl delete serviceaccount <service-account-name> -n <namespace>
Configuration Management
- View all ConfigMaps in a namespace:
kubectl get configmap -n <namespace>
- Describe a specific ConfigMap:
kubectl describe configmap <configmap-name> -n <namespace>
- List Secrets (API keys, credentials, etc.) in a namespace:
kubectl get secrets -n <namespace>
- 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.