LATEST 🚀 Welcome to Upgradix by Shrash Studio – Your destination for free technology learning and career growth.
🕘 Updated:
Kubernetes

Navigating Your Cluster with Kubectl & K9s

Navigating Your Cluster with Kubectl & K9s

When you start working with Kubernetes, the complexity can feel overwhelming. A cluster is a massive network of nodes, pods, services, and configuration files. To manage this ecosystem, you need the right tools. While kubectl is the undisputed, standard command-line interface (CLI) for Kubernetes, a tool called k9s has emerged as the favorite terminal user interface (TUI) for engineers who need to navigate clusters rapidly in real-time.

Mastering both tools ensures you can write reliable deployment scripts with kubectl, while responding instantly to production fires with k9s.

What you will learn
  • The core differences between standard kubectl commands and the k9s interface.
  • How to fetch resources, read logs, and describe pods using kubectl.
  • How to rapidly navigate namespaces and filter resources visually using k9s keyboard shortcuts.
  • How your local terminal authenticates and communicates with the Kubernetes API Server.

1. The Command Line vs. The Terminal UI

Both kubectl and k9s perform the same fundamental job: they read your local authentication file (the `kubeconfig`), convert your commands into secure REST API requests, and send them to the Kubernetes Control Plane. However, they serve very different day-to-day purposes.

Feature / Tool kubectl (The Standard CLI) k9s (The Terminal UI)
Primary Use Case Writing automation scripts, CI/CD pipelines, and declarative infrastructure. Real-time cluster monitoring, fast debugging, and manual log tailing.
Interaction Style Stateless commands. You ask a question, it prints the answer, and exits. Interactive dashboard. Data refreshes automatically every few seconds.
Speed of Navigation kubectl get pods -n backend (Requires typing full flags). Type :pods and press Enter. Navigate with arrow keys.

2. Essential kubectl Commands & k9s Navigation

To be effective, you must know how to investigate a failing application. Let's look at how to achieve this using both tools.

Using kubectl for Diagnostics

With kubectl, you construct sentences indicating the action, the resource, and the namespace.

Example 1: Fetching all pods in a specific namespace

kubectl get pods -n production-api

Expected output

NAME                                READY   STATUS             RESTARTS   AGE
payment-service-5d78c9869d-j6m5b    1/1     Running            0          2d
auth-service-7789f5bc7d-x2q1p       0/1     CrashLoopBackOff   12         15m

When you see an error like CrashLoopBackOff, your next steps with kubectl are to describe the pod for Kubernetes events, and fetch the logs for application errors:

Example 2: Describing a pod and fetching logs

# 1. View Kubernetes system events (ImagePullBackOff, OOMKilled, etc.)
kubectl describe pod auth-service-7789f5bc7d-x2q1p -n production-api

# 2. View the actual application console output
kubectl logs auth-service-7789f5bc7d-x2q1p -n production-api --tail=50
Important: Typing out long pod hashes (like -7789f5bc7d-x2q1p) is error-prone. Use your terminal's autocomplete, or switch to k9s when investigating complex outages.

To start k9s, you simply type k9s in your terminal. It instantly hijacks your terminal window and turns it into a graphical dashboard.

Instead of typing long commands, you use simple keystrokes:

  • : (Colon): Enters command mode. Type :ns to list namespaces, or :deployments to view deployments.
  • / (Forward Slash): Filters the current view. If you have 200 pods, type /auth to instantly filter for the auth service.
  • l (Lowercase L): With your arrow key hovering over a pod, press l to instantly tail its logs.
  • d (Lowercase D): Instantly describes the highlighted resource.
  • esc (Escape): Goes back to the previous screen.

3. Architecture / Workflow Visualized

Whether you use kubectl or k9s, your computer does not talk directly to your application containers. It communicates strictly with the Kubernetes Control Plane.

Kubernetes API Communication Flow

Local Terminal kubectl / k9s REST API Kube API Server (Control Plane) Worker Node 1 kubelet → Pods Worker Node 2 kubelet → Pods

Notice that your terminal never touches the Worker Nodes directly. All interactions are brokered by the API Server, ensuring strict access control and auditing.

4. Step-by-Step Execution Breakdown

When you type kubectl get pods or open k9s, a highly orchestrated security and routing process takes place.

Step 1: Reading Kubeconfig
The tool immediately looks for a file at ~/.kube/config on your local machine. This file contains the IP address of the cluster, your user certificates, and your authentication tokens.
Step 2: API Request via HTTPS
The CLI serializes your command into an HTTP GET request (e.g., GET /api/v1/namespaces/default/pods) and sends it over an encrypted tunnel to the Kube API Server.
Step 3: Cluster Authorization (RBAC)
The API server verifies your token and checks Role-Based Access Control (RBAC) to ensure you are actually allowed to view pods in that specific namespace.
Step 4: Fetching State from etcd
If authorized, the API Server queries the cluster's internal database (etcd) to get the live status of the pods, formats the data, and returns it to your terminal to be displayed.
Watch out: A very common error is The connection to the server localhost:8080 was refused. This simply means your tool cannot find a valid kubeconfig file, or your local cloud provider session (like AWS SSO) has expired. Re-authenticate your terminal!

5. Hands-On Practice

If you have Minikube, Docker Desktop, or a cloud cluster running, open your terminal and try these practical navigation exercises.

Beginner Exercise 1: View all cluster components

Find out what services are running across the entire cluster, regardless of their namespace.

Hint: Use kubectl get all --all-namespaces or type :all inside k9s.

Intermediate Exercise 2: Filter and find crashing pods

Open k9s and find a pod that is crashing, then pull its previous logs to see why it died.

Expected result: Press / to search, type part of the pod name. Highlight the pod, press l for logs, and press p to view the logs of the *previously* crashed container instance.

Advanced Exercise 3: Port-forwarding a database

Your database pod is isolated inside the cluster, but you need to connect to it from your local database client (like DBeaver or pgAdmin).

Hint: In k9s, highlight the database pod and press shift + f to open a port-forward dialog. Or use kubectl port-forward pod/db-pod 5432:5432.

6. Interview Questions

As you progress toward senior roles, interviewers will expect you to understand not just the commands, but the architecture behind them.

Q1. How does kubectl know which cluster to connect to, and how is authentication handled?

Answer: kubectl relies on the kubeconfig file, usually located at ~/.kube/config. This YAML file defines "Contexts" which map a cluster's API endpoint, a specific user (and their authentication tokens/certificates), and a default namespace together. When you run a command, kubectl passes these credentials in the headers of its REST API requests to the API Server.

Q2. We have a pod stuck in "CrashLoopBackOff". Walk me through exactly how you would troubleshoot this.

Answer: First, I would run kubectl describe pod <pod-name> and check the "Events" section at the very bottom. This tells me if Kubernetes is failing to pull the Docker image, or if the node is out of memory (OOMKilled). If the events look fine, it's an application crash. I would then run kubectl logs <pod-name> --previous to read the console output of the container that just died, looking for missing environment variables, database connection timeouts, or unhandled code exceptions.

Conclusion

Relying purely on kubectl for cluster navigation is like walking across a city: it works, but it takes time. Incorporating k9s gives you a real-time dashboard that dramatically reduces the cognitive load of troubleshooting. A senior architect uses declarative kubectl YAML files to deploy applications reliably, and interactive k9s interfaces to debug them efficiently.

Official Documentation

Upgradix by Shrash Studio – Learn Technology. Build Your Future.

Comments & Ratings (0)

Log in or create a student account to comment and rate this article.

Be the first to comment on this article.