Essential Kubernetes Commands
Kubernetes gives you a toolkit of commands that every DevOps engineer should know. These let you interact with your cluster, manage resources, and troubleshoot—all without having to jump into individual containers or nodes.
Working With kubectl
The kubectl
command-line tool is your main way to work with Kubernetes clusters. Start by checking cluster info with kubectl cluster-info
—that tells you if you’re connected. To see all the nodes, run kubectl get nodes
.
Namespaces help you organize resources:
kubectl get namespaces
: Lists all namespaceskubectl create namespace <name>
: Makes a new namespacekubectl config set-context --current --namespace=<name>
: Switches your current namespace
For general resource management, you’ll use these a lot:
kubectl get <resource> # List resources
kubectl describe <resource> # Show details
kubectl apply -f <filename> # Create/update from file
kubectl delete <resource> # Remove resources
Labels keep your resources organized, and you can add them with kubectl label pod <name> key=value
.
Managing Deployments and Services
Deployments help you keep your app running the way you want and make updates smoother. Create one with kubectl create deployment <name> --image=<image>
, or apply a YAML with kubectl apply -f deployment.yaml
.
Some deployment commands you’ll reach for often:
kubectl get deployments
: Lists deploymentskubectl rollout status deployment/<name>
: Checks rollout statuskubectl scale deployment/<name> --replicas=<number>
: Changes the number of podskubectl rollout undo deployment/<name>
: Rolls back to the previous version
Services expose your apps inside or outside the cluster. You can create one with kubectl expose deployment <name> --port=<port>
or use a YAML file.
Secrets matter for configuration:
kubectl create secret generic <name> --from-literal=key=value
kubectl get secrets
kubectl describe secret <name>
Inspecting and Debugging
When things go sideways, Kubernetes hands you some solid troubleshooting tools. Kick off with kubectl get pods
—that’ll show you which pods are acting up, along with their status and how many times they’ve restarted.
Dive in deeper with these commands:
kubectl describe pod <pod-name>
: Gives you details, events, and conditions for a pod.kubectl logs <pod-name>
: Grabs logs from the container.kubectl logs <pod-name> -c <container-name>
: Targets logs from a specific container.kubectl logs <pod-name> --previous
: Shows logs from the last instance of a container.
If you need to poke around inside a container, use kubectl exec -it <pod-name> -- <command>
. That lets you interact directly with the app’s environment, which can be a lifesaver for troubleshooting.
To keep tabs on pod resource usage, check out kubectl top pods
or kubectl top nodes
. If you want to test services in real time, kubectl port-forward <pod-name> <local-port>:<pod-port>
sets up a direct connection.
Here’s a handy list of must-know debugging commands that help DevOps folks spot and fix issues before users even notice.