MySQL data persistence based PV and PVC
Demonstrates how to provide persistent storage for the MySQL database, the following steps:
- Creating PV and PVC.
- Deploy MySQL.
- Add data to MySQL.
- Fault simulation node goes down, Kubernetes MySQL will automatically migrate to another node.
- Verify data consistency.
The first step: a control node deployment NFS
[root@ken1 ~]# cat /etc/exports
/ken/mysql *(rw,no_root_squash)
Note: This is not meant to be added no_root_squash reduce root privileges, without this being given as follows:
chown: changing ownership of '/var/lib/mysql/': Operation not permitted
When no_root_squash access the shared directory, if the user is root access to the shared directory also have root privileges
root_squash if access to the shared directory is the root user's permissions on the shared directory permissions will be compressed into a user's right nfsnobody
Whether you all_squash user access to the shared directory of who they are, it must be compressed to nfsnobody user permissions
Step two: First create PV and PVC, configuration is as follows:
pv.yml
[root@ken1 ~]# cat pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
nfs:
path: /ken
server: 192.168.64.11
pvc.yml
[root@ken1 ~]# cat pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
spec:
volumeName: mypv
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
Step 3: Create pv and pvc
[root@ken1 ~]# kubectl apply -f pv.yml
persistentvolume/mypv unchanged
[root@ken1 ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mypv 1Gi RWX Retain Available 8d
[root@ken1 ~]# kubectl apply -f pvc.yml
persistentvolumeclaim/mypvc created
[root@ken1 ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc Bound mypv 1Gi RWX 3s
Step Four: Next to deploy MySQL, the configuration file as follows
[root@ken1 ~]# cat mysql.yml
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
spec:
type: NodePort
selector:
run: mysql
ports:
- port: 80
targetPort: 3306
NodePort: 3306
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
template:
metadata:
labels:
run: mysql
spec:
containers:
- name: mysql
image: mysql
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
value: ken
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: mysql
mountPath: /var/lib/mysql
volumes:
- name: mysql
persistentVolumeClaim:
claimName: mypvc
Step five: View pod
[root@ken1 ~]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-587dbd4dd6-79tn5 1/1 Running 0 9m20s 10.244.1.60 ken2 <none> <none>
Step Six: Create a database login database ken
[root@ken1 ~]# kubectl exec -it mysql-587dbd4dd6-79tn5 bash
root@mysql-587dbd4dd6-79tn5:/# mysql -uroot -pken
mysql> create database ken;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ken |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
Step Seven: run off the pod ken2 node simulated fault
poweroff
Eighth step: after a period of time the pod k8s mobile node to ken3
[root@ken1 ~]# kubectl get po -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-587dbd4dd6-79tn5 1/1 Running 0 14m 10.244.1.60 ken2 <none> <none>
mysql-587dbd4dd6-79tn5 1/1 Terminating 0 18m 10.244.1.60 ken2 <none> <none>
mysql-587dbd4dd6-gxnpq 0/1 Pending 0 0s <none> <none> <none> <none>
mysql-587dbd4dd6-gxnpq 0/1 Pending 0 0s <none> ken3 <none> <none>
mysql-587dbd4dd6-gxnpq 0/1 ContainerCreating 0 0s <none> ken3 <none> <none>
mysql-587dbd4dd6-gxnpq 1/1 Running 0 3s 10.244.2.139 ken3 <none> <none>
Step 9: Verify database consistency in the login node data ken3
[root@ken3 ~]# docker exec -it 2aaf1b234ebf bash
root@mysql-587dbd4dd6-gxnpq:/# mysql -uroot -pken
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ken |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.02 sec)
MySQL service recovery, data also intact.