Lesson 95 · Section 8: Storage
Lesson 95: Container Storage Interface: how Kubernetes talks to storage
CSI is the standard that lets any storage system plug into Kubernetes without changing Kubernetes. What the interface contains, which Pods make up a CSI driver, what happens from a claim to a mounted volume, and where to look when a volume will not attach. · 16 min read · 4 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
What this lesson is about
The last two lessons showed storage in Docker: a volume belongs to a driver, and a plugin can put that volume on external storage. Kubernetes needs the same thing for a whole cluster, with hundreds of nodes and many storage vendors. Its answer is the Container Storage Interface, CSI.
You will not write a CSI driver, and in the exam you will not install one from scratch. But every dynamically provisioned volume in a modern cluster passes through CSI, and when a Pod hangs with a volume that will not attach, you need to know which part to look at. The CKA curriculum lists CSI next to CRI and CNI as an extension interface you must understand.
Why it exists
Kubernetes first supported storage with in-tree volume plugins: Go code for AWS EBS, GCE Persistent Disk,
Azure Disk, Ceph, GlusterFS and many others, compiled into kube-controller-manager and the kubelet. You can
still see the traces in the API, for example the awsElasticBlockStore field of a volume.
That design had four problems:
- Release coupling. A vendor could ship a fix only with a Kubernetes release, and users got it only after a cluster upgrade.
- Stability. A bug in one plugin could crash the kubelet or the controller manager for everybody.
- Security. Third-party code ran with the rights of core components.
- Maintenance. Kubernetes maintainers had to test and review code for storage systems they could not access.
Kubernetes had already solved this problem for two other areas with a standard interface.
Parts:
- Kubernetes (kubelet and controllers)
- CRI (run containers)
- CNI (connect Pods)
- CSI (provide volumes)
- containerd, CRI-O
- Calico, Cilium, Flannel
- EBS, Ceph, NFS, Longhorn (one driver each)
Connections:
- Kubernetes to CRI
- Kubernetes to CNI
- Kubernetes to CSI
- CRI to containerd, CRI-O: plugs in
- CNI to Calico, Cilium, Flannel: plugs in
- CSI to EBS, Ceph, NFS, Longhorn: plugs in
| Interface | Kubernetes asks | Implementations |
|---|---|---|
| CRI, Container Runtime Interface | "Pull this image, start this container" | containerd, CRI-O |
| CNI, Container Network Interface | "Connect this Pod to the network" | Calico, Cilium, Flannel |
| CSI, Container Storage Interface | "Create this volume, attach it, mount it" | One driver per storage system |
CSI is not a Kubernetes standard alone. The specification was written together with other orchestrators, such as Mesos, Nomad and Cloud Foundry, so one driver works with all of them. CSI has been stable in Kubernetes since version 1.13.
What the interface contains
CSI is a set of remote procedure calls, defined with gRPC and served by the driver on a Unix socket. They are grouped in three services.
| Service | Runs | Main calls | Purpose |
|---|---|---|---|
| Identity | Everywhere | GetPluginInfo, GetPluginCapabilities, Probe | Name, version and abilities of the driver, health |
| Controller | Once per cluster | CreateVolume, DeleteVolume, ControllerPublishVolume, ControllerUnpublishVolume, ControllerExpandVolume, CreateSnapshot | Work done through the storage system's API, from anywhere |
| Node | On every node | NodeStageVolume, NodeUnstageVolume, NodePublishVolume, NodeUnpublishVolume, NodeExpandVolume, NodeGetInfo | Work that must happen on the machine where the Pod runs |
The words are worth translating once:
| CSI call | Plain meaning |
|---|---|
CreateVolume | Provision: create a 10 GiB disk in the storage system |
ControllerPublishVolume | Attach: connect that disk to the virtual machine node-1 |
NodeStageVolume | Format the device if it is empty and mount it once at a node-wide directory |
NodePublishVolume | Mount: bind-mount it into the directory of one Pod |
Many calls are optional. An NFS driver has nothing to attach, so it does not implement
ControllerPublishVolume, and Kubernetes skips that step. A driver reports what it can do through capability
calls.
How a driver is deployed
A CSI driver is ordinary software running in Pods, usually in kube-system or in a namespace of its own. It has
two halves.
Parts:
- kube-apiserver (PVC, PV, VolumeAttachment)
- external-provisioner (sidecar: watches PVCs), in Controller plugin
- external-attacher (sidecar: VolumeAttachments), in Controller plugin
- CSI driver (controller service), in Controller plugin
- Storage system (cloud API, Ceph, array)
- kubelet (on each node)
- CSI driver (node service), in Node plugin
- Pod volume directory (under /var/lib/kubelet/pods)
Connections:
- kube-apiserver to external-provisioner: watch
- kube-apiserver to external-attacher: watch
- external-provisioner to CSI driver: gRPC
- external-attacher to CSI driver: gRPC
- CSI driver to Storage system: create, attach
- kubelet to CSI driver: gRPC
- CSI driver to Pod volume directory: mounts
- Storage system to Pod volume directory: the attached disk
The controller plugin is a Deployment (or StatefulSet) with one active replica. It can run on any node, because it only talks to the storage system's API. Next to the vendor's driver container run sidecar containers maintained by the Kubernetes storage team. They are the glue: the driver itself knows nothing about Kubernetes objects.
| Sidecar | Watches | Calls on the driver |
|---|---|---|
external-provisioner | PersistentVolumeClaims | CreateVolume, DeleteVolume |
external-attacher | VolumeAttachment objects | ControllerPublishVolume, ControllerUnpublishVolume |
external-resizer | Claims whose requested size grew | ControllerExpandVolume |
external-snapshotter | VolumeSnapshot objects | CreateSnapshot, DeleteSnapshot |
livenessprobe | Nothing | Probe, so the kubelet can restart a hung driver |
The node plugin is a DaemonSet, because volumes must be mounted on the node where the Pod runs. It contains
the driver container and one sidecar, node-driver-registrar, which tells the kubelet that the driver exists.
The kubelet then calls the driver directly over its socket under /var/lib/kubelet/plugins/. The node plugin
runs privileged, since mounting file systems on the host needs it.
The API objects
| Object | Scope | Who creates it | What it tells you |
|---|---|---|---|
CSIDriver | Cluster | The driver's installer | A driver with this name exists, and how Kubernetes should treat it (for example attachRequired) |
CSINode | Cluster, one per node | The kubelet, on registration | Which drivers are ready on this node, and the node's ID in the storage system |
VolumeAttachment | Cluster | The attach/detach controller | "Volume X should be attached to node Y", and whether it is |
StorageClass | Cluster | The administrator | Which driver (provisioner) creates volumes of this class, with which parameters |
kubectl get csidrivers
kubectl get csinodes
kubectl get volumeattachments
NAME ATTACHREQUIRED PODINFOONMOUNT STORAGECAPACITY TOKENREQUESTS REQUIRESREPUBLISH MODES AGE
ebs.csi.aws.com true false false <unset> false Persistent 12d
NAME DRIVERS AGE
node-1 1 12d
node-2 1 12d
NAME ATTACHER PV NODE ATTACHED AGE
csi-3f2a91c07d… ebs.csi.aws.com pvc-8d1c04e2-5b7f… node-1 true 2m
From a claim to a mounted volume
Parts:
- 1. PVC created (names a StorageClass)
- 2. CreateVolume (controller plugin)
- 3. PV created (bound to the PVC)
- 4. Pod scheduled (to node-1)
- 5. ControllerPublish (attach disk to node-1)
- 6. NodeStage (format, mount on the node)
- 7. NodePublish (mount into the Pod)
- 8. Container starts (volume at mountPath)
Connections:
- 1. PVC created to 2. CreateVolume: watch
- 2. CreateVolume to 3. PV created: makes
- 3. PV created to 4. Pod scheduled: Pod uses the claim
- 4. Pod scheduled to 5. ControllerPublish: attach
- 5. ControllerPublish to 6. NodeStage: then
- 6. NodeStage to 7. NodePublish: then
- 7. NodePublish to 8. Container starts: ready
- A user creates a PersistentVolumeClaim that names a StorageClass.
external-provisionersees a claim for its driver and callsCreateVolumewith the size and the class'sparameters. The driver creates the disk.- The provisioner creates a PersistentVolume that describes the new disk, and the claim is bound to it.
- A Pod that uses the claim is scheduled to
node-1. - The attach/detach controller in
kube-controller-managercreates a VolumeAttachment.external-attachersees it and callsControllerPublishVolume. The disk is attached tonode-1. - The kubelet on
node-1callsNodeStageVolume. The node plugin formats the device if needed and mounts it once at a node-wide directory. - The kubelet calls
NodePublishVolume. The node plugin bind-mounts the staged volume into the Pod's directory under/var/lib/kubelet/pods/POD-UID/volumes/kubernetes.io~csi/. - The kubelet starts the container with that directory mounted at
mountPath.
Deleting the Pod runs steps 7 to 5 in reverse: unpublish, unstage, detach. Deleting the claim leads to
DeleteVolume if the reclaim policy is Delete.
Where you meet CSI as an administrator
In a StorageClass. The provisioner is the name of the CSI driver, and parameters are passed to
CreateVolume unchanged. Their meaning is defined by the driver, not by Kubernetes.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
parameters:
type: gp3
encrypted: "true"
reclaimPolicy: Delete
volumeBindingMode: WaitForFirstConsumer
allowVolumeExpansion: true
In a PersistentVolume. Every volume a driver provisioned has a csi section. You can also write one by hand
for a disk that already exists:
apiVersion: v1
kind: PersistentVolume
metadata:
name: reports
spec:
capacity:
storage: 20Gi
accessModes: ["ReadWriteOnce"]
persistentVolumeReclaimPolicy: Retain
csi:
driver: ebs.csi.aws.com
volumeHandle: vol-0a1b2c3d4e5f67890
fsType: ext4
volumeHandle is the ID of the volume in the storage system. It is the one value Kubernetes hands back to the
driver in every later call.
In a Pod, directly. Some drivers provide small volumes that live and die with the Pod, called CSI ephemeral volumes. The Secrets Store CSI driver, which mounts secrets from an external vault, is the best-known example:
volumes:
- name: vault-secrets
csi:
driver: secrets-store.csi.k8s.io
readOnly: true
volumeAttributes:
secretProviderClass: app-secrets
Common driver names:
| Driver name | Storage |
|---|---|
ebs.csi.aws.com, efs.csi.aws.com | AWS EBS disks, EFS file systems |
disk.csi.azure.com, file.csi.azure.com | Azure Disk, Azure Files |
pd.csi.storage.gke.io | Google Persistent Disk |
nfs.csi.k8s.io | Any NFS server |
rbd.csi.ceph.com, cephfs.csi.ceph.com | Ceph, often through Rook |
driver.longhorn.io | Longhorn |
hostpath.csi.k8s.io | A demo driver for test clusters |
Under the hood · CSI migration: what happened to the in-tree plugins
Old manifests still say provisioner: kubernetes.io/aws-ebs or use the awsElasticBlockStore volume type. The
in-tree code behind them has been removed from Kubernetes. To keep those objects working, the CSI migration
feature (stable since v1.25) translates every operation on such a volume into a call to the matching CSI driver:
kubernetes.io/aws-ebs goes to ebs.csi.aws.com, kubernetes.io/gce-pd to pd.csi.storage.gke.io,
kubernetes.io/azure-disk to disk.csi.azure.com, and so on.
Existing PersistentVolumes and StorageClasses need no change. But the translation only redirects: the cluster administrator must have installed the CSI driver. Kubernetes does not install it. A cluster that was upgraded without the driver has volumes that can no longer attach.
Only a few volume types remain truly built in, the ones that need no vendor: emptyDir, hostPath, local,
nfs, configMap, secret, downwardAPI, projected and image, with iscsi and fc for raw network
block devices.
Under the hood · Why NodeStage and NodePublish are two steps
One disk can be used by several Pods on the same node. A block device can be formatted and mounted only once,
so NodeStageVolume does that at a node-wide path:
/var/lib/kubelet/plugins/kubernetes.io/csi/ebs.csi.aws.com/<hash>/globalmount
NodePublishVolume then makes a cheap bind mount from that path into each Pod's own directory. The second Pod
on the node needs only another publish, not another stage. Drivers for shared file systems such as NFS often
skip staging and mount in the publish step.
Using it
Example · Check which drivers a cluster has, and which is the default
kubectl get csidrivers
kubectl get storageclass
NAME ATTACHREQUIRED PODINFOONMOUNT STORAGECAPACITY TOKENREQUESTS REQUIRESREPUBLISH MODES AGE
nfs.csi.k8s.io false false false <unset> false Persistent 3d
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-csi (default) nfs.csi.k8s.io Delete Immediate true 3d
The PROVISIONER column of the StorageClass must match a name in the first list. ATTACHREQUIRED false says
this driver has no attach step, so no VolumeAttachment objects will appear for it.
Example · Find the driver's Pods and read their logs
kubectl get pods -A -o wide | grep -i csi
kube-system csi-nfs-controller-6f7d9c5b8-x2lqp 4/4 Running 0 3d 10.244.1.7 node-1
kube-system csi-nfs-node-4kz8m 3/3 Running 0 3d 192.168.56.11 node-1
kube-system csi-nfs-node-tq9wd 3/3 Running 0 3d 192.168.56.12 node-2
One controller Pod with several containers (driver plus sidecars), and one node Pod per node. A Pod with several
containers needs -c for logs:
kubectl -n kube-system logs deploy/csi-nfs-controller -c csi-provisioner --tail=20
kubectl -n kube-system logs csi-nfs-node-4kz8m -c nfs --tail=20
Provisioning problems are in the provisioner sidecar's log. Mount problems are in the driver container of the node Pod on the node where the application Pod was scheduled.
Example · Debug a Pod stuck in ContainerCreating
kubectl describe pod reports-0 | sed -n '/Events/,$p'
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m default-scheduler Successfully assigned default/reports-0 to node-2
Warning FailedAttachVolume 12s attachdetach-controller AttachVolume.Attach failed for volume "pvc-8d1c04e2" : rpc error: code = Internal desc = Could not attach volume "vol-0a1b" to node "i-07f3": volume is in use
The Pod was scheduled, so provisioning worked. The attach step failed, and the message comes from the driver: the disk is still attached somewhere else.
kubectl get volumeattachments | grep pvc-8d1c04e2
csi-3f2a91c07d… ebs.csi.aws.com pvc-8d1c04e2-5b7f… node-1 true 3h
A ReadWriteOnce disk is still attached to node-1, where an old Pod of the same workload has not finished
terminating. Once that Pod is gone, the controller detaches the volume and the attach to node-2 succeeds.
Example · See which driver is behind a volume
kubectl get pv -o custom-columns=NAME:.metadata.name,DRIVER:.spec.csi.driver,HANDLE:.spec.csi.volumeHandle
NAME DRIVER HANDLE
pvc-8d1c04e2-5b7f-4a55-9c1e-2f6f0c7d1a11 ebs.csi.aws.com vol-0a1b2c3d4e5f67890
legacy-nfs <none> <none>
<none> means the volume does not use CSI. Here it is a built-in nfs volume.
Tricks
Common mistakes
- Thinking CSI is part of Kubernetes that is "just there". The interface is. The drivers are not: a cluster built with kubeadm has no CSI driver until you install one.
- Looking for the problem in the kubelet when attach fails. Attaching is done by the controller plugin, far from the node. The kubelet only waits.
- Reading the wrong container's log. Controller and node Pods have several containers. Without
-cyou get one of them, often a sidecar with nothing useful. - Expecting StorageClass parameters to be validated. Kubernetes passes them through. A wrong parameter surfaces only as a provisioning error in the claim's events.
- Confusing a StorageClass with a driver. Several classes can use one driver with different parameters, for example fast and cheap disks from the same cloud.
- Assuming old in-tree names work without a driver.
kubernetes.io/aws-ebsis redirected to the CSI driver, which must be installed.
Recap
- CSI is a vendor-neutral set of gRPC calls for storage, like CRI for runtimes and CNI for networks. It replaced the storage code that used to be compiled into Kubernetes.
- A driver has a controller plugin (create, delete, attach, resize, snapshot) and a node plugin on every node (stage and publish, which means mount).
- Sidecars such as
external-provisionerandexternal-attachertranslate claims and VolumeAttachments into CSI calls. kubectl get csidrivers,csinodesandvolumeattachmentsshow the state. You select a driver withprovisionerin a StorageClass.- Read Pod events by step: unbound claim,
FailedAttachVolume(controller plugin),FailedMount(node plugin).
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.