k8s安装mysql进阶版

2022-10-27 13:32:08 浏览数 (2)

# MySQL实现数据持久化

# 1.搭建nfs存储

  1. 这里选择node2搭建nfs储存服务
代码语言:javascript复制
[root@node2 ~]# yum -y install nfs-utils
[root@node2 ~]# mkdir /nfsdata/mysql -p
[root@node2 ~]# vim /etc/exports
[root@node2 ~]# cat /etc/exports
/nfsdata *(rw,sync,no_root_squash)
[root@node2 ~]# systemctl restart nfs-server
[root@node2 ~]# systemctl enable rpcbind
[root@node2 ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@node2 ~]# showmount -e
Export list for node2:
/nfsdata *
  1. 在其他节点验证
代码语言:javascript复制
[root@node1 ~]# showmount -e 192.168.0.192
Export list for 192.168.0.192:
/nfsdata *

[root@master ~]# showmount -e 192.168.0.192
Export list for 192.168.0.192:
/nfsdata *

# 2.创建PV

代码语言:javascript复制
apiVersion: v1
kind: PersistentVolume
metadata: 
  name: mysql-pv
spec: 
  accessModes: 
    - ReadWriteOnce
  capacity: 
    storage: 1Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs: 
    path: "/nfsdata/mysql"
    server: 192.168.0.192
  • 执行
代码语言:javascript复制
[root@master kmysql]# vim mysql-pv.yaml
[root@master kmysql]# kubectl create -f mysql-pv.yaml 
persistentvolume/mysql-pv created

# 3.创建PVC

代码语言:javascript复制
[root@master kmysql]# cat mysql-pvc.yaml 
kind: PersistentVolumeClaim
apiVersion:  v1
metadata: 
  name: mysql-pvc
spec: 
  accessModes: 
    - ReadWriteOnce       
  resources: 
    requests: 
      storage: 1Gi
  storageClassName: nfs  
  • 执行
代码语言:javascript复制
[root@master kmysql]# vim mysql-pvc.yaml
[root@master kmysql]# kubectl create -f mysql-pvc.yaml
persistentvolumeclaim/mysql-pvc created

# 4.确认pv及pvc的状态

代码语言:javascript复制
[root@master kmysql]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE
mysql-pv   1Gi        RWO            Retain           Bound    default/mysql-pvc   nfs                     66s
[root@master kmysql]# kubectl get pvc
NAME        STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-pvc   Bound    mysql-pv   1Gi        RWO            nfs            24s

# 5.创建pod svc

代码语言:javascript复制
---
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: mysql
spec: 
  selector: 
    matchLabels: 
      app: mysql
  template: 
    metadata: 
      labels: 
        app: mysql
    spec: 
      containers: 
      - name: mysql 
        image: mysql:5.7
        env:       
        - name: MYSQL_ROOT_PASSWORD
          value: hadoop
        ports: 
        - containerPort: 3306
        volumeMounts: 
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql   
      volumes: 
      - name: mysql-persistent-storage
        persistentVolumeClaim: 
          claimName: mysql-pvc        
---
apiVersion: v1
kind: Service
metadata: 
  name: mysql
spec: 
  type: NodePort
  ports: 
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: http
    nodePort: 30306
  selector: 
    app: mysql
  • 执行
代码语言:javascript复制
[root@master kmysql]# kubectl create -f mysql-pod.yaml
deployment.apps/mysql created
service/mysql created

# 5.验证

  • 进入MySQL数据库,添加测试数据
代码语言:javascript复制
[root@master kmysql]# kubectl get pod -w
NAME                    READY   STATUS    RESTARTS   AGE
mysql-ddd86f8fb-hx6fs   1/1     Running   0          16s
[root@master kmysql]# kubectl exec -it mysql-ddd86f8fb-hx6fs -- mysql -uroot -phadoop
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 2
Server version: 5.7.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> create database test;
Query OK, 1 row affected (0.01 sec)

mysql> use test
Database changed
mysql> create table my_id(id int(11));
Query OK, 0 rows affected (0.01 sec)

mysql> insert my_id values(1234);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test.my_id; 
 ------ 
| id   |
 ------ 
| 1234 |
 ------ 
1 row in set (0.00 sec)

mysql> 
  • 手动删除节点上的容器,验证数据库内的数据是否还存在
