How the CSI (Container Storage Interface) Works

https://sklar.rocks/how-container-storage-interface-works/

If you work with persistent storage in Kubernetes, maybe you've seen articles about how to migrate from in-tree to CSI volumes, but aren't sure what all the fuss is about? Or perhaps you're trying to debug a stuck VolumeAttachment that won't unmount from a node, holding up your important StatefulSet rollout? A clear understanding of what the Container Storage Interface (or CSI for short) is and how it works will give you confidence when dealing with persistent data in Kubernetes, allowing you to answer these questions and more!

The Container Storage Interface is an API specification that enables developers to build custom drivers which handle the provisioning, attaching, and mounting of volumes in containerized workloads. As long as a driver correctly implements the CSI API spec, it can be used in any supported Container Orchestration system, like Kubernetes. This decouples persistent storage development efforts from core cluster management tooling, allowing for the rapid development and iteration of storage drivers across the cloud native ecosystem.

In Kubernetes, the CSI has replaced legacy in-tree volumes with a more flexible means of managing storage mediums. Previously, in order to take advantage of new storage types, one would have had to upgrade an entire cluster's Kubernetes version to access new PersistentVolume API fields for a new storage type. But now, with the plethora of independent CSI drivers available, you can add any type of underlying storage to your cluster instantly, as long as there's a driver for it.

But what if existing drivers don't provide the features that you require and you want to build a new custom driver? Maybe you're concerned about the ramifications of migrating from in-tree to CSI volumes? Or, you simply want to learn more about how persistent storage works in Kubernetes? Well, you're in the right place! This article will describe what the CSI is and detail how it's implemented in Kubernetes.

It's APIs All the Way Down

Like many things in the Kubernetes ecosystem, the Container Storage Interface is actually just an API specification. In the container-storage-interface/spec GitHub repo, you can find this spec in 2 different versions:

  1. A protobuf file that defines the API schema in gRPC terms
  2. A markdown file that describes the overall system architecture and goes into detail about each API call

What I'm going to discuss in this section is an abridged version of that markdown file, while borrowing some nice ASCII diagrams from the repo itself!

Architecture

A CSI Driver has 2 components, a Node Plugin and a Controller Plugin. The Controller Plugin is responsible for high-level volume management; creating, deleting, attaching, detatching, snapshotting, and restoring physical (or virtualized) volumes. If you're using a driver built for a cloud provider, like EBS on AWS, the driver's Controller Plugin communicates with AWS HTTPS APIs to perform these operations. For other storage types like NFS, EXSI, ZFS, and more, the driver sends these requests to the underlying storage's API endpoint, in whatever format that API accepts.

On the other hand, the Node Plugin is responsible for mounting and provisioning a volume once it's been attached to a node. These low-level operations usually require privileged access, so the Node Plugin is installed on every node in your cluster's data plane, wherever a volume could be mounted.

The Node Plugin is also responsible for reporting metrics like disk usage back to the Container Orchestration system (referred to as the "CO" in the spec). As you might have guessed already, I'll be using Kubernetes as the CO in this post! But what makes the spec so powerful is that it can be used by any container orchestration system, like Nomad for example, as long as it abides by the contract set by the API guidelines.

The specification doc provides a few possible deployment patterns, so let's start with the most common one.

                             CO "Master" Host
+-------------------------------------------+
|                                           |
|  +------------+           +------------+  |
|  |     CO     |   gRPC    | Controller |  |
|  |            +----------->   Plugin   |  |
|  +------------+           +------------+  |
|                                           |
+-------------------------------------------+

                            CO "Node" Host(s)
+-------------------------------------------+
|                                           |
|  +------------+           +------------+  |
|  |     CO     |   gRPC    |    Node    |  |
|  |            +----------->   Plugin   |  |
|  +------------+           +------------+  |
|                                           |
+-------------------------------------------+

Figure 1: The Plugin runs on all nodes in the cluster: a centralized
Controller Plugin is available on the CO master host and the Node
Plugin is available on all of the CO Nodes.

Since the Controller Plugin is concerned with higher-level volume operations, it does not need to run on a host in your cluster's data plane. For example, in AWS, the Controller makes AWS API calls like ec2:CreateVolume, ec2:AttachVolume, or ec2:CreateSnapshot to manage EBS volumes. These functions can be run anywhere, as long as the caller is authenticated with AWS. All the CO needs is to be able to send messages to the plugin over gRPC. So in this architecture, the Controller Plugin is running on a "master" host in the cluster's control plane.

