Cheatsheet
Adam
- 3 minutes read - 632 wordsKubernetes Cheatsheet
Common Commands
Cluster Management
kubectl get nodes- List all nodeskubectl get pods -A- List all pods in all namespaceskubectl get events -A- View cluster events
Debugging
kubectl describe pod <pod-name>- Get detailed pod informationkubectl logs <pod-name>- View pod logskubectl exec -it <pod-name> -- /bin/bash- Execute command in pod
Resource Management
kubectl top nodes- View node resource usagekubectl top pods- View pod resource usagekubectl get pvc- List persistent volume claims
Troubleshooting Tips
Node Issues
- Check kubelet status:
systemctl status kubelet - Check disk space:
df -h - Check memory usage:
free -h
Pod Issues
- Check pod events:
kubectl describe pod <pod-name> - Check pod logs:
kubectl logs <pod-name> --previous(for previous instance) - Check resource limits:
kubectl describe pod <pod-name> | grep -i limit
Network Issues
- Test connectivity:
kubectl exec <pod-name> -- ping <target> - Check DNS resolution:
kubectl exec <pod-name> -- nslookup <service> - Check service endpoints:
kubectl get endpoints <service-name>
Commands
- Get resources
$ kubectl api-resources --api-group ""
NAME SHORTNAMES APIVERSION NAMESPACED KIND
bindings v1 true Binding
componentstatuses cs v1 false ComponentStatus
configmaps cm v1 true ConfigMap
endpoints ep v1 true Endpoints
events ev v1 true Event
limitranges limits v1 true LimitRange
namespaces ns v1 false Namespace
nodes no v1 false Node
persistentvolumeclaims pvc v1 true PersistentVolumeClaim
persistentvolumes pv v1 false PersistentVolume
pods po v1 true Pod
podtemplates v1 true PodTemplate
replicationcontrollers rc v1 true ReplicationController
resourcequotas quota v1 true ResourceQuota
secrets v1 true Secret
serviceaccounts sa v1 true ServiceAccount
services svc v1 true Service
Namespaces
Namespaces are a way to divide cluster resources between multiple users.
Namespaces cannot be nested.
Create a namespace via declration.
$ kubectl create namespace kubernetes_fail -oyaml --dry-run=client > namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: kubernetes_fail
spec: {}
status: {}
- Create the namespace from file
kubectl apply --from-file namespace.yaml kubectl get pod --all-namespacesGet all pods in all namespaces.
Debugging
kubectl describe pod --namespace kubernetes-fail debugdescribe the pod debug in namespace kubernetes-fail, look for any errors.
Normal Pulled 95s kubelet spec.containers{debug}: Successfully pulled image "cgr.dev/chainguard/busybox:latest" in 419ms (419ms including waiting). Image size: 3982143 bytes.
Warning Failed 95s kubelet spec.containers{debug}: Error: failed to generate container "b1cce48c2bbfd3519eb476674b7e78aa6645f4dda02dddf8aa83f41c6da767f9" spec: failed to generate spec: no command specified
kubectl logs --namespace kubernetes-fail debugget logs from pod debug in namespace kubernetes-failCreate a busybox pod in namespace, and enable interactive shell
cat pod-busybox.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: debug
name: debug
namespace: kubernetes-fail
spec:
containers:
- image: cgr.dev/chainguard/busybox:latest
name: debug
resources: {}
args:
- sh
stdin: true
tty: true
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
- Attach to pod,
kubectl attach --namespace kubernetes-fail debug -it
Deployments
kubectl get deployments --namespace kubernetes-failget deployments in namespace kubernetes-fail- multiple resources can be created from a single file, by separating them with
---in the file, here the order matters, first create the namespace then the deployment and all its other resources. - when appying multiple resources from files (eg:
kubectl apply -f), the order matters.
Show all pods based on label
kubectl get pods --all-namespaces --selector app=webserverList all pods with the label app=webserverkubectl get pods -A -l app=webserverSame in short.
Show pods with exta info
kubectl get pods --output wideShow pods with extra info like IP address
Get ports of a pod
kubectl exec -it webserver-1234567890-abcde -- netstat -tulpnGet open ports of a pod
create a loadbalancer with a persistent (cluster private) IP address
kubectl create service clusterip webserver --tcp=8080 --namespace kubernetes-fail -oyaml --dry-run=client > service.yamlkubectl apply -f service.yamlkubectl get service,endpointsshow services and its endpointskubectl get svcshow serviceskubectl describe svc webserverdescribe the details of the loadbalancer
Kubectl explain
kubectl explain pods.spec.containers.livenessProbeshow the documentation for a given resourcekubectl explain pods.spec.containers.envshow the documentation for continer environment variables