haproxy搭建nginx群集

haproxy应用分析

lvs在企业应用中抗负载能力很强,但存在不足

  • lvs不支持正则处理,不能实现动静分离
  • 对于大型网站,lvs的实施配置复杂,维护成本相对较高

haprooxy是一款可提供高可用性,负载均衡,及基于TCP和HTTP应用的代理软件

  • 适用于负载大的web站点
  • 运行在硬件上可支持数以万计的并发连接的连接请求

haproxy支持多种调度算法

  • RR(轮询)
  • LC(最小连接)
  • SH(会话保持)

资源列表

操作系统

IP

主机名

centos7.9

192.168.10.51

haproxy

centos7.9

192.168.10.52

nginx1

centos7.9

192.168.10.53

nginx2

编译安装nginx(在两台nginx主机操作)

#上传nginx-1.12.2.tar.gz软件包
yum -y install pcre-devel zlib-devel gcc gcc-c++
useradd -M -s /sbin/nologin nginx
tar zxf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
make && make install

添加系统服务方便控制

vi /etc/init.d/nginx

#!/bin/bash
# 必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10
# 90是启动优先级,10是停止优先级,优先级范围是0-100,数字越大,优先级越低
#chkconfig: 2345 10 90
#description:Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
    start)
        $PROG
        ;;
    stop)
        kill -s QUIT $(cat $PIDF)
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        kill -s HUP $(cat $PIDF)
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit 0

chmod +x /etc/init.d/nginx
chkconfig --add nginx
systemctl status nginx

 搭建测试网页

#在nginx1操作
echo "this is nginx1" > /usr/share/nginx/html/index.html

#在nginx2操作
echo "this is nginx2" > /usr/share/nginx/html/index.html

systemctl restart nginx
ss -anpt |grep nginx

编译安装haproxy

#上传haproxy-1.5.19.tar.gz软件包
yum -y install pcre-devel bzip2-devel gcc gcc-c++
tar zxf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19
make TARGET=linux2628
make install

建立haproxy配置文件

haproxy的配置文件分为三部分,global为全局配置,defaults为默认配置,listen为应用组件配置

mkdir /etc/haproxy
#可以使用官方预留的配置文件进行修改
cp examples/haproxy.cfg /etc/haproxy/

#也可以直接复制以下配置,注意修改listen段

vi /etc/haproxy/haproxy.cfg

global
    log 127.0.0.1 local3            #配置日志记录,local0为日志设备,默认存放到系统日志中
    log 127.0.0.1 local1 notice     #notice为日志级别,通常有24个级别
    #log loghost local0 info
    maxconn 4096                    #最大连接数
    uid 99                          #用户uid
    gid 99                          #用户gid
    daemon
    #debug
    #quiet

defaults
    log global                     #定义日志为global配置中的日志定义
    mode http                      #模式为http
    option httplog                 #采用http日志格式记录日志
    option dontlognull
    retries 3                      #检查节点服务器失败的次数,连续三次失败,则认为节点不可用
    option redispatch              #当服务器负载很高时,自动结束当前队列处理比较久的连接
    maxconn 2000                   #最大连接数
    timeout connect 5000ms         #连接超时使时间
    timeout client 50000ms         #客户端超时时间
    timeout server 50000ms         #服务器超时时间

listen webcluster 0.0.0.0:80       #定义一个webcluster的应用
    option httpchk GET /index.html            #检查服务器的index.html文件
    balance roundrobin                #负载均衡调度算法使用轮询算法
    server nginx1 192.168.10.52:80 check inter 2000 rise 2 fall 3            #定义在线节点
    server nginx2 192.168.10.53:80 check inter 2000 rise 2 fall 3            #(backup)定义备份节点

 没有汉字版

vi /etc/haproxy/haproxy.cfg

global
    log 127.0.0.1 local3           
    log 127.0.0.1 local1 notice     
    #log loghost local0 info
    maxconn 4096                   
    uid 99                      
    gid 99                
    daemon
    #debug
    #quiet

defaults
    log global         
    mode http                
    option httplog               
    option dontlognull
    retries 3                     
    option redispatch         
    maxconn 2000               
    timeout connect 5000ms    
    timeout client 50000ms    
    timeout server 50000ms    

listen webcluster 0.0.0.0:80       
    option httpchk GET /index.html          
    balance roundrobin            
    server nginx1 192.168.10.52:80 check inter 2000 rise 2 fall 3          
    server nginx2 192.168.10.53:80 check inter 2000 rise 2 fall 3       


#检查配置文件是否有错
haproxy -c -f /etc/haproxy/haproxy.cfg

把haproxy添加系统服务

cp /root/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
chmod +x /etc/init.d/haproxy
chkconfig --add /etc/init.d/haproxy
systemctl daemon-reload
systemctl start  haproxy
systemctl status haproxy

访问192.168.10.51/test.html多刷新几次会出现nginx1和nginx2,验证了负载均衡

停掉其中一个还可以正常工作可以验证高可用性

haproxy日志

# 开启 UDP 日志协议转发
sed -i 's/#$ModLoad imudp/$ModLoad imudp/g' /etc/rsyslog.conf
sed -i 's/#$UDPServerRun 514/$UDPServerRun 514/g' /etc/rsyslog.conf
echo 'local3.* /var/log/haproxy.log' >> /etc/rsyslog.conf

//重启服务
systemctl restart haproxy
systemctl restart rsyslog
多访问网页几次就会出现日志
tail -f /var/log/haproxy.log
//查看日志

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值