Before you can learn Kubernetes you need a cluster to practise on and kubectl, the command-line tool that talks to it. This lesson covers the main ways to get a cluster (local, managed and self-built), how kubectl decides which cluster to talk to, and the kubectl habits that make you fast.
In simple words
- Managed cluster: a cluster where the cloud provider (EKS, AKS, GKE) runs the control plane for you, so you only look after your apps and nodes.
- Local cluster: a small practice cluster on your laptop (kind, minikube, k3d). Free and safe to break.
- kubeconfig: the file (usually ~/.kube/config) that tells kubectl which cluster to talk to and how to log in.
- Context: a saved combination of cluster, user and namespace inside kubeconfig, so you can switch between clusters with one command.
Ways to get a cluster
| Option | Runs where | Best for | Cost |
|---|---|---|---|
| kind (Kubernetes in Docker) | Containers on your laptop | Learning, CI tests | Free |
| minikube | VM or container on your laptop | Learning, add-ons | Free |
| k3d / k3s | Lightweight Kubernetes | Edge, small servers, laptops | Free |
| Docker Desktop / OrbStack | Built-in single-node cluster | Quick local testing | Free for personal use |
| kubeadm | Your own VMs | Learning internals, CKA exam | VM cost |
| EKS / AKS / GKE | Cloud, control plane managed for you | Production | Control plane fee plus nodes |
A local cluster with kind
kind create cluster --name devkubectl get nodesA multi-node cluster is one config file away:
# kind-3-nodes.yamlkind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes: - role: control-plane - role: worker - role: workerkind create cluster --name dev --config kind-3-nodes.yamlkind delete cluster --name devA managed cluster on AWS
eksctl create cluster --name demo --region ap-south-1 --nodes 2 --node-type t3.mediumaws eks update-kubeconfig --name demo --region ap-south-1In real companies clusters are created with Terraform (covered in the Terraform and AWS courses), not by hand.
Installing kubectl
# macOSbrew install kubectl# Linuxcurl -LO "https://dl.k8s.io/release/$(curl -Ls https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"sudo install kubectl /usr/local/bin/Keep kubectl within one minor version of the cluster (a 1.36 kubectl works with 1.35, 1.36 and 1.37 clusters).
kubectl versionkubeconfig: how kubectl knows where to go
kubectl reads ~/.kube/config (or the files in the KUBECONFIG variable). It contains three lists:
| Section | Holds |
|---|---|
clusters | API server URLs and CA certificates |
users | Credentials: client certificates, tokens, or a command that fetches a token (EKS uses aws eks get-token) |
contexts | A named pair of cluster + user (+ default namespace) |
kubectl config get-contextskubectl config current-contextkubectl config use-context kind-devCURRENT NAME CLUSTER AUTHINFO NAMESPACE* kind-dev kind-dev kind-dev arn:aws:eks:... arn:aws:eks:... arn:aws:eks:... paymentsSet a default namespace for the current context so you stop typing -n:
kubectl config set-context --current --namespace=paymentsAlways check your context before running destructive commands. Many production incidents start with "I thought I was on staging". Tools like kubectx/kubens and a prompt that shows the context help a lot.
kubectl command structure
kubectl VERB RESOURCE NAME FLAGSkubectl get pods web-1 -n shop -o wide| Verb | Use |
|---|---|
get | List resources |
describe | Detailed view plus recent events |
apply -f | Create or update from YAML (declarative) |
create, run, expose | Create quickly from the command line (imperative) |
delete | Remove |
logs, exec, port-forward | Work with running pods |
explain | Field documentation from the cluster itself |
Short names save time: po (pods), deploy, svc, ns, cm, pvc, ing, no (nodes).
Output formats
kubectl get pods -o widekubectl get nodes -o widekubectl get pod web -o yaml # the full objectkubectl get pods -o name # just nameskubectl get pods -o jsonpath='{.items[*].metadata.name}'kubectl get pods --sort-by=.status.startTimekubectl get pods -w # watch for changesGenerate YAML instead of writing it
kubectl create deployment web --image=nginx:1.27 --replicas=3 --dry-run=client -o yaml > web.yamlThis is the fastest way to start a manifest, and essential for the CKA exam.
Productivity setup
alias k=kubectlsource <(kubectl completion zsh) # or bashcomplete -o default -F __start_kubectl k # completion for the alias (bash)Popular helpers: k9s (terminal UI), kubectx / kubens (switch context and namespace), stern (tail logs from many pods), krew (kubectl plugin manager).
Real-world scenario
An engineer meant to delete a test namespace on a kind cluster but their context still pointed at production from an earlier session. The namespace was deleted in production. Afterwards the team set production contexts to require a separate kubeconfig file, added the context to everyone's shell prompt, and restricted delete permissions on production through RBAC.
Common mistakes
- Wrong context. Check
kubectl config current-context. - Forgetting
-nand wondering why nothing is found (you are looking indefault). - Hand-writing YAML from scratch instead of generating it with
--dry-run=client -o yaml. - Sharing one admin kubeconfig across a team instead of individual identities.
Practice
- Create a kind cluster and list its nodes.
- Show all contexts, switch between two, and set a default namespace.
- Generate a Deployment YAML with
--dry-run=client -o yamlwithout creating it. - Set up the
kalias with completion.
Cheat sheet
| Command | What it does |
|---|---|
kind create cluster --name N | Local cluster |
kubectl config get-contexts | List contexts |
kubectl config use-context C | Switch cluster |
kubectl config set-context --current --namespace=NS | Default namespace |
kubectl get pods -o wide | Pods with node and IP |
--dry-run=client -o yaml | Generate YAML |
Key takeaways
- kind or minikube for learning, managed EKS/AKS/GKE for production, kubeadm for understanding internals.
- kubeconfig contexts decide which cluster kubectl talks to; always check before destructive commands.
- Generate YAML with dry runs and use aliases, completion and k9s to work quickly.
You've finished the free preview of Kubernetes
Upgrade to Pro to unlock the remaining 23 lessons, every practice lab and a certificate when you finish.
See plans