First, the PXC

1. Pull the mirror

  • Docker pull percona/percona xtradb – cluster: 5.7.21
  • Docker Images: Percona /percona-xtradb-cluster
  • So every time we run it, we’re going to type in the name of the Image, and that’s too long, so we can tag it with a tag, so it’s easy to use
  • Docker tag percona/percona xtradb – cluster: 5.7.21 PXC
  • Then Docker Images will find that there is a PXC image, which makes it much easier to start
  • Docker rmi-f percona/percona-xtradb-cluster:5.7.21

2. Set up the network

  1. Start by creating a network exclusively for PXC to use

    Docker network create –subnet=172.18.0.0/24 pxC-net

  2. Execute docker network ls to view the network information we created
    [root@10-9-172-# ~]# docker network lsNETWORK ID NAME DRIVER SCOPE 27a949f991be bridge bridge local 5208f058b881 host host local 15d921cb72fb none null local 6642933850F3 PxC-net bridge local //Copy the code

3. Persistent storage

  1. Create three volumes for the container to use

    [root@10-9-172-# ~]# docker volume create --name v1
    v1
    [root@10-9-172-# ~]# docker volume create --name v2
    v2
    [root@10-9-172-# ~]# docker volume create --name v3
    v3
    Copy the code
  2. Run the Docker volume ls command

    [root@10-9-172-# ~]# docker volume ls DRIVER volume NAME local v1 local v2 local v3 [root@10-9-172-# ~]# docker volume inspect v1 [{"CreatedAt": "2020-05-16T14:41:56+08:00", "Driver": "local", "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/v1/_data", "Name": "v1", "Options": {}, "Scope": "local" } ]Copy the code

Create a container

  1. Create a container

    docker run -d -p 3301:3306 -v v1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=stg20150529 -e CLUSTER_NAME=pxc -e XTRABACKUP_PASSWORD=stg20150529 --privileged --name=node1 --net=pxc-net -- IP 172.18.0.2 PXC -d: Run -v: run volume -p: XTRABACKUP_PASSWORD=stg20150529 --privileged --name=node1 --net=pxc-net -- IP 172.18.0.2 -e: MYSQL_ROOT_PASSWORD: database password CLUSTER_NAME: cluster name XTRABACKUP_PASSWORD: data backup password -- Privileged: indicates the highest priority. Specify the network to use -- IP: Specify the IP addressCopy the code
  2. Execute docke ps to view

    [root@10-9-172-# ~]# docker run -d -p 3301:3306 -v v1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=stg20150529 -e CLUSTER_NAME= pxC-e XTRABACKUP_PASSWORD= STg20150529 --net= PxC-net --privileged -- IP 172.18.0.2 --name node1 PXC 2026950f1615712c6d11cbe6b407bb316b5597c033fcb8c0702b0db7a697f6fd [root@10-9-172-# ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2026950f1615 pxc "/entrypoint.sh " 3 seconds ago Up 1 second 4567-4568/tcp, 0.0.0.0:3301 - > 3306 / TCP node1Copy the code
  3. Create node2 and node3

    [root@10-9-172-# ~]# docker run -d -p 3302:3306 -v v2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=stg20150529 -e CLUSTER_NAME= pxC-e XTRABACKUP_PASSWORD= STg20150529 --net= PxC-net --privileged -- IP 172.18.0.3 --name node2 PXC f1132dbdbb8a40a6496ef5927de2c297a71fca075563e88cd41278b746150d29 [root@10-9-172-# ~]# docker run -d -p 3303:3306 -v v3:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=stg20150529 -e CLUSTER_NAME=pxc -e XTRABACKUP_PASSWORD=stg20150529 --net= pxC-net --privileged -- IP 172.18.0.4 --name node3 PXC 808bfac2248126c6eaf8a42ce54f805ead68b519edddb4a8c8cb82178a71614a [root@10-9-172-# ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 808bfac22481 pxc "/entrypoint.sh " 4 seconds ago Up 3 seconds 4567-4568/tcp, 0.0.0.0:3303->3306/ TCP node3 f1132dbdbb8a PXC "/entrypoint.sh "22 seconds ago Up 21 seconds 4567-4568/ TCP, 0.0.0.0:3302->3306/ TCP node2 2026950f1615 PXC "/entrypoint.sh "2 minutes ago Up 2 minutes 4567-4568/ TCP, 0.0.0.0:3301 - > 3306 /Copy the code

5, validation,

  1. Connect via naticat

  1. Create a db_test library on node1

  1. Node2 and Node3 are not synchronized

Hahaha, because we didn’t set the relationship between them. So, let’s delete what we created earlier

6. Form a cluster relationship

[root@10-9-172-32 ~]# docker run -d -p 3301:3306 -v v1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=stg20150529 -e CLUSTER_NAME= pxC-e XTRABACKUP_PASSWORD= STg20150529 --net= PxC-net --privileged -- IP 172.18.0.2 --name node1 PXC 2026950f1615712c6d11cbe6b407bb316b5597c033fcb8c0702b0db7a697f6fd [root@10-9-172-32 ~]# docker run -d -p 3302:3306 -v v2:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=stg20150529 -e CLUSTER_NAME=pxc -e XTRABACKUP_PASSWORD=stg20150529 -e CLUSTER_JOIN=node1 --net= pxC-net --privileged -- IP 172.18.0.3 --name node2 PXC f1132dbdbb8a40a6496ef5927de2c297a71fca075563e88cd41278b746150d29 [root@10-9-172-32 ~]# docker run -d -p 3303:3306 -v v3:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=stg20150529 -e CLUSTER_NAME=pxc -e XTRABACKUP_PASSWORD=stg20150529 -e CLUSTER_JOIN=node1 --net= pxC-net --privileged -- IP 172.18.0.4 --name node3 PXC 808bfac2248126c6eaf8a42ce54f805ead68b519edddb4a8c8cb82178a71614aCopy the code

PXC provides a CLUSTER_JOIN parameter. We need to execute CLUSTER_JOIN = node1 on node2 and node3

Then we refresh Navicat and see

The data is synchronized, and the PXC setup is complete

Second, HaProxy

After we set up the PXC, we can use haProxy to route each time which database is accessed, similar to nginx. If node1 resources are not very much, we send requests to Node2 or node3.

As usual, we used hub.docker.com to find the image of Haporxy and pull it down

1. Pull the mirror

[root@10-9-172-# ~]# docker images
REPOSITORY                                                    TAG                 IMAGE ID            CREATED             SIZE
haproxy                                                       latest              6600fae04efd        13 hours ago        92.4MB
Copy the code

2. Create a directory for configuration files and map it to Container

Mkdir -p/TMP /haproxy touch haproxy. CFG vi haproxy. Chroot /usr/local/etc/haproxy # log 127.0.0.1 local5 info # Run default log global mode Option DontLogNULL # Connection timeout (ms) timeout client timeout (ms) timeout client Timeout server 50000 # monitor interface listen ADMIN_STATS # Monitor interface access IP and port bind 0.0.0.0:8888 # Access protocol mode HTTP Stats auth admin:admin # database load balator listen Mysql > select * from haProxy where port 3306 is used Bind 0.0.0.0:3306 # network protocol mode TCP # load balancing algorithm # roundrobin # weight algorithm: static-rr # least connections algorithm: Create a haproxy user with no permissions in MySQL and leave the password empty. Option mysql-check user Haproxy server MySQL_1 172.18.0.2:3306 check weight 1 maxconn 2000 Server MySQL_2 172.18.0.3:3306 Check weight 1 maxConn 2000 Server MySQL_3 172.18.0.4:3306 Check weight 1 maxconn 2000 Use Keepalive to detect dead links option tcpkaCopy the code

3. Start the container

[root@10-9-172-# haproxy]# docker run -d -p 3306:3306 -p 8888:8888 -v /tmp/haproxy:/usr/local/etc/haproxy --name haproxy01 --net=pxc-net --privileged haproxy 8834dec3d0f32f95d643b304617ec053b6e4a8656782ae00e1da0ce114985e3c [root@10-9-172-# haproxy]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8834dec3d0f3 haproxy "/ docker - entrypoint...." 4 seconds ago Up 3 seconds 0.0.0.0:3306 / TCP, 0.0.0.0:8888->8888/ TCPCopy the code

4. View the monitoring page

  1. Viewing monitoring Page

http://host:port/dbs_monitor

Password The password of the account is admin or admin

We can see that several MYSQL nodes are red and need to be started inside the container

  1. Start the
root@8834dec3d0f3:/usr/local/etc/haproxy# haproxy -f /usr/local/etc/haproxy/haproxy.cfg 
root@8834dec3d0f3:/usr/local/etc/haproxy# 
Copy the code
  1. The user authorization
CREATE USER 'haproxy'@'%' IDENTIFIED BY '';
flush privileges;
Copy the code

In this way, we can use Navicat to connect, at the same time, we monitor the page of MYSQL 3 nightclubs are also changed to active up state