部署NFS

安装nfs服务

1
[root@pool1 ~]# yum -y install nfs-utils rpcbind

创建共享目录

1
[root@pool1 ~]# mkdir /data/nfs_data/mysql_data

编写共享配置文件

1
2
[root@pool1 ~]# vi /etc/exports
/data/nfs_data/mysql_data *(rw,sync)

启动服务

1
[root@pool1 ~]# systemctl start nfs nfs-utils rpcbind

node节点挂载nfs共享目录

1
2
3
4
5
6
7
[root@pool2 ~]# showmount -e 10.99.2.155
Export list for 10.99.2.155:
/data/nfs_data/mysql_data *
[root@pool2 ~]# mkdir /data/nfs_data/mysql_data -p
[root@pool2 ~]# mount -t nfs 10.99.2.155:/data/nfs_data/mysql_data/ /data/nfs_data/mysql_data/
[root@pool2 ~]# df -hT | grep mysql_data
10.99.2.155:/data/nfs_data/mysql_data nfs4 47G 36G 12G 77% /data/nfs_data/mysql_data

部署MySQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@pool1 yaml]# vi mysql-deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deploy
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- env:
- name: MYSQL_ROOT_PASSWORD
value: "as2a2nf0"
- name: MYSQL_USER
value: "zusir"
- name: MYSQL_PASSWORD
value: "as2a2nf0"
name: mysql
image: mysql:8.0
imagePullPolicy: Never
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-data
mountPath: "/var/lib/mysql"
readOnly: false
volumes:
- name: mysql-data
nfs:
server: 10.99.2.155
path: "/data/nfs_data/mysql_data"

[root@pool1 yaml]# vi mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
ports:
- port: 3306
targetPort: 3306

[root@pool1 yaml]# kubectl apply -f mysql-deploy.yaml
[root@pool1 yaml]# kubectl apply -f mysql-svc.yaml
[root@pool1 yaml]# kubectl get pods -o wide | grep mysql
mysql-deploy-69c6869d65-9ffqr 1/1 Running 0 20h 240.185.2.76 pool3 <none> <none>
[root@pool1 yaml]# kubectl get svc | grep mysql
mysql ClusterIP 10.102.237.57 <none> 3306/TCP 7d4h

登录数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@pool1 yaml]# kubectl exec -it mysql-deploy-69c6869d65-9ffqr bash
root@mysql-deploy-69c6869d65-9ffqr:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

创建数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
mysql> create database dangan;
Query OK, 1 row affected (0.01 sec)

mysql> create database zx;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| dangan |
| information_schema |
| mysql |
| performance_schema |
| sys |
| zx |
+--------------------+
6 rows in set (0.00 sec)

mysql>

删除POD 模拟pod故障down掉

1
2
3
4
5
6
[root@pool1 yaml]# kubectl delete -f mysql-deploy.yaml 
deployment.apps "mysql-deploy" deleted
[root@pool1 yaml]# kubectl get pods | grep mysql
mysql-deploy-69c6869d65-9ffqr 0/1 Terminating 0 20h

# POD已经是终止状态,不再运行

启用新POD

1
2
3
4
[root@pool1 yaml]# kubectl apply -f mysql-deploy.yaml 
deployment.apps/mysql-deploy created
[root@pool1 yaml]# kubectl get pods | grep mysql
mysql-deploy-69c6869d65-sbzhl 1/1 Running 0 3s

登录新POD数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@pool1 yaml]# kubectl exec -it mysql-deploy-69c6869d65-sbzhl bash
root@mysql-deploy-69c6869d65-sbzhl:/# mysql -u root -pas2a2nf0
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| dangan |
| information_schema |
| mysql |
| performance_schema |
| sys |
| zx |
+--------------------+
6 rows in set (0.01 sec)
# 此时数据库中的数据已经恢复