您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
我们接着之前的文档的架构规划进行下面的操作。
IP | 角色 |
---|---|
192.168.1.200 | k8s-master |
192.168.1.201 | k8s-node01 |
192.168.1.202 | k8s-node02 |
192.168.1.203 | k8s-store |
我们演示如何为 MySQL 数据库提供持久化存储,主要分为下面几个步骤:
我们 PV 的配置文件mysql-pv.yaml
如下:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
nfs:
path: /data/volumes/mysql-pv
server: 192.168.1.203
创建 PV。
[root@master ~]# kubectl apply -f mysql-pv.yaml
persistentvolume/mysql-pv created
[root@master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv 1Gi RWO Retain Available 7s
PVC 的配置文件mysql-pvc.yaml
内容如下:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
创建 PVC。
[root@master ~]# kubectl apply -f mysql-pvc.yaml
persistentvolumeclaim/mysql-pvc created
[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pvc Bound mysql-pv 1Gi RWO 7s
MySQL 的配置文件mysql.yaml
如下:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.6
env:
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
PVC mysql-pvc
Bound 的 PV mysql-pv
将被 mount 到 MySQL 的数据目录 /var/lib/mysql
。
[root@master ~]# kubectl apply -f mysql.yaml
service/mysql created
deployment.extensions/mysql created
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-7686899cf9-d4m42 1/1 Running 0 62s 10.244.2.17 node02 <none> <none>
MySQL 被部署到 k8s-node02
,下面通过客户端访问 Service mysql:
[root@master ~]# kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword
If you don't see a command prompt, try pressing enter.
mysql>
我们在mysql
库中创建一个表myid
,然后在表里新增几条数据。
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table myid( id int(4) );
Query OK, 0 rows affected (0.04 sec)
mysql> insert myid values( 111 );
Query OK, 1 row affected (0.00 sec)
mysql> select * from myid;
+------+
| id |
+------+
| 111 |
+------+
1 row in set (0.00 sec)
我们现在把 node02 机器关机,模拟节点宕机故障。
一段时间之后,Kubernetes 将 MySQL 迁移到 k8s-node01
。
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-7686899cf9-8z6tc 1/1 Running 0 21s 10.244.1.19 node01 <none> <none>
mysql-7686899cf9-d4m42 1/1 Terminating 0 23m 10.244.2.17 node02 <none> <none>
验证数据的一致性。
[root@master ~]# kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword
If you don't see a command prompt, try pressing enter.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from myid;
+------+
| id |
+------+
| 111 |
+------+
1 row in set (0.00 sec)
MySQL 服务恢复,数据也完好无损,我们可以可以在存储节点上面查看一下生成的数据库文件。
[root@datanode03 mysql-pv]# ll
总用量 110604
-rw-rw---- 1 systemd-bus-proxy ssh_keys 56 12月 14 09:53 auto.cnf
-rw-rw---- 1 systemd-bus-proxy ssh_keys 12582912 12月 14 10:15 ibdata1
-rw-rw---- 1 systemd-bus-proxy ssh_keys 50331648 12月 14 10:15 ib_logfile0
-rw-rw---- 1 systemd-bus-proxy ssh_keys 50331648 12月 14 09:53 ib_logfile1
drwx------ 2 systemd-bus-proxy ssh_keys 4096 12月 14 10:05 mysql
drwx------ 2 systemd-bus-proxy ssh_keys 4096 12月 14 09:53 performance_schema
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。