当前位置:   article > 正文

使用k8s部署Mysql实例~hostpath,nfs服务器,PV和PVC持久化_k8s mysql hostpath

k8s mysql hostpath

1.k8s数据存储的分类:

1.基础存储的分类:

分类说明
EmptyDirEmptyDir是在Pod被分配到Node时创建的,无须手动指定,当Pod销毁时, EmptyDir中的数据也会被永久删除
HostPathHostPath就是将Node主机中一个实际目录挂载到Pod中,保证Pod销毁了,但是数据依旧可以存在于Node主机上
NFSNFS是一个网络文件存储系统,解决了节点故障数据异常的问题

2.高级存储:

分类说明
PV(Persistent Volume)持久化卷的意思,是对底层的共享存储的一种抽象,PV相似于卷组(存储池)
PVC(Persistent Volume Claim)持久卷声明的意思,用户对于存储需求的一种声明,PVC如同在卷组中划分出的逻辑卷

3.高级存储的分类:

分类说明
ConfigMapConfigMap是一种比较特殊的存储卷,它的主要作用是用来存储配置信息的,明文保存
Secret它主要用于存储敏感信息,例如密码、秘钥、证书等等,可加密保存

2.基础存储-hostpath

1.创建mysql的yaml文件

  1. cat << eof > mysql.yaml
  2. apiVersion: apps/v1 # 通过kubectl explain deployment命令查看版本
  3. kind: Deployment # 资源类型
  4. metadata:
  5. name: mysql-deployment # 资源名称
  6. namespace: mysql # 指定命名空间
  7. spec:
  8. replicas: 1 # 控制器下的pod数
  9. selector:
  10. matchLabels:
  11. app: mysql # 这是控制器用来控制pod的标签
  12. template:
  13. metadata:
  14. labels:
  15. app: mysql # pod的标签
  16. spec:
  17. containers:
  18. - name: mysql # 容器名
  19. image: mysql:8.0.20 # 容器镜像
  20. env: # mysql环境参数
  21. - name: MYSQL_ROOT_PASSWORD # 指定ROOT用户密码
  22. value: "你的密码"
  23. ports: # 容器暴露的端口
  24. - name: mysql
  25. containerPort: 3306
  26. volumeMounts: # 容器内的被挂载目录
  27. - name: mysql-data
  28. mountPath: /var/lib/mysql
  29. - name: mysql-config
  30. mountPath: /etc/mysql/conf.d
  31. volumes: # 声明volume(宿主机目录), name为logs-volume
  32. - name: mysql-data
  33. hostPath:
  34. path: /root/mysql/data
  35. type: DirectoryOrCreate # 目录存在就使用,不存在就先创建后使用
  36. - name: mysql-config
  37. hostPath:
  38. path: /root/mysql/conf
  39. type: DirectoryOrCreate # 目录存在就使用,不存在就先创建后使用
  40. ---
  41. apiVersion: v1
  42. kind: Service
  43. metadata:
  44. name: mysql-nodeport
  45. namespace: mysql
  46. spec:
  47. selector:
  48. app: mysql # 服务通过此标签连接deployment资源来暴露端口
  49. type: NodePort # 服务类型:NodePort
  50. ports: # 不指定NodePort端口就会自动分配,(默认的取值范围是:30000-32767)
  51. - protocol: TCP
  52. port: 3306
  53. targetPort: 3306
  54. eof

2.创建mysq应用:

kubectl apply -f mysql.yaml

3.查看pod:

  1. [root@master mysql]# kubectl get deployment,pod,svc -n mysql -o wide
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. deployment.apps/mysql-deployment 1/1 1 1 7m57s
  4. deployment.apps/nginx 1/1 1 1 15d
  5. NAME READY STATUS RESTARTS AGE
  6. pod/mysql-deployment-b68b5b8bc-jps5c 1/1 Running 0 7m57s
  7. pod/nginx-6867cdf567-phn5l 1/1 Running 1 148m
  8. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  9. service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15d
  10. service/mysql-nodeport NodePort 10.98.118.107 <none> 3306:30160/TCP 7m57s
  11. service/nginx NodePort 10.103.194.224 <none> 80:30105/TCP 15d

