Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Register a Kubernetes workload with SPIRE

After this lesson: I can register a Kubernetes workload so that its pods, and only they, get its SVID.

75 min lesson9 commandsWorkload identity lab: SPIFFE and SPIRE on k3s, with mTLS between services, on a private Ubuntu VM

A pod with a name that keeps changing

Registering a Kubernetes workload with SPIRE means writing an entry that picks its pods by what Kubernetes says about them. A pod's name changes every time it is replaced.

So the entry must pick something that stays: the namespace and the service account. Then every new pod of the workload gets the same identity, and no other pod does.

By the end of this lesson

Note

I can register a Kubernetes workload so that its pods, and only they, get its SVID.

Key words

WordWhat it meansExample
Namespacea named group of Kubernetes objectsdemo
Service accountthe identity Kubernetes gives a podprinter
Entrya SPIFFE ID, a parent ID and selectors
hostPatha folder of the node mounted into a pod
Workload APIthe agent's socket that hands out SVIDs

The cells students fill in are left blank in this preview.

A pod reaches the agent through a socket

The agent runs on the node, outside the cluster. A pod reaches it through the Workload API socket, mounted into the pod as a hostPath folder. In this lab the spire-agent program is mounted too, so a busybox pod can ask.

The two mounts below are in every pod of this lab. In production a CSI driver often does the same job.

The two mounts a pod needs (an example)

Code: bash
volumeMounts:
  - { name: spire-socket, mountPath: /tmp/spire-agent/public, readOnly: true }
  - { name: spire-bin, mountPath: /opt/spire/bin, readOnly: true }
volumes:
  - { name: spire-socket, hostPath: { path: /tmp/spire-agent/public, type: Directory } }
  - { name: spire-bin, hostPath: { path: /opt/spire/bin, type: Directory } }

Make the namespace, the account and the pods

The next Runs make the namespace demo and the service account printer. Then spirelab pods applies two Deployments: printer runs under that account, and copier under the namespace's default one.

The two rollout Runs wait until each pod is ready and end with successfully rolled out. This part needs k3s, so a 2 GB machine.

Make the namespace demo

Command
kubectl create namespace demo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Make the service account printer

Command
kubectl create serviceaccount printer -n demo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Start the printer and copier pods

Command
spirelab pods

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Wait for printer

Command
kubectl rollout status deployment/printer -n demo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Wait for copier

Command
kubectl rollout status deployment/copier -n demo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

An entry picks the namespace and the account

The entry gives spiffe://lab.test/ns/demo/sa/printer to any pod with both k8s:ns:demo and k8s:sa:printer. Naming the ID after the namespace and account is a common habit, so the ID says where it came from.

Wait a few seconds after it, then ask from inside each pod.

Register the printer workload

Command
sudo spire-server entry create -parentID spiffe://lab.test/spire/agent/x509pop/spire-lab -spiffeID spiffe://lab.test/ns/demo/sa/printer -selector k8s:ns:demo -selector k8s:sa:printer

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Ask for an SVID inside printer

Command
kubectl exec -n demo deploy/printer -- /opt/spire/bin/spire-agent api fetch x509

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Ask for an SVID inside copier

Command
kubectl exec -n demo deploy/copier -- /opt/spire/bin/spire-agent api fetch x509

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

One pod got an identity, one did not

Inside printer the Run prints SPIFFE ID: spiffe://lab.test/ns/demo/sa/printer. Inside copier it prints no identity issued, and kubectl adds command terminated with exit code 1.

Both pods reach the same socket. Only the attested facts differ: copier runs under the account default.

A common mistake

Warning

Many people register a pod by its name because that is what kubectl shows. The name of a Deployment's pod changes whenever it is replaced, so the new pod gets nothing. Pick the namespace and service account, which stay.

Delete the namespace demo

Command
kubectl delete namespace demo

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

About this lab and its source: Workload identity lab: SPIFFE and SPIRE on k3s, with mTLS between services, on a private Ubuntu VM

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.