On the other hand, the Node Plugin must be running on a host in the cluster's data plane. Once the Controller Plugin has done its job by attaching a volume to a node for a workload to use, the Node Plugin (running on that node) will take over by mounting the volume to a well-known path and optionally formatting it. At this point, the CO is free to use that path as a volume mount when creating a new containerized process; so all data on that mount will be stored on the underlying volume that was attached by the Controller Plugin. It's important to note that the Container Orchestrator, not the Controller Plugin, is responsible for letting the Node Plugin know that it should perform the mount.

Volume Lifecycle

The spec provides a flowchart of basic volume operations, also in the form of a cool ASCII diagram:

   CreateVolume +------------+ DeleteVolume
 +------------->|  CREATED   +--------------+
 |              +---+----^---+              |
 |       Controller |    | Controller       v
+++         Publish |    | Unpublish       +++
|X|          Volume |    | Volume          | |
+-+             +---v----+---+             +-+
                | NODE_READY |
                +---+----^---+
               Node |    | Node
            Publish |    | Unpublish
             Volume |    | Volume
                +---v----+---+
                | PUBLISHED  |
                +------------+

Figure 5: The lifecycle of a dynamically provisioned volume, from
creation to destruction.

Mounting a volume is a synchronous process: each step requires the previous one to have run successfully. For example, if a volume does not exist, how could we possibly attach it to a node?

When publishing (mounting) a volume for use by a workload, the Node Plugin first requires that the Controller Plugin has successfully published a volume at a directory that it can access. In practice, this usually means that the Controller Plugin has created the volume and attached it to a node. Now that the volume is attached, it's time for the Node Plugin to do its job. At this point, the Node Plugin can access the volume at its device path to create a filesystem and mount it to a directory. Once it's mounted, the volume is considered to be published and it is ready for a containerized process to use. This ends the CSI mounting workflow.

Continuing the AWS example, when the Controller Plugin publishes a volume, it calls ec2:CreateVolume followed by ec2:AttachVolume. These two API calls allocate the underlying storage by creating an EBS volume and attaching it to a particular instance. Once the volume is attached to the EC2 instance, the Node Plugin is free to format it and create a mount point on its host's filesystem.

Here is an annotated version of the above volume lifecycle diagram, this time with the AWS calls included in the flow chart.

   CreateVolume +------------+ DeleteVolume
 +------------->|  CREATED   +--------------+
 |              +---+----^---+              |
 |       Controller |    | Controller       v
+++         Publish |    | Unpublish       +++
|X|          Volume |    | Volume          | |
+-+                 |    |                 +-+
                    |    |
 <ec2:CreateVolume> |    | <ec2:DeleteVolume>
                    |    |
 <ec2:AttachVolume> |    | <ec2:DetachVolume>
                    |    |
                +---v----+---+
                | NODE_READY |
                +---+----^---+
               Node |    | Node
            Publish |    | Unpublish
             Volume |    | Volume
                +---v----+---+
                | PUBLISHED  |
                +------------+

If a Controller wants to delete a volume, it must first wait for the Node Plugin to safely unmount the volume to preserve data and system integrity. Otherwise, if a volume is forcibly detatched from a node before unmounting it, we could experience bad things like data corruption. Once the volume is safely unpublished (unmounted) by the Node Plugin, the Controller Plugin would then call ec2:DetachVolume to detatch it from the node and finally ec2:DeleteVolume to delete it, assuming that the you don't want to reuse the volume elsewhere.

What makes the CSI so powerful is that it does not prescribe how to publish a volume. As long as your driver correctly implements the required API methods defined in the CSI spec, it will be compatible with the CSI and by extension, be usable in COs like Kubernetes and Nomad.

Running CSI Drivers in Kubernetes

What I haven't entirely make clear yet is why the Controller and Node Plugins are plugins themselves! How does the Container Orchestrator call them, and where do they plug into?

Well, the answer depends on which Container Orchestrator you are using. Since I'm most familiar with Kubernetes, I'll be using it to demonstrate how a CSI driver interacts with a CO.

Deployment Model

