Kubernetes Cheat Sheet
Pods Logs
# Get all pods in the current namespace # Show logs (stdout) of a pod
kubectl get pods kubectl logs <pod>
# Get pods in all namespaces # Show logs (stdout) of pods that match a label
kubectl get pods --all-namespaces kubectl logs -l <label>=<value>
# Get pods with more details # Show logs of a previous instantiation of a container
kubectl get pods -o wide kubectl logs <pod> --previous
# Get the yaml for a pod # Show logs for a specific container in a pod (i.e. init
kubectl get pod <pod> -o yaml container)
kubectl logs <pod> -c <container>
# Inspect a pod
kubectl describe pods <pod> # Following logs from a pod
kubectl logs -f <pod>
# Get pods sorted by a metric
kubectl get pods \ # Follow all logs from a pod that match a label
--sort-by='.status.containerStatuses[0].restartCount' kubectl logs -f -l <label>=<value> --all-containers
# Get pods with their labels # Show logs with verbosity level of logs from 0 - 9
kubectl get pods --show-labels kubectl logs <pod> --v=<0:9>
# Get pods that match a label
kubectl get pods -l <label>=<value> Deployments
# Forward traffic from a localhost port to a pod port # Get all deployments in the current namespace
kubectl port-forward <pod> <localhost-port>:<pod-port> kubectl get deployment
# Run a command on a pod # Get deployments in all namespaces
kubectl exec <pod> -- <command> kubectl get deployment --all-namespaces
# Run a command on a container in a pod # Get deployments with more details
kubectl exec <pod> -c <container> -- <command> kubectl get deployment -o wide
# Get the yaml for a deployment
kubectl get deployment <deployment> -o yaml
Secrets
# Get all secrets in the current namespace # Inspect a deployment
kubectl get secrets kubectl describe deployment <deployment>
# Get secrets in all namespaces # Get deployment's labels
kubectl get secrets --all-namespaces kubectl get deployment --show-labels
# Get secrets with more details # Get deployments that match a label
kubectl get secrets -o wide kubectl get deployment -l <label>=<value>
# Get the contents of a secret
kubectl get secrets <secret> -o yaml Ingress
# Get all ingress in the current namespace
kubectl get ingress
Services
# Get ingress in all namespaces
# Get all services in the current namespace
kubectl get ingress --all-namespaces
kubectl get services
# Get ingress with more details
# Get services in all namespaces
kubectl get ingress -o wide
kubectl get service --all-namespaces
# Get the yaml for a ingress
# Get services with more details
kubectl get ingress <ingress> -o yaml
kubectl get service -o wide
# Inspect a ingress
# Get the yaml for a services
kubectl describe ingress <ingress>
kubectl get service <service> -o yaml
# Get ingress labels
# Inspect a service
kubectl get ingress --show-labels
kubectl describe service <service>
# Get ingress that match a label
# Get service's labels
kubectl get ingress -l <label>=<value>
kubectl get service --show-labels
# Get services that match a label
kubectl get service -l <label>=<value> Creating Resources
# Create a kubernetes resource from a file
kubectl apply -f ./<manifest>.yaml
Updating Resources
# Create kubernetes resources from multiple files
# Roll a new version of a deployment
kubectl apply -f ./<manifest>.yaml -f ./<manifest>.yaml
kubectl set image deployment/<deployment> <container-
name>=image:<version>
# Create resources from all manifest files in a directory
kubectl apply -f ./<directory>
# Check the deployment history
kubectl rollout history deployment/<deployment>
# Create resource from a url
kubectl apply -f <url_to_manifest>
# Rollback a deployment
kubectl rollout undo deployment/<deployment>
# Start a single instance of an image
kubectl create deployment <deployment_name> --
# Rollback to a specific version
image=<image>
kubectl rollout undo deployment/<deployment> --to-revision=2
# Watch a rolling update
kubectl rollout status -w deployment/<deployment> Nodes
# Mark node as unschedulable
# Restart the rolling deploy kubectl cordon <node>
kubectl rollout restart deployment/<deployment>
# Drain a node for maintenance
# Edit a resource’s yaml kubectl drain <node>
kubectl edit deployment/<deployment>
# Mark node as schedulable
# Scale a deployment to 3 pods kubectl uncordon <node>
kubectl scale --replicas=3 deployment/<deployment>
# Show ‘top’ metrics for a node
# Delete a pod kubectl top node <node>
kubectl delete pod <pod>
# Display addresses of the master and services
kubectl cluster-info
Context
# Dump current cluster state to stdout
# Show contexts
kubectl cluster-info dump
kubectl config get-contexts
# Show a list of eligible kube resource (i.e. pods, service, pv,
# Show current context
etc)
kubectl config current-context
kubectl api-resources
# Switch context to another cluster
# Show a list of eligible kube resources in your namespace
kubectl config use-context <my-cluster-name>
kubectl api-resources --namespaced=true
# Change Namespace
kubectl config set-context --current --namespace=<namespace>