docker build -t haproxy-keepalived .
docker tag docker.io/percona/percona-xtradb-cluster pxc
docker network create pxc-net
docker volume create --name mysql-data-node1
docker volume create --name mysql-data-node2
docker volume create --name mysql-data-node3
docker inspect <数据卷名称>
创建各个mysql节点时需要注意,最好先等第一个节点启动完成才创建第二或第三节点,如果第一节点的mysql还没初始化完成就创建第二或第三的话,有可能会造成同步数据失败。怎么才确认第一个节点的mysql初始化完毕呢?使用mysql客户端连接,如果能正常连接操作,说明第一个节点初始化完毕。
docker run -d --net=pxc-net -e MYSQL_ROOT_PASSWORD=123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123456 --privileged \
-p 3306:3306 \
-v mysql-data-node1:/var/lib/mysql \
--name=mysql-node1 \
pxc
docker run -d --net=pxc-net -e MYSQL_ROOT_PASSWORD=123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123456 --privileged \
-p 3307:3306 \
-v mysql-data-node2:/var/lib/mysql \
--name=mysql-node2 \
-e CLUSTER_JOIN=mysql-node1 \
pxc
docker run -d --net=pxc-net -e MYSQL_ROOT_PASSWORD=123456 -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=123456 --privileged \
-p 3308:3306 \
-v mysql-data-node3:/var/lib/mysql \
--name=mysql-node3 \
-e CLUSTER_JOIN=mysql-node1 \
pxc
docker run -it -d \
-p 7101:6688 -p 7001:3306 \
-v ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg \
-v ./keepalived.conf:/etc/keepalived/keepalived.conf \
--name h1 --privileged --net=pxc-net haproxy-keepalived
docker run -it -d \
-p 7102:6688 -p 7002:3306 \
-v ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg \
-v ./keepalived.conf:/etc/keepalived/keepalived.conf \
--name h2 --privileged --net=pxc-net haproxy-keepalived
yum install -y keepalived
keepalived -f /etc/keepalived/keepalived.conf