← Back to DevOps Commands

Kubernetes

kubectl Commands Cheat Sheet

Practical kubectl commands for managing Kubernetes clusters, pods, deployments, services and troubleshooting.

What is kubectl?

kubectl is the command-line tool used to communicate with a Kubernetes cluster. It allows you to create, inspect, update and delete Kubernetes resources.

kubectl [command] [TYPE] [NAME] [flags]

1. Check Kubernetes Cluster

kubectl cluster-info

Displays information about the Kubernetes control plane and cluster services.

2. Get Nodes

kubectl get nodes

Lists all nodes in the current Kubernetes cluster.

kubectl get nodes
  
  NAME       STATUS   ROLES           AGE
  worker01   Ready    <none>          10d

3. Get Pods

kubectl get pods

Lists pods in the current namespace.

kubectl get pods -A

Lists pods across all namespaces.

kubectl get pods -o wide

Shows additional information such as pod IP and node.

4. Describe a Pod

kubectl describe pod <pod-name>

Shows detailed information about a pod, including events, containers, volumes and scheduling information.

Useful when troubleshooting a pod that is not starting.

5. View Pod Logs

kubectl logs <pod-name>

Displays logs from a pod's container.

kubectl logs -f <pod-name>

Continuously follows the container logs.

6. Execute Commands Inside a Pod

kubectl exec -it <pod-name> -- /bin/bash

Opens an interactive shell inside a running container.

Some minimal container images do not include bash. In those cases, try /bin/sh if available.

7. Get Deployments

kubectl get deployments

Lists deployments in the current namespace.

8. Get Services

kubectl get services

Lists Kubernetes Services and their networking information.

kubectl get svc is the shorter form.

9. Apply a Kubernetes Manifest

kubectl apply -f deployment.yaml

Creates or updates Kubernetes resources defined in a YAML manifest.

10. Delete a Pod

kubectl delete pod <pod-name>

Deletes the specified pod.

If the pod is managed by a Deployment, ReplicaSet or another controller, Kubernetes may create a replacement pod.

Quick Troubleshooting Workflow

kubectl get pods
  kubectl describe pod <pod-name>
  kubectl logs <pod-name>
  kubectl get events
  kubectl get nodes
  kubectl get deployments
  kubectl get services

A common troubleshooting approach is to first check the pod status, inspect its events, review application logs and then check the surrounding Kubernetes resources.

DevOps Commands Tip

When troubleshooting Kubernetes, don't immediately delete the pod. First inspect its status, events and logs. This often reveals the actual problem and preserves useful diagnostic information.