Hands-On With StatefulSets

Let's begin with the practical implementation of StatefulSets.

We'll cover the following

In this section, we’ll deploy a working StatefulSet.

The demos are designed and tested on Google Kubernetes Engine (GKE) and a local Docker cluster. If your cluster is on a different cloud, you’ll have to use a different StorageClass. We’ll tell you when to do this.

We’re about to deploy the following three objects:

  1. A StorageClass

  2. A headless Service

  3. A StatefulSet

To make things easier to follow, we’ll deploy and inspect each object individually. However, it’s possible to group them into a single YAML file and deploy them with a single command (see app.yml in the statefulsets folder).

Deploy the StorageClass

StatefulSets need to create volumes dynamically. To do this, they need:

  • A StorageClass (SC)

  • A PersistentVolumeClaim (PVC)

The following YAML is from the gcp-sc.yml file and defines a StorageClass object called flash that dynamically provisions SSD volumes on the Google Cloud using the GKE persistent disk CSI driver. It only works on GKE or GCP clusters. If you’re using a Docker Desktop cluster, you should use the dd-sc.yml file instead. If your cluster is on a different cloud, you can do either of the following:

  • Create a new StorageClass called flash for your own cloud — you’ll need to create this yourself and configure the provisioner and parameters sections appropriately

  • Use one of the cluster’s existing StorageClasses and change the StorageClass name in the PVC in a later step.

Get hands-on with 1400+ tech skills courses.