最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

K8s中MySQL?數(shù)據(jù)持久化存儲的實現(xiàn)

 更新時間:2023年01月08日 09:34:26   作者:云計算-Security  
本文主要介紹了K8s中MySQL?數(shù)據(jù)持久化存儲的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、步驟

(1)創(chuàng)建 PV 和 PVC。

(2)部署 MySQL 服務(wù)。

(3)向 MySQL 添加數(shù)據(jù)。

(4)模擬節(jié)點宕機(jī),Kubernetes 實現(xiàn) MySQL 自動故障轉(zhuǎn)移。

(5)自動故障轉(zhuǎn)移后進(jìn)行數(shù)據(jù)一致性驗證。

:本次通過 NFS 做數(shù)據(jù)持久化存儲。

二、實踐

2.1 創(chuàng)建 PV 和 PVC

1、創(chuàng)建 PV

vim mysql-pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  labels:
    pv: pv-test
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /home/data/app
    server: 192.168.56.160

2、創(chuàng)建 PVC

vim mysql-pvc.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: pv-test

3、創(chuàng)建 PV 和 PVC 資源

kubectl apply -f mysql-pv.yml
kubectl apply -f mysql-pvc.yml

查看 PV、PVC 資源

kubectl get pv,pvc

image-20230105100708675

2.2 部署 MySQL 服務(wù)

1、創(chuàng)建 YAML 文件

vim mysql.yml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306
      nodePort: 31306
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql-test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql-test
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: Zhurs@123
        ports:
        - containerPort: 3306
        volumeMounts:
        - mountPath: "/var/lib/mysql"
          name: mysql-data
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql-pvc

PVC 資源 mysql-pvc Bound 的 PV 資源 mysql-pv 將被 mount 到 MySQL 的數(shù)據(jù)目錄 /var/lib/mysql

2、部署 MySQL 服務(wù)

kubectl apply -f mysql.yml

查看 Pod、Service 資源,看是否正常運(yùn)行。

image-20230105105124485

2.3 創(chuàng)建測試數(shù)據(jù)

1、進(jìn)入MySQL 數(shù)據(jù)庫

kubectl exec -it mysql-574654d66c-hhvxc -- mysql -uroot -pZhurs@123

image-20230105105638454

2、創(chuàng)建測試數(shù)據(jù)

mysql> create database testdb;
mysql> use testdb;
mysql> create table t1(id int);
mysql> insert into t1 values(12);
mysql> select * from t1;

image-20230105110029135

2.4 模擬節(jié)點宕機(jī)

1、先看看 MySQL 服務(wù)運(yùn)行在哪個節(jié)點上

kubectl get pod -o wide

image-20230105110508216

2、停掉 k8s-work1 節(jié)點

init 0

image-20230105110707873

image-20230105110912194

3、一段時間后 MySQL 遷移到 k8s-work2 節(jié)點上

原來的 Pod 停止服務(wù),新的 Pod 正在創(chuàng)建,而且是在 k8s-work2 節(jié)點上進(jìn)行創(chuàng)建。

image-20230105111322680

最終 MySQL 服務(wù)成功遷移至 k8s-work2 節(jié)點上,我原本的 Nginx 服務(wù)也成功遷移到了 k8s-work2 節(jié)點了。

image-20230105113349459

2.5 數(shù)據(jù)一致性驗證

1、進(jìn)入MySQL 數(shù)據(jù)庫

kubectl exec -it mysql-574654d66c-jst7g -- mysql -uroot -pZhurs@123

image-20230105113543824

2、驗證數(shù)據(jù)一致性

image-20230105113730556

可見,遷移后的 MySQL 數(shù)據(jù)依然是完整的。

FAQ

1、報錯類型

注意一點,因為我們使用的是 NFS 文件共享存儲,因此目標(biāo)節(jié)點必須要有 NFS 的客戶端服務(wù),下圖就是我沒安裝 NFS 客戶端服務(wù)的原因。

image-20230105112755921

2、解決方案

在目標(biāo)節(jié)點(k8s-work1)部署 NFS 服務(wù)

yum -y install rpcbind
yum -y install nfs-utils
systemctl start nfs-server
systemctl enable nfs-server
systemctl start rpcbind
systemctl enable rpcbind

最后在看看輸出日志,Pod 正常運(yùn)行了。

image-20230105112956370

到此這篇關(guān)于K8s中MySQL 數(shù)據(jù)持久化存儲的實現(xiàn)的文章就介紹到這了,更多相關(guān)K8s MySQL數(shù)據(jù)持久化存儲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

噶尔县| 宣威市| 张家口市| 仁布县| 永定县| 新平| 酒泉市| 崇左市| 碌曲县| 新化县| 金塔县| 赫章县| 和政县| 阿克苏市| 罗江县| 班玛县| 大宁县| 赫章县| 张家港市| 余姚市| 怀集县| 孟津县| 新干县| 司法| 阳山县| 吉安市| 商洛市| 荣昌县| 哈密市| 安达市| 丹寨县| 常德市| 凤凰县| 当涂县| 昭平县| 临武县| 涿鹿县| 临安市| 缙云县| 临沧市| 高阳县|