In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article is mainly about building a K8s cluster to achieve MySQL data persistence. You can check the relevant professional terms on the Internet or find some related books to supplement them. We will not dabble here, so let's go straight to the topic. I hope that building a K8s cluster to achieve MySQL data persistence can bring you some practical help.
1. Set up nfs storage
For ease of operation, I build nfs storage directly on master.
[root@master ~] # yum-y install nfs-utils [root@master ~] # systemctl enable rpcbind [root@master lv] # mkdir-p / nfsdata/mysql [root@master ~] # vim / etc/exports/nfsdata * (rw,sync No_root_squash) [root@master ~] # systemctl start nfs-server [root@master ~] # systemctl enable nfs-server [root@master ~] # showmount-eExport list for master:/nfsdata * 2, Create PV [root@master lv] # vim mysql-pv.yaml # write pv yaml file kind: PersistentVolumeapiVersion: v1metadata: name: mysql-pvspec: accessModes:-ReadWriteOnce # access mode is defined as capacity: storage: 1Gi persistentVolumeReclaimPolicy: Retain storageClassName: nfs nfs: path: / nfsdata/mysql server: 192.168.20.6 [root@master lv] # kubectl apply-f Mysql-pv.yaml # execute yaml file 3. Create PVC [root@master lv] # vim mysql-pvc.yaml # write pvc's yaml file kind: PersistentVolumeClaimapiVersion: v1metadata: name: mysql-pvcspec: accessModes:-ReadWriteOnce resources: requests: storage: 1Gi storageClassName: nfs # here specify the associated PV name [root@master lv] # kubectl apply-f mysql-pvc.yaml # execute yaml file 4, confirm the status of pv and pvc
At this point, the status class for both PV and PVC should be Bound.
[root@master lv] # kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEmysql-pv 1Gi RWO Retain Bound default/mysql-pvc nfs 5m8s [root@master lv] # kubectl get pvcNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEmysql-pvc Bound mysql-pv 1Gi RWO nfs 60s5 、 Create pod+svc (service)
This pod is the MySQL service provided and mapped to the host, which can communicate with the client.
Here I write the yaml files of pod and svc in the same file.
[root@master lv] # vim mysql-pod.yaml # write the yaml file of pod apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: mysqlspec: selector: matchLabels: app: mysql template: metadata: labels: app: mysqlspec: containers:-name: mysql image: mysql:5.7 env: # the following is to set up the MySQL database Password-name: MYSQL_ROOT_PASSWORD value: 123.com ports:-containerPort: 3306 volumeMounts:-name: mysql-persistent-storage mountPath: / var/lib/mysql # MySQL Container data is stored in this directory To persist data to this directory volumes:-name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pvc # specify the name of pvc-# the following is the yaml file that creates svc apiVersion: v1kind: Servicemetadata: name: mysqlspec: NodePort ports:-port: 3306 targetPort: 3306 nodePort: 31111 selector: app: mysql selector: app: mysql [root@master Lv] # kubectl apply-f mysql-pod.yaml # execute the yaml file [root@master lv] # kubectl get pod # to make sure that pod is running normally NAME READY STATUS RESTARTS AGEmysql-6d898f8bcb-lhqxb 1 prime 1 Running 0 51s6, Access to MySQL database Add test data [root@master lv] # kubectl exec-it mysql-6d898f8bcb-lhqxb-- mysql- uroot-p123.commysql > create database test Mysql > use test;mysql > create table my_id (id int (4)); mysql > insert my_id values (9527); mysql > select * from my_id # confirm the existence of data. 7. Delete the container on the node manually Verify whether the data in the database still exists [root@master lv] # kubectl get pod-o wide # to see which node pod is running on. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE mysql-6d898f8bcb-cz8gr 1Accord 1 Running 0 10m 10.244.1.3 node01 # I am running on the node01 node Now go to the node01 node to manually delete its container [root@node01 ~] # docker ps # check the ID of the MySQL container [root @ node01 ~] # docker rm-f 1a524e001732 # delete the MySQL container # because of Deployment's protection policy, when the container is deleted, it will generate a new container based on pod's yaml file, but the ID number of the new container has changed # go back to the master node and log in to the database Check whether the data still exists [root@master lv] # kubectl exec-it mysql-6d898f8bcb-cz8gr-- mysql- uroot-p123.commysql > select * from test.my_id # the data is still there, OK+-+ | id | +-+ | 1996 | +-+ 1 row in set (0.01 sec)
At this point, it can be proved that when using PV for data persistence, the metabolism of the container will not cause data loss.
8. Simulate the downtime of the node where the MySQL container is located, and verify whether the data will be lost [root@master lv] # kubectl get pod-o wide # check the node where the container resides. I am still here in node01#, and then I shut down node01 here, and then dynamically check the status of pod [root@master lv] # kubectl get pod-o wide-w # dynamically check the status of pod # about five minutes after node01 shutdown, you can see that pod has been transferred to node02 to run. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmysql-6d898f8bcb-cz8gr 1/1 Running 0 20m 10.244.1.3 node01 mysql-6d898f8bcb-cz8gr 1/1 Running 0 20m 10.244.1.3 node01 mysql-6d898f8bcb-cz8gr 1/1 Terminating 0 25m 10.244.1.3 node01 mysql-6d898f8bcb-gp4t5 0/1 Pending 0 0s mysql-6d898f8bcb-gp4t5 0/1 Pending 0 0s node02 mysql-6d898f8bcb-gp4t5 0/1 ContainerCreating 0 0s node02 mysql-6d898f8bcb-gp4t5 1/1 Running 0 1s 10.244.2.4 node02 9 、 Client side accesses MySQL database
Because we set the port mapping type of its svc to nodeport in the yaml file that created the pod, we can log in to the database by accessing port 31111 of any host in the cluster (the custom port in the yaml file). As follows:
[root@master lv] # mysql-uroot-p123.com-h 192.168.20.8-P 31111MySQL [(none)] > select * from test.my_id; # View data +-+ | id | +-+ | 1996 | +-+ 1 row in set (0.01 sec)
Set up a k8s cluster to achieve MySQL data persistence first to tell you here, for other related issues you want to know can continue to pay attention to our industry information. Our section will capture some industry news and professional knowledge to share with you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.