k8s配置mysql主从复制

文章目录

    • 一、概述
    • 三、创建pv和pvc
    • 四、部署mysql
    • 五、测试

一、概述

本文档采用kubernetes8.5+mysql5.7+NFS数据持久化,搭建mysql的主从服务。

##二、准备mysql主从镜像

dockerfile、docker-entrypoint.sh 文件下载地址如下
https://github.com/docker-library/mysql/tree/master/5.7
由于我们要配置mysql主从,所以需要对dockerfile、docker-entrypoint.sh 文件做一点的修改,主要是在mysql主从配置部分。

  1. 准备master的镜像

    将Dockerfile, docker-entrypoint.sh复制一份用于build master镜像文件。
    在Dockerfile中添加如下内容,将mysql master的server-id设置为1

RUN sed -i ‘/[mysqld]/a server-id=1\nlog-bin’ /etc/mysql/mysql.conf.d/mysqld.cnf

k8s配置mysql主从复制_第1张图片
在docker-entrypoint.sh中添加如下内容,创建一个复制用户并赋权限,刷新系统权限表

echo "CREATE USER '$MYSQL_REPLICATION_USER'@'%' IDENTIFIED BY '$MYSQL_REPLICATION_PASSWORD' ;" | "${mysql[@]}" 
echo "GRANT REPLICATION SLAVE ON *.* TO '$MYSQL_REPLICATION_USER'@'%' IDENTIFIED BY '$MYSQL_REPLICATION_PASSWORD' ;" | "${mysql[@]}" 
echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"

k8s配置mysql主从复制_第2张图片

  1. 准备slave的镜像
    将Dockerfile, docker-entrypoint.sh复制一份用于build slave镜像文件。
    在Dockerfile中添加如下内容,将mysql slave的server-id设置为一个随机数
    RUN RAND="$(date +%s | rev | cut -c 1-2)$(echo ${RANDOM})" && sed -i '/\[mysqld\]/a server-id='$RAND'\nlog-bin' /etc/mysql/mysql.conf.d/mysqld.cnf

k8s配置mysql主从复制_第3张图片
在docker-entrypoint.sh中添加如下内容,配置连接master主机的host、user、password等参数,并启动复制进程。

echo "STOP SLAVE;" | "${mysql[@]}" 
echo "CHANGE MASTER TO master_host='$MYSQL_MASTER_SERVICE_HOST', master_user='$MYSQL_REPLICATION_USER', master_password='$MYSQL_REPLICATION_PASSWORD' ;" | "${mysql[@]}" 
echo "START SLAVE;" | "${mysql[@]}"

k8s配置mysql主从复制_第4张图片

  1. 使用Dockerfile创建镜像
    创建镜像前需要确保docker-entrypoint.sh有可执行权限
    chmod +x docker-entrypoint.sh
     # cd /root/mysql/master/	# master Dockerfile所在目录
     # docker build -t 192.168.1.98/library/mysql-master:0.1		#IP地址为harbor仓库地址
     # cd /root/mysql/slave		# slave Dockerfile所在目录
     # docker build -t 192.168.1.98/library/mysql-slabe:0.1
     # docker push 192.168.1.98/library/mysql-master:0.1		# 上传镜像到仓库中
     # docker push 192.168.1.98/library/mysql-slave:0.1
    

三、创建pv和pvc

  1. 在nfs服务器创建目录
    编辑/etc/exports目录,添加如下两行,确保本地有这两个目录。

    /var/hitsm/mysql_master *(rw,no_root_squash,no_all_squash,sync)
    /var/hitsm/mysql_slave *(rw,no_root_squash,no_all_squash,sync)
    

    使配置生效
    exportfs -r

  2. 通过yml文件创建两组pv 和 pvc

    # cat nfs-pv-master.yaml
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-nfs-mysql-master
    spec:
      capacity:
        storage: 5Gi 
      accessModes:
      - ReadWriteOnce
      nfs: 
        path: /var/hitsm/mysql-master
        server: 192.168.1.98
      persistentVolumeReclaimPolicy: Recycle 
    # cat nfs-pvc-master.yaml
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: pv-nfs-mysql-master
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 5Gi
    # cat nfs-pv-slave.yaml 
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv-nfs-mysql-slave
    spec:
      capacity:
        storage: 6Gi 
      accessModes:
      - ReadWriteOnce
      nfs: 
        path: /var/hitsm/mysql_slave
        server: 192.168.1.98
      persistentVolumeReclaimPolicy: Recycle 
    
    # cat nfs-pvc-slave.yaml 
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: pv-nfs-mysql-slave
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 6Gi
    # kubectl create -f nfs-pv-master.yaml
    # kubectl create -f nfs-pvc-master.yaml
    # kubectl create -f nfs-pv-slave.yaml 
    # kubectl create -f nfs-pvc-slave.yaml 
    

