Post

Kubectl Cheat Sheet

Helpful Kubectl commands to reference as a cheat sheet

Kubectl Cheat Sheet

Contexts and Configuration

CommandDescription
kubectl config viewShow the current kubeconfig file
kubectl config get-contextsList all contexts in the kubeconfig file
kubectl config current-contextShow the current context
kubectl config use-context <context>Change the current context
kubectl config set-context <context> --namespace=<namespace>Set the namespace for a context
kubectl config set-context <context> --cluster=<cluster>Set the cluster for a context
kubectl config set-context <context> --user=<user>Set the user for a context
kubectl config set-context <context> --namespace=<namespace> --cluster=<cluster> --user=<user>Set all context properties

Cluster Management

CommandDescription
kubectl cluster-infoDisplay addresses of the master and services
kubectl get nodesList all nodes in the cluster
kubectl get podsList all pods in the cluster
kubectl get servicesList all services in the cluster
kubectl get deploymentsList all deployments in the cluster
kubectl get namespacesList all namespaces in the cluster
kubectl get eventsList all events in the cluster

Resource Management

CommandDescription
kubectl apply -f <file>Apply a configuration file
kubectl delete -f <file>Delete a configuration file
kubectl get <resource>List all resources of a type
kubectl describe <resource> <name>Describe a resource
kubectl edit <resource> <name>Edit a resource
kubectl exec -it <pod> -- <command>Execute a command in a pod

Pod Management

CommandDescription
kubectl run <name> --image=<image>Create a new pod
kubectl delete pod <name>Delete a pod
kubectl get pod <name>Get details of a pod
kubectl describe pod <name>Describe a pod
kubectl logs <name>Show logs of a pod
kubectl exec -it <name> -- /bin/bashExecute a command in a pod
kubectl cp <pod>:<source> <destination>Copy files from a pod
kubectl top nodeShow metrics for all nodes
kubectl top podShow metrics for all pods
kubectl top pod <name>Show metrics for a specific pod

Service Management

CommandDescription
kubectl expose pod <name> --port=444 --target-port=555Expose a pod as a service
kubectl delete service <name>Delete a service
kubectl get service <name>Get details of a service
kubectl describe service <name>Describe a service

Deployment Management

CommandDescription
kubectl create deployment <name> --image=<image>Create a new deployment
kubectl delete deployment <name>Delete a deployment
kubectl get deployment <name>Get details of a deployment
kubectl describe deployment <name>Describe a deployment
kubectl scale deployment <name> --replicas=3Scale a deployment to 3 replicas
kubectl rollout status deployment/<name>Check the status of a deployment rollout
kubectl rollout history deployment/<name>Show the history of a deployment rollout
kubectl rollout undo deployment/<name>Rollback a deployment to the previous version
kubectl rollout undo deployment/<name> --to-revision=1Rollback a deployment to a specific revision

Namespace Management

CommandDescription
kubectl create namespace <name>Create a new namespace
kubectl delete namespace <name>Delete a namespace
kubectl get namespace <name>Get details of a namespace
kubectl describe namespace <name>Describe a namespace
This post is licensed under CC BY 4.0 by the author.