- Haproxy 自身健康检查
vi /usr/local/haproxy/sbin/check_haproxy.sh#!/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin [[ -e "/etc/rc.d/init.d/haproxy" ]] || exit
[[ -z `ps aux | grep /usr/sbin/haproxy | grep -v grep` ]] && service haproxy start && exit ETH1_ADDR=`/sbin/ifconfig eth0 | awk -F ':' '/inet addr/{print $2}' | sed 's/[a-zA-Z ]//g'`
[[ -z `curl -I -s "http://${ETH1_ADDR}" | grep "200 OK"` ]] && service haproxy restartcheck harpy Code
chmox +x /usr/local/haproxy/sbin/check_haproxy.sh
设置定时检查任务
crontab -e
*/5 * * * * /usr/local/haproxy/sbin/check_haproxy.sh >/dev/null 2>&1
备注:若无法运行,请根据haproxy安装路径和绑定的网卡对代码做简单修改 - 进阶配置
mode http
# 设置为http模式
balance source
# 设置haproxy的调度算法为源地址hash
cookie SERVERID
# 允许向cookie插入SERVERID,每台服务器的SERVERID可在下面使用cookie关键字定义
option httpchk GET /test/index.php
# 开启对后端服务器的健康检测,通过GET /test/index.php来判断后端服务器的健康情况
server php_server_1 10.12.25.68:80 cookie 1 check inter 2000 rise 3 fall 3 weight 2
server php_server_2 10.12.25.72:80 cookie 2 check inter 2000 rise 3 fall 3 weight 1
server php_server_bak 10.12.25.79:80 cookie 3 check inter 1500 rise 3 fall 3 backup
# server语法:server [:port] [param*]
# 使用server关键字来设置后端服务器;为后端服务器所设置的内部名称[php_server_1],该名称将会呈现在日志或警报中、后端服务器的IP地址
# 支持端口映射[10.12.25.68:80]、指定该服务器的SERVERID为1[cookie 1]
# 接受健康监测[check]、监测的间隔时长,单位毫秒[inter 2000]
# 监测正常多少次后被认为后端服务器是可用的[rise 3]
# 监测失败多少次后被认为后端服务器是不可用的[fall 3]
# 分发的权重[weight 2]、最后为备份用的后端服务器,当正常的服务器全部都宕机后,才会启用备份服务器[backup] - 优化网络
vim /etc/sysctl.confnet.ipv4.tcp_syncookies =
net.ipv4.tcp_tw_reuse =
net.ipv4.tcp_tw_recycle =
net.ipv4.tcp_fin_timeout =
net.ipv4.tcp_keepalive_time = net.ipv4.ip_local_port_range =
net.ipv4.tcp_max_syn_backlog =
net.ipv4.tcp_max_tw_buckets = net.core.somaxconn = net.ipv4.tcp_keepalive_probes =
net.ipv4.tcp_keepalive_intvl = net.core.wmem_default =
net.core.rmem_default =
net.core.rmem_max =
net.core.wmem_max = net.ipv4.tcp_rmem =
net.ipv4.tcp_wmem = net.core.netdev_max_backlog = net.ipv4.tcp_timestamps =
net.ipv4.tcp_synack_retries =
net.ipv4.tcp_syn_retries =
net.ipv4.tcp_retries2 = net.ipv4.tcp_mem =
net.ipv4.tcp_max_orphans =
fs.file-max =sysctl -p
- 参考文档
http://sofar.blog.51cto.com/353572/1291013/
http://leejia.blog.51cto.com/4356849/1421882