Tech Notes

Series Kubernetes & Helm from scratch · Part 2

Minikube setup and your first deployment

Start a local Minikube cluster, create the namespaces for the project, and deploy, probe, expose and clean up your first workload.

In part 1 we built the mental model: a control plane that decides, nodes that run pods, and objects such as Deployments and Services that describe what we want. Now we need a real cluster to try it on. Paying for a cloud cluster while learning is unnecessary, so we’ll run everything on a laptop with Minikube.

By the end of this part we’ll have a running single-node cluster, the three namespaces the project uses, the ingress and metrics addons switched on, and a first workload deployed with labels, resource settings and health probes. Then we’ll tear it all down again, because being able to clean up confidently matters as much as deploying.

The setup here is macOS on Apple Silicon, but every kubectl command is identical on Linux and Windows.

Install the tools

We need three command-line tools and a container runtime:

  1. Docker Desktop (or another Docker engine) Minikube runs the whole cluster inside a container, so Docker must be running first.
  2. minikube Creates and manages the local cluster.
  3. kubectl The client that talks to the cluster's API server.
  4. helm Not needed until part 3, but installing it now means the checkpoint at the end covers everything.

With Homebrew:

brew install minikube kubectl helm

Check each one answers:

minikube version
kubectl version --client
helm version

Start the cluster

minikube start --driver=docker --cpus=4 --memory=8192
  • --driver=docker runs the Minikube node as a Docker container. On Apple Silicon this is the simplest driver that just works.
  • --cpus and --memory set what the node can use. Later in the series we’ll run PostgreSQL, Redis, a JVM and the Prometheus stack together, so 4 CPUs and 8 GB is a sensible starting point. Docker Desktop’s own resource limit must be at least this high, or the start will fail.

Minikube also writes a context into ~/.kube/config and switches kubectl to it. That matters: kubectl talks to whichever cluster the current context points at, so if you also use a work cluster, check before you run anything destructive:

kubectl config current-context   # should print: minikube
minikube status
kubectl get nodes

minikube status should show the host, kubelet and apiserver as Running, and kubectl get nodes should list a single node called minikube with status Ready. That one node is acting as both control plane and worker.

To see the control plane components from part 1 running as ordinary pods:

kubectl get pods -n kube-system

You’ll find etcd-minikube, kube-apiserver-minikube, kube-scheduler-minikube, kube-controller-manager-minikube, kube-proxy and CoreDNS. Kubernetes runs a lot of itself on Kubernetes.

Create the project namespaces

A new cluster comes with default, kube-system, kube-public and kube-node-lease. We won’t put our apps in default; the project separates stateless apps, stateful data and monitoring. Create k8s/namespaces.yml:

apiVersion: v1
kind: Namespace
metadata:
  name: apps
  labels:
    purpose: applications
---
apiVersion: v1
kind: Namespace
metadata:
  name: data
  labels:
    purpose: stateful-services
---
apiVersion: v1
kind: Namespace
metadata:
  name: monitoring
  labels:
    purpose: observability

The --- lines separate several objects in one file. The labels aren’t required, but they let us select namespaces later (network policies, for example, can match on namespace labels). Apply it:

kubectl apply -f k8s/namespaces.yml
kubectl get ns

kubectl apply is declarative: it compares the file with what’s in the cluster and creates or updates whatever differs. Running it twice is safe, which is exactly what we want from anything kept in Git.

Enable the addons

Minikube ships optional components as addons. Two are needed for this series:

minikube addons enable ingress
minikube addons enable metrics-server
minikube addons list | grep -E 'ingress|metrics-server'
  • ingress installs the NGINX Ingress controller into the ingress-nginx namespace. An Ingress object on its own does nothing; it needs a controller to read it and route traffic. We use it from part 11.
  • metrics-server collects CPU and memory usage from each kubelet. It powers kubectl top and, later, the Horizontal Pod Autoscaler.

Give metrics-server a minute to collect its first sample, then try kubectl top nodes.

Deploy hello-world, properly

The quickest possible deployment is a one-liner such as kubectl create deployment. It’s fine for a smoke test, but it hides everything we need to learn, so let’s write the manifest ourselves. We’ll use kicbase/echo-server, a tiny image the Minikube docs use, which answers HTTP on port 8080 and echoes back the request. Save this as k8s/hello/deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
  namespace: apps
  labels:
    app: hello
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello            # which pods this Deployment owns
  template:
    metadata:
      labels:
        app: hello          # must match the selector above
    spec:
      containers:
        - name: hello
          image: kicbase/echo-server:1.0
          ports:
            - name: http
              containerPort: 8080
          resources:
            requests:
              cpu: 50m
              memory: 32Mi
            limits:
              cpu: 200m
              memory: 64Mi
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 2
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 10
            periodSeconds: 10
            failureThreshold: 3

There’s a lot in there, so let’s go through the parts that aren’t obvious.

Labels and selectors

spec.selector.matchLabels tells the Deployment (and its ReplicaSet) which pods belong to it. spec.template.metadata.labels is stamped on every pod it creates. They must match, and the API server rejects the Deployment if they don’t. The selector is also immutable after creation: change it later and kubectl apply fails. Keep selector labels small and stable, and put anything that changes, such as a version, in other labels.

The named port http lets the probes and, shortly, the Service refer to the port by name instead of repeating 8080.

Requests and limits

