docker-compose部署mariadb集群

环境

主机名 机器IP
10.0.102.214 node1
10.0.102.220 node2
10.0.102.116 node3

部署

compose-mariadb.yaml文件说明

[root@k8s-node-1 ~]# vim compose-mariadb.yaml
version: '3'
services:
  mariadb_galera0:                   #名称修改下便于区别,各个节点不同
    image: severalnines/mariadb:10.1    #镜像
ports:
      - "3306:3306"     #各个节点访问端口
    environment:        #环境变量
      CLUSTER_NAME: "mariadb-cluster-new"   #定义mariadb集群名称
      DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
      MYSQL_ROOT_PASSWORD: "mypassword123"
      XTRABACKUP_PASSWORD: "mypassword123"
    command:        #容器启动参数
      - --innodb_buffer_pool_size=256M
      - --max_connections=81
healthcheck:
interval: 5s
timeout: 3s
retries: 200 # interval * retries > SST duration
volumes:
      - /data:/var/lib/mysql                #各个节点的挂载目录,预先创好
      - /etc/my.cnf.d:/etc/my.cnf.d         #是否开通binlog配置好,放此目录
      - /etc/localtime:/etc/localtime       #定义时区
network_mode: "host"                        #使用本机网络

【见图1】
1.png

k8s-node-1

部署:
创建挂载所需目录与文件
mkdir /data/
mkdir /etc/my.cnf.d

[root@10-0-102-165 ~]# vim /etc/my.cnf.d/bin-log.cnf
[mysqld]
log-bin= mysql-bin
log_slave_updates = 1
expire_logs_days = 20
server-id = 211

执行docker-compose命令进行部署(部署新集群时确保/data目录为空)
[root@10-0-102-165 ~]# rm -rf /data/*
[root@10-0-102-165 ~]# docker-compose -f compose-mariadb.yaml up -d
Creating root_mariadb_galera0_1 ... done
【见图2】


查询容器启动日志
# docker logs -f $(dockerps -a | grep 'severalnines/mariadb:10.1' |awk '{print $NF}')

验证容器是否启动及集群状态
mysql -uroot -pmypassword123 -h10.0.102.165
MariaDB [(none)]> show status like 'wsrep%';
【见图3】
2.png

3.png

k8s-node-2

创建挂载所需目录与文件
#mkdir /data/
#mkdir /etc/my.cnf.d      #不开通binlog只创建目录即可

启动容器
# rm -rf /data/*
# docker-compose -f compose-mariadb.yaml up -d
Creating root_mariadb_galera1_1 ... done
# docker logs -f $(dockerps -a | grep 'severalnines/mariadb:10.1' |awk '{print $NF}')
【见图4】 

验证
mysql -uroot -pmypassword123 -h 10.0.102.165 -e "show status like 'wsrep%';"
可以发现节点2的ip已经加入到mariadb集群中,只等第三个节点了
【见图5】
4.png

5.png

k8s-node-3

创建挂载所需目录与文件
mkdir /data/
mkdir /etc/my.cnf.d      #不开通binlog只创建目录即可

2.启动容器
# rm -rf /data/*
# docker-compose -f compose-mariadb.yaml up -d
Creating root_mariadb_galera1_1 ... done
# docker logs -f $(dockerps -a | grep 'severalnines/mariadb:10.1' |awk '{print $NF}')
 【见图6】

3.验证
mysql -uroot -pmypassword123 -h 10.0.102.165 -e "show status like 'wsrep%';"
【见图7】
6.png

7.png

注意事项

1.扩容
扩容比较简单,只需copy一份compose文件,简单修改名称即可,创建相应的目录,使用docker-compose -f 文件名 up –d即可,请确保启动前/data目录为空

2.缩容
缩容时,先使用docker stop NF}')停止容器,然后使用dockerrmNF}')删除退出的容器即可

3.集群启动与停止时请按照顺序执行,不可并行,而且停止时建议使用docker stop命令

4.某个节点异常后重新加入集群需要停止服务并清空/data目录再使用docker-compose,请确保compose文件中集群信息的配置与集群相符合

5备份恢复操作与k8s中mariadb集群的备份恢复操作相同
五、配置文件(对应各个节点,在相应节点运行)

yaml文件

compose-mariadb1.yaml

version: '3'

services:

  mariadb_galera0:
    image: severalnines/mariadb:10.1
    ports:
      - "3306:3306"
    environment:
      CLUSTER_NAME: "mariadb-cluster-new"
      DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
      MYSQL_ROOT_PASSWORD: "mypassword123"
      XTRABACKUP_PASSWORD: "mypassword123"
    command:
      - --innodb_buffer_pool_size=256M
      - --max_connections=81
    healthcheck:
      interval: 5s
      timeout: 3s
      retries: 200 # interval * retries > SST duration
    volumes:
      - /data:/var/lib/mysql
      - /etc/my.cnf.d:/etc/my.cnf.d
      - /etc/localtime:/etc/localtime
    network_mode: "host"

compose-mariadb2.yaml

version: '3'

services:

  mariadb_galera1:
    image: severalnines/mariadb:10.1
    ports:
      - "3306:3306"
      - "4567:4567"
    environment:
      CLUSTER_NAME: "mariadb-cluster-new"
      DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
      MYSQL_ROOT_PASSWORD: "mypassword123"
      XTRABACKUP_PASSWORD: "mypassword123"
    command:
      - --innodb_buffer_pool_size=256M
      - --max_connections=81
    healthcheck:
      interval: 8s
      timeout: 3s
      retries: 300 # interval * retries > SST duration
    volumes:
      - /data:/var/lib/mysql
      - /etc/my.cnf.d:/etc/my.cnf.d
      - /etc/localtime:/etc/localtime

compose-mariadb3.yaml

version: '3'

services:

  mariadb_galera2:
    image: severalnines/mariadb:10.1
    ports:
      - "3306:3306"
      - "4567:4567"
    environment:
      CLUSTER_NAME: "mariadb-cluster-new"
      DISCOVERY_SERVICE: "10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379"
      MYSQL_ROOT_PASSWORD: "mypassword123"
      XTRABACKUP_PASSWORD: "mypassword123"
    command:
      - --innodb_buffer_pool_size=256M
      - --max_connections=81
    healthcheck:
      interval: 5s
      timeout: 3s
      retries: 200 # interval * retries > SST duration
    volumes:
      - /data:/var/lib/mysql
      - /etc/my.cnf.d:/etc/my.cnf.d
      - /etc/localtime:/etc/localtime
    network_mode: host

你可能感兴趣的:(docker-compose部署mariadb集群)