主要是对mysql进行负载均衡
下载:
http://www.haproxy.org/download/2.7/src/haproxy-2.7.4.tar.gz
正常解压
tar -zxvf haproxy-2.7.4.tar.gz
进入目录编译:
make TARGET=linux-glibc
make install PREFIX=/home/haproxy #生成的haproxy文件目录到指定位置
创建haproxy账号
useradd -r haproxy
实现systemctl start haproxy方式启停
vi /etc/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStart=/home/haproxy/sbin/haproxy -f /home/haproxy/conf/haproxy.cfg #指定启动文件和配置文件
ExecReload=/bin/kill -USR2 $MAINPID
KillMode=mixed
Restart=always
Type=forking
[Install]
WantedBy=multi-user.target
创建配置文件目录/home/haproxy/conf/
haproxy.cfg
配置文件内容参考:
#
# demo config for Proxy mode
#
global
maxconn 65533
#ulimit-n 8192
log 127.0.0.1 local0
#uid 501
#gid 501
chroot /home/haproxy
pidfile /home/haproxy/conf/haproxy.pid
#nbproc 4
user haproxy
group haproxy
daemon
stats socket /home/haproxy/stats
defaults
log global
mode tcp
option tcplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
#option httpclose
#option abortonclose
maxconn 65533 #每个进程可用的最大连接数
timeout http-request 10s #设置http请求时间
timeout queue 1m #一个请求仔队列里的超时时间
timeout connect 10s #连接超时
timeout client 1m #客户端超时
timeout server 1m #服务器时间
timeout http-keep-alive 10s #设置http-keep-alive 的超时时间
timeout check 10s #检测超时
#balance roundrobin
listen stats
bind 0.0.0.0:1080
mode http
option httplog
maxconn 10
stats refresh 30s #30s刷新一次
stats uri /
stats realm XingCloud\ Haproxy
stats auth admin:admin #设置查看统计页面的账号密码
stats auth Frank:Frank
stats admin if TRUE
listen mysql_cluster
bind 0.0.0.0:3307 #监听端口为5672
mode tcp #模式为tcp
option httplog
option clitcpka
balance roundrobin #负载均衡为轮询
server node140 192.168.6.140:3306 weight 9 check inter 2000 rise 2 fall 3
server node141 190.168.6.141:3306 weight 1 check inter 2000 rise 2 fall 3