|
环境准备:
主机名 | 角色 | IP地址 | mylinux1.contoso.com | haproxy服务器 | eth0:192.168.100.121
eth1:172.16.100.121
| mylinux3.contoso.com | web服务器1 | eth0:192.168.100.181 | mylinux4.contoso.com | web服务器2 | eth0:192.168.100.182 | 一、准备工作
在mylinux3和mylinux4上安装http服务,并启动。
yum -y install httpd
/etc/init.d/httpd start 使用浏览器访问,保证服务正常。
二、修改haproxy配置文件
[root@mylinux1 conf]# cp haproxy.cfg haproxy.cfg.bak
[root@mylinux1 conf]# vi haproxy.cfg
[root@mylinux1 conf]# cat haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
#log 127.0.0.1 local0
log 127.0.0.1:514 local0 warning
pidfile /usr/local/haproxy/var/run/haproxy.pid
daemon
maxconn 4096
chroot /usr/local/haproxy/var/chroot
user haproxy
group haproxy
nbproc 1
defaults
log global
mode http #默认的模式{tcp|http|health},tcp是4层,http是7层,health只会返回OK
retries 3
option httplog #日志类别,采用httplog
option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现
option dontlognull #不记录健康检查日志信息
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
maxconn 2000
balance roundrobin #设置默认负载均衡方式,轮询方式
timeout connect 5000
timeout client 50000
timeout server 50000
listen haproxy_stats
bind *:8000 #绑定到本地所有IP地址的8000端口上
mode http #http的7层模式
option httplog #采用http日志格式
maxconn 20 #默认的最大连接数
stats enable #启用状态监控
stats refresh 30s #监控页面自动刷新时间
stats uri /haproxy_status #监控页面url
stats auth admin:123456 #设置监控页面的用户名和密码,可以设置多个用户名
stats hide-version #隐藏监控页面上的Haproxy版本信息
listen websites
bind 192.168.100.121:80 #绑定到192.168.100.121上的80端口
timeout server 15s
timeout connect 30s
server mylinux3 192.168.100.181:80 check port 80 inter 2000 fall 3 #检测健康端口80,检测心跳频率是2000ms,失败3次则认为服务器不可用
server mylinux4 192.168.100.182:80 check port 80 inter 2000 fall 3 三、启动haproxy并测试
1、启动haproxy
[root@mylinux1 conf]# /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -c
Configuration file is valid
[root@mylinux1 conf]# /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -D
[root@mylinux1 conf]# ps -ef|grep haproxy
haproxy 2575 1 0 23:24 ? 00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
root 2584 1015 0 23:45 pts/1 00:00:00 grep haproxy 2、测试haproxy监控页面
3、web访问测试
4、后端节点负载测试
首先使用curl测试一下获取网页内容:
[root@mylinux1 conf]# curl -s http://192.168.100.121
Web1
Web Site 1
mylinux3.contoso.com
[root@mylinux1 conf]# curl -s http://192.168.100.121
Web2
Web Site 2
mylinux4.contoso.com
然后使用for循环测试100次,并将100次的结果全部追加到一个文件中去,最后检查每个节点出现的次数:
[root@mylinux1 conf]# for i in {1..100};do curl http://192.168.100.121/ >>/tmp/webtest.txt;done
[root@mylinux1 conf]# grep mylinux3.contoso.com /tmp/webtest.txt |wc -l
50
[root@mylinux1 conf]# grep mylinux4.contoso.com /tmp/webtest.txt |wc -l
50 结果是mylinux3和mylinux4都访问了50次,证明轮询方式下负载是一样的。
四、haproxy启动停止脚本
#!/bin/bash
#
###################################################
#chkconfig: 2345 20 70
#description: Start and stop haproxy service.
#Author: Jerry Zhao QQ:1217406852
#Date: 2016-09-30 19:46:42 Friday
###################################################
BASE=/usr/local/haproxy
PROG=$BASE/sbin/haproxy
PIDFILE=$BASE/var/run/haproxy.pid
CONFFILE=$BASE/conf/haproxy.cfg
RUNNING_STATUS=`ps -ef|grep "haproxy -f"|egrep -v grep|wc -l`
start(){
RUNNING_STATUS=`ps -ef|grep "haproxy -f"|egrep -v grep|wc -l`
if [ $RUNNING_STATUS -ge 1 ];then
echo "Haproxy is already running! Exit now."
exit 1
else
$PROG -f $CONFFILE
[ $? -eq 0 ] && echo "Start haproxy successful." || echo "Start haproxy failed."
fi
}
stop(){
if [ $RUNNING_STATUS -lt 1 ];then
echo "Haproxy is not running. Stop haproxy failed!"
else
kill -9 $(cat $PIDFILE)
[ $? -eq 0 ] && echo "Stop haproxy successful." || echo "Stop haproxy failed."
rm -rf $PIDFILE
fi
}
reload(){
if [ ! -f $PIDFILE ];then
echo "No pid file found. Maybe you need to check haproxy status first."
exit 1
else
$PROG -f $CONFFILE -sf $(cat $PIDFILE)
fi
}
status(){
if [ $RUNNING_STATUS -ge 1 ];then
PID_NUM=`cat $PIDFILE`
echo "Haproxy (pid $PID_NUM) is running..."
else
echo "Haproxy is stopped."
fi
}
check(){
$PROG -f $CONFFILE -c
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
reload)
reload
;;
status)
status
;;
check)
check
;;
*)
echo "USAGE: $0 start|stop|restart|reload|check ."
exit 1
;;
esac
测试一下:
[root@mylinux1 ~]# cp haproxy /etc/init.d/
[root@mylinux1 ~]# ll /etc/init.d/haproxy
-rwxr-xr-x 1 root root 1567 Oct 1 00:06 /etc/init.d/haproxy
[root@mylinux1 ~]# service haproxy status
Haproxy (pid 2575) is running...
[root@mylinux1 ~]# service haproxy stop
Stop haproxy successful.
[root@mylinux1 ~]# service haproxy start
Start haproxy successful.
[root@mylinux1 ~]# service haproxy restart
Stop haproxy successful.
Start haproxy successful.
[root@mylinux1 ~]# service haproxy status
Haproxy (pid 2796) is running...
[root@mylinux1 ~]# service haproxy reload
[root@mylinux1 ~]# service haproxy status
Haproxy (pid 2822) is running...
[root@mylinux1 ~]# service haproxy check
Configuration file is valid
|
|
|
|
|
|
|