ITPub博客

首页 > 数据库 > MySQL > Kubernetes部署集群Mysql服务

Kubernetes部署集群Mysql服务

原创 MySQL 作者:pingdanorcale 时间:2023-03-10 13:56:49 0 删除 编辑

Mysql 集群服务描述:

   1. 搭建一个主从复制的Mysql集群

   2. 一个主节点“Master”  多个从节点“Slave”

   3. 所有的写操作,只能在主节点上执行;

   4. 读操作可以在所有节点上执行 

   5. 从节点需要能水平扩展; 

(注:本文的存储券pv采用的nfs服务,如果想对节点进行拓展需要添加PV券。)

所需服务资源

   1. 配置数据持久化(PersistentVolume采用的是nfs)

   2. 配置Master和Slave节点所需的不同配置信息(ConfigMap)

   3. 创建Mysql对外服务(Service)

   4. 定义Mysql容器(StatefulSet)

 1 、配置数据持久化

. 我使用的是NFS服务进行资源存储。直接将NFS服务器装在了Master主节点上,如果有条件的也可以专门制作一台NFS服务器或者搭建一套ceph服务器。由于搭建的是Mysql服务集群,所以就涉及到了 数据持久化。那么这里准备了三个持久化硬盘提供给一个Master和两个Slave节点使用

1.1 配置NFS服务器

server ip: 192.168.137.101

[root@master ~]# yum install nfs-utils rpcbind -y
[root@master ~]# systemctl start nfs
[root@master ~]# systemctl start rpcbind
[root@ master ~]# systemctl enable nfs
[root@master ~]# systemctl enable rpcbind
 

[root@master ~]# mkdir -p /data/nfs/
[root@master ~]# chmod 777 /data/nfs/
 

[root@master ~]# cat /etc/exports
/data/nfs/    192.168.137.0/24(rw,sync,no_root_squash,no_all_squash)
[root@ master ~]# exportfs -arv
exporting 192.168.137.0/24:/data/nfs
 

[root@ master ~]# showmount -e localhost
Export list for localhost:
/data/nfs 192.168.137.0/24
 

参数:
sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性
async:将数据先保存在内存缓冲区中,必要时才写入磁盘

所有node节点安装 nfs-utils rpcbind

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

1.2 创建动态卷提供者

# wget https://raw.githubusercontent.com/kubernetes-incubator/external-storage/master/nfs-client/deploy/rbac.yaml
# kubectl apply -f rbac.yaml

1.3 创建 Storageclass

# cat class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"

1.4 创建nfs-client-provisioner自动配置程序,以便自动创建持久卷(PV)

  • 自动创建的 PV 以 ${namespace}-${pvcName}-${pvName} 的命名格式创建在 NFS 上
  • 当这个 PV 被回收后会以 archieved-${namespace}-${pvcName}-${pvName} 的命名格式存在 NFS 服务器上
# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: quay.io/external_storage/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 192.168.137.101
            - name: NFS_PATH
              value: /data/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.137.101
            path: /data/nfs

 

2 、配置Master和Slave节点所需的不同配置信息

2.1 创建configMap配置字典(以为有主从复制和读写分离的区分,所以在配置信息上也有所不同)---

kubectl apply -f https://k8s.io/examples/application/mysql/mysql-configmap.yaml 没有做修改

3 、创建Mysql对外服务(Service)

3.1 负责统一管理所有的Mysql服务Pod

apiVersion: v1
kind: Service
metadata:
  name: mysql-headless
  namespace: kerry
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql

3.2 负责统一管理拥有读写权限的Pod,也就是Master节点pod

# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
  name: mysql-write
  labels:
    app: mysql
    app.kubernetes.io/name: mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: http
    nodePort: 30307
  selector:
    app: mysql
---

3.3 负责统一管理拥有读权限的Pod 

 

apiVersion: v1
kind: Service
metadata:
  name: mysql-read
  labels:
    app: mysql
    app.kubernetes.io/name: mysql
    readonly: "true"
spec:
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    name: http
    nodePort: 30308
  selector:
    app: mysql

4 、 定义Mysql容器(配置StatefulSet)

  使用的

Wget https://k8s.io/examples/application/mysql/mysql-statefulset.yaml

修改如下:

4.1 image: gcr.io/google-samples/xtrabackup:1.0    《==这个镜像是来自国外仓库,如果访问不到的话用上面1模块进行拉取所需镜像

  修改为      -

 name: xtrabackup

   image: registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0

 4.2 修改存储类型使用创建的 NFS 动态存储卷

  volumeClaimTemplates:
  - metadata:
      name: data
      annotations:
        volume.beta.kubernetes.io/storage-class: managed-nfs-storage
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 1Gi

验证:

[root@master mysql]# kubectl get svc

NAME          TYPE        CLUSTER-IP       EXTERNAL-IP       PORT(S)          AGE

kubernetes    ClusterIP   10.96.0.1        <none>            443/TCP          76d

my-mysql      ClusterIP   10.100.34.86     192.168.137.101   3306/TCP         7h53m

mysql         ClusterIP   None             <none>            3306/TCP         2d1h

mysql-nosvc   NodePort    10.110.0.99      <none>            3306:30306/TCP   134m

mysql-read    NodePort    10.100.43.3      <none>            3306:30308/TCP   2d1h

mysql1        NodePort    10.104.186.112   <none>            3306:30307/TCP   88m

[root@master mysql]# kubectl exec -it mysql-0 -- bash

Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)

root@mysql-0:/# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 106150

Server version: 5.7.36-log 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> SHOW MASTER STATUS \G;

*************************** 1. row ***************************

             File: mysql-0-bin.000005

         Position: 728

     Binlog_Do_DB:

 Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

 

ERROR:

No query specified

[root@master mysql]# kubectl exec -it mysql-1 -- bash

Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)

root@mysql-1:/# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 105944

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> show slave status \G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: mysql-0.mysql

                  Master_User: root

                  Master_Port: 3306

                Connect_Retry: 10

              Master_Log_File: mysql-0-bin.000005

          Read_Master_Log_Pos: 728

               Relay_Log_File: mysql-1-relay-bin.000002

                Relay_Log_Pos: 896

        Relay_Master_Log_File: mysql-0-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 728

              Relay_Log_Space: 1105

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 100

                  Master_UUID: 2f44c37a-755f-11ed-80b9-eaeff980c584

             Master_Info_File: /var/lib/mysql/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

         Replicate_Rewrite_DB:

                 Channel_Name:

           Master_TLS_Version:

1 row in set (0.00 sec)

 

ERROR:

No query specified

结束语

通过 K8S 搭建了 mysql 主从,将数据进行了持久化存储及提供对外访问的方式。

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10201716/viewspace-2939053/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: kubeadm搭建K8S
请登录后发表评论 登录
全部评论

注册时间:2009-01-02

  • 博文量
    158
  • 访问量
    221199