• K8S搭建共享存储(以MySQL例)


    持久卷使用(nfs存储数据)

    Kubernetes 为了使应用程序及其开发人员能够正常请求存储资源,避免处理存储设施细节,引入了 PV 和 PVC。创建 PV
    有两种方式:

    • 集群管理员通过手动方式静态创建应用所需要的 PV;
    • 用户手动创建 PVC 并由 Provisioner 组件动态创建对应的 PV。

    搭建nfs服务器(ip:192.168.3.210)

    1. 安装工具
    yum -y install nfs-utils
    
    • 1
    1. 创建nfs目录
    yum -y install nfs-utils
    
    • 1
    1. 修改全新
    chmod -R 777 /nfs/data
    
    • 1
    1. 编辑export文件
    nano /etc/exports
    
    • 1
    1. 写入如下内容

    * 代表所有人都能连接,建议换成具体ip或ip段,如192.168.3.0/24

    /nfs/data 192.168.3.0/24(rw,no_root_squash,sync)
    
    • 1
    1. 配置生效
    exportfs -r
    
    • 1
    1. 查看生效
    exportfs
    
    • 1
    1. 启动rpcbind、nfs服务
    systemctl restart rpcbind && systemctl enable rpcbind
    systemctl restart nfs && systemctl enable nfs
    
    • 1
    • 2
    1. 查看 RPC 服务的注册状况
    rpcinfo -p localhost
    
    • 1
    1. 所有node节点安装nfs客户端
    yum -y install nfs-utils && systemctl start nfs && systemctl enable nfs
    
    • 1

    静态创建PV卷

    添加pv卷对应目录,这里创建个pv卷,则添加个pv卷的目录作为挂载点。

    1. 创建pv卷对应的目录
    mkdir -p /nfs/data/mysql
    
    • 1
    1. 配置exportrs,新增如下内容
    /nfs/data/mysql 192.168.3.0/24(rw,no_root_squash,sync)
    
    • 1
    1. 配置生效
    exportfs -r
    
    • 1
    1. 重启rpcbind、nfs服务
    systemctl restart rpcbind && systemctl restart nfs
    
    • 1

    创建 NFS PV与PVC

    创建命名空间

    cat >> common-tools.yaml << EOF
    # 创建命名空间
    apiVersion: v1
    kind: Namespace
    metadata:
      name: common-tools
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    kubectl apply -f common-tools.yaml
    
    • 1

    创建nfs-mysql.yaml文件

    注意:修改server里的地址为您的nfs服务地址

    cat >> nfs-mysql.yaml << EOF
    # 创建PV
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: nfs-mysql
      namespace: common-tools
      labels:
        pv: nfs-mysql
    spec:
      capacity:
        storage: 1Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Recycle
      storageClassName: nfs
      nfs:
        path: /nfs/data/mysql
        server: 192.168.3.210
    
    ---
    
    # 创建PVC
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: nfs-mysql
      namespace: common-tools
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 1Gi
      storageClassName: nfs
      selector:
        matchLabels:
          pv: nfs-mysql
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    创建pv与pvc

    kubectl apply -f nfs-mysql.yaml
    
    • 1
    配置说明:
    ① capacity 指定 PV 的容量为 1G。
    ② accessModes 指定访问模式为 ReadWriteOnce,支持的访问模式有:
    
        ReadWriteOnce – PV 能以 read-write 模式 mount 到单个节点。
        ReadOnlyMany – PV 能以 read-only 模式 mount 到多个节点。
        ReadWriteMany – PV 能以 read-write 模式 mount 到多个节点。
    
    ③ persistentVolumeReclaimPolicy 指定当 PV 的回收策略为 Recycle,支持的策略有:
    
        Retain – 需要管理员手工回收。
        Recycle – 清除 PV 中的数据,效果相当于执行 rm -rf /thevolume/*。
        Delete – 删除 Storage Provider 上的对应存储资源,例如 AWS EBS、GCE PD、Azure
        Disk、OpenStack Cinder Volume 等。
    
    ④ storageClassName 指定 PV 的 class 为 nfs。相当于为 PV 设置了一个分类,PVC 可以指定 class 申请相应 class 的 PV。
    ⑤ 指定 PV 在 NFS 服务器上对应的目录。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    安装MySQL

    创建配置文件

    cat >> mysql.yaml << EOF
    # 创建配置
    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: mysql-config
      namespace: common-tools
      labels:
        app: mysql
    data:
      my.cnf: |-
        [client]
        default-character-set=utf8mb4
        [mysql]
        default-character-set=utf8mb4
        [mysqld]
        character-set-server = utf8mb4
        collation-server = utf8mb4_unicode_ci
        init_connect='SET NAMES utf8mb4'
        skip-character-set-client-handshake = true
        max_connections=2000
        secure_file_priv=/var/lib/mysql
        bind-address=0.0.0.0
        symbolic-links=0
        sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
    
    ---
    
    # 创建服务
    kind: Service
    apiVersion: v1
    metadata:
      labels:
        app: mysql
      name: mysql-svc
      namespace: common-tools
    spec:
      type: NodePort
      ports:
        - name: http
          port: 3306
          nodePort: 30306
          protocol: TCP
          targetPort: 3306
      selector:
        app: mysql
    
    ---
    
    # 部署配置
    kind: Deployment
    apiVersion: apps/v1
    metadata:
      name: mysql
      namespace: common-tools
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mysql
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
            - args:
                - --datadir
                - /var/lib/mysql/datadir
              env:
                - name: MYSQL_ROOT_PASSWORD
                  value: root
                - name: MYSQL_USER
                  value: user
                - name: MYSQL_PASSWORD
                  value: user
              image: mysql:5.7
              name: mysql-container
              ports:
                - containerPort: 3306
                  name: dbapi
              volumeMounts:
                - mountPath: /var/lib/mysql
                  name: mysql-storage
                - name: config
                  mountPath: /etc/mysql/conf.d/my.cnf
                  subPath: my.cnf
          volumes:
            - name: mysql-storage
              persistentVolumeClaim:
                claimName: nfs-mysql
            - name: config
              configMap:
                name: mysql-config
            - name: localtime
              hostPath:
                type: File
                path: /etc/localtime
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    执行mysql.yaml

    kubectl apply -f mysql.yaml
    
    • 1
  • 相关阅读:
    我对移相器的理解(1):单bit的移相结构
    【Vue】组件化编程
    Spring封装数据结果
    论文阅读:Explainability for Large Language Models: A Survey
    虚拟内存初探CSAPP
    SpringBoot+百度地图+Mysql实现中国地图可视化
    Qt之随机数
    乡村电商人才齐聚浙江建德,这场农播氛围值已拉满!
    【数据结构】结构实现:顺序存储模式实现堆的相关操作
    Ai数字人直播系统SaaS源码大开源,源码独立部署助力中小企业发展!
  • 原文地址:https://blog.csdn.net/qq_32662595/article/details/126968759