代码语言:javascript复制
[root@master kmysql]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
mysql-ddd86f8fb-hx6fs   1/1     Running   0          17m
[root@master kmysql]# kubectl delete pods mysql-ddd86f8fb-hx6fs
pod "mysql-ddd86f8fb-hx6fs" deleted
[root@master kmysql]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
mysql-ddd86f8fb-qccqc   1/1     Running   0          13s
[root@master kmysql]# kubectl exec -it mysql-ddd86f8fb-qccqc -- mysql -uroot -phadoop
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 2
Server version: 5.7.32 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> showdatabases;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'showdatabases' at line 1
mysql> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
 -------------------- 
5 rows in set (0.01 sec)

mysql> select * from test.my_id;
 ------ 
| id   |
 ------ 
| 1234 |
 ------ 
1 row in set (0.01 sec)

mysql> 
  • 模拟MySQL容器所在的节点宕机,验证数据是否会丢失
代码语言:javascript复制
//查看容器所在节点
[root@master kmysql]# kubectl get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
mysql-ddd86f8fb-k7jrn   1/1     Running   0          20m   100.66.209.199   node1   <none>           <none>

可以看到目前容器所在节点为node1,于是关闭node1节点机器:

代码语言:javascript复制
[root@node1 kmysql]# shutdown -h now
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.0.191) at 14:02:24.

继续验证:

代码语言:javascript复制
[root@master kmysql]# kubectl get pod -o wide
NAME                    READY   STATUS        RESTARTS   AGE   IP               NODE    NOMINATED NODE   READINESS GATES
mysql-ddd86f8fb-gn8kc   1/1     Running       0          42s   100.108.11.198   node2   <none>           <none>
mysql-ddd86f8fb-k7jrn   1/1     Terminating   0          27m   100.66.209.199   node1   <none>           <none>

pod由node1转移到了node2并继续运行

  • 由于Deployment的保护策略,当删除容器后,它会根据pod的yaml文件生成一个新的容器,只是新容器的ID号变了
  • 还可以在远程客户端上下载mysql客户端Navicat,进行验证这里就不贴图了
  • 结论:当使用PV来做数据持久化的话,容器的新陈代谢并不会造成丢失数据的

# 5.查看持久化数据卷

代码语言:javascript复制
[root@node2 ~]# cd /nfsdata/
[root@node2 nfsdata]# ll
total 4
drwxr-xr-x. 6 polkitd root 4096 Oct 23 21:41 mysql
[root@node2 nfsdata]# cd mysql/
[root@node2 mysql]# ll
total 188484
-rw-r-----. 1 polkitd input       56 Oct 23 19:30 auto.cnf
-rw-------. 1 polkitd input     1680 Oct 23 19:30 ca-key.pem
-rw-r--r--. 1 polkitd input     1112 Oct 23 19:30 ca.pem
-rw-r--r--. 1 polkitd input     1112 Oct 23 19:30 client-cert.pem
-rw-------. 1 polkitd input     1676 Oct 23 19:30 client-key.pem
-rw-r-----. 1 polkitd input      455 Oct 23 21:39 ib_buffer_pool
-rw-r-----. 1 polkitd input 79691776 Oct 23 21:41 ibdata1
-rw-r-----. 1 polkitd input 50331648 Oct 23 21:41 ib_logfile0
-rw-r-----. 1 polkitd input 50331648 Oct 23 19:30 ib_logfile1
-rw-r-----. 1 polkitd input 12582912 Oct 23 21:46 ibtmp1
drwxr-x---. 2 polkitd input     4096 Oct 23 19:30 mysql
drwxr-x---. 2 polkitd input     8192 Oct 23 19:30 performance_schema
-rw-------. 1 polkitd input     1676 Oct 23 19:30 private_key.pem
-rw-r--r--. 1 polkitd input      452 Oct 23 19:30 public_key.pem
-rw-r--r--. 1 polkitd input     1112 Oct 23 19:30 server-cert.pem
-rw-------. 1 polkitd input     1680 Oct 23 19:30 server-key.pem
drwxr-x---. 2 polkitd input     8192 Oct 23 19:31 sys
drwxr-x---. 2 polkitd input       54 Oct 23 19:45 test
[root@node2 mysql]# pwd
/nfsdata/mysql

0 人点赞