4.远程连接:

  1. [root@localhost ~]# mysql -h192.168.174.30 -P30160 -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.20 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>

3.基础存储-nfs服务器

1.安装nfs服务器:

  • master节点上:
    • 使配置生效

      exportfs -r
    • 将共享目录以读写权限暴露给192.168.174.0/24网段中的所有主机

      1. cat << eof >> /etc/exports
      2. /root/nfs/mysql 192.168.174.0/24(rw,no_root_squash)
      3. eof
    • 启动nfs服务

      systemctl enable nfs-server;systemctl restart nfs-server
    • 准备一个共享目录

      mkdir -p /root/nfs/mysql -pv;chmod o=rwx /root/nfs/mysql
    • 在master上安装nfs服务:

      yum install nfs-utils -y
  • node节点:

    • 接下来,要在的每个node节点上都安装下nfs,这样的目的是为了node节点可以驱动nfs设备

      yum install nfs-utils -y
      systemctl enable nfs-server;systemctl restart nfs-server

2.创建mysql的yaml文件:

  1. cat << eof > mysql.yaml
  2. apiVersion: apps/v1 # 通过kubectl explain deployment命令查看版本
  3. kind: Deployment # 资源类型
  4. metadata:
  5. name: mysql-deployment # 资源名称
  6. namespace: mysql # 指定命名空间
  7. spec:
  8. replicas: 1 # 控制器下的pod数
  9. selector:
  10. matchLabels:
  11. app: mysql # 这是控制器用来控制pod的标签
  12. template:
  13. metadata:
  14. labels:
  15. app: mysql # pod的标签
  16. spec:
  17. containers:
  18. - name: mysql # 容器名
  19. image: mysql:8.0.20 # 容器镜像
  20. env: # mysql环境参数
  21. - name: MYSQL_ROOT_PASSWORD # 指定ROOT用户密码
  22. value: "你的密码"
  23. ports: # 容器暴露的端口
  24. - name: mysql
  25. containerPort: 3306
  26. volumeMounts: # 容器内的被挂载目录
  27. - name: mysql-data
  28. mountPath: /var/lib/mysql
  29. volumes: # 声明volume(宿主机目录), name为logs-volume
  30. - name: mysql-data
  31. nfs:
  32. server: 192.168.174.30 # nfs服务器地址
  33. # /root/nfs/mysql是共享目录,data是创建的一个目录用来存储mysql数据
  34. path: /root/nfs/mysql/data # 共享文件路径(服务器地址)
  35. ---
  36. apiVersion: v1
  37. kind: Service
  38. metadata:
  39. name: mysql-nodeport
  40. namespace: mysql
  41. spec:
  42. selector:
  43. app: mysql # 服务通过此标签连接deployment资源来暴露端口
  44. type: NodePort # 服务类型:NodePort
  45. ports: # 不指定NodePort端口就会自动分配,(默认的取值范围是:30000-32767)
  46. - protocol: TCP
  47. port: 3306
  48. targetPort: 3306
  49. eof

3.创建mysq应用:

kubectl apply -f mysql.yaml

4.查看pod:

  1. [root@master ~]# kubectl get deployment,pod,svc -n mysql -o wide
  2. NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
  3. deployment.apps/mysql-deployment 1/1 1 1 3m17s mysql mysql:8.0.20 app=mysql
  4. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  5. pod/mysql-deployment-5fb47b585b-k7bdm 1/1 Running 0 3m17s 10.244.2.9 node2 <none> <none>
  6. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  7. service/mysql-nodeport NodePort 10.99.39.122 <none> 3306:31819/TCP 3m17s app=mysql

5.查看master节点上的目录:

