What is kubectl and how does it communicate with the cluster?
Quick Answer
kubectl is the command-line tool for interacting with the Kubernetes API server. It reads credentials from ~/.kube/config, sends HTTPS REST requests to the API server, and formats the JSON responses into human-readable output.
Detailed Answer
Think of kubectl like a TV remote control. The remote doesn't contain the TV's brain — it just sends signals (infrared or bluetooth) to the TV telling it what to do: change the channel, adjust the volume, open settings. Similarly, kubectl doesn't contain Kubernetes logic — it just sends HTTP requests to the API server, which is the actual brain of the cluster. You could do everything kubectl does by sending raw curl commands, but kubectl makes it convenient.
When you run a command like kubectl get pods, here's the full chain of events: First, kubectl reads your kubeconfig file (usually at ~/.kube/config) to find three things — the API server's URL (like https://my-cluster.region.eks.amazonaws.com), your authentication credentials (certificates, bearer tokens, or a cloud provider auth plugin), and which namespace to use by default. Then it constructs an HTTPS request — for get pods, that's a GET to /api/v1/namespaces/default/pods — and sends it to the API server.
On the server side, the API server processes the request through a pipeline: Authentication (who are you? — checking certificates or tokens), Authorization (are you allowed to list pods? — checking RBAC policies), Admission Controllers (should this request be modified or rejected? — checking policies like PodSecurityAdmission), and finally the request reaches etcd (the key-value store that holds all cluster state). The response flows back through the same chain, kubectl receives the JSON, and formats it into the table or YAML you see in your terminal.
The most important kubectl commands for daily operations form a natural debugging workflow: kubectl get lists resources with summaries, kubectl describe gives deep detail including events (your first stop when something is wrong), kubectl logs shows container stdout/stderr output, kubectl exec gives you a shell inside a container for live debugging, and kubectl apply -f creates or updates resources from YAML files (the declarative approach used in production). Beyond these, kubectl port-forward tunnels traffic for local testing, kubectl top shows live CPU/memory usage, and kubectl rollout manages deployment updates.
A productivity tip that separates experienced operators from beginners: set up shell aliases and completions. Typing kubectl get pods -n production dozens of times a day is wasteful. With alias k=kubectl, shell tab-completion, and a default namespace set via kubectl config set-context, you can operate 3x faster. For CKA/CKAD exams, this time savings is the difference between finishing and running out of time.