Kubernetes部署叢集Mysql服務

pingdanorcale發表於2023-03-10

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 沒有做修改

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

修改如下:

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部落格 ” ,連結:http://blog.itpub.net/10201716/viewspace-2939053/,如需轉載,請註明出處,否則將追究法律責任。

相關文章