kubectl Cheat Sheet

Here’s a cheat sheet for kubectl (Kubernetes command-line tool) commands:

Cluster Information

Display cluster information:

kubectl cluster-info

Display information about the nodes in the cluster:

kubectl get nodes

Pods

List all pods in the current namespace:

kubectl get pods

Describe details of a specific pod:

Describe details of a specific pod

Create a pod from a YAML file:

kubectl create -f pod.yaml

Delete a pod:

kubectl delete pod pod_name

Deployments

List all deployments:

kubectl get deployments

Describe details of a specific deployment:

kubectl describe deployment deployment_name

Scale a deployment:

kubectl scale deployment deployment_name --replicas=3

Update a deployment with a new image:

kubectl set image deployment/deployment_name container_name=new_image:tag

Services

List all services:

kubectl get services

Describe details of a specific service:

kubectl describe service service_name

Expose a deployment as a service:

kubectl expose deployment deployment_name --type=LoadBalancer --port=80

ConfigMaps and Secrets

Create a ConfigMap:

kubectl create configmap configmap_name --from-file=path/to/files

Create a Secret:

kubectl create secret generic secret_name --from-literal=key1=value1 --from-literal=key2=value2

Namespaces

List all namespaces:

kubectl get namespaces

Switch to a different namespace:

kubectl config set-context --current --namespace=new_namespace

Here is a dedicated Namespaces Cheat Sheet.

Logs and Exec

View logs of a pod:

kubectl logs pod_name

Execute a command in a running pod:

kubectl exec -it pod_name -- /bin/sh

Rolling Updates and Rollbacks

Perform a rolling update on a deployment:

kubectl set image deployment/deployment_name container_name=new_image:tag

Rollback a deployment to a previous revision:

kubectl rollout undo deployment/deployment_name

Miscellaneous

Apply a configuration file to create or update resources:

kubectl apply -f filename.yaml

Delete resources defined in a configuration file:

kubectl delete -f filename.yaml

Get detailed information about a resource:

kubectl get resource_type resource_name -o yaml

These commands cover some common kubectl operations. For more details and advanced usage, refer to the official Kubernetes documentation.