[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 *
[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 *
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
[root@master kmysql]# vim mysql-pv.yaml
[root@master kmysql]# kubectl create -f mysql-pv.yaml
persistentvolume/mysql-pv created
[root@master kmysql]# cat mysql-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
[root@master kmysql]# vim mysql-pvc.yaml
[root@master kmysql]# kubectl create -f mysql-pvc.yaml
persistentvolumeclaim/mysql-pvc created
[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
---
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
[root@master kmysql]# kubectl create -f mysql-pod.yaml
deployment.apps/mysql created
service/mysql created
[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>
[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>
//查看容器所在节点
[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节点机器:
[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.
继续验证:
[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并继续运行
[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
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有