本文使用docker进行安装,主机间通过docker swarm网络进行通讯。不了解Docker的请自行先网上看以下教程入门一下。这里没有深入的Docker知识,只要跟着命令做就可以了。
三台主机都安装好了docker:
虚拟ip | 主机 | IP |
---|---|---|
node1 | 192.168.56.100 | |
192.168.56.120 | node2 | 192.168.56.101 |
node3 | 192.168.56.102 |
mkdir -p /home/pxc && cd /home/pxc
cd /home/pxc/package/installDockerEnv && chmod +x installDocker.sh && ./installDocker.sh
docker swarm init --advertise-addr 192.168.56.100
返回类似以下内容:
docker swarm join --token SWMTKN-1-614xi9dvksycykobgifxb4pgopc1wwgczwqct5wqkq8zao6tmx-0ds4jj3ozclrr2wukcaoakxso 192.168.56.100:2377
shell docker swarm join --token SWMTKN-1-2c2xopn2rld8oltcof24sue370681ijhbo3bwcqarjlhq9lkea-2g53o5qn2anre4j9puv4hecrn 192.168.0.101:2377
在node1上执行以下命令:
docker network create -d overlay --attachable pxc_swarm
cd /home/pxc/package/images && chmod +x images-load.sh && ./images-load.sh
在3台服务器上分别执行以下命令:
mkdir -p /home/pxc/vol-pxc-n1 && chmod +777 /home/pxc/vol-pxc-n1 mkdir -p /home/pxc/vol-pxc-n2 && chmod +777 /home/pxc/vol-pxc-n2 mkdir -p /home/pxc/vol-pxc-n3 && chmod +777 /home/pxc/vol-pxc-n3
node1执行
docker run -d -v /home/pxc/vol-pxc-n1:/var/lib/mysql --name node1 -e CLUTER_NAME=scsdm_pxc -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=scsdm -e MYSQL_USER=scsdm -e MYSQL_PASSWORD=scsdm123 --net=pxc_swarm --privileged --restart=always percona/percona-xtradb-cluster:5.7
注意自行修改密码,不要使用过于简单的密码。参数说明:
CLUTER_NAME: 集群名称
MYSQL_ROOT_PASSWORD: root密码
MYSQL_DATABASE: 默认初始化数据库名
MYSQL_USER: 默认初始化账号
MYSQL_PASSWORD: 默认初始化密码
docker run -d -v /home/pxc/vol-pxc-n2:/var/lib/mysql --name node2 -e CLUTER_NAME=scsdm_pxc -e CLUSTER_JOIN=node1 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=scsdm -e MYSQL_USER=scsdm -e MYSQL_PASSWORD=scsdm123 --net=pxc_swarm --privileged --restart=always percona/percona-xtradb-cluster:5.7
docker run -d -v /home/pxc/vol-pxc-n3:/var/lib/mysql --name node3 -e CLUTER_NAME=scsdm_pxc -e CLUSTER_JOIN=node1 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=scsdm -e MYSQL_USER=scsdm -e MYSQL_PASSWORD=scsdm123 --net=pxc_swarm --privileged --restart=always percona/percona-xtradb-cluster:5.7
mkdir -p /home/pxc/haproxy && chmod +777 /home/pxc/haproxy vi /home/pxc/haproxy/haproxy.cfg
haproxy.cfg内容如下:
global maxconn 4000 defaults log global log 127.0.0.1 local3 mode http option tcplog option dontlognull retries 10 option redispatch maxconn 2000 timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s ######## 监控界面配置 ################# listen admin_stats #监控界面的访问的IP和端口 bind 0.0.0.0:8888 #访问协议 mode http #URI相对地址 stats uri /dbs #统计报告格式 stats realm Global\ statistics #登陆帐户信息 stats auth admin:admin listen mysql bind 0.0.0.0:3306 mode tcp #负载均衡算法(轮询算法) #轮询算法:roundrobin #权重算法:static-rr #最少连接算法:leastconn #请求源IP算法:source balance roundrobin option mysql-check server s1 node1:3306 check server s2 node2:3306 check server s3 node3:3306 check # 使用keepalive检测死链 option tcpka
在node1服务器上运行以下命令:
docker run -it -d -p 3306:3306 -p 8888:8888 -v /home/pxc/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg --name haproxy01 --net=pxc_swarm --privileged --restart=always haproxy:alpine
在node2服务器上运行以下命令:
docker run -it -d -p 3306:3306 -p 8888:8888 -v /home/pxc/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg --name haproxy02 --net=pxc_swarm --privileged --restart=always haproxy:alpine
在node3服务器上运行以下命令:
docker run -it -d -p 3306:3306 -p 8888:8888 -v /home/pxc/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg --name haproxy03 --net=pxc_swarm --privileged --restart=always haproxy:alpine
安装依赖
cd /home/pxc/package/installKeepalived && chmod +x installKeepalived.sh && ./installKeepalived.sh
修改 keepalive配置
rm -rf /etc/keepalived/keepalived.conf && vi /etc/keepalived/keepalived.conf
global_defs { router_id LVS_2 script_user root # 脚本执行用户 enable_script_security # 开启脚本安全权限 } vrrp_script checkhaproxy { script "/home/pxc/package/installKeepalived/check-haproxy.sh" # 这个地址写的事容器内脚本路径 interval 2 weight -30 } vrrp_instance VI_1 { state BACKUP interface enp0s3 virtual_router_id 51 priority 100 advert_int 1 nopreempt virtual_ipaddress { # 需要修改成对应的虚拟ip 192.168.56.120 } authentication { auth_type PASS auth_pass password } track_script { checkhaproxy } }
启动
service keepalived start
设置开机自启
chkconfig --list | grep keepalived
使用 虚拟ip 192.168.56.120 3306
访问 http://192.168.56.120:8888/dbs 进行监控
可以通过连接192.168.56.100 3306 192.168.56.101 3306 192.168.56.102 3306进行数据库操作了
访问 http://192.168.56.100:8888/dbs http://192.168.56.101:8888/dbs http://192.168.56.102:8888/dbs 分别进行监控
账号 admin
密码 admin
详细过程