Kubernetes kubectl commands

This reference lists some Kubernetes kubectl commands for troubleshooting problems with operations management components. It assumes that you have custom namespaces, such as controlplane, fission, cert-manager, and user-defined agent namespaces (for example, agent-xyz).

Check the cluster health

kubectl get nodes -o wide 
kubectl get componentstatuses
Check resources in all namespaces

kubectl get pods --all-namespaces 
kubectl get svc --all-namespaces 
kubectl get deployments --all-namespaces
Check resources in a specific namespace
control plane

kubectl get pods -n controlplane 
kubectl describe pod <pod-name> -n controlplane 
kubectl logs <pod-name> -n controlplane
fission

kubectl get pods -n fission 
kubectl logs <pod-name> -n fission
cert-manager

kubectl get certificates,issuers,clusterissuers -A 
kubectl describe pod <pod-name> -n cert-manager 
custom agent namespace (for example, agent-xyz)

kubectl get all -n agent-xyz 
kubectl describe pod <pod-name> -n agent-xyz 
kubectl logs <pod-name> -n agent-xyz
Troubleshoot common pod issues
Pod state of Pending or CrashLoopBackOff

kubectl describe pod <pod-name> -n <namespace> 
kubectl logs <pod-name> -n <namespace> 
kubectl logs <pod-name> -c <container-name> -n <namespace>
Check restarted pods

kubectl get pods -A --sort-by='.status.containerStatuses[0].restartCount'
Check resource utilization (requires the Kubernetes Metrics Server)

kubectl top pods -n <namespace> 
kubectl top nodes
Clean up unhealthy resources

kubectl delete pod <pod-name> -n <namespace> 
kubectl delete pod <pod-name> -n <namespace> --grace-period=0 --force
Get YAML for troubleshooting or reapplying

kubectl get pod <pod-name> -n <namespace> -o yaml > pod-debug.yaml 
Exec into a pod

kubectl exec -it <pod-name> -n <namespace> -- /bin/sh 
# Or use /bin/bash if available 
Check services and endpoints

kubectl get svc -n <namespace> 
kubectl get endpoints -n <namespace> 
Restart a deployment

kubectl rollout restart deployment <deployment-name> -n <namespace> 
Discover custom namespaces

kubectl get ns 
kubectl get ns | grep agent 
Remove stuck finalizers

kubectl patch <resource type> <resource name> -p '{"metadata":{"finalizers":[]}}' --type=merge
List unite-imagepullsecret secrets
Use the following command to list the unite-imagepullsecret secrets:
kubectl get secrets -A | grep unite-imagepullsecret
Save logs for IBM® Support

kubectl logs <pod-name> -n <namespace> > pod-logs.txt