pod所在node节点是没有目录的,直接使用nfs服务器的目录

  1. [root@master ~]# ll nfs/mysql/data/
  2. total 181696
  3. -rw-r----- 1 polkitd input 56 Nov 29 05:20 auto.cnf
  4. -rw-r----- 1 polkitd input 912 Nov 29 05:20 binlog.000001
  5. -rw-r----- 1 polkitd input 16 Nov 29 05:20 binlog.index
  6. -rw------- 1 polkitd input 1680 Nov 29 05:20 ca-key.pem
  7. -rw-r--r-- 1 polkitd input 1112 Nov 29 05:20 ca.pem
  8. -rw-r--r-- 1 polkitd input 1112 Nov 29 05:20 client-cert.pem
  9. -rw------- 1 polkitd input 1680 Nov 29 05:20 client-key.pem
  10. -rw-r----- 1 polkitd input 196608 Nov 29 05:20 #ib_16384_0.dblwr
  11. -rw-r----- 1 polkitd input 8585216 Nov 29 05:20 #ib_16384_1.dblwr
  12. -rw-r----- 1 polkitd input 5498 Nov 29 05:20 ib_buffer_pool
  13. -rw-r----- 1 polkitd input 12582912 Nov 29 05:20 ibdata1
  14. -rw-r----- 1 polkitd input 50331648 Nov 29 05:20 ib_logfile0
  15. -rw-r----- 1 polkitd input 50331648 Nov 29 05:20 ib_logfile1
  16. -rw-r----- 1 polkitd input 12582912 Nov 29 05:20 ibtmp1
  17. drwxr-x--- 2 polkitd input 187 Nov 29 05:20 #innodb_temp
  18. drwxr-x--- 2 polkitd input 143 Nov 29 05:20 mysql
  19. -rw-r----- 1 polkitd input 29360128 Nov 29 05:20 mysql.ibd
  20. drwxr-x--- 2 polkitd input 8192 Nov 29 05:20 performance_schema
  21. -rw------- 1 polkitd input 1680 Nov 29 05:20 private_key.pem
  22. -rw-r--r-- 1 polkitd input 452 Nov 29 05:20 public_key.pem
  23. -rw-r--r-- 1 polkitd input 1112 Nov 29 05:20 server-cert.pem
  24. -rw------- 1 polkitd input 1680 Nov 29 05:20 server-key.pem
  25. drwxr-x--- 2 polkitd input 28 Nov 29 05:20 sys
  26. -rw-r----- 1 polkitd input 10485760 Nov 29 05:20 undo_001
  27. -rw-r----- 1 polkitd input 11534336 Nov 29 05:20 undo_002

6.远程连接:

  1. [root@localhost ~]# mysql -h192.168.174.30 -P30160 -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.20 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>

4.高级存储-PV和PVC:

1.安装nfs服务器:

  • master节点上:
    • 使配置生效

      exportfs -r
    • 将共享目录以读写权限暴露给192.168.174.0/24网段中的所有主机

      1. cat << eof >> /etc/exports
      2. /root/nfs/mysql 192.168.174.0/24(rw,no_root_squash)
      3. eof
    • 启动nfs服务

      systemctl enable nfs-server;systemctl restart nfs-server
    • 准备一个共享目录

      mkdir -p /root/nfs/mysql -pv;chmod o=rwx /root/nfs/mysql
    • 在master上安装nfs服务:

      yum install nfs-utils -y
  • node节点:

    • 接下来,要在的每个node节点上都安装下nfs,这样的目的是为了node节点可以驱动nfs设备

      yum install nfs-utils -y
      systemctl enable nfs-server;systemctl restart nfs-server

