Kubernetes (kubectl)
Contexts, pods, logs, deployments, debugging and port-forward.
Contexts and namespaces
Current context
kubectl config current-contextList contexts
kubectl config get-contextsSwitch context
kubectl config use-context <context>
Default namespace for the current context
kubectl config set-context --current --namespace=<namespace>
Viewing resources
Pods in a namespace
kubectl get pods -n <namespace>
Pods with nodes and IPs
kubectl get pods -n <namespace> -o wide
All main resources in a namespace
kubectl get all -n <namespace>
Pod details and events
kubectl describe pod <pod> -n <namespace>
Events sorted by time
kubectl get events -n <namespace> --sort-by=.lastTimestamp
Pods not in Running stateadvanced
kubectl get pods -A --field-selector=status.phase!=RunningPod resource usage
kubectl top pods -n <namespace>
Logs and debugging
Follow pod logs
kubectl logs -f <pod> -n <namespace>
Logs of the previous (crashed) container
kubectl logs <pod> -n <namespace> --previous
Logs of all pods in a deployment
kubectl logs -f deployment/<deployment> -n <namespace> --all-containers
Open a shell in a pod
kubectl exec -it <pod> -n <namespace> -- sh
Forward a service port to localhost
kubectl port-forward svc/<k8s_service> 8080:80 -n <namespace>
Temporary pod for network debuggingadvanced
kubectl run debug --rm -it --image=nicolaka/netshoot -n <namespace> -- bash
Deployments
Apply a manifest
kubectl apply -f manifest.yamlRestart a deployment
kubectl rollout restart deployment/<deployment> -n <namespace>
Rollout status
kubectl rollout status deployment/<deployment> -n <namespace>
Roll back to the previous version
kubectl rollout undo deployment/<deployment> -n <namespace>
Scale
kubectl scale deployment/<deployment> --replicas=3 -n <namespace>
Decode a secretadvanced
kubectl get secret <k8s_secret> -n <namespace> -o jsonpath='{.data.password}' | base64 -d