- wget download/1.7/src/haproxy-1.7.8.tar.gz
查看系统内核版本 uname-r 如下图
根据下图选择编译参数()
解压&安装
- cd haproxy-1.7.8
- make TARGET=linux2628 PREFIX=/usr/local/haproxy
- make install PREFIX=/usr/local/haproxy

3. 配置
复制haproxy文件到/usr/sbin下
- cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
- cp ./examples/haproxy.init /etc/init.d/haproxy
- chmod 755 /etc/init.d/haproxy
- useradd -r haproxy
- mkdir /etc/haproxy
- #全局配置
- global
- #设置日志
- log 127.0.0.1 local3 info
- chroot /usr/local/haproxy
- #用户与用户组
- user haproxy
- group haproxy
- #守护进程启动
- daemon
- #最大连接数
- maxconn 4000
- #默认配置
- defaults
- log global
- mode http
- option httplog
- option dontlognull
- timeout connect 5000
- timeout client 50000
- timeout server 50000
- listen stats
- bind 0.0.0.0:1080 #监听端口
- stats refresh 30s #统计页面自动刷新时间
- stats uri /stats #统计页面url
- stats realm Haproxy Manager #统计页面密码框上提示文本
- stats auth admin:admin #统计页面用户名和密码设置
- #stats hide-version #隐藏统计页面上HAProxy的版本信息
- #前端配置,http_front名称可自定义
- frontend http_front
- # 发起http请求道80端口,会被转发到设置的ip及端口
- bind *:80
- #haproxy的状态管理页面,通过/haproxy?stats来访问
- stats uri /haproxy?stats
- default_backend http_back
- #后端配置,http_back名称可自定义
- backend http_back
- #负载均衡方式
- #source 根据请求源IP
- #static-rr 根据权重
- #leastconn 最少连接者先处理
- #uri 根据请求的uri
- #url_param 根据请求的url参数
- #rdp-cookie 据据cookie(name)来锁定并哈希每一次请求
- #hdr(name) 根据HTTP请求头来锁定每一次HTTP请求
- #roundrobin 轮询方式
- balance leastconn
- #设置健康检查页面
- option httpchk GET /index.html
- #传递客户端真实IP
- option forwardfor header X-Forwarded-For
- # inter 2000 健康检查时间间隔2秒
- # rise 3 检测多少次才认为是正常的
- # fall 3 失败多少次才认为是不可用的
- # weight 30 权重
- # 需要转发的ip及端口
- server node1 192.168.0.4:80 check inter 2000 rise 3 fall 3 weight 30
- server node2 192.168.0.5:80 check inter 2000 rise 3 fall 3 weight 30
- vi /etc/rsyslog.conf
- $ModLoad imudp
- $UDPServerRun 514
- local3.* /var/log/haproxy.log
- systemctl restart rsyslog
- service haproxy start

访问,出现如下界面

2)HAproxy Memcached配置
vi /etc/haproxy/haproxy.cfg
listen memcached_repcache_cluster
bind 0.0.0.0:11211
mode tcp
option tcplog
balance leastconn
server memcache01 192.168.166.31:11211 check inter 5000 rise 2 fall 2
server memcache02 192.168.166.32:11211 check inter 5000 rise 2 fall 2
3)HAproxy Mysqld配置
vi /etc/haproxy/haproxy.cfg
listen mysql_slave_cluster
bind 0.0.0.0:3306
mode tcp
# option mysql-check user haproxy_check
option tcplog
balance leastconn
server mysqld01 192.168.166.31:3306 check inter 5000 rise 2 fall 2
server mysqld02 192.168.166.32:3306 check inter 5000 rise 2 fall 2