Skip to content
LF Cert Trainer

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

1/10Container Storage Interface
0:00 / 4:06

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.

Three interfaces keep vendors out of the Kubernetes codeplugs inplugs inplugs inKuberneteskubelet and controllersCRIrun containersCNIconnect PodsCSIprovide volumescontainerd, CRI-OCalico, Cilium, FlannelEBS, Ceph, NFS, Longhornone driver each
Three interfaces keep vendors out of the Kubernetes code. Kubernetes defines what it needs and calls a standard interface. Whoever implements the interface can be swapped without a new Kubernetes release.

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
InterfaceKubernetes asksImplementations
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.

ServiceRunsMain callsPurpose
IdentityEverywhereGetPluginInfo, GetPluginCapabilities, ProbeName, version and abilities of the driver, health
ControllerOnce per clusterCreateVolume, DeleteVolume, ControllerPublishVolume, ControllerUnpublishVolume, ControllerExpandVolume, CreateSnapshotWork done through the storage system's API, from anywhere
NodeOn every nodeNodeStageVolume, NodeUnstageVolume, NodePublishVolume, NodeUnpublishVolume, NodeExpandVolume, NodeGetInfoWork that must happen on the machine where the Pod runs

The words are worth translating once:

CSI callPlain meaning
CreateVolumeProvision: create a 10 GiB disk in the storage system
ControllerPublishVolumeAttach: connect that disk to the virtual machine node-1
NodeStageVolumeFormat the device if it is empty and mount it once at a node-wide directory
NodePublishVolumeMount: 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.

The parts of a CSI driver in a clusterController pluginNode pluginwatchwatchgRPCgRPCcreate, attachgRPCmountsthe attached diskkube-apiserverPVC, PV, VolumeAttachmentexternal-provisionersidecar: watches PVCsexternal-attachersidecar: VolumeAttachmentsCSI drivercontroller serviceStorage systemcloud API, Ceph, arraykubeleton each nodeCSI drivernode servicePod volume directoryunder /var/lib/kubelet/pods
The parts of a CSI driver in a cluster. The controller plugin runs once and talks to the storage system's API. The node plugin runs on every node and mounts volumes for the kubelet. Sidecar containers translate Kubernetes objects into CSI calls.

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.

SidecarWatchesCalls on the driver
external-provisionerPersistentVolumeClaimsCreateVolume, DeleteVolume
external-attacherVolumeAttachment objectsControllerPublishVolume, ControllerUnpublishVolume
external-resizerClaims whose requested size grewControllerExpandVolume
external-snapshotterVolumeSnapshot objectsCreateSnapshot, DeleteSnapshot
livenessprobeNothingProbe, 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

ObjectScopeWho creates itWhat it tells you
CSIDriverClusterThe driver's installerA driver with this name exists, and how Kubernetes should treat it (for example attachRequired)
CSINodeCluster, one per nodeThe kubelet, on registrationWhich drivers are ready on this node, and the node's ID in the storage system
VolumeAttachmentClusterThe attach/detach controller"Volume X should be attached to node Y", and whether it is
StorageClassClusterThe administratorWhich 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

From a claim to a mounted volumewatchmakesPod uses the claimattachthenthenready1. PVC creatednames a StorageClass2. CreateVolumecontroller plugin3. PV createdbound to the PVC4. Pod scheduledto node-15. ControllerPublishattach disk to node-16. NodeStageformat, mount on the node7. NodePublishmount into the Pod8. Container startsvolume at mountPath
From a claim to a mounted volume. Three CSI steps hide behind one starting Pod: the volume is created, attached to the node, then mounted. Each step is done by a different part, and each can fail on its own.

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
  1. A user creates a PersistentVolumeClaim that names a StorageClass.
  2. external-provisioner sees a claim for its driver and calls CreateVolume with the size and the class's parameters. The driver creates the disk.
  3. The provisioner creates a PersistentVolume that describes the new disk, and the claim is bound to it.
  4. A Pod that uses the claim is scheduled to node-1.
  5. The attach/detach controller in kube-controller-manager creates a VolumeAttachment. external-attacher sees it and calls ControllerPublishVolume. The disk is attached to node-1.
  6. The kubelet on node-1 calls NodeStageVolume. The node plugin formats the device if needed and mounts it once at a node-wide directory.
  7. 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/.
  8. 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 nameStorage
ebs.csi.aws.com, efs.csi.aws.comAWS EBS disks, EFS file systems
disk.csi.azure.com, file.csi.azure.comAzure Disk, Azure Files
pd.csi.storage.gke.ioGoogle Persistent Disk
nfs.csi.k8s.ioAny NFS server
rbd.csi.ceph.com, cephfs.csi.ceph.comCeph, often through Rook
driver.longhorn.ioLonghorn
hostpath.csi.k8s.ioA 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 -c you 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-ebs is 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-provisioner and external-attacher translate claims and VolumeAttachments into CSI calls.
  • kubectl get csidrivers, csinodes and volumeattachments show the state. You select a driver with provisioner in 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.