签到成功

知道了

CNDBA社区CNDBA社区

k8s mysql数据持久化

2023-06-24 16:00 759 0 原创 kubernetes
作者: hbhe0316

使用NFS作为数据共享存储
1.创建ClassStoragehttp://www.cndba.cn/hbhe0316/article/131397

[root@master mysql]# cat nfs-storageclass.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  namespace: default 
  name: ml-pv1 
  annotations:
    storageclass.kubernetes.io/is-default-class: "false"  
provisioner: storage-nfs
parameters:
  archiveOnDelete: "true" 
mountOptions: 
  - hard
  - nfsvers=4

[root@master mysql]# kubectl apply -f nfs-storageclass.yaml 
storageclass.storage.k8s.io/ml-pv1 created

2.创建PVC

http://www.cndba.cn/hbhe0316/article/131397
http://www.cndba.cn/hbhe0316/article/131397http://www.cndba.cn/hbhe0316/article/131397
http://www.cndba.cn/hbhe0316/article/131397
http://www.cndba.cn/hbhe0316/article/131397

[root@master mysql]# cat mysql-pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: model-db-pv-claim
  namespace: default
spec:
  storageClassName: ml-pv1
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

[root@master mysql]# kubectl apply -f mysql-pvc.yaml 
persistentvolumeclaim/model-db-pv-claim created

3.创建mysql podshttp://www.cndba.cn/hbhe0316/article/131397http://www.cndba.cn/hbhe0316/article/131397

[root@master mysql]# cat mysql.yaml 
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: wwwwww
        ports:
        - containerPort: 3306
        volumeMounts:
        - mountPath: "/var/lib/mysql"
          name: mysql-data
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: model-db-pv-claim
[root@master mysql]# kubectl apply -f mysql.yaml 
service/mysql created
deployment.apps/mysql created

查看sc,pvc,pods状态http://www.cndba.cn/hbhe0316/article/131397

[root@master mysql]# kubectl get sc -A
NAME          PROVISIONER   RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
ml-pv1        storage-nfs   Delete          Immediate           false                  5m50s
nfs-storage   storage-nfs   Delete          Immediate           false                  2d18h
[root@master mysql]# kubectl get pvc -A
NAMESPACE   NAME                                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
default     elasticsearch-data-es-es-master-0   Bound    pvc-74f05911-d39a-4bfe-9c6b-8c06a1ed1c4c   1Gi        RWO            nfs-storage    11d
default     model-db-pv-claim                   Bound    pvc-f99d7409-ed8e-47f3-bc5f-f7e1eb7ea859   5Gi        RWO            ml-pv1         4m33s
dev         storage-pvc                         Bound    pvc-fb376b03-4f35-4ebf-9053-6c67b1deca9d   10Gi       RWO            nfs-storage    2d18h

[root@master mysql]# kubectl get po --namespace=default
NAME                          READY   STATUS             RESTARTS       AGE
beat-beat-filebeat-22wnc      1/1     Running            14 (70m ago)   11d
beat-beat-filebeat-6t8jj      1/1     Running            22 (70m ago)   11d
beat-beat-filebeat-sr5dm      1/1     Running            13 (70m ago)   11d
demo-tomcat-7456f7f48-6v84k   0/1     ImagePullBackOff   10 (70m ago)   11d
es-es-master-0                1/1     Running            13 (70m ago)   11d
kibana-kb-676f6bff87-98bvq    1/1     Running            13 (70m ago)   11d
mysql-76b486b9f5-t84tt        1/1     Running            0              4m44s

测试mysql pod漂移后数据是否存在

http://www.cndba.cn/hbhe0316/article/131397

[root@master mysql]# kubectl exec -it mysql-76b486b9f5-t84tt -- mysql -uroot -pwwwwww
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.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 testdb;
Query OK, 1 row affected (0.03 sec)

mysql> use testdb;
Database changed
mysql> create table t1 (id int);
Query OK, 0 rows affected (0.04 sec)

mysql> insert into t1 values (1111);
Query OK, 1 row affected (0.02 sec)

mysql> select * from t1;
+------+
| id   |
+------+
| 1111 |
+------+
1 row in set (0.00 sec)

[root@master 0613]# kubectl get po -o wide
NAME                          READY   STATUS             RESTARTS       AGE     IP               NODE     NOMINATED NODE   READINESS GATES
beat-beat-filebeat-22wnc      1/1     Running            14 (73m ago)   11d     192.168.56.200   master   <none>           <none>
beat-beat-filebeat-6t8jj      1/1     Running            22 (73m ago)   11d     192.168.56.202   node02   <none>           <none>
beat-beat-filebeat-sr5dm      1/1     Running            13 (73m ago)   11d     192.168.56.201   node01   <none>           <none>
demo-tomcat-7456f7f48-6v84k   0/1     ImagePullBackOff   10 (73m ago)   11d     10.244.0.50      master   <none>           <none>
es-es-master-0                1/1     Running            13 (73m ago)   11d     10.244.2.57      node02   <none>           <none>
kibana-kb-676f6bff87-98bvq    1/1     Running            13 (73m ago)   11d     10.244.1.36      node01   <none>           <none>
mysql-76b486b9f5-t84tt        1/1     Running            0              7m45s   10.244.0.52      master   <none>           <none>
[root@master 0613]# kubectl delete pod mysql-76b486b9f5-t84tt
pod "mysql-76b486b9f5-t84tt" deleted
[root@master 0613]# kubectl get po -o wide
NAME                          READY   STATUS             RESTARTS       AGE   IP               NODE     NOMINATED NODE   READINESS GATES
beat-beat-filebeat-22wnc      1/1     Running            14 (73m ago)   11d   192.168.56.200   master   <none>           <none>
beat-beat-filebeat-6t8jj      1/1     Running            22 (73m ago)   11d   192.168.56.202   node02   <none>           <none>
beat-beat-filebeat-sr5dm      1/1     Running            13 (73m ago)   11d   192.168.56.201   node01   <none>           <none>
demo-tomcat-7456f7f48-6v84k   0/1     ImagePullBackOff   10 (73m ago)   11d   10.244.0.50      master   <none>           <none>
es-es-master-0                1/1     Running            13 (73m ago)   11d   10.244.2.57      node02   <none>           <none>
kibana-kb-676f6bff87-98bvq    1/1     Running            13 (73m ago)   11d   10.244.1.36      node01   <none>           <none>
mysql-76b486b9f5-nmbjz        1/1     Running            0              7s    10.244.1.38      node01   <none>           <none>

[root@master mysql]# kubectl exec -it mysql-76b486b9f5-nmbjz -- mysql -uroot -pwwwwww
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.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> use testdb;
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 t1;
+------+
| id   |
+------+
| 1111 |
+------+
1 row in set (0.00 sec)

版权声明:本文为博主原创文章,未经博主允许不得转载。

k8s

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
hbhe0316

hbhe0316

关注

1.只有承认无知,才能装下新的东西; 2.进步来自一点点滴滴的积累; 3.广博让你更优秀,而专业让你无法替代; 4.挫折和失败能够转换为一种财富。

  • 889
    原创
  • 1
    翻译
  • 13
    转载
  • 24
    评论
  • 访问:1136687次
  • 积分:1523
  • 等级:核心会员
  • 排名:第6名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