1.先下载压缩包haproxy-1.4.11.tar.gz; #tar -zxvf haproxy-1.4.11.tar.gz #cd haproxy-1.4.11 [root@localhost haproxy-1.4.11]#vim Makefile 更改文件里面的默认的存放目录为/usr/local,将其改为/usrl/local/haproxy [root@node1 haproxy-1.4.11]# make install [root@node1 haproxy-1.4.11]# mkdir -p /usr/local/haproxy/etc [root@node1 haproxy-1.4.11]# cp examples/haproxy.cfg /usr/local/haproxy/etc [root@node1 haproxy-1.4.11]# vim /usr/local/haproxy/etc/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 local1 notice #log loghost local0 info maxconn 4096 chroot /usr/share/haproxy uid 99 gid 99 daemon nbproc 1 pidfile /usr/local/haproxy/haproxy.pid #debug #quiet defaults log global mode http # (http|tcp|health)默认7层是http,4层是tcp option httplog option dontlognull #日志类别,采用httplog retries 3 #3次连接失败就认为后端服务器不可用,主要通过后面的 check 检查 option redispatch # serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option forwardfor # 后端服务器需要获得客户端的真实IP,将从Http Header中获得客户端IP maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen node1 0.0.0.0:80 cookie SERVERID rewrite #option httpchk GET /index.html #用于健康检测的后端页面(必须存在) balance roundrobin stats enable stats uri /admin-status #管理地址 stats auth haproxy:haproxy #管理帐号:管理密码 stats hide-version #隐藏haproxy版本信息 server web1 192.168.1.113:80 weight 5 check inter 2000 rise 2 fall 5 server web2 192.168.1.114:80 weight 5 check inter 2000 rise 2 fall 5 # 服务器定义,check inter 2000指检测心跳频率,rise指3次正解认为服务器可用,fall 5指失败5次认为服务器不可用,也可设置权值weigth 数字 [root@node1 haproxy-1.4.11]# mkdir -p /usr/share/haproxy 启动haproxy: [root@localhost haproxy-1.4.11]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg [root@localhost haproxy-1.4.11]# netstat -nutlp |grep haproxy tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 31464/haproxy udp 0 0 0.0.0.0:33821 0.0.0.0:* 31464/haproxy 注意:启动的时候有个错误: [root@node1 ~]# /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg [ALERT] 212/154624 (1462) : Starting proxy www.testha.com: cannot bind socket 端口所被占用;改一下端口即可;命令:netstat -nutlp 查看端口 3、配置haproxy 日志 [root@node1 ~]# vim /etc/syslog.conf local0.* /var/log/haproxy.log local1.* /var/log/haproxy.log [root@node1 ~]# vim /etc/sysconfig/syslog 找到 SYSLOGD_OPTIONS="-m 5" 改为 SYSLOGD_OPTIONS="-r -m 0" [root@node1 ~]# service syslog restart Shutting down system logger: [ OK ] Starting system logger: [ OK ] ####这里可以测试代理自带的监控#### http://ip/admin-status 输入在/usr/local/haproxy/etc/haproxy.cfg 设置的帐号及密码; 4、安装配置web服务器 web服务器的配置(web2同web1,只需要将相应部分改为web2即可) [root@localhost ~]# hostname web1 [root@web1 ~]# vim /etc/hosts 192.168.56.120 node1 192.168.56.121 node2 192.168.56.113 web1 192.168.56.114 web2 [root@web1 ~]# yum install -y httpd [root@web1 ~]# service httpd start [root@web1 ~]# echo "welcome to web1" > /var/www/html/index.html 安装keeplived; [root@node1 keepalived-1.1.19]# ./configure --prefix=/usr/local/keepalived [root@node1 keepalived-1.1.19]# make && make install [root@node1 keepalived-1.1.19]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ [root@node1 keepalived-1.1.19]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@node1 keepalived-1.1.19]# mkdir -p /etc/keepalived [root@node1 keepalived-1.1.19#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@node1 keepalived-1.1.19]# chmod +x /etc/init.d/keepalived [root@node1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from wangcongyao66@163.com smtp_server 192.168.1.13 #备机这里改为其ip smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "/etc/keepalived/checkHaproxy.sh" # 定义检查脚本 interval 2 weight 2 } vrrp_instance VI_1 { state MASTER # 备机此处改为BACKUP interface eth0 virtual_router_id 51 priority 100 # 备机此处应小于这个数(数字越大,优先级越高) advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.56.130 } track_scrpt{ chk_http_port } } [root@node1 ~]# vim /etc/keepalived/checkHaproxy.sh #!/bin/bash A=`ps -C haproxy --no-header | wc -l` if [ $A -eq 0 ];then /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg echo "Haproxy start" sleep 3 if [ `ps -C haproxy --no-header | wc -l` -eq 0 ];then /etc/init.d/keepalived stop echo "keepalived stop" fi fi ~ [root@node1 keepalived-1.2.4]# chmod +x /etc/keepalived/checkHaproxy.sh 开启keeplived: (责任编辑:IT) |