Section 1: How Kubernetes thinks
A local cluster with kind and kubectl
To learn Kubernetes you need a cluster you can break freely. kind (Kubernetes in Docker) runs each node as a Docker container, which makes clusters cheap to create and delete.
Install the tools
# macOS with Homebrew; see the official docs for other platforms
brew install kind kubectl
Create a cluster
A small config gives us one control plane node and two workers, and maps ports so we can reach the cluster from the browser later:
# kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30080
hostPort: 8080
- role: worker
- role: worker
kind create cluster --name course --config kind-config.yaml
kubectl get nodes
You should see three nodes in the Ready state after a minute.
How kubectl finds the cluster
kubectl reads a kubeconfig file, by default ~/.kube/config. It contains clusters, users and contexts, which combine a cluster with credentials and a default namespace. kind added a context called kind-course.
kubectl config get-contexts
kubectl config current-context
Checking the current context before running commands becomes a lifelong habit once you work with real clusters. Applying a test manifest to production because you were in the wrong context is a rite of passage you can skip.
Namespaces
Namespaces divide a cluster into named areas for teams or applications. Create one for the course:
kubectl create namespace shop
kubectl config set-context --current --namespace=shop
Commands worth memorising
kubectl get pods # list objects of a kind
kubectl get pods -o wide # with node and IP
kubectl describe pod <name> # details and recent events
kubectl logs <pod> # container output
kubectl apply -f file.yaml # create or update from a manifest
kubectl delete -f file.yaml # remove what the file describes
kubectl explain deployment.spec # built in documentation for any field
kubectl explain is underused. It documents every field of every object straight from the API server, matching your cluster's version exactly.
When you are done for the day, kind delete cluster --name course frees all resources.