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
| Word | What it means | Example |
|---|---|---|
| Namespace | a named group of Kubernetes objects | demo |
| Service account | the identity Kubernetes gives a pod | printer |
| Entry | a SPIFFE ID, a parent ID and selectors | |
| hostPath | a folder of the node mounted into a pod | |
| Workload API | the 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)
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
kubectl create namespace demoShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the service account printer
kubectl create serviceaccount printer -n demoShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start the printer and copier pods
spirelab podsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Wait for printer
kubectl rollout status deployment/printer -n demoShown here, not run. Press Start lab to type it into your own lab’s terminal.
Wait for copier
kubectl rollout status deployment/copier -n demoShown 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
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:printerShown here, not run. Press Start lab to type it into your own lab’s terminal.
Ask for an SVID inside printer
kubectl exec -n demo deploy/printer -- /opt/spire/bin/spire-agent api fetch x509Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Ask for an SVID inside copier
kubectl exec -n demo deploy/copier -- /opt/spire/bin/spire-agent api fetch x509Shown 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
kubectl delete namespace demoShown here, not run. Press Start lab to type it into your own lab’s terminal.