Cheat Sheet of common Kubernetes commands.

 

kubectl version: Check the Kubernetes client and server version.

kubectl version

 

kubectl cluster-info: Display cluster information.

kubectl cluster-info

 

kubectl get: List resources in the cluster.

# List all pods in the default namespace
kubectl get pods

# List all services in the kube-system namespace
kubectl get services -n kube-system

# List all nodes in the cluster
kubectl get nodes

# Get detailed information about a specific resource
kubectl get pods my-pod

# Get pods matching a string in a given namespace
kubectl -n my-namespace get pods | grep resource-first-letters-of-name

# Get deployments in a given namespace
kubectl -n my-namespace get deployments

kubectl describe: Get detailed information about a resource.

# Get detailed information about a pod
kubectl describe pod my-pod

# Get detailed information about a service
kubectl describe service my-service

 

kubectl create: Create resources in the cluster.

# Create a new deployment named "webapp" with one replica and using the 'nginx' image.
kubectl create deployment webapp --replicas=1 --image=nginx

# Create a new service named "my-service" to expose the deployment on port 80
kubectl create service clusterip my-service --tcp=80:80

 

kubectl apply: Apply configurations from a YAML file to the cluster.

# Apply a deployment configuration from a file
kubectl apply -f deployment.yaml

# Apply a service configuration from a file
kubectl apply -f service.yaml

# Apply all configurations in a directory
kubectl apply -f ./my-configs/

 

kubectl delete: Delete resources in the cluster.

# Delete a pod by name
kubectl delete pod my-pod

# Delete a deployment by name
kubectl delete deployment my-deployment

# Delete a service by name
kubectl delete service my-service

# Delete all resources defined in a YAML file
kubectl delete -f my-configs.yaml

 

kubectl edit: Edit a resource in the default text editor.

# Edit a deployment named "my-deployment"
kubectl edit deployment my-deployment

 

kubectl logs: View the logs of a specific container within a pod.

# View the logs of a container named "web" in a pod named "my-pod"
kubectl logs my-pod -c web

# Stream the logs of a pod in real-time
kubectl logs -f my-pod

 

kubectl exec: Execute commands in a running container.

# Run a bash shell in a pod named "my-pod"
kubectl exec -it my-pod -- bash

# Run shell in a pod in a given namespace
kubectl exec -it -n my-namespace my-pod -c my-container /bin/sh 

# Execute a command in a container named "web" in a pod named "my-pod"
kubectl exec my-pod -c web -- ls /app

 

kubectl port-forward: Forward local ports to a running pod.

# Forward port 8080 of a pod to your local machine's port 8888
kubectl port-forward pod/my-pod 8888:8080

 

kubectl expose: Expose a deployment, replicaset, or pod as a service.

# Expose a deployment named "webapp" as a NodePort service on port 30080
kubectl expose deployment webapp --type=NodePort --port=80 --target-port=80 --name=webapp-service

 

kubectl scale: Scale the number of replicas for a deployment or replicaset.

# Scale a deployment named "my-deployment" to 3 replicas
kubectl scale deployment my-deployment --replicas=3

 

kubectl rollout: Perform rolling updates and rollbacks for a deployment.

# Trigger a rollout update for a deployment named "my-deployment"
kubectl rollout restart deployment my-deployment

# Rollback to the previous revision of a deployment
kubectl rollout undo deployment my-deployment

 

kubectl get events: Display events related to resources in the cluster.

kubectl get events

# Display events for a specific resource (e.g., a pod)
kubectl get events --field-selector involvedObject.kind=Pod --field-selector involvedObject.name=my-pod

 

kubectl top: Display resource usage statistics for nodes or pods.

# Display CPU and memory usage of nodes
kubectl top nodes

# Display CPU and memory usage of pods in a specific namespace
kubectl top pods -n my-namespace

 

kubectl label: Add or update labels on resources.

# Add a label "app=frontend" to a pod named "my-pod"
kubectl label pod my-pod app=frontend

# Update a label on a deployment
kubectl label deployment my-deployment version=v2

 

kubectl annotate: Add or update annotations on resources.

# Add an annotation "description=backend service" to a service named "my-service"
kubectl annotate service my-service description="backend service"

 

kubectl apply: Apply a configuration from stdin.

# Apply a resource configuration directly from a YAML string
echo 'apiVersion: v1\nkind: Pod\nmetadata:\n name: my-pod\nspec:\n containers:\n - name: nginx\n image: nginx:latest' | kubectl apply -f -

 

kubectl create secret: Create secrets to be used by pods or other resources.

# Create a generic secret named "my-secret" with key-value pairs
kubectl create secret generic my-secret --from-literal=username=myuser --from-literal=password=mypassword

 

kubectl get secrets: List all secrets in the cluster.

kubectl get secrets

 

These are some of the most commonly used kubectl commands with examples. Remember to have kubectl configured to connect to your Kubernetes cluster before running these commands. Additionally, you can always explore more commands and options by checking kubectl --help or referring to the Kubernetes documentation in their website.