This cheat sheet covers the most frequently used kubectl commands that every developer working with Kubernetes should know.
1. Cluster Information
kubectl version
Displays the version information of the client and server.
kubectl cluster-info
Displays cluster information, including the master node address and Kubernetes version.
kubectl get nodes
Lists all nodes in the cluster and their status.
kubectl describe node <node-name>
Provides detailed information about a specific node.
kubectl top nodes
Displays resource usage (CPU and memory) of nodes.
2. Namespaces
kubectl get namespaces
Lists all namespaces in the cluster.
kubectl create namespace <namespace-name>
Creates a new namespace.
kubectl delete namespace <namespace-name>
Deletes a namespace.
kubectl config set-context --current --namespace=<namespace-name>
Sets the current context to a specific namespace.
3. Deployments
kubectl apply -f <deployment-file.yaml>
Creates or updates a Deployment from a YAML file.
kubectl create deployment <deployment-name> --image=<image-name>
Creates a Deployment with a specified image.
kubectl get deployments
Lists all Deployments in the current namespace.
kubectl describe deployment <deployment-name>
Shows detailed information about a Deployment.
kubectl scale deployment <deployment-name> --replicas=<number>
Scales a Deployment to the specified number of replicas.
kubectl rollout status deployment <deployment-name>
Tracks the rollout status of a Deployment.
kubectl rollout undo deployment <deployment-name>
Rolls back a Deployment to the previous revision.
kubectl delete deployment <deployment-name>
Deletes a Deployment.
4. Pods
kubectl get pods
Lists all Pods in the current namespace.
kubectl get pods -o wide
Lists all Pods with additional information like Node and IP.
kubectl describe pod <pod-name>
Shows detailed information about a Pod.
kubectl logs <pod-name>
Displays logs from a Pod.
kubectl logs <pod-name> -c <container-name>
Displays logs from a specific container in a Pod.
kubectl exec -it <pod-name> -- <command>
Executes a command inside a Pod.
kubectl delete pod <pod-name>
Deletes a Pod.
5. Services
kubectl apply -f <service-file.yaml>
Creates or updates a Service from a YAML file.
kubectl expose deployment <deployment-name> --port=<port> --target-port=<target-port>
Exposes a Deployment as a Service.
kubectl get services
Lists all Services in the current namespace.
kubectl describe service <service-name>
Shows detailed information about a Service.
kubectl delete service <service-name>
Deletes a Service.
6. ConfigMaps and Secrets
kubectl create configmap <configmap-name> --from-literal=<key>=<value>
Creates a ConfigMap from literal values.
kubectl create configmap <configmap-name> --from-file=<path-to-file>
Creates a ConfigMap from a file.
kubectl create secret generic <secret-name> --from-literal=<key>=<value>
Creates a Secret from literal values.
kubectl create secret generic <secret-name> --from-file=<path-to-file>
Creates a Secret from a file.
kubectl get configmaps
Lists all ConfigMaps in the current namespace.
kubectl get secrets
Lists all Secrets in the current namespace.
7. Other Useful Commands
kubectl get events
Displays cluster events, helpful for debugging.
kubectl get all
Lists all resources (Pods, Deployments, Services, etc.) in the current namespace.
kubectl describe <resource-type> <resource-name>
Provides detailed information about a specific resource.
kubectl port-forward <pod-name> <local-port>:<container-port>
Forwards a local port to a port in a Pod.
kubectl apply -f -
Applies YAML configuration directly from the command line.
kubectl explain <resource-type>
Provides documentation and field definitions for a resource type.
Tips
-
-n <namespace-name>
to specify a namespace for most commands.
-
-o yaml
or
-o json
to get output in YAML or JSON format.
-
--help
with any command to get help and see available options.
-
kubectl api-resources
to see a list of all available resource types.
This cheat sheet provides a comprehensive overview of commonly used Kubernetes commands for developers. As you gain more experience with Kubernetes, you’ll discover additional commands and options that suit your specific needs.