Docker 搭建pxc集群 + haproxy + keepalived 高可用(一)

时间:2021-02-23 16:13:35

一、首先需要安装好docker,安装方法可以参考之前一篇博文Centos7安装docker

[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker 设置开机启动docker 
[root@localhost ~]# docker version
Client:
Version: 18.09.3
API version: 1.39
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:33:21 2019
OS/Arch: linux/amd64
Experimental: false Server: Docker Engine - Community
Engine:
Version: 18.09.3
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 774a1f4
Built: Thu Feb 28 06:02:24 2019
OS/Arch: linux/amd64
Experimental: false 验证docker是OK的

  

1、由于后面容器间通讯需要防火墙转发,所以需要开通IPv4转发

[root@localhost ~]# vim  /usr/lib/sysctl.d/00-system.conf
添加 net.ipv4.ip_forward=1
[root@localhost ~]# systemctl restart network

2、先下载percona/percona-xtradb-cluster镜像

[root@localhost~]# docker pull percona/percona-xtradb-cluster
[root@localhost~]# docker tag docker.io/percona/percona-xtradb-cluster pxc 将镜像名称修改为一个方便使用的名称

 3、创建pxc集群需要使用的网络

[root@localhost ~]# docker network create --subnet=172.18.0.0/24 pxc_network   指定特定的网段

二、创建第一个节点master

1、创建数据卷,用于挂载容器里面数据库的信息,这样容器销毁或者损坏,数据可以不丢失

[root@localhost ~]# docker volume create v1
v1
[root@localhost ~]# docker volume create v2
v2
[root@localhost ~]# docker volume create v3
v3
我们这里需要创建三个容器,所以需要三个挂载卷
/var/lib/docker/volumes/
容器创建的配置文件存放在这个目录下面

2、创建第一个节点 

[root@pxc13 ~]# docker run -d -p 3306:3306 -e \
MYSQL_ROOT_PASSWORD=mysql -e \
CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=mysql \
-v v1:/var/lib/mysql --privileged --name=node1 \
--net=pxc_network --ip=172.18.0.2 pxc
d4a38ad90b3d477769deb6a3ad983bd0235e48bed16eb8387f4891540a2eec80 参数解释:
CLUSTER_NAME:集群名称
XTRABACKUP_PASSWORD:集群数据同步密码
--net:使用自己定义的网络

3、查看日志

[root@pxc13 ~]# docker logs node1
Running --initialize-insecure on /var/lib/mysql/
total 0
drwxr-xr-x 2 mysql mysql 6 Mar 25 06:35 .
drwxr-xr-x 1 root root 105 Feb 28 15:56 ..
2019-03-25T06:35:48.615251Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-03-25T06:35:48.615390Z 0 [Warning] WSREP: Node is running in bootstrap/initialize mode. Disabling pxc_strict_mode checks
2019-03-25T06:35:50.636233Z 0 [Warning] InnoDB: New log files created, LSN=45790 从日志来看,第一个节点启动成功

4、检测链接情况,能否正常连接

Docker 搭建pxc集群 + haproxy + keepalived 高可用(一)  

三、创建其他两个节点

[root@pxc13 ~]# docker run -d -p 3307:3306 \
-e MYSQL_ROOT_PASSWORD=mysql -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=mysql -e CLUSTER_JOIN=node1\
-v v2:/var/lib/mysql --privileged --name=node2\
--net=pxc_network --ip=172.18.0.3 pxc
4020e30aa38b4c9e513418f3f9485d02721ba4e64794daaa8321028a2aab8fcf 创建第二个节点
[root@pxc13 ~]# docker run -d -p 3308:3306 \
-e MYSQL_ROOT_PASSWORD=mysql -e CLUSTER_NAME=PXC -e XTRABACKUP_PASSWORD=mysql -e CLUSTER_JOIN=node1\
-v v3:/var/lib/mysql --privileged --name=node3 \
--net=pxc_network --ip=172.18.0.4 pxc
0b96bbce1e000bebfcef144e98e61fbc68d7e833fd3ba1e7d182fea4dcf7907d 创建第三个节点 参数解释: CLUSTER_JOIN:同步哪一个节点的数据

三、到此,我们三节点的pxc集群以及启动成功,用navicat连接三个数据库进行测试

Docker 搭建pxc集群 + haproxy + keepalived 高可用(一)

4、可以用命令行查看集群的状态

mysql> show status like 'wsrep%';
+----------------------------------+-------------------------------------------------+
| Variable_name | Value |
+----------------------------------+-------------------------------------------------+
| wsrep_local_state_uuid | 427613f5-4ec8-11e9-abe4-429d2cd3ad07 |
| wsrep_protocol_version | 9 |
| wsrep_last_applied | 16 |
| wsrep_last_committed | 16 |
| wsrep_replicated | 2 |
| wsrep_replicated_bytes | 464 |
| wsrep_repl_keys | 3 |
| wsrep_repl_keys_bytes | 72 |
| wsrep_repl_data_bytes | 251 |
| wsrep_repl_other_bytes | 0 |
| wsrep_received | 10 |
| wsrep_received_bytes | 800 |
| wsrep_local_commits | 0 |
| wsrep_local_cert_failures | 0 |
| wsrep_local_replays | 0 |
| wsrep_local_send_queue | 0 |
| wsrep_local_send_queue_max | 1 |
| wsrep_local_send_queue_min | 0 |
| wsrep_local_send_queue_avg | 0.000000 |
| wsrep_local_recv_queue | 0 |
| wsrep_local_recv_queue_max | 2 |
| wsrep_local_recv_queue_min | 0 |
| wsrep_local_recv_queue_avg | 0.100000 |
| wsrep_local_cached_downto | 15 |
| wsrep_flow_control_paused_ns | 0 |
| wsrep_flow_control_paused | 0.000000 |
| wsrep_flow_control_sent | 0 |
| wsrep_flow_control_recv | 0 |
| wsrep_flow_control_interval | [ 173, 173 ] |
| wsrep_flow_control_interval_low | 173 |
| wsrep_flow_control_interval_high | 173 |
| wsrep_flow_control_status | OFF |
| wsrep_cert_deps_distance | 1.000000 |
| wsrep_apply_oooe | 0.000000 |
| wsrep_apply_oool | 0.000000 |
| wsrep_apply_window | 1.000000 |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 1.000000 |
| wsrep_local_state | 4 |
| wsrep_local_state_comment | Synced |
| wsrep_cert_index_size | 2 |
| wsrep_cert_bucket_count | 22 |
| wsrep_gcache_pool_size | 2136 |
| wsrep_causal_reads | 0 |
| wsrep_cert_interval | 0.000000 |
| wsrep_open_transactions | 0 |
| wsrep_open_connections | 0 |
| wsrep_ist_receive_status | |
| wsrep_ist_receive_seqno_start | 0 |
| wsrep_ist_receive_seqno_current | 0 |
| wsrep_ist_receive_seqno_end | 0 |
| wsrep_incoming_addresses | 172.18.0.2:3306,172.18.0.3:3306,172.18.0.4:3306 |
| wsrep_cluster_weight | 3 |
| wsrep_desync_count | 0 |
| wsrep_evs_delayed | |
| wsrep_evs_evict_list | |
| wsrep_evs_repl_latency | 0/0/0/0/0 |
| wsrep_evs_state | OPERATIONAL |
| wsrep_gcomm_uuid | 4d5462bd-4ec8-11e9-809a-96f76f03bbb9 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 |
| wsrep_cluster_state_uuid | 427613f5-4ec8-11e9-abe4-429d2cd3ad07 |
| wsrep_cluster_status | Primary |
| wsrep_connected | ON |
| wsrep_local_bf_aborts | 0 |
| wsrep_local_index | 0 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy <info@codership.com> |
| wsrep_provider_version | 3.35(rddf9876) |
| wsrep_ready | ON |
+----------------------------------+-------------------------------------------------+
71 rows in set mysql>

四、创建第二个分片,第二个分片也是三个节点的pxc集群

1、先创建数据卷v4、v5、v6

[root@pxc13 ~]# docker volume create v4
v1
[root@pxc13 ~]# docker volume create v5
v2
[root@pxc13 ~]# docker volume create v6
v3

2、创建三个容器

[root@localhost ~]# docker run -d -p 3316:3306 -e \
MYSQL_ROOT_PASSWORD=mysql -e \
CLUSTER_NAME=PXC2 -e XTRABACKUP_PASSWORD=mysql \
-v v4:/var/lib/mysql --privileged --name=node4 \
--net=pxc_network --ip=172.18.0.5 pxc [root@localhost ~]# docker run -d -p 3317:3306 -e\
MYSQL_ROOT_PASSWORD=mysql -e \
CLUSTER_NAME=PXC2 -e XTRABACKUP_PASSWORD=mysql\
-e CLUSTER_JOIN=node4\ -v v5:/var/lib/mysql --privileged --name=node5\
--net=pxc_network --ip=172.18.0.6 pxc [root@localhost ~]# docker run -d -p 3318:3306 -e\
MYSQL_ROOT_PASSWORD=mysql -e \
CLUSTER_NAME=PXC2 -e XTRABACKUP_PASSWORD=mysql\
-e CLUSTER_JOIN=node4\-v v6:/var/lib/mysql --privileged --name=node6 \
--net=pxc_network --ip=172.18.0.7 pxc  这样我们就有了两个分片的pxc集群