2.创建pv:

  1. cat << eof > pv1.yaml
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: pv1
  6. spec:
  7. capacity: # 存储能力,目前只支持存储空间的设置
  8. storage: 3Gi
  9. accessModes: # 访问模式:读写权限,可以被多个节点挂载
  10. - ReadWriteMany
  11. persistentVolumeReclaimPolicy: Retain # 回收策略
  12. nfs:
  13. path: /root/nfs/mysql/data # 这里的路径必须和nfs服务器配置文件中的某一个一致
  14. server: 192.168.174.30 # nfs服务器地址
  15. eof
kubectl apply -f pv1.yaml

3.创建mysql的yaml文件:

  1. cat << eof > mysql.yaml
  2. apiVersion: v1
  3. kind: PersistentVolumeClaim
  4. metadata:
  5. name: mysql-pvc
  6. namespace: mysql
  7. spec:
  8. accessModes: # 访问模式:读写权限,可以被多个节点挂载
  9. - ReadWriteMany
  10. resources:
  11. requests:
  12. storage: 1Gi
  13. ---
  14. apiVersion: apps/v1 # 通过kubectl explain deployment命令查看版本
  15. kind: Deployment # 资源类型
  16. metadata:
  17. name: mysql-deployment # 资源名称
  18. namespace: mysql # 指定命名空间
  19. spec:
  20. replicas: 3 # 控制器下的pod数
  21. selector:
  22. matchLabels:
  23. app: mysql # 这是控制器用来控制pod的标签
  24. template:
  25. metadata:
  26. labels:
  27. app: mysql # pod的标签
  28. spec:
  29. containers:
  30. - name: mysql # 容器名
  31. image: mysql:8.0.20 # 容器镜像
  32. env: # mysql环境参数
  33. - name: MYSQL_ROOT_PASSWORD # 指定ROOT用户密码
  34. value: "你的密码"
  35. ports: # 容器暴露的端口
  36. - name: mysql
  37. containerPort: 3306
  38. volumeMounts: # 容器内的被挂载目录
  39. - name: mysql-data
  40. mountPath: /var/lib/mysql
  41. volumes:
  42. - name: mysql-data
  43. persistentVolumeClaim:
  44. claimName: mysql-pvc
  45. readOnly: false
  46. ---
  47. apiVersion: v1
  48. kind: Service
  49. metadata:
  50. name: mysql-nodeport
  51. namespace: mysql
  52. spec:
  53. selector:
  54. app: mysql # 服务通过此标签连接deployment资源来暴露端口
  55. type: NodePort # 服务类型:NodePort
  56. ports: # 不指定NodePort端口就会自动分配,(默认的取值范围是:30000-32767)
  57. - protocol: TCP
  58. port: 3306
  59. targetPort: 3306
  60. eof

4.创建mysq应用:

kubectl apply -f mysql.yaml

5.查看pod:

  1. [root@master ~]# kubectl get deployment,pod,svc,pvc -n mysql -o wide
  2. NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
  3. deployment.apps/mysql-deployment 3/3 3 3 3m4s mysql mysql:8.0.20 app=mysql
  4. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  5. pod/mysql-deployment-574486b789-l9f4h 1/1 Running 0 3m4s 10.244.2.14 node2 <none> <none>
  6. pod/mysql-deployment-574486b789-ng4x8 1/1 Running 0 3m4s 10.244.1.6 node1 <none> <none>
  7. pod/mysql-deployment-574486b789-sqsll 1/1 Running 2 3m4s 10.244.2.13 node2 <none> <none>
  8. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  9. service/mysql-nodeport NodePort 10.105.41.7 <none> 3306:30165/TCP 3m4s app=mysql
  10. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE VOLUMEMODE
  11. persistentvolumeclaim/mysql-pvc Bound pv1 3Gi RWX 3m4s Filesystem
  12. [root@master ~]# kubectl get pv -o wide
  13. NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE
  14. pv1 3Gi RWX Retain Bound mysql/mysql-pvc 4m Filesystem

6.远程连接:

  1. [root@localhost ~]# mysql -h192.168.174.30 -P30165 -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.20 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/214373
推荐阅读
相关标签
  

闽ICP备14008679号