Since the Node Plugin, responsible for low-level volume operations, must be running on every node in your data plane, it is typically installed using a DaemonSet. If you have heterogeneous nodes and only want to deploy the plugin to a subset of them, you can use node selectors, affinities, or anti-affinities to control which nodes receive a Node Plugin Pod. Since the Node Plugin requires root access to modify host volumes and mounts, these Pods will be running in privileged mode. In this mode, the Node Plugin can escape its container's security context to access the underlying node's filesystem when performing mounting and provisioning operations. Without these elevated permissions, the Node Plugin could only operate inside of its own containerized namespace without the system-level access that it requires to provision volumes on the node.

The Controller Plugin is usually run in a Deployment because it deals with higher-level primitives like volumes and snapshots, which don't require filesystem access to every single node in the cluster. Again, lets think about the AWS example I used earlier. If the Controller Plugin is just making AWS API calls to manage volumes and snapshots, why would it need access to a node's root filesystem? Most Controller Plugins are stateless and highly-available, both of which lend themselves to the Deployment model. The Controller also does not need to be run in a privileged context.

Event-Driven Sidecar Pattern

Now that we know how CSI plugins are deployed in a typical cluster, it's time to focus on how Kubernetes calls each plugin to perform CSI-related operations. A series of sidecar containers, that are registered with the Kubernetes API server to react to different events across the cluster, are deployed alongside each Controller and Node Plugin. In a way, this is similar to the typical Kubernetes controller pattern, where controllers react to changes in cluster state and attempt to reconcile the current cluster state with the desired one.

There are currently 6 different sidecars that work alongside each CSI driver to perform specific volume-related operations. Each sidecar registers itself with the Kubernetes API server and watches for changes in a specific resource type. Once the sidecar has detected a change that it must act upon, it calls the relevant plugin with one or more API calls from the CSI specification to perform the desired operations.

Controller Plugin Sidecars

Here is a table of the sidecars that run alongside a Controller Plugin:

Sidecar NameK8s Resources WatchedCSI API Endpoints Called
external-provisionerPersistentVolumeClaimCreateVolume,DeleteVolume
external-attacherVolumeAttachmentController(Un)PublishVolume
external-snapshotterVolumeSnapshot(Content)CreateSnapshot,DeleteSnapshot
external-resizerPersistentVolumeClaimControllerExpandVolume

How do these sidecars work together? Let's use an example of a StatefulSet to demonstrate. In this example, we're dynamically provisioning our PersistentVolumes (PVs) instead of mapping PersistentVolumeClaims (PVCs) to existing PVs. We start at the creation of a new StatefulSet with a VolumeClaimTemplate.

---
apiVersion: apps/v1
kind: StatefulSet
spec:
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
         storage: 1Gi

Creating this StatefulSet will trigger the creation of a new PVC based on the above template. Once the PVC has been created, the Kubernetes API will notify the external-provisioner sidecar that this new resource was created. The external-provisioner will then send a CreateVolume message to its neighbor Controller Plugin over gRPC. From here, the CSI driver's Controller Plugin takes over by processing the incoming gRPC message and will create a new volume based on its custom logic. In the AWS EBS driver, this would be an ec2:CreateVolume call.

At this point, the control flow moves to the built-in PersistentVolume controller, which will create a matching PV and bind it to the PVC. This allows the StatefulSet's underlying Pod to be scheduled and assigned to a Node.

Here, the external-attacher sidecar takes over. It will be notified of the new PV and call the Controller Plugin's ControllerPublishVolume endpoint, mounting the volume to the StatefulSet's assigned node. This would be the equivalent to ec2:AttachVolume in AWS.

At this point, we have an EBS volume that is mounted to an EC2 instance, all based on the creation of a StatefulSet, PersistentVolumeClaim, and the work of the AWS EBS CSI Controller Plugin.

Node Plugin Sidecars

There is only one unique sidecar that is deployed alongside the Node Plugin; the node-driver-registrar. This sidecar, running as part of a DaemonSet, registers the Node Plugin with a Node's kubelet. During the registration process, the Node Plugin will inform the kubelet that it is able to mount volumes using the CSI driver that it is part of. The kubelet itself will then wait until a Pod is scheduled to its corresponding Node, at which point it is then responsible for making the relevant CSI calls (PublishVolume) to the Node Plugin over gRPC.

Common Sidecars

