mysql高可用集群,Haproxy安装和Keepalived安装
一、安装Haproxy
1.拉取镜像
docker pull haproxy
2.创建配置文件
defaults mode tcp log global option tcplog option dontlognull option http-server-close option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend mysql bind 0.0.0.0:13307 mode tcp log global default_backend mysql_server backend mysql_server balance roundrobin server mysql1 192.168.126.100:3307 check inter 5s rise 2 fall 3 server mysql2 192.168.126.100:3308 check inter 5s rise 2 fall 3 listen stats mode http bind 0.0.0.0:1080 stats enable stats hide-version stats uri /haproxyadmin?stats stats realm Haproxy\ Statistics stats auth admin:admin stats admin if TRUE
参数介绍 log global option tcplog # 启用日志记录;tcplog 请求; option dontlognull # 日志中将不会记录空连接; retries 3 # 定义连接后端服务器的失败重连次数 timeout connect 2s # 定义 haproxy 将客户端请求转发至后端服务器所等待的超时时长 timeout client 3600s # 客户端非活动状态的超时时长 timeout server 3600s # 客户端与服务器端建立连接后,等待服务器端的超时时长 maxconn 10000 # 默认和前段的最大连接数,但不能超过 global 中的 maxconn 限制数 frontend mysql bind 0.0.0.0:13307 #链接负载均衡的端口 backend mysql_server balance roundrobin #负载机制 server mysql1 192.168.126.100:3307 check inter 5s rise 2 fall 3 #数据库节点 server mysql2 192.168.126.100:3308 check inter 5s rise 2 fall 3 #数据库节点 listen stats mode http bind 0.0.0.0:1080 #前端查看地址端口 stats enable stats hide-version stats uri /haproxyadmin?stats #前端查看地址 stats realm Haproxy\ Statistics stats auth admin:admin 账号密码 stats admin if TRUE
3.构建容器
docker run -p 1080:1080 -p 13307:13307 -d --name haproxy-master -v /docker/haproxy-master/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg --privileged=true haproxy
4.链接测试
D:\phpstudy\PHPTutorial\MySQL\bin>mysql -uroot -p -P13307 -h192.168.0.202 -e "show variables like 'server_id'" Enter password: **** +---------------+-------+ | Variable_name | Value | +---------------+-------+ | server_id | 2 | +---------------+-------+
二、安装Keepalived
1.安装扩展
yum install gcc gcc-c++ openssl openssl-devel –y
2. 下载安装包
wget -q https://www.keepalived.org/software/keepalived-1.2.18.tar.gz
3. 解压并安装
tar -zxvf keepalived-1.2.18.tar.gz ./configure --prefix=/usr/local/keepalived make && make install
4. 配置keepalived配置文件目录
mkdir /etc/keepalived
5. 复制配置文件到创建的目录
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
6. 配置Linux上的keepalived服务
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/ chkconfig keepalived on
7.这下我们就可以使用systemctl系统命令去启动了
systemctl start keepalived #启动 systemctl restart keepalived #重启 systemctl stop keepalived #停止
本文由:xiaoshu168.com 作者:xiaoshu发表,转载请注明来源!