我們接著之前的文檔的架構(gòu)規(guī)劃進(jìn)行下面的操作。
創(chuàng)新互聯(lián)公司是專業(yè)的蒙自網(wǎng)站建設(shè)公司,蒙自接單;提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行蒙自網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
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 數(shù)據(jù)庫(kù)提供持久化存儲(chǔ),主要分為下面幾個(gè)步驟:
我們 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
創(chuàng)建 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
內(nèi)容如下:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
創(chuàng)建 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 的數(shù)據(jù)目錄 /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
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>
我們?cè)?code>mysql庫(kù)中創(chuàng)建一個(gè)表myid
,然后在表里新增幾條數(shù)據(jù)。
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)
我們現(xiàn)在把 node02 機(jī)器關(guān)機(jī),模擬節(jié)點(diǎn)宕機(jī)故障。
一段時(shí)間之后,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
mysql-7686899cf9-d4m42 1/1 Terminating 0 23m 10.244.2.17 node02
驗(yàn)證數(shù)據(jù)的一致性。
[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 服務(wù)恢復(fù),數(shù)據(jù)也完好無損,我們可以可以在存儲(chǔ)節(jié)點(diǎn)上面查看一下生成的數(shù)據(jù)庫(kù)文件。
[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