k8s实战--编写mysql的持久化deployment
1.实验-编写mysql的持久化deployment
#先打标签
[root@node1 ~/volume]# kubectl label nodes node3 disktype=SSD
#编写资源清单
[root@node1 ~/volume]# cat mysql-hostpath-ssd.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-dp
namespace: default
spec:
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
name: mysql-pod
namespace: default
labels:
app: mysql
spec:
containers:
- name: mysql-pod
image: mysql:5.7
ports:
- name: mysql-port
containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql-volume
volumes:
- name: mysql-volume
hostPath:
path: /data/mysql
type: DirectoryOrCreate
nodeSelector:
disktype: SSD
2.检查是否生成容器
[root@node1 ~/volume]# kubectl get pod
mysql-dp-d9dd6799d-rn4lf 1/1 Running 0 3m56s
3.登录并进入mysql镜像
[root@node1 ~/volume]# kubectl exec -it mysql-dp-d9dd6799d-rn4lf /bin/bash
root@mysql-dp-d9dd6799d-rn4lf:/#
4.在容器里面登录数据库
root@mysql-dp-d9dd6799d-rn4lf:/# mysql -uroot -p123456
5.创建数据库并查看
mysql> create database dyc;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dyc |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
6.退出并删除mysql容器的资源清单
[root@node1 ~/volume]# kubectl delete -f mysql-hostpath-ssd.yaml
7.再次应用资源清单
[root@node1 ~/volume]# kubectl create -f mysql-hostpath-ssd.yaml
8.登录新生成的数据库容器
[root@node1 ~/volume]# kubectl exec -it mysql-dp-d9dd6799d-dg6p8 /bin/bash
root@mysql-dp-d9dd6799d-dg6p8:/#
9.登录容器中的数据可并查看之前创建的数据库是否存在
root@mysql-dp-d9dd6799d-dg6p8:/# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dyc | #之前创建的数据库存在,即持久化成功
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
10.查看加点mysql容器是否把数据映射到宿主机
[root@node3 ~]# ll /data/mysql/
drwxr-x--- 2 polkitd input 20 3月 2 21:46 dyc
drwxr-x--- 2 polkitd input 4096 3月 2 21:40 mysql