1、下载镜像
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io 加速器(https://www.daocloud.io/mirror)
systemctl restart docker
docker pull percona/percona-xtradb-cluster
2、tag镜像
docker tag percona/percona-xtradb-cluster pxc
docker rmi percona/percona-xtradb-cluster
3、创建volume
docker volume create --name v1
docker volume create --name v2
docker volume create --name v3
docker volume create --name v4
docker volume create --name v5
4、创建桥接网络
docker network create pxc_network --subnet 172.19.0.0/16
5、依次启动容器
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -v v1:/var/lib/mysql --privileged --name=node1 --net=pxc_network --ip 172.19.0.2 pxc docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v2:/var/lib/mysql --privileged --name=node2 --net=pxc_network --ip 172.19.0.3 pxc docker run -d -p 3308:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v3:/var/lib/mysql --privileged --name=node3 --net=pxc_network --ip 172.19.0.4 pxc docker run -d -p 3309:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v4:/var/lib/mysql --privileged --name=node4 --net=pxc_network --ip 172.19.0.5 pxc docker run -d -p 3310:3306 -e MYSQL_ROOT_PASSWORD=abc123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=abc123456 -e CLUSTER_JOIN=node1 -v v5:/var/lib/mysql --privileged --name=node5 --net=pxc_network --ip 172.19.0.6 pxc
6、连接测试
使用navicat连接mysql进行测试
haproxy
docker pull haproxy
vim /usr/local/etc/haproxy/haproxy.cfg
global chroot /usr/local/etc/haproxy log 127.0.0.1 local5 info daemon defaults log global mode http option httplog option dontlognull timeout connect 5000 timeout client 50000 timeout server 50000 listen admin_stats bind 0.0.0.0:8888 mode http stats uri /dbs stats realm Global statistics stats auth admin:abc123456 listen proxy-mysql bind 0.0.0.0:3306 mode tcp balance roundrobin option tcplog option mysql-check user haproxy server MySQL_1 172.19.0.2:3306 check weight 1 maxconn 2000 server MySQL_2 172.19.0.3:3306 check weight 1 maxconn 2000 server MySQL_3 172.19.0.4:3306 check weight 1 maxconn 2000 server MySQL_4 172.19.0.5:3306 check weight 1 maxconn 2000 server MySQL_5 172.19.0.6:3306 check weight 1 maxconn 2000 option tcpka
docker run -it -d -p 4001:8888 -p 4002:3306 -v /usr/local/etc/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg --name h1 --privileged --net=pxc_network --ip 172.19.0.7 haproxy