How Kubernetes works
Kubernetes is based on a client-server model, it implements a layered architecture with master server controlling several nodes ( worker nodes ) on which containers are hosted. On each node there are a variable number of containers ( aka POD ) which run your services ( deployments or workloads )
Read more here on creating a POD with Persistent volume
Running MySQL Databases on Kubernetes
When running MySQL in containers user will get some key benefits like
- No Installation after container startup
- Portability across environments
- Easy to Start / Stop and Update
Stateful Set and persistent volumes
The stateful set is API objects that ensure that PODs are unique and ordered, when pods are deployed in StaatefulSet each has a persistent id that it retains regardless of deployment or rescheduling. This persistent ID enables to re-connect the PODs to the correct EBS volume after ta pod has failed
Persistent volumes are storage resources in Kubernetes that operate independently of attached pods. These resources can be provisioned statically at configuration or dynamically through Storage Classes. When you use persistent volumes, you create a PersistentVolumeClaim that operates like a pod. You can attach pods to this claim to allow them to use the persistent storage you created.
Creating the PV and PVC
YAML file ( pv.yaml ) to create PV, edit the volume ID with the EBS Volume ID which you created and the _PV_ with the unique identifier name
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: _PV_
spec:
accessModes:
- ReadWriteOnce
awsElasticBlockStore:
fsType: xfs
volumeID: aws://us-east-1a/vol-xxxxxxxxx
capacity:
storage: 10Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: gp2-retain
volumeMode: Filesystem
YAML file ( pvc.yaml ) to create PVC, change the volumeName with the PV name you used to create using the above script, and mention the PVC name
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
app: asvignesh
name: _PVC_
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: gp2-retain
volumeMode: Filesystem
volumeName: _PV_
YAML file ( mysql.yaml ) for the deployment of MySQL, give the app name which is suitable for your deployment, and use the PVC name which you used in the earlier command
---
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: asvignesh
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: asvignesh
tier: mysql
clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
labels:
app: asvignesh
spec:
selector:
matchLabels:
app: asvignesh
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: asvignesh
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: _PVC_
To create the PV, PVC and Deployment, save the above code or get it from the GIST link mentioned below and run kubectl command
kubectl apply -f pv.yaml
kubectl apply -f pvc.yaml
kubectl apply -f mysql.yaml
Also published on Medium.