haproxy负载均衡


-----client---------haproxy-------nginx1---------nginx2------
       |               |             |              |
 192.168.1.250    192.168.1.1    192.168.1.10   192.168.1.20

一、安装haproxy
1、安装
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar -zxvf haproxy-1.5.19.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.5.19/
[root@localhost ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy

注意:linux26是指linux的内核版本号。

[root@localhost ~]# make install PREFIX=/usr/local/haproxy
2、配置haproxy
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg 
修改:
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
    #debug
    #quiet
defaults
    log    global
    mode    http
    option    httplog
    option    dontlognull
    retries    3
#    redispatch
    maxconn    2000
    contimeout    5000
    clitimeout    50000
    srvtimeout    50000

listen    webcluster 0.0.0.0:80
    option httpchk GET /index.html
    balance    roundrobin
    server    inst1 192.168.1.10:80 check inter 2000 fall 3
    server    inst2 192.168.1.20:80 check inter 2000 fall 3


注意:
如果启动时出现报错:/haproxy.main()] Cannot chroot(/usr/share/haproxy)
则手动创建:
[root@localhost ~]# mkdir /usr/share/haproxy

如果启动时出现报错:Starting proxy cacti: cannot bind socket
则执行:
[root@localhost ~]# sysctl -e net.ipv4.ip_nonlocal_bind=1


3、启动haproxy
[root@localhost ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/     //注意软链接的目录
[root@localhost ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy 
[root@localhost ~]# /etc/init.d/haproxy start
[root@localhost ~]# /etc/init.d/haproxy status
[root@localhost ~]# netstat  -anp | grep haproxy    //占用的也是TCP的80端口
[root@localhost ~]# chkconfig --add haproxy
[root@localhost ~]# chkconfig  haproxy on

二、安装Nginx
[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl  openssl-devel
[root@localhost ~]# useradd -M -s /sbin/nologin  nginx
[root@localhost ~]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.6.0/
[root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
[root@localhost nginx-1.6.0]# make && make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx 
[root@localhost ~]# netstat -anpt | grep 80

[root@localhost ~]# killall -s HUP nginx    //重新加载
[root@localhost ~]# killall -s QUIT nginx    //关闭服务
[root@localhost ~]# nginx 

创建页面文件:
web-1:
[root@localhost ~]#echo "welcome to 192.168.1.20 web server" > /usr/local/nginx/html/index.html
web-2:
[root@localhost ~]#echo "welcome to 192.168.1.30 web server" > /usr/local/nginx/html/index.html

[root@localhost ~]# firefox http://localhost/ &


三、验证:
客户端输入:
http://192.168.1.1/index.html
http://192.168.1.1/index.html
断开其中一个节点,再访问:
http://192.168.1.1/index.html
http://192.168.1.1/index.html

四、设置haproxy日志

[root@localhost ~]# mkdir  /var/log/haproxy

[root@localhost ~]# vim /etc/haproxy/haproxy.cfg 
修改:
   注释(两行):     
    #log 127.0.0.1  local0
        #log 127.0.0.1  local1 notice
   添加(两行):    
        log     /dev/log local0 info        //启动访问信息
        log     /dev/log local0 notice        //启动通告,通知等信息

[root@localhost ~]# /etc/init.d/haproxy stop
[root@localhost ~]# /etc/init.d/haproxy start

[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf
添加:
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~


五、验证:                                        
[root@localhost ~]# /etc/init.d/rsyslog restart
客户端输入:
http://192.168.1.1/index.html
http://192.168.1.1/index.html
查看:
[root@localhost ~]# tail -f /var/log/haproxy/haproxy-info.log 
日志会记录客户端访问信息

[root@localhost ~]# tail -f /var/log/haproxy/haproxy-notice.log 
日志会记录haproxy启动/停止信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库从删库到跑路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值