Managing storage in a Kubernetes cluster is crucial for ensuring that applications have access to the data they need. Kubernetes provides several tools and mechanisms to manage storage, including Persistent Volumes (PVs), Persistent Volume Claims (PVCs), and Storage Classes. However, for more advanced use cases, additional tools like StatefulSets, StorageClass parameters, and third-party solutions can be leveraged.
In this tutorial, we will explore some of the advanced tools and techniques available for managing storage in Kubernetes. We'll cover how to configure and use these tools effectively to meet the needs of your applications.
A Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. PVs are resources in the cluster just like Nodes, and they have a lifecycle independent of any individual Pod that uses them.
A Persistent Volume Claim (PVC) is a request for storage by a user. It is similar to a Pod requesting compute resources. PVCs consume PVs based on labels and the requested storage capacity.
Storage Classes abstract the underlying details of how storage is provided from the Kubernetes API. They allow dynamic provisioning of Persistent Volumes based on policies that are defined by the administrator.
Dynamic provisioning allows for automatic creation of Persistent Volumes when a Persistent Volume Claim is made. This is achieved using Storage Classes.
First, let's create a StorageClass that uses a cloud provider's storage service (e.g., AWS EBS).
1apiVersion: storage.k8s.io/v12kind: StorageClass3metadata:4name: standard5provisioner: kubernetes.io/aws-ebs6parameters:7type: gp28reclaimPolicy: Retain9allowVolumeExpansion: true10mountOptions:11- debug12volumeBindingMode: Immediate
Next, create a PVC that requests storage using the StorageClass.
1apiVersion: v12kind: PersistentVolumeClaim3metadata:4name: my-pvc5spec:6accessModes:7- ReadWriteOnce8resources:9requests:10storage: 5Gi11storageClassName: standard
After creating the PVC, Kubernetes will automatically provision a Persistent Volume to satisfy the claim.
$ kubectl get pvc my-pvc$ kubectl get pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE my-pvc Bound pvc-12345678-90ab-cdef-ghij-klmnopqrstuv 5Gi RWO standard 1m NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pvc-12345678-90ab-cdef-ghij-klmnopqrstuv 5Gi RWO Retain Bound default/my-pvc standard 1m
StatefulSets are used to manage stateful applications. They provide stable network identities and persistent storage.
Define a PVC template in the StatefulSet specification.
1apiVersion: apps/v12kind: StatefulSet3metadata:4name: web5spec:6serviceName: "nginx"7replicas: 38selector:9matchLabels:10app: nginx11template:12metadata:13labels:14app: nginx15spec:16containers:17- name: nginx18image: k8s.gcr.io/nginx-slim:0.819ports:20- containerPort: 8021name: web22volumeMounts:23- name: www24mountPath: /usr/share/nginx/html25volumeClaimTemplates:26- metadata:27name: www28spec:29accessModes: [ "ReadWriteOnce" ]30storageClassName: "standard"31resources:32requests:33storage: 1Gi
Apply the configuration to create the StatefulSet.
$ kubectl apply -f statefulset.yaml
Check that the StatefulSet has created Pods and Persistent Volume Claims.
$ kubectl get pods$ kubectl get pvc
NAME READY STATUS RESTARTS AGE web-0 1/1 Running 0 2m web-1 1/1 Running 0 2m web-2 1/1 Running 0 2m NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE www-web-0 Bound pvc-abcdefgh-ijkl-mnopqrstuvwx-yzabcdefg 1Gi RWO standard 2m www-web-1 Bound pvc-hijklmnopqr-stuvwxyzabcdefg-123456789 1Gi RWO standard 2m www-web-2 Bound pvc-vwx-yzabcdefg-123456789-hijklmnopqr 1Gi RWO standard 2m
In the next section, we will explore Kubernetes Advanced Multitenancy Strategy Management Tools. This will cover how to manage resources and access control in a multi-tenant environment.
Stay tuned for more advanced topics and best practices in Kubernetes management.