There is also a livenessprobe sidecar that runs in both the Container and Node Plugin Pods that monitors the health of the CSI driver and reports back to the Kubernetes Liveness Probe mechanism.

Communication Over Sockets

How do these sidecars communicate with the Controller and Node Plugins? Over gRPC through a shared socket! So each sidecar and plugin contains a volume mount pointing to a single unix socket.

CSI Controller Deployment

This diagram highlights the pluggable nature of CSI Drivers. To replace one driver with another, all you have to do is simply swap the CSI Driver container with another and ensure that it's listening to the unix socket that the sidecars are sending gRPC messages to. Becase all drivers advertise their own different capabilities and communicate over the shared CSI API contract, it's literally a plug-and-play solution.

Conclusion

In this article, I only covered the high-level concepts of the Container Storage Interface spec and implementation in Kubernetes. While hopefully it has provided a clearer understanding of what happens once you install a CSI driver, writing one requires significant low-level knowledge of both your nodes' operating system(s) and the underlying storage mechanism that your driver is implementing. Luckily, CSI drivers exist for a variety of cloud providers and distributed storage solutions, so it's likely that you can find a CSI driver that already fulfills your requirements. But it always helps to know what's happening under the hood in case your particular driver is misbehaving.

If this article interests you and you want to learn more about the topic, please let me know! I'm always happy to answer questions about CSI Drivers, Kubernetes Operators, and a myriad of other DevOps-related topics.

