kubernetes云平台管理实战:持久化实战(二十)

一、创建tomcat+mysql集群

详细过程参考博文:https://www.cnblogs.com/luoahong/p/12917822.html

未添加数据截图如下

二、数据库未持久化数据库重启数据丢失

1、添加数据

2、删除数据库容器

[root@master tomcat_demo]# kubectl get pods
NAME              READY     STATUS    RESTARTS   AGE
po/mysql-3qkf1    1/1       Running   0          2d
po/myweb-d338g    1/1       Running   0          1m
po/myweb1-xxxqs   1/1       Running   0          1d
[root@master tomcat_demo]# kubectl delete pod mysql-3qkf1
pod "mysql-3qkf1" deleted
[root@master tomcat_demo]# kubectl get pods
NAME           READY     STATUS    RESTARTS   AGE
mysql-8tmlv    1/1       Running   0          8s
myweb-s7gm5    1/1       Running   0          8m
myweb1-xxxqs   1/1       Running   0          1d

3、再次访问数据丢失啦

三、创建pv和pvc

1、创建运行PV

[root@master tomcat_demo]# cp ../volume/test-pv* .
[root@master tomcat_demo]# ls
mysql-rc.yml  mysql-svc.yml  test-pvc.yaml  test-pv.yaml  tomcat-rc.yml  tomcat-svc.yml
[root@master tomcat_demo]# mv test-pv.yaml mysql-pv.yaml 
[root@master tomcat_demo]# vim mysql-pv.yaml
[root@master tomcat_demo]# cat mysql-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql
  labels:
    type: mysql
spec:
  capacity:
    storage: 10Gi 
  accessModes:
    - ReadWriteMany 
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path:  "/data/mysql"
    server: 192.168.118.18
    readOnly: false
[root@master tomcat_demo]# mkdir /data/mysql
[root@master tomcat_demo]# kubectl create -f mysql-pv.yaml 
persistentvolume "mysql" created

2、创建运行PVC

[root@master tomcat_demo]# mv test-pvc.yaml mysql-pvc.yaml 
[root@master tomcat_demo]# vim mysql-pvc.yaml 
[root@master tomcat_demo]# cat mysql-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
[root@master tomcat_demo]# kubectl create -f  mysql-pvc.yaml 
persistentvolumeclaim "mysql" created
[root@master tomcat_demo]# 
[root@master tomcat_demo]# kubectl get pvc
NAME      STATUS    VOLUME    CAPACITY   ACCESSMODES   AGE
mysql     Bound     mysql     10Gi       RWX           1m
nfs       Bound     test2     5Gi        RWX           43m
nfs2      Bound     test      10Gi       RWX           39m

四、pod使用pvc

创建mysql-rc-pvc.yaml

[root@master tomcat_demo]# vim mysql-rc-pvc.yaml 
[root@master tomcat_demo]# cat mysql-rc-pvc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: 192.168.118.18:5000/mysql:5.7
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: '123456'
          volumeMounts:
          - name: data
            mountPath: /var/lib/mysql
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: mysql

 运行检查

[root@master tomcat_demo]# kubectl apply -f mysql-rc-pvc.yaml 
replicationcontroller "mysql" configured
[root@master tomcat_demo]# kubectl get pods
NAME           READY     STATUS    RESTARTS   AGE
mysql-8stxt    1/1       Running   0          7s
myweb-s7gm5    1/1       Running   0          29m
myweb1-xxxqs   1/1       Running   0          1d

四、测试持久化

1、web页面添加数据

2、查看共享目录

[root@master tomcat_demo]# ls /data/mysql/
auto.cnf  HPE_APP  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  sys
[root@master tomcat_demo]# ls /data/mysql/
auto.cnf  HPE_APP  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  mysql  performance_schema  sys

3、删除数据库容器

[root@master tomcat_demo]# kubectl delete pod mysql-8stxt
pod "mysql-8stxt" deleted
[root@master tomcat_demo]# kubectl get all -o wide
NAME              READY     STATUS    RESTARTS   AGE       IP            NODE
po/mysql-lfp5h    1/1       Running   1          4s        172.16.60.3   192.168.118.19
po/myweb-s7gm5    1/1       Running   0          37m       172.16.56.6   192.168.118.18
po/myweb1-xxxqs   1/1       Running   0          1d        172.16.60.2   192.168.118.19

4、查看web界面数据依然存在

posted @ 2020-05-22 22:14  活的潇洒80  阅读(154)  评论(0编辑  收藏