Storage Solutions in K8s
- Stateful applications store data locally. In Containers the data created locally will be lost once you delete it. So to solve this in docker we have used volumes. Volumes have a lifecycle which has no relation to container lifecycle (refer docker containers, image layers, volumes)
- IN k8s we are running docker containers, k8s is an orchestration solutions.
- Lets see what are options for storage provisioning in k8s Refer Here
-
The most widely used storage types
- Volumes
- Persistent Volumes
- Storage Classes
- Persistent Volume Claims
- Volumes Refer Here
Volumes
- Volumes can be mounted to containers and they have lifetime equivalent to Pods.
- The types of Volumes Refer Here
- The types
- storage on cloud
- ebs
- azure disk
- efs
- azure file
- gcs
- empty dir
- hostPath
- storage on cloud
- Lets create a manifest with mysql-pod with volume
---
apiVersion: v1
kind: Pod
metadata:
name: mysql-vol
labels:
app: mysql
layer: db
spec:
containers:
- name: mysql
image: mysql:8
ports:
- containerPort: 3306
volumeMounts:
- name: test-volume
mountPath: /var/lib/mysql
volumes:
- name: test-volume
emptyDir:
sizeLimit: 100Mi
Persistent Volumes
- These volumes will have a lifetime different than Pod i.e. they exist even after pod is dead.
- Types of PVC Refer Here
- Persistent Volumes in Azure Refer Here
- Storage classes in Azure Refer Here
- Storage classes in AWS Refer Here
Lets create a mysql-pod with dynamic volume provisioing
- Get storage classes
- Lets create a Persistent Volume Claim and a mysql pod claiming pvc
- Refer Here for the initial manifests written by us
- Now lets modify the spec to add mysql related environment variables
- Refer Here for the mysql env values
- INsert some data into mysql
-
now delete pod and recreate and check for the data created
- For static provisioning of volume Refer Here
-
If you want to scale Pods we can use
- Replica Sets
- Deployments
StatefulSets
- Statefulset is like deployment with replicas. But each pod gets its own volume.
- Stateful Set is for stateful applications
- When we create replicas in Stateful Set we get predictable names
- Refer Here for official docs
-
We can access individual pod, by creating headless service and by using
. . .svc.cluster.local - Lets experiment with stateful sets and create nginx pods
- Refer Here for the manifests
- Naming is predictable in stateful sets
- Now create a different html page for every pod
- Send a curl request from any alpine pod
<pod-name>.<headless.svc-name>.<namespace>.svc.cluster.local
Kuberentes namespace
- Refer Here for official docs