{
"by": "noctarius",
"descendants": 0,
"id": 40245852,
"score": 1,
"time": 1714729719,
"title": "How the CSI (Container Storage Interface) Works",
"type": "story",
"url": "https://sklar.rocks/how-container-storage-interface-works/"
}
{
"author": null,
"date": "2024-01-12T00:00:00.000Z",
"description": "A deep dive into what the CSI (Container Storage Interface) is and how it works in Kubernetes (k8s).",
"image": "https://sklar.rocks/img/blog/how-container-storage-interface-works/controller-deployment.png",
"logo": null,
"publisher": "sklar.rocks",
"title": "How the CSI (Container Storage Interface) Works",
"url": "https://sklar.rocks/how-container-storage-interface-works/"
}
{
"url": "https://sklar.rocks/how-container-storage-interface-works/",
"title": "How the CSI (Container Storage Interface) Works",
"description": "If you work with persistent storage in Kubernetes, maybe you've seen articles about how to migrate from in-tree to CSI volumes, but aren't sure what all the fuss is about? Or perhaps you're trying to debug a...",
"links": [
"https://sklar.rocks/how-container-storage-interface-works/"
],
"image": "",
"content": "<div>\n <p>If you work with persistent storage in Kubernetes, maybe you've seen articles about how to migrate from <a target=\"_blank\" href=\"https://kubernetes.io/blog/2022/09/26/storage-in-tree-to-csi-migration-status-update-1.25/\">in-tree to CSI volumes</a>, but aren't sure what all the fuss is about? Or perhaps you're trying to debug a stuck VolumeAttachment that won't unmount from a node, holding up your important StatefulSet rollout? A clear understanding of what the Container Storage Interface (or CSI for short) is and how it works will give you confidence when dealing with persistent data in Kubernetes, allowing you to answer these questions and more!</p>\n<p>The Container Storage Interface is an API specification that enables developers to build custom drivers which handle the provisioning, attaching, and mounting of volumes in containerized workloads. As long as a driver correctly implements the CSI API spec, it can be used in any supported Container Orchestration system, like Kubernetes. This decouples persistent storage development efforts from core cluster management tooling, allowing for the rapid development and iteration of storage drivers across the cloud native ecosystem.</p>\n<p>In Kubernetes, the CSI has replaced legacy in-tree volumes with a more flexible means of managing storage mediums. Previously, in order to take advantage of new storage types, one would have had to upgrade an entire cluster's Kubernetes version to access new PersistentVolume API fields for a new storage type. But now, with the <a target=\"_blank\" href=\"https://kubernetes-csi.github.io/docs/drivers.html\">plethora of independent CSI drivers</a> available, you can add any type of underlying storage to your cluster instantly, as long as there's a driver for it.</p>\n<p>But what if existing drivers don't provide the features that you require and you want to build a new custom driver? Maybe you're concerned about the ramifications of migrating from in-tree to CSI volumes? Or, you simply want to learn more about how persistent storage works in Kubernetes? Well, you're in the right place! This article will describe what the CSI is and detail how it's implemented in Kubernetes.</p>\n<h2 id=\"it-s-apis-all-the-way-down\">It's APIs All the Way Down</h2>\n<p>Like many things in the Kubernetes ecosystem, the Container Storage Interface is actually just an API specification. In the <a target=\"_blank\" href=\"https://github.com/container-storage-interface/spec\">container-storage-interface/spec</a> GitHub repo, you can find this spec in 2 different versions:</p>\n<ol>\n<li>A <a target=\"_blank\" href=\"https://github.com/container-storage-interface/spec/blob/master/csi.proto\">protobuf file</a> that defines the API schema in gRPC terms</li>\n<li>A <a target=\"_blank\" href=\"https://github.com/container-storage-interface/spec/blob/master/spec.md\">markdown file</a> that describes the overall system architecture and goes into detail about each API call</li>\n</ol>\n<p>What I'm going to discuss in this section is an abridged version of that markdown file, while borrowing some nice ASCII diagrams from the repo itself!</p>\n<h3 id=\"architecture\">Architecture</h3>\n<p>A CSI Driver has 2 components, a <strong>Node Plugin</strong> and a <strong>Controller Plugin</strong>. The Controller Plugin is responsible for high-level volume management; creating, deleting, attaching, detatching, snapshotting, and restoring physical (or virtualized) volumes. If you're using a driver built for a cloud provider, like EBS on AWS, the driver's Controller Plugin communicates with AWS HTTPS APIs to perform these operations. For other storage types like NFS, EXSI, ZFS, and more, the driver sends these requests to the underlying storage's API endpoint, in whatever format that API accepts.</p>\n<p>On the other hand, the Node Plugin is responsible for mounting and provisioning a volume once it's been attached to a node. These low-level operations usually require privileged access, so the Node Plugin is installed on every node in your cluster's data plane, wherever a volume could be mounted.</p>\n<p>The Node Plugin is also responsible for reporting metrics like disk usage back to the <strong>Container Orchestration</strong> system (referred to as the \"CO\" in the spec). As you might have guessed already, I'll be using Kubernetes as the CO in this post! But what makes the spec so powerful is that it can be used by any container orchestration system, like Nomad for example, as long as it abides by the contract set by the API guidelines.</p>\n<p>The specification doc provides a few possible deployment patterns, so let's start with the most common one.</p>\n<pre><code><span> CO \"Master\" Host\n</span><span>+-------------------------------------------+\n</span><span>| |\n</span><span>| +------------+ +------------+ |\n</span><span>| | CO | gRPC | Controller | |\n</span><span>| | +-----------&gt; Plugin | |\n</span><span>| +------------+ +------------+ |\n</span><span>| |\n</span><span>+-------------------------------------------+\n</span><span>\n</span><span> CO \"Node\" Host(s)\n</span><span>+-------------------------------------------+\n</span><span>| |\n</span><span>| +------------+ +------------+ |\n</span><span>| | CO | gRPC | Node | |\n</span><span>| | +-----------&gt; Plugin | |\n</span><span>| +------------+ +------------+ |\n</span><span>| |\n</span><span>+-------------------------------------------+\n</span><span>\n</span><span>Figure 1: The Plugin runs on all nodes in the cluster: a centralized\n</span><span>Controller Plugin is available on the CO master host and the Node\n</span><span>Plugin is available on all of the CO Nodes.\n</span></code></pre>\n<p>Since the Controller Plugin is concerned with higher-level volume operations, it does not need to run on a host in your cluster's data plane. For example, in AWS, the Controller makes AWS API calls like <code>ec2:CreateVolume</code>, <code>ec2:AttachVolume</code>, or <code>ec2:CreateSnapshot</code> to manage EBS volumes. These functions can be run anywhere, as long as the caller is authenticated with AWS. All the CO needs is to be able to send messages to the plugin over gRPC. So in this architecture, the Controller Plugin is running on a \"master\" host in the cluster's <strong>control plane</strong>.</p>\n<p>On the other hand, the Node Plugin <strong>must</strong> be running on a host in the cluster's data plane. Once the Controller Plugin has done its job by attaching a volume to a node for a workload to use, the Node Plugin (running on that node) will take over by mounting the volume to a well-known path and optionally formatting it. At this point, the CO is free to use that path as a volume mount when creating a new containerized process; so all data on that mount will be stored on the underlying volume that was attached by the Controller Plugin. It's important to note that the Container Orchestrator, not the Controller Plugin, is responsible for letting the Node Plugin know that it should perform the mount.</p>\n<h3 id=\"volume-lifecycle\">Volume Lifecycle</h3>\n<p>The spec provides a flowchart of basic volume operations, also in the form of a cool ASCII diagram:</p>\n<pre><code><span> CreateVolume +------------+ DeleteVolume\n</span><span> +-------------&gt;| CREATED +--------------+\n</span><span> | +---+----^---+ |\n</span><span> | Controller | | Controller v\n</span><span>+++ Publish | | Unpublish +++\n</span><span>|X| Volume | | Volume | |\n</span><span>+-+ +---v----+---+ +-+\n</span><span> | NODE_READY |\n</span><span> +---+----^---+\n</span><span> Node | | Node\n</span><span> Publish | | Unpublish\n</span><span> Volume | | Volume\n</span><span> +---v----+---+\n</span><span> | PUBLISHED |\n</span><span> +------------+\n</span><span>\n</span><span>Figure 5: The lifecycle of a dynamically provisioned volume, from\n</span><span>creation to destruction.\n</span></code></pre>\n<p>Mounting a volume is a synchronous process: each step requires the previous one to have run successfully. For example, if a volume does not exist, how could we possibly attach it to a node?</p>\n<p>When publishing (mounting) a volume for use by a workload, the Node Plugin first requires that the Controller Plugin has successfully published a volume at a directory that it can access. In practice, this usually means that the Controller Plugin has created the volume and attached it to a node. Now that the volume is attached, it's time for the Node Plugin to do its job. At this point, the Node Plugin can access the volume at its device path to create a filesystem and mount it to a directory. Once it's mounted, the volume is considered to be published and it is ready for a containerized process to use. This ends the CSI mounting workflow.</p>\n<p>Continuing the AWS example, when the Controller Plugin publishes a volume, it calls <code>ec2:CreateVolume</code> followed by <code>ec2:AttachVolume</code>. These two API calls allocate the underlying storage by creating an EBS volume and attaching it to a particular instance. Once the volume is attached to the EC2 instance, the Node Plugin is free to format it and create a mount point on its host's filesystem.</p>\n<p>Here is an annotated version of the above volume lifecycle diagram, this time with the AWS calls included in the flow chart.</p>\n<pre><code><span> CreateVolume +------------+ DeleteVolume\n</span><span> +-------------&gt;| CREATED +--------------+\n</span><span> | +---+----^---+ |\n</span><span> | Controller | | Controller v\n</span><span>+++ Publish | | Unpublish +++\n</span><span>|X| Volume | | Volume | |\n</span><span>+-+ | | +-+\n</span><span> | |\n</span><span> &lt;ec2:CreateVolume&gt; | | &lt;ec2:DeleteVolume&gt;\n</span><span> | |\n</span><span> &lt;ec2:AttachVolume&gt; | | &lt;ec2:DetachVolume&gt;\n</span><span> | |\n</span><span> +---v----+---+\n</span><span> | NODE_READY |\n</span><span> +---+----^---+\n</span><span> Node | | Node\n</span><span> Publish | | Unpublish\n</span><span> Volume | | Volume\n</span><span> +---v----+---+\n</span><span> | PUBLISHED |\n</span><span> +------------+\n</span></code></pre>\n<p>If a Controller wants to delete a volume, it must first wait for the Node Plugin to safely unmount the volume to preserve data and system integrity. Otherwise, if a volume is forcibly detatched from a node before unmounting it, we could experience bad things like data corruption. Once the volume is safely unpublished (unmounted) by the Node Plugin, the Controller Plugin would then call <code>ec2:DetachVolume</code> to detatch it from the node and finally <code>ec2:DeleteVolume</code> to delete it, assuming that the you don't want to reuse the volume elsewhere.</p>\n<p>What makes the CSI so powerful is that it does not prescribe <em>how</em> to publish a volume. As long as your driver correctly implements the required API methods defined in the CSI spec, it will be compatible with the CSI and by extension, be usable in COs like Kubernetes and Nomad.</p>\n<h2 id=\"running-csi-drivers-in-kubernetes\">Running CSI Drivers in Kubernetes</h2>\n<p>What I haven't entirely make clear yet is <em>why</em> the Controller and Node Plugins are plugins themselves! How does the Container Orchestrator call them, and where do they plug into?</p>\n<p>Well, the answer depends on which Container Orchestrator you are using. Since I'm most familiar with Kubernetes, I'll be using it to demonstrate how a CSI driver interacts with a CO.</p>\n<h3 id=\"deployment-model\">Deployment Model</h3>\n<p>Since the Node Plugin, responsible for low-level volume operations, must be running on every node in your data plane, it is typically installed using a <strong>DaemonSet</strong>. If you have heterogeneous nodes and only want to deploy the plugin to a subset of them, you can use node selectors, affinities, or anti-affinities to control which nodes receive a Node Plugin Pod. Since the Node Plugin requires <code>root</code> access to modify host volumes and mounts, these Pods will be running in privileged mode. In this mode, the Node Plugin can escape its container's security context to access the underlying node's filesystem when performing mounting and provisioning operations. Without these elevated permissions, the Node Plugin could only operate inside of its own containerized namespace without the system-level access that it requires to provision volumes on the node.</p>\n<p>The Controller Plugin is usually run in a <strong>Deployment</strong> because it deals with higher-level primitives like volumes and snapshots, which don't require filesystem access to every single node in the cluster. Again, lets think about the AWS example I used earlier. If the Controller Plugin is just making AWS API calls to manage volumes and snapshots, why would it need access to a node's root filesystem? Most Controller Plugins are stateless and highly-available, both of which lend themselves to the Deployment model. The Controller also does not need to be run in a privileged context.</p>\n<h3 id=\"event-driven-sidecar-pattern\">Event-Driven Sidecar Pattern</h3>\n<p>Now that we know how CSI plugins are deployed in a typical cluster, it's time to focus on <em>how</em> Kubernetes calls each plugin to perform CSI-related operations. A series of sidecar containers, that are registered with the Kubernetes API server to react to different events across the cluster, are deployed alongside each Controller and Node Plugin. In a way, this is similar to the typical Kubernetes controller pattern, where controllers react to changes in cluster state and attempt to reconcile the current cluster state with the desired one.</p>\n<p>There are currently 6 different sidecars that work alongside each CSI driver to perform specific volume-related operations. Each sidecar registers itself with the Kubernetes API server and watches for changes in a specific resource type. Once the sidecar has detected a change that it must act upon, it calls the relevant plugin with one or more API calls from the CSI specification to perform the desired operations.</p>\n<h4 id=\"controller-plugin-sidecars\">Controller Plugin Sidecars</h4>\n<p>Here is a table of the sidecars that run alongside a Controller Plugin:</p>\n<table><thead><tr><th>Sidecar Name</th><th>K8s Resources Watched</th><th>CSI API Endpoints Called</th></tr></thead><tbody>\n<tr><td>external-provisioner</td><td>PersistentVolumeClaim</td><td>CreateVolume,DeleteVolume</td></tr>\n<tr><td>external-attacher</td><td>VolumeAttachment</td><td>Controller(Un)PublishVolume</td></tr>\n<tr><td>external-snapshotter</td><td>VolumeSnapshot(Content)</td><td>CreateSnapshot,DeleteSnapshot</td></tr>\n<tr><td>external-resizer</td><td>PersistentVolumeClaim</td><td>ControllerExpandVolume</td></tr>\n</tbody></table>\n<p>How do these sidecars work together? Let's use an example of a StatefulSet to demonstrate. In this example, we're dynamically provisioning our PersistentVolumes (PVs) instead of mapping PersistentVolumeClaims (PVCs) to existing PVs. We start at the creation of a new StatefulSet with a VolumeClaimTemplate.</p>\n<pre><code><span>---\n</span><span>apiVersion</span><span>: </span><span>apps/v1\n</span><span>kind</span><span>: </span><span>StatefulSet\n</span><span>spec</span><span>:\n</span><span> </span><span>volumeClaimTemplates</span><span>:\n</span><span> - </span><span>metadata</span><span>:\n</span><span> </span><span>name</span><span>: </span><span>www\n</span><span> </span><span>spec</span><span>:\n</span><span> </span><span>accessModes</span><span>: [ \"</span><span>ReadWriteOnce</span><span>\" ]\n</span><span> </span><span>storageClassName</span><span>: \"</span><span>my-storage-class</span><span>\"\n</span><span> </span><span>resources</span><span>:\n</span><span> </span><span>requests</span><span>:\n</span><span> </span><span>storage</span><span>: </span><span>1Gi\n</span></code></pre>\n<p>Creating this StatefulSet will trigger the creation of a new PVC based on the above template. Once the PVC has been created, the Kubernetes API will notify the <code>external-provisioner</code> sidecar that this new resource was created. The <code>external-provisioner</code> will then send a <code>CreateVolume</code> message to its neighbor Controller Plugin over gRPC. From here, the CSI driver's Controller Plugin takes over by processing the incoming gRPC message and will create a new volume based on its custom logic. In the AWS EBS driver, this would be an <code>ec2:CreateVolume</code> call.</p>\n<p>At this point, the control flow moves to the built-in PersistentVolume controller, which will create a matching PV and bind it to the PVC. This allows the StatefulSet's underlying Pod to be scheduled and assigned to a Node.</p>\n<p>Here, the <code>external-attacher</code> sidecar takes over. It will be notified of the new PV and call the Controller Plugin's <code>ControllerPublishVolume</code> endpoint, mounting the volume to the StatefulSet's assigned node. This would be the equivalent to <code>ec2:AttachVolume</code> in AWS.</p>\n<p>At this point, we have an EBS volume that is mounted to an EC2 instance, all based on the creation of a StatefulSet, PersistentVolumeClaim, and the work of the AWS EBS CSI Controller Plugin.</p>\n<h4 id=\"node-plugin-sidecars\">Node Plugin Sidecars</h4>\n<p>There is only one unique sidecar that is deployed alongside the Node Plugin; the <code>node-driver-registrar</code>. This sidecar, running as part of a DaemonSet, registers the Node Plugin with a Node's kubelet. During the registration process, the Node Plugin will inform the kubelet that it is able to mount volumes using the CSI driver that it is part of. The kubelet itself will then wait until a Pod is scheduled to its corresponding Node, at which point it is then responsible for making the relevant CSI calls (<code>PublishVolume</code>) to the Node Plugin over gRPC.</p>\n<h4 id=\"common-sidecars\">Common Sidecars</h4>\n<p>There is also a <code>livenessprobe</code> sidecar that runs in both the Container and Node Plugin Pods that monitors the health of the CSI driver and reports back to the Kubernetes Liveness Probe mechanism.</p>\n<h3 id=\"communication-over-sockets\">Communication Over Sockets</h3>\n<p>How do these sidecars communicate with the Controller and Node Plugins? Over gRPC through a shared socket! So each sidecar and plugin contains a volume mount pointing to a single unix socket.</p>\n<p><img src=\"https://sklar.rocks/img/blog/how-container-storage-interface-works/controller-deployment.png\" alt=\"CSI Controller Deployment\" /></p>\n<p>This diagram highlights the pluggable nature of CSI Drivers. To replace one driver with another, all you have to do is simply swap the CSI Driver container with another and ensure that it's listening to the unix socket that the sidecars are sending gRPC messages to. Becase all drivers advertise their own different capabilities and communicate over the shared CSI API contract, it's literally a plug-and-play solution.</p>\n<h2 id=\"conclusion\">Conclusion</h2>\n<p>In this article, I only covered the high-level concepts of the Container Storage Interface spec and implementation in Kubernetes. While hopefully it has provided a clearer understanding of what happens once you install a CSI driver, writing one requires significant low-level knowledge of both your nodes' operating system(s) and the underlying storage mechanism that your driver is implementing. Luckily, CSI drivers exist for a variety of cloud providers and distributed storage solutions, so it's likely that you can find a CSI driver that already fulfills your requirements. But it always helps to know what's happening under the hood in case your particular driver is misbehaving.</p>\n<p>If this article interests you and you want to learn more about the topic, please <a target=\"_blank\" href=\"https://sklar.rocks/contact/me/\">let me know</a>! I'm always happy to answer questions about CSI Drivers, Kubernetes Operators, and a myriad of other DevOps-related topics.</p>\n </div>",
"author": "",
"favicon": "",
"source": "sklar.rocks",
"published": "",
"ttr": 521,
"type": ""
}