Skip to content
$ lynuxpro
← Kubernetes

LESSON 2 OF 25 · BEGINNER · 18 MIN

Setting up a cluster and kubectl

Free previewLesson 2 of the 2 free lessons in this Pro course.Unlock all 25

HOW IT WORKSHow kubectl finds and talks to a cluster

readYour laptopkubectlkubeconfig~/.kube/config
1/5

kubectl is only a client. It reads ~/.kube/config to learn which clusters exist and how to log in to them.

$ kubectl config get-contexts

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

OptionRuns whereBest forCost
kind (Kubernetes in Docker)Containers on your laptopLearning, CI testsFree
minikubeVM or container on your laptopLearning, add-onsFree
k3d / k3sLightweight KubernetesEdge, small servers, laptopsFree
Docker Desktop / OrbStackBuilt-in single-node clusterQuick local testingFree for personal use
kubeadmYour own VMsLearning internals, CKA examVM cost
EKS / AKS / GKECloud, control plane managed for youProductionControl plane fee plus nodes

A local cluster with kind

kind create cluster --name devkubectl get nodes
SHELLbashutf-8 · 2L

A multi-node cluster is one config file away:

# kind-3-nodes.yamlkind: ClusterapiVersion: kind.x-k8s.io/v1alpha4nodes:  - role: control-plane  - role: worker  - role: worker
NORMALyamlutf-8 · 7L
kind create cluster --name dev --config kind-3-nodes.yamlkind delete cluster --name dev
OUTPUTterminalutf-8 · 2L

A 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-1
OUTPUTterminalutf-8 · 2L

In 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/
OUTPUTterminalutf-8 · 5L

Keep kubectl within one minor version of the cluster (a 1.36 kubectl works with 1.35, 1.36 and 1.37 clusters).

kubectl version
SHELLbashutf-8 · 1L

kubeconfig: how kubectl knows where to go

kubectl reads ~/.kube/config (or the files in the KUBECONFIG variable). It contains three lists:

SectionHolds
clustersAPI server URLs and CA certificates
usersCredentials: client certificates, tokens, or a command that fetches a token (EKS uses aws eks get-token)
contextsA named pair of cluster + user (+ default namespace)
kubectl config get-contextskubectl config current-contextkubectl config use-context kind-dev
SHELLbashutf-8 · 3L
CURRENT   NAME              CLUSTER           AUTHINFO          NAMESPACE*         kind-dev          kind-dev          kind-dev          arn:aws:eks:...   arn:aws:eks:...   arn:aws:eks:...   payments
OUTPUTterminalutf-8 · 3L

Set a default namespace for the current context so you stop typing -n:

kubectl config set-context --current --namespace=payments
OUTPUTterminalutf-8 · 1L

Always 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
OUTPUTterminalutf-8 · 2L
VerbUse
getList resources
describeDetailed view plus recent events
apply -fCreate or update from YAML (declarative)
create, run, exposeCreate quickly from the command line (imperative)
deleteRemove
logs, exec, port-forwardWork with running pods
explainField 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 wide
SHELLbashutf-8 · 2L
kubectl 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 changes
OUTPUTterminalutf-8 · 5L

Generate YAML instead of writing it

kubectl create deployment web --image=nginx:1.27 --replicas=3 --dry-run=client -o yaml > web.yaml
OUTPUTterminalutf-8 · 1L

This 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)
OUTPUTterminalutf-8 · 3L

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 -n and wondering why nothing is found (you are looking in default).
  • 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

  1. Create a kind cluster and list its nodes.
  2. Show all contexts, switch between two, and set a default namespace.
  3. Generate a Deployment YAML with --dry-run=client -o yaml without creating it.
  4. Set up the k alias with completion.

Cheat sheet

CommandWhat it does
kind create cluster --name NLocal cluster
kubectl config get-contextsList contexts
kubectl config use-context CSwitch cluster
kubectl config set-context --current --namespace=NSDefault namespace
kubectl get pods -o widePods with node and IP
--dry-run=client -o yamlGenerate 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