搭建swarm集群

安装前的准备,安装docker和docker-compose

vi pre_install.sh

#!/bin/bash
###关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
###配置拉取国内镜像仓库
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
 "https://5sr5uink.mirror.aliyuncs.com",
 "https://docker.mirrors.ustc.edu.cn",
 "https://hub-mirror.c.163.com",
 "https://mirror.baidubce.com"
  ],
 "insecure-registries": ["harbor.xxx.com"]
}
EOF
###启动docker
systemctl start docker
systemctl enable docker
### 下载docker-compose
curl -L "https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/bin/docker-compose
chmod +x /usr/bin/docker-compose
docker-compose -version

sh pre_install.sh

准备2台主机,一台管理节点,一台工作节点

在管理节点:

docker swarm init 初始化管理节点后会产生一串加入集群的命令,复制到工作节点执行 如果忘记了产生的token可以执行以下查看:

docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-0iq85hac12m20vmc19s5xzlmnxi77ab74dn4adpuoa44bi0xmj-4sgl3edr6qu9sbp375okw90so 10.10.10.203:2377
在工作节点:

执行以下命令 docker swarm join --token SWMTKN-1-0iq85hac12m20vmc19s5xzlmnxi77ab74dn4adpuoa44bi0xmj-4sgl3edr6qu9sbp375okw90so 10.10.10.203:2377

查看集群

docker node ls

[root@localhost-203-root-all ~]# docker node ls
ID                            HOSTNAME                    STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
t82for6td6ouficu9pr2r89qs *   localhost-203-root-all      Ready     Active         Leader           20.10.6
y5fuwec4lpoim6tyqkrl5lo67     swarm-client-10-10-10-206   Ready     Active                          20.10.6

leader表示这台主机是管理节点

创建master主配置文件

在管理节点创建/deploy/sorts/swarm/mysql目录

mkdir -p /deploy/sorts/swarm/mysql

创建子目录

cd /deploy/sorts/swarm/mysql mkdir conf data log

编辑my.cnf文件

vi conf/my.cnf

[mysqld]
server_id=1
log-bin= mysql-bin

replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

read-only=0
relay_log=mysql-relay-bin
log-slave-updates=on
auto-increment-offset=1
auto-increment-increment=2

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

创建slave主配置文件

在工作节点创建/deploy/sorts/swarm/mysql目录

mkdir -p /deploy/sorts/swarm/mysql

创建子目录

cd /deploy/sorts/swarm/mysql mkdir conf data log

编辑my.cnf文件

vi conf/my.cnf

[mysqld]
server_id=2
log-bin= mysql-bin

replicate-ignore-db=mysql
replicate-ignore-db=sys
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema

read-only=0
relay_log=mysql-relay-bin
log-slave-updates=on
auto-increment-offset=2
auto-increment-increment=2

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

管理节点编辑docker-compose.yml文件

version: "3.7"
networks:
  network:
    driver: overlay
services:
#mysql1服务
    mysql-1:
      image: mysql:5.7
      ports:
        - 3310:3306
      networks:
        #指定容器加入的网络
        - network
      environment:
        #最好使用此设定时区,其它静像也可以使用
        - TZ=CST-8
        #指定mysql的访问密码
        - MYSQL_ROOT_PASSWORD=123456
      #可以加--default-time-zone='+8:00'设定时区
      volumes:
        #本地文件目录,持久化
        - ./data/:/var/lib/mysql
        #指定开启binlog的配置文件
        - ./conf/my.cnf:/etc/mysql/my.cnf
      command: --character-set-server=utf8
               --collation-server=utf8_general_ci
               --sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
      deploy:
        placement:
          constraints:
            #该hostname为指定容器在哪个主机启动
            - node.hostname == localhost-203-root-all
        restart_policy:
          condition: on-failure

在管理节点启动mysql主从

docker stack deploy -c docker-swarm-mysql.yml mysql

查看状态

在管理节点

[root@localhost-203-root-all swarm]# docker stack ls
NAME      SERVICES   ORCHESTRATOR
mysql     2          Swarm
[root@localhost-203-root-all swarm]# docker service ls
ID             NAME            MODE         REPLICAS   IMAGE       PORTS
gxnxt6fr3t64   mysql_mysql-1   replicated   1/1        mysql:5.7   *:3310->3306/tcp
3idz9gcbeqol   mysql_mysql-2   replicated   1/1        mysql:5.7   *:3311->3306/tcp
[root@localhost-203-root-all swarm]# docker ps -f "name=myql1"
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost-203-root-all swarm]# docker ps -f "name=myql-1"
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost-203-root-all swarm]# docker ps -f "name=mysql-1"
CONTAINER ID   IMAGE       COMMAND                  CREATED        STATUS        PORTS                 NAMES
f51adcd1f37c   mysql:5.7   "docker-entrypoint.s…"   24 hours ago   Up 24 hours   3306/tcp, 33060/tcp   mysql_mysql-1.1.nx5g3hj2tp0e1q4x8vfavh7qo

在工作节点

[root@swarm-client-10-10-10-206 conf]# docker ps -f "name=mysql-2"
CONTAINER ID   IMAGE       COMMAND                  CREATED        STATUS        PORTS                 NAMES
1b1b8dcd88fb   mysql:5.7   "docker-entrypoint.s…"   24 hours ago   Up 24 hours   3306/tcp, 33060/tcp   mysql_mysql-2.1.2hfieodttb2j00c4rr3mwjipb

开始配置主从

在管理节点

登陆管理节点mysql容器

[root@localhost-203-root-all swarm]# docker exec -it f51adcd1f37c bash

创建复制用户
root@f51adcd1f37c:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.35-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> grant replication slave on *.* to 'repl'@'%' identified by 'abc.123';
mysql> flush privileges;
查看主状态
mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000011
         Position: 327
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

记住File和Position值

在工作节点

 docker exec -it 1b1b8dcd88fb bash
 mysql -uroot -p123456
 mysql> change master to master_host='mysql-1',master_port=3306,master_user='repl',master_password='abc.123',master_log_file='mysql-bin.000011',master_log_pos=327;
 mysql> start slave
 mysql> show slave status\G;
 mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-1
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 327
               Relay_Log_File: mysql-relay-bin.000015
                Relay_Log_Pos: 540
        Relay_Master_Log_File: mysql-bin.000011
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

Slave_IO_Running和Slave_SQL_Running都为Yes说明配置成功,可以在主上做一些操作,再去从上查看是否有同步