$ skip to content
devresurge

k8s-internals · chapter 22 of 22

21 — CSI deep dive: attach, mount, and the sidecar circus

Chapters
  1. Kubernetes Internals — From Linux Up (Book Length)
  2. 01 — Linux processes: the real unit of work
  3. 02 — Namespaces: walls, not VMs
  4. 03 — cgroups: budgets, not walls
  5. 04 — Security primitives: least privilege at the kernel
  6. 05 — OCI images: suitcases of files + config
  7. 06 — containerd & CRI: who actually starts containers
  8. 07 — Pods: shared fate, shared network namespace
  9. 08 — kubelet: the node babysitter
  10. 09 — API server & etcd: the desired-state store
  11. 10 — Controllers: reconcile forever
  12. 11 — Scheduler: who goes where
  13. 12 — Networking: Pod IP → Service → world
  14. 13 — Ingress & Gateway: the L7 front door
  15. 14 — Storage: disks that outlive pods
  16. 15 — Cluster security: trust on the API and the wire
  17. 16 — Troubleshoot: debug like an owner
  18. 17 — etcd deep dive: the notebook behind the librarian
  19. 18 — Controllers deep dive: informers, workqueues, and level triggers
  20. 19 — CNI deep dive: plugging Pods into the network
  21. 20 — eBPF dataplane: services and policy in the kernel
  22. 21 — CSI deep dive: attach, mount, and the sidecar circus
21 — CSI deep dive: attach, mount, and the sidecar circus
Prerequisite: 14 — Storage

ELI5 hook

Chapter 14 said CSI is the moving company. This chapter is the warehouse tour: which workers do CreateVolume, which do ControllerPublish (attach), which do NodeStage/NodePublish (mount into the Pod), and why Kubernetes runs a sidecar band next to every driver.

Mental model

CSI splits responsibilities:

RPC / stepWhereMeaning
CreateVolume / DeleteVolumeControllerProvision/delete the disk/share
ControllerPublish / UnpublishControllerAttach/detach to a node
NodeStageNodeFormat/mount to a global host path once
NodePublishNodeBind-mount into the Pod’s mount namespace
NodeUnpublish / UnstageNodeReverse on teardown
sequenceDiagram
  participant PVC
  participant ExtProv as external-provisioner
  participant Driver as CSI driver
  participant ExtAtt as external-attacher
  participant Kubelet
  PVC->>ExtProv: provision
  ExtProv->>Driver: CreateVolume
  ExtAtt->>Driver: ControllerPublish
  Kubelet->>Driver: NodeStage
  Kubelet->>Driver: NodePublish

VolumeAttachment API objects track attach state. Stuck attachments = Pods pending with FailedAttachVolume / TimedOut.

The sidecar circus (why so many containers?)

Out-of-tree drivers ship as Deployments/DaemonSets with helper containers:

SidecarJob
external-provisionerWatch PVC → call CreateVolume → bind PV
external-attacherWatch VolumeAttachment → ControllerPublish
external-resizerPVC size ↑ → ControllerExpand + NodeExpand
external-snapshotterVolumeSnapshot → CreateSnapshot
node-driver-registrarTell kubelet the driver socket exists
livenessprobeRestart unhealthy driver

The driver implements gRPC CSI; sidecars implement Kubernetes glue so every vendor doesn’t rewrite the same controllers.

What Linux does on the node

After attach, the block device (or NFS) appears on the node. Then:

  1. NodeStage — mount to /var/lib/kubelet/plugins/kubernetes.io/csi/... (global)
  2. NodePublish — bind into /var/lib/kubelet/pods/<uid>/volumes/kubernetes.io~csi/...
  3. That path is bind-mounted into the container mount namespace at volumeMounts.mountPath
# On a node with a mounted CSI volume (names vary):
find /var/lib/kubelet/pods -name 'mount' 2>/dev/null | head
mount | grep -i csi | head

ReadWriteOnce attach is per-node: scheduler + attacher must agree. Multi-attach errors mean two nodes fighting one RWO volume.

Topology & scheduling

StorageClasses can set WaitForFirstConsumer so the volume is provisioned in the zone where the Pod actually schedules — avoiding “disk in zone A, Pod in zone B.”

volumeBindingMode: WaitForFirstConsumer
allowedTopologies: ...

Lab

./courses/k8s-internals/labs/21-csi-inspect.sh

Manual:

kubectl get csidrivers
kubectl get csinodes -o wide
kubectl get storageclass
kubectl get volumeattachment

# Dynamic PVC lifecycle (needs a default StorageClass — kind usually has one)
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: csi-lab-pvc
spec:
  accessModes: ["ReadWriteOnce"]
  resources:
    requests:
      storage: 100Mi
---
apiVersion: v1
kind: Pod
metadata:
  name: csi-lab-pod
spec:
  containers:
    - name: app
      image: busybox:1.36
      command: ["sh", "-c", "echo hi > /data/x; sleep 3600"]
      volumeMounts:
        - name: data
          mountPath: /data
  volumes:
    - name: data
      persistentVolumeClaim:
        claimName: csi-lab-pvc
EOF

kubectl wait --for=condition=Ready pod/csi-lab-pod --timeout=120s || \
  kubectl describe pod csi-lab-pod | tail -40
kubectl get pvc csi-lab-pvc
kubectl get volumeattachment
kubectl exec csi-lab-pod -- cat /data/x
kubectl delete pod csi-lab-pod pvc csi-lab-pvc

Gotchas

  • Stuck Terminating PVC/Pod — VolumeAttachment or finalizers; don’t force-delete blindly in prod.
  • Expand without ExpandCSIVolumes — Feature/driver/sidecar must all support resize.
  • Snapshots ≠ backups — Crash-consistent ≠ application-consistent; know your DB story.
  • Privileged node plugins — Drivers often need elevated privileges; audit them.
  • Filesystem vs Block — volumeMode: Block skips FS mount; apps must handle raw devices.

Checkpoint

CSI = gRPC driver + Kubernetes sidecars implementing provision → attach → stage → publish into Linux mount namespaces.

You now have book-depth on etcd, controllers, CNI, eBPF, and CSI.


Quiz: /quizzes/k8s-deep-dives/

Milestone: pass deep dives + the five core K8s quizzes → Cluster Scholar.

Back to the syllabus.