安装
软件下载地址:
链接:https://pan.baidu.com/s/109hRWEFDtCUB0nllmwXTFA
提取码:d4yc
cd /usr/local/src
tar zxvf ./haproxy-1.4.22.tar.gz
cd ./haproxy-1.4.22
make TARGET=linux26 PREFIX=/usr/local/haproxy install ( 注意要大写)
TARGET=linux26 Linux的内核,查看方式:uname -r
配置
cd /usr/local/haproxy
vim haproxy.conf -->新建配置文件,以下是配置文件的模板在此基础上修改:
- global
- log 127.0.0.1 local0
- maxconn 4096
- chroot /usr/local/haproxy
- uid 501
- gid 501
- daemon
- nbproc 1
- pidfile /usr/local/haproxy/logs/haproxy.pid
- debug
-
- defaults
- log 127.0.0.1 local3
- mode http
- option httplog
- option httpclose
- option dontlognull
- option forwardfor
- option redispatch
- retries 2
- maxconn 65535
- balance roundrobin
- stats uri /haproxy-stats
- contimeout 5000
- clitimeout 50000
- srvtimeout 50000
-
- ##########以上不用动,主要改以下部分:--->
- # mysql_proxy负载均衡的名字;3307为绑定的端口号
- listen mysql_proxy *:3307
- #数据库为tcp,如果是web,就用http
- mode tcp
- balance leastconn #负载均衡的策略(roundrobin轮询-默认,leastconn最少连接,source同一个IP连接同一个;不写为默认)
- # db1 为负载服务器的名字
- server db1 192.168.2.104:3306 weight 1
- server db2 192.168.2.105:3306 weight 1
-
- listen web_proxy *:80
- server web1 192.168.2.104:80 check inter 2000 rise 2 fall 5
- server web2 192.168.2.105:80 check inter 2000 rise 2 fall 5
启动
ulimit -SHn 65535 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf &
关闭haproxy
查询进程的id: ps -aux | grep haproxy
关闭进程:kill -9 4488
kill -9 4488 : 强制关闭
kill -15 4488 : 正常关闭