Here is a list of 100 kubectl
commands that can be useful for diagnosing issues in a Kubernetes cluster.
Cluster Information:
- Show the Kubernetes version:
kubectl version
- Display cluster information:
kubectl cluster-info
- List all nodes in the cluster:
kubectl get nodes
- Describe a specific node:
kubectl describe node <node-name>
- List all namespaces:
kubectl get namespaces
- List all pods in all namespaces:
kubectl get pods --all-namespaces
Pod Diagnostics:
- List pods in a specific namespace:
kubectl get pods -n <namespace>
- Describe a pod:
kubectl describe pod <pod-name> -n <namespace>
- View pod logs:
kubectl logs <pod-name> -n <namespace>
- Tail pod logs:
kubectl logs -f <pod-name> -n <namespace>
- Execute a command in a pod:
kubectl exec -it <pod-name> -n <namespace> -- <command>
Pod Health Checks:
- Check pod readiness:
kubectl get pods <pod-name> -n <namespace> -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'
- Check pod events:
kubectl get events -n <namespace> --field-selector involvedObject.name=<pod-name>
Service Diagnostics:
- List all services in a namespace:
kubectl get svc -n <namespace>
- Describe a service:
kubectl describe svc <service-name> -n <namespace>
Deployment Diagnostics:
- List all deployments in a namespace:
kubectl get deployments -n <namespace>
- Describe a deployment:
kubectl describe deployment <deployment-name> -n <namespace>
- View rollout status:
kubectl rollout status deployment/<deployment-name> -n <namespace>
- View rollout history:
kubectl rollout history deployment/<deployment-name> -n <namespace>
StatefulSet Diagnostics:
- List all StatefulSets in a namespace:
kubectl get statefulsets -n <namespace>
- Describe a StatefulSet:
kubectl describe statefulset <statefulset-name> -n <namespace>
ConfigMap and Secret Diagnostics:
- List ConfigMaps in a namespace:
kubectl get configmaps -n <namespace>
- Describe a ConfigMap:
kubectl describe configmap <configmap-name> -n <namespace>
- List Secrets in a namespace:
kubectl get secrets -n <namespace>
- Describe a Secret:
kubectl describe secret <secret-name> -n <namespace>
Namespace Diagnostics:
- Describe a namespace:
kubectl describe namespace <namespace-name>
Resource Usage:
- Check resource usage for a pod:
kubectl top pod <pod-name> -n <namespace>
- Check resource usage for nodes:
kubectl top nodes
Networking Diagnostics:
- Show the IP addresses of pods in a namespace:
kubectl get pods -n <namespace -o custom-columns=POD:metadata.name,IP:status.podIP --no-headers
- List all network policies in a namespace:
kubectl get networkpolicies -n <namespace>
- Describe a network policy:
kubectl describe networkpolicy <network-policy-name> -n <namespace>
Persistent Volume (PV) and Persistent Volume Claim (PVC) Diagnostics:
- List PVs:
kubectl get pv
- Describe a PV:
kubectl describe pv <pv-name>
- List PVCs in a namespace:
kubectl get pvc -n <namespace>
- Describe a PVC:
kubectl describe pvc <pvc-name> -n <namespace>
Node Diagnostics:
- Get the list of pods running on a specific node:
kubectl get pods --field-selector spec.nodeName=<node-name> -n <namespace>
Resource Quotas and Limits:
- List resource quotas in a namespace:
kubectl get resourcequotas -n <namespace>
- Describe a resource quota:
kubectl describe resourcequota <resource-quota-name> -n <namespace>
Custom Resource Definitions (CRD) Diagnostics:
- List custom resources in a namespace:
kubectl get <custom-resource-name> -n <namespace>
- Describe a custom resource:
kubectl describe <custom-resource-name> <custom-resource-instance-name> -n <namespace>
Remember to replace <namespace>
, <pod-name>
, <service-name>
, <deployment-name>
, <statefulset-name>
, <configmap-name>
, <secret-name>
, <namespace-name>
, <pv-name>
, <pvc-name>
, <node-name>
, <network-policy-name>
, <resource-quota-name>
, <custom-resource-name>
, and <custom-resource-instance-name>
with your specific values when using these commands.
These commands should help you diagnose various aspects of your Kubernetes cluster and applications running within it.
Resource Scaling and Autoscaling:
- Scale a deployment:
kubectl scale deployment <deployment-name> --replicas=<replica-count> -n <namespace>
- Set autoscaling for a deployment:
kubectl autoscale deployment <deployment-name> --min=<min-pods> --max=<max-pods> --cpu-percent=<cpu-percent> -n <namespace>
- Check horizontal pod autoscaler status:
kubectl get hpa -n <namespace>
Job and CronJob Diagnostics:
- List all jobs in a namespace:
kubectl get jobs -n <namespace>
- Describe a job:
kubectl describe job <job-name> -n <namespace>
- List all cron jobs in a namespace:
kubectl get cronjobs -n <namespace>
- Describe a cron job:
kubectl describe cronjob <cronjob-name> -n <namespace>
Volume Diagnostics:
- List persistent volumes (PVs) sorted by capacity:
kubectl get pv --sort-by=.spec.capacity.storage
- Check PV reclaim policy:
kubectl get pv <pv-name> -o=jsonpath='{.spec.persistentVolumeReclaimPolicy}'
- List all storage classes:
kubectl get storageclasses
Ingress and Service Mesh Diagnostics:
- List all ingresses in a namespace:
kubectl get ingress -n <namespace>
- Describe an ingress:
kubectl describe ingress <ingress-name> -n <namespace>
- List all VirtualServices (Istio) in a namespace:
kubectl get virtualservices -n <namespace>
- Describe a VirtualService (Istio):
kubectl describe virtualservice <virtualservice-name> -n <namespace>
Pod Network Troubleshooting:
- Run a network diagnostic pod (e.g., busybox) for debugging:
kubectl run -it --rm --restart=Never --image=busybox net-debug-pod -- /bin/sh
- Test connectivity from a pod to a specific endpoint:
kubectl exec -it <pod-name> -n <namespace> -- curl <endpoint-url>
- Trace network path from one pod to another:
kubectl exec -it <source-pod-name> -n <namespace> -- traceroute <destination-pod-ip>
- Check DNS resolution from a pod:
kubectl exec -it <pod-name> -n <namespace> -- nslookup <domain-name>
Config and Resource Validation:
- Validate a Kubernetes YAML file without applying it:
kubectl apply --dry-run=client -f <yaml-file>
- Validate a pod’s security context and capabilities:
kubectl auth can-i list pods --as=system:serviceaccount:<namespace>:<serviceaccount-name>
RBAC and Security:
- List roles and role bindings in a namespace:
kubectl get roles,rolebindings -n <namespace>
- Describe a role or role binding:
kubectl describe role <role-name> -n <namespace>
Service Account Diagnostics:
- List service accounts in a namespace:
kubectl get serviceaccounts -n <namespace>
- Describe a service account:
kubectl describe serviceaccount <serviceaccount-name> -n <namespace>
Node Drain and Uncordon:
- Drain a node for maintenance:
kubectl drain <node-name> --ignore-daemonsets
- Uncordon a previously drained node:
kubectl uncordon <node-name>
Resource Cleanup:
- Delete a pod forcefully (not recommended):
kubectl delete pod <pod-name> -n <namespace> --grace-period=0 --force
Pod Affinity and Anti-Affinity:
- List pod affinity rules for a pod:
kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity}'
- List pod anti-affinity rules for a pod:
kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity.podAntiAffinity}'
Pod Security Policies (PSP):
- List all pod security policies (if enabled):
kubectl get psp
Kubernetes Events:
- View recent cluster events:
kubectl get events --sort-by=.metadata.creationTimestamp
- Filter events by a specific namespace:
kubectl get events -n <namespace>
Node Troubleshooting:
- Check node conditions:
kubectl describe node <node-name> | grep Conditions -A5
- List node capacity and allocatable resources:
kubectl describe node <node-name> | grep -E "Capacity|Allocatable"
Ephemeral Containers (Kubernetes 1.18+):
- Run an ephemeral debugging container:
kubectl debug -it <pod-name> -n <namespace> --image=<debug-image> -- /bin/sh
Resource Metrics (Metrics Server required):
- Get CPU and Memory usage for pods:
kubectl top pod -n <namespace>
Kubelet Diagnostics:
- View kubelet logs on a node:
kubectl logs -n kube-system kubelet-<node-name>
Advanced Debugging with Telepresence:
- Debug a pod with Telepresence:
telepresence --namespace <namespace> --swap-deployment <pod-name>
Kubeconfig and Contexts:
- List available contexts:
kubectl config get-contexts
- Switch to a different context:
kubectl config use-context <context-name>
Pod Security Standards (PodSecurity admission controller):
- List PodSecurityPolicy (PSP) violations:
kubectl get psp -A | grep -vE 'NAME|REVIEWED'
Pod Disruption Budget (PDB) Diagnostics:
- List all PDBs in a namespace:
kubectl get pdb -n <namespace>
- Describe a PDB:
kubectl describe pdb <pdb-name> -n <namespace>
Resource Lock Diagnostics (if using resource locks):
- List resource locks in a namespace:
kubectl get resourcelocks -n <namespace>
Service Endpoints and DNS:
- List service endpoints for a service:
kubectl get endpoints <service-name> -n <namespace>
- Check DNS configuration in a pod:
kubectl exec -it <pod-name> -n <namespace> -- cat /etc/resolv.conf
Custom Metrics (Prometheus, Grafana):
- Query Prometheus metrics: Use
kubectl port-forward
to access Prometheus and Grafana services to query custom metrics.
Pod Priority and Preemption:
- List priority classes:
kubectl get priorityclasses
Pod Overhead (Kubernetes 1.18+):
- List overhead in a pod:
kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.overhead}'
Volume Snapshot Diagnostics (if using volume snapshots):
- List volume snapshots:
kubectl get volumesnapshot -n <namespace>
- Describe a volume snapshot:
kubectl describe volumesnapshot <snapshot-name> -n <namespace>
Resource Deserialization Diagnostics:
- Deserialize and print a Kubernetes resource:
kubectl get <resource-type> <resource-name> -n <namespace> -o=json
Node Taints:
- List node taints:
kubectl describe node <node-name> | grep Taints
Mutating and Validating Webhook Configurations:
- List mutating webhook configurations:
kubectl get mutatingwebhookconfigurations
- List validating webhook configurations:
kubectl get validatingwebhookconfigurations
Pod Network Policies:
- List pod network policies in a namespace:
kubectl get networkpolicies -n <namespace>
Node Conditions (Kubernetes 1.17+):
- List node conditions:
kubectl get nodes -o custom-columns=NODE:.metadata.name,READY:.status.conditions[?(@.type=="Ready")].status -l 'node-role.kubernetes.io/worker='
Audit Logs:
- Retrieve audit logs (if enabled): Check your Kubernetes audit log configuration for the location of audit logs.
Node Operating System Details:
- Get the node’s OS information:
kubectl get node <node-name> -o jsonpath='{.status.nodeInfo.osImage}'
List All Running Pods in All Namespaces (Short Command):
- List all running pods in all namespaces in a short format:
kubectl get pods --all-namespaces
These commands should cover a wide range of diagnostics scenarios in Kubernetes. Make sure to replace placeholders like <namespace>
, <pod-name>
, <deployment-name>
, etc., with actual values specific to your cluster and use case.