docker pull percona/percona-xtradb-cluster:5.7.30
docker tag percona/percona-xtradb-cluster:5.7.30 pxc
docker network create --subnet=172.30.0.0/24 pxc-network
docker volume create --name v1
docker volume create --name v2
docker volume create --name v3
docker volume create --name v1_conf_v
docker volume create --name v2_conf_v
docker volume create --name v3_conf_v
docker network inspect pxc-network
注意:在创建完第一个node1,需要等待一段时间,大概1分钟左右,等node1启动初始化完成,才能创建node2和node3,不然会出错,大家一定要注意哦
docker run -d -p 3301:3306 -v v1:/var/lib/mysql -v v1_conf_v:/etc/mysql/ -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 --privileged --name=pxc-node1 --net=pxc-network --ip 172.30.0.11 pxc
docker run -d -p 3302:3306 -v v2:/var/lib/mysql -v v2_conf_v:/etc/mysql/ -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node2 --net=pxc-network --ip 172.30.0.12 pxc
docker run -d -p 3303:3306 -v v3:/var/lib/mysql -v v3_conf_v:/etc/mysql/ -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node3 --net=pxc-network --ip 172.30.0.13 pxc
192.168.0.1 slave01
192.168.0.2 slave02
192.168.0.3 slave03
[root@izuf65psc9rebqdd0o6sbvz ~]# docker pull swarm
Using default tag: latest
latest: Pulling from library/swarm
38e5683d7755: Pull complete
083aff163606: Pull complete
2064f1a73c6b: Pull complete
Digest: sha256:2de8883e2933840ed7ee7360ea1eed314bf8aeac37c0692b9ca651630fde3b7f
Status: Downloaded newer image for swarm:latest
docker.io/library/swarm:latest
[root@izuf65psc9rebqdd0o6sbvz ~]# docker swarm init --advertise-addr 192.168.0.1
Swarm initialized: current node (o6xalvl620mxuefrux1rhjas5) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0jgv1x8wwp0c7eb50w7eyv5hcj0nsrcx3su64ikuifghzgd36j-12iruzk75cc9yxekoi26tjnvk 192.168.0.1:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
上面命令执行后,该机器自动加入到swarm集群。这个会创建一个集群token,获取全球唯一的 token,作为集群唯一标识。后续将其他节点加入集群都会用到这个token值。
slave01: 192.168.0.2
slave02: 192.168.0.3
两台从节点机子依次执行上面步骤复制得join 脚本
docker swarm join --token SWMTKN-1-0jgv1x8wwp0c7eb50w7eyv5hcj0nsrcx3su64ikuifghzgd36j-12iruzk75cc9yxekoi26tjnvk 192.168.0.1:2377
结果查看:
[root@izuf65psc9rebqdd0o6sbwz ~]# docker swarm join --token SWMTKN-1-0jgv1x8wwp0c7eb50w7eyv5hcj0nsrcx3su64ikuifghzgd36j-12iruzk75cc9yxekoi26tjnvk 192.168.0.1:2377
This node joined a swarm as a worker.
[root@izuf65psc9rebqdd0o6sbvz ~]# docker node list
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
o6xalvl620mxuefrux1rhjas5 * swarm01 Ready Active Leader 20.10.8
1c7hfcr870umzxrbhlqnfgkqf swarm02 Ready Active 20.10.8
op30wpyz9381s9a0gguyhwxqr swarm03 Ready Active 20.10.8
[root@izbp176acsirlvx1jfu9uxz ~]# docker pull percona/percona-xtradb-cluster:5.7.30
5.7.30: Pulling from percona/percona-xtradb-cluster
524b0c1e57f8: Pull complete
27b947d6be19: Pull complete
26efa431217c: Pull complete
7d4afafca533: Pull complete
05e05cf6b007: Pull complete
868bb5b06c86: Pull complete
f0794e3eeb9c: Pull complete
3a988f4f1cd9: Pull complete
46f38e1635eb: Pull complete
75805c1fcaed: Pull complete
575a6d0fc78f: Pull complete
c8bb3fa6ebc4: Pull complete
Digest: sha256:b03a060e9261b37288a2153c78f86dcfc53367c36e1bcdcae046dd2d0b0721af
Status: Downloaded newer image for percona/percona-xtradb-cluster:5.7.30
docker.io/percona/percona-xtradb-cluster:5.7.30
docker tag percona/percona-xtradb-cluster:5.7.30 pxc
docker network create -d overlay --attachable pxc-network
docker run -d --restart=always -p 3306:3306 -v mysql_v:/var/lib/mysql -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 --privileged --name=pxc-node1 --net=pxc-network pxc
docker run -d --restart=always -p 3306:3306 -v mysql_v:/var/lib/mysql -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node2 --net=pxc-network pxc
docker run -d --restart=always -p 3306:3306 -v mysql_v:/var/lib/mysql -v mysql_conf_v:/etc/mysql/ -v /etc/localtime:/etc/localtime -e MYSQL_ROOT_PASSWORD=test.888 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=test.888 -e CLUSTER_JOIN=pxc-node1 --privileged --name=pxc-node3 --net=pxc-network pxc
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "test.888";
flush privileges;
ok,完毕!!!