A container's resource range: the request is reserved and used for scheduling, and the limit is a ceiling where CPU is throttled and memory over the limit is killed. CPU request 50m: reserved, used by the scheduler limit 200m above this: throttled Memory request 32Mi: reserved, used by the scheduler limit 64Mi above this: OOMKilled Scheduler: "does this node have 50m CPU and 32Mi left unreserved?" It looks only at requests, never at actual usage or limits.
Requests decide where a pod can run. Limits decide what happens when it uses too much: CPU slows down, memory gets the container killed.
  • A request is what the container is guaranteed. The scheduler adds up requests on each node and only places a pod where its requests fit. 50m means 50 millicores, a twentieth of a CPU core.
  • A limit is the ceiling. CPU is compressible, so a container that wants more than its CPU limit is throttled and simply runs slower. Memory isn’t compressible, so a container that goes over its memory limit is killed (you’ll see OOMKilled as the reason) and restarted.

Without requests, the scheduler is guessing, and a busy node can starve your pods. Without memory limits, one leaking container can take a whole node down. We’ll set both on everything in this series, and revisit sizing for the JVM in part 9.

Readiness and liveness probes

A Service sends traffic only to the ready pod; a pod failing readiness is skipped, and a pod failing liveness is restarted by the kubelet. Service helloapp=hello Pod Aready, live Pod Breadiness failing Pod Cliveness failing receives traffic removed from endpoints,left running, re-added when ready container restartedby the kubelet on its node
Readiness controls traffic; liveness controls restarts. Mixing them up is how a slow dependency turns into a restart loop.

The kubelet runs both probes against every container, but it does different things when they fail:

  • Readiness answers “should this pod get traffic right now?”. While it fails, the pod is taken out of the Service’s endpoints, so no requests reach it, but nothing is restarted. Use it for start-up and for temporary unavailability.
  • Liveness answers “is this process stuck beyond repair?”. After failureThreshold failures in a row, the kubelet kills and restarts the container.

That’s why the liveness probe above starts later and is more forgiving than the readiness probe. A liveness probe that fires while the app is still starting causes an endless restart loop, which is a classic first-week Kubernetes problem. For Spring Boot in part 9 we’ll point these at separate Actuator endpoints for exactly this reason.

Apply the Deployment and watch it come up:

kubectl apply -f k8s/hello/deployment.yml
kubectl get pods -n apps -l app=hello -w

-l app=hello filters by label, the same selector the Deployment uses. -w watches for changes; press Ctrl+C once both pods show 1/1 Running. 1/1 is the readiness probe at work: one of one containers is ready.

Gotcha: forget -n apps and kubectl get pods shows the default namespace and reports nothing. To avoid typing it every time, run kubectl config set-context --current --namespace=apps.

Expose it with a Service

Pods come and go, and so do their IP addresses. A Service gives them one stable name. Save k8s/hello/service.yml:

apiVersion: v1
kind: Service
metadata:
  name: hello
  namespace: apps
spec:
  type: ClusterIP
  selector:
    app: hello           # send traffic to pods with this label
  ports:
    - name: http
      port: 80           # the port the Service listens on
      targetPort: http   # the named container port (8080)
kubectl apply -f k8s/hello/service.yml
kubectl get svc,endpoints -n apps

The endpoints list shows the IPs of the two ready pods. Inside the cluster, any pod can now reach them at http://hello.apps.svc.cluster.local, or just http://hello from the same namespace.

ClusterIP is the default Service type and is reachable only from inside the cluster. That’s what we want for almost everything in this project; only the Ingress will face the outside world. To test from the laptop, use port-forwarding:

kubectl port-forward -n apps svc/hello 8080:80
# in another terminal:
curl http://localhost:8080/
kubectl port-forward tunnels localhost:8080 through the API server to a pod behind the ClusterIP Service. curllocalhost:8080 kubectlport-forward Minikube cluster API servertunnel Pod:8080 Podnot used
Port-forwarding picks one pod behind the Service and tunnels to it through the API server. Handy for testing; not load-balanced and not for real traffic.

Two things to know about port-forward. It picks one pod when it starts and sticks with it, so it doesn’t load-balance and breaks if that pod is replaced. And it only lasts as long as the command runs. It’s a debugging tool, not a way to publish an app. Minikube also offers minikube service hello -n apps, which opens a tunnel to a Service in your browser, but we’ll use Ingress for real access later.

See the reconciliation loop

Delete a pod and watch the Deployment replace it:

kubectl delete pod -n apps -l app=hello --wait=false
kubectl get pods -n apps -l app=hello

New pods with new names appear straight away, because the ReplicaSet saw fewer pods than desired. Scale up, and check resource usage with metrics-server:

kubectl scale deployment hello -n apps --replicas=3
kubectl top pods -n apps
kubectl describe deployment hello -n apps

kubectl describe is the first command to reach for when something looks wrong: the Events section at the bottom shows scheduling decisions, image pulls and probe failures in plain language.

Clean up

Delete by file, so you remove exactly what you created:

kubectl delete -f k8s/hello/

Keep the namespaces; everything from part 3 onwards uses them. When you’re done for the day, minikube stop frees the CPU and memory while keeping the cluster’s state, and minikube start brings it back. minikube delete destroys the cluster entirely, which is the fastest way to start again from scratch.

Checkpoint

Before moving on, these should all succeed:

minikube status
kubectl get nodes          # one node, Ready
kubectl get ns             # apps, data, monitoring listed
minikube addons list | grep -E 'ingress|metrics-server'   # both enabled
helm version

Takeaways

  • Minikube runs a real single-node cluster in Docker; check kubectl config current-context before running anything.
  • Namespaces in a file applied with kubectl apply are repeatable and belong in Git.
  • Selector labels must match the pod template and can’t change later, so keep them small and stable.
  • Requests drive scheduling; limits throttle CPU and kill containers that exceed memory.
  • Readiness controls traffic, liveness controls restarts. Make liveness the more forgiving of the two.
  • ClusterIP keeps Services internal; port-forward is for testing, not for serving traffic.

Next, in part 3, we stop hand-writing YAML for every environment and build our first Helm chart.