k8s配置mysql主从复制_第5张图片

四、部署mysql

  1. mysql master部署

    # cat mysql-master-rc.yaml 
    apiVersion: v1
    kind: ReplicationController
    metadata:
     name: mysql-master
     labels:
      name: mysql-master
    spec:
      replicas: 1
      selector:
       name: mysql-master
      template:
       metadata:
        labels:
         name: mysql-master
       spec:
        containers:
        - name: mysql-master
          image: 192.168.1.98/library/mysql-master:0.1
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-master-data
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: "12345678"
          - name: MYSQL_REPLICATION_USER
            value: "repl"
          - name: MYSQL_REPLICAITON_PASSWORD
            value: "12345678"
        volumes:
         - name: mysql-master-data
           persistentVolumeClaim:
            claimName: pv-nfs-mysql-master
    
    # cat mysql-master-svc.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-master
      labels:
       name: mysql-master
    spec:
      type: NodePort
      ports:
      - port: 3306
        targetPort: 3306
        name: http
        nodePort: 30066
      selector:
        name: mysql-master
    
    # kubectl create -f mysql-master-rc.yaml -n mysql
    # kubectl create -f mysql-master-svc.yaml -n mysql
    

    这里写图片描述

  2. mysql slave部署

    # cat mysql-slave-rc.yaml 
    apiVersion: v1
    kind: ReplicationController
    metadata:
     name: mysql-slave
     labels:
      name: mysql-slave
    spec:
      replicas: 1
      selector:
       name: mysql-slave
      template:
       metadata:
        labels:
         name: mysql-slave
       spec:
        containers:
        - name: mysql-slave
          image: 192.168.1.98/library/mysql-slave:0.1
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: mysql-slave-data
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: "12345678"
          - name: MYSQL_REPLICATION_USER
            value: "repl"
          - name: MYSQL_REPLICAITON_PASSWORD
            value: "12345678"
        volumes:
         - name: mysql-slave-data
           persistentVolumeClaim:
            claimName: pv-nfs-mysql-slave
    
    # cat mysql-slave-svc.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-slave
      labels:
       name: mysql-slave
    spec:
      type: NodePort
      ports:
      - port: 3306
        targetPort: 3306
        name: http
        nodePort: 30067
      selector:
        name: mysql-slave
    
    # kubectl create -f mysql-slave-rc.yaml -n mysql
    # kubectl create -f mysql-slave-svc.yaml -n mysql
    

五、测试

  1. 登录到slave mysql,使用 “show slave status\G” 命令查看,出现如下,表示主从连接成功,如果报错,也可以通过这个页面看出。

    k8s配置mysql主从复制_第6张图片

  2. 在master mysql中创建数据库、表、插入数据等,来验证主从是否配置成功。

    在master数据库执行

    mysql>create database hitsm;
    mysql>usehitsm;
    mysql>
    CREATE TABLE databasechangeloglock (
    ID int(11) NOT NULL,
    LOCKED bit(1) NOT NULL,
    LOCKGRANTED datetime DEFAULT NULL,
    LOCKEDBY varchar(255)
    DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
    mysql>
    INSERT INTO databasechangeloglock (ID, LOCKED,LOCKGRANTED, LOCKEDBY) VALUES (1, b'0', NULL, NULL);
    

你可能感兴趣的:(运维,数据库)