Top 10 Kubernetes Commands Every DevOps Engineer Should Know for Efficient Cluster Management

Top 10 Kubernetes Commands Every DevOps Engineer Should Know for Efficient Cluster Management

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 namespaces
  • kubectl create namespace <name>: Makes a new namespace
  • kubectl 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 deployments
  • kubectl rollout status deployment/<name>: Checks rollout status
  • kubectl scale deployment/<name> --replicas=<number>: Changes the number of pods
  • kubectl 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.

Share this article:
As a passionate DevOps Engineer, I thrive on bridging the gap between development and operations. My expertise lies in crafting efficient, scalable infrastructure solutions, with a particular fondness for Linux and Ubuntu environments. I'm constantly exploring innovative ways to streamline processes, enhance system reliability, and boost productivity through automation. My toolkit includes a wide array of cutting-edge technologies and best practices in continuous integration, deployment, and monitoring. When I'm not immersed in code or fine-tuning server configurations, you'll find me staying up-to-date with the latest industry trends and sharing knowledge with the tech community. Let's connect and discuss how we can revolutionize your infrastructure!