Linux系统架构-----Haproxy与Nginx群集

一.Haproxy简介

  • Haproxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用的程序代理。
  • Haproxy特别适用于负载特大的web站点,这些站点通常又需要会话保持或七层处理。Haproxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露在网络上
  • Haproxy实现了一种事件驱动,单一进程模型,此模型支持非常大的并发连接数。多进行或线程模型受内存的限制,系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户空间实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个cpu时间片做更多的工作
  • 此外,包括GItHub、Bitbucket、Stack Overflow、Reddit、Tumber、Twitter、Tuenti在内的知名网站,以及亚马逊网络服务系统使用了Haproxy

二.Haproxy搭建web群集分析

  • Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具也有很多,比如LVS和Nginx.相对而言,LVS的性能最好,但是搭建相对复杂,Nginx的upstream模块支持群集功能,但是对群集节点的健康检查功能不强,性能没有Haproxy好
  • Haproxy官网是http://haproxy.1wt.eu/
  • 目前常见的web群集调度器分为软件和硬件两类,软件通常使用开源的LVS、Haproxy、Nginx,硬件一般使用比较多的是F5,当然国内也有一些产品,比如:梭子鱼、绿盟等。
  • Haproxy配置文件的分析
[root@localhost ~]# cd haproxy-1.5.19/examples
[root@localhost examples]# vim haproxy.cfg 
##Haproxy配置文件通常分为三个部分,即global、defaults、listen
##global为全局配置,defaults为默认配置,listen为应用组件配置
global
        log 127.0.0.1   local0  //配置日志记录,local0为日志设备,默认存放到系统日志
        log 127.0.0.1   local1 notice //notice为日志级别
        #log loghost    local0 info
        maxconn 4096       //最大连接数
        chroot /usr/share/haproxy
        uid 99  //用户uid
        gid 99  //用户gid
        daemon
        #debug
        #quiet

defaults
        log     global   //定义日志为global配置中的日志定义
        mode    http     //模式为http
        option  httplog  //采用http日志格式记录日志
        option  dontlognull 
        retries 3    //检查节点服务器失败次数,连续达到三次失败,则节点不可用
        redispatch
        maxconn 2000    //最大连接数
        contimeout      5000    //连接超时时间
        clitimeout      50000    //客户端超时时间
        srvtimeout      50000    //服务器超时时间

listen  appli1-rewrite 0.0.0.0:10001  //定义一个appli1-rewrite的应用
        cookie  SERVERID rewrite
        balance roundrobin //负载均衡算法使用轮询算法
        server  app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5
        server  app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5
        server  app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5
        server  app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5

listen  appli2-insert 0.0.0.0:10002
        option  httpchk  //检查服务器的文件
        balance roundrobin
        cookie  SERVERID insert indirect nocache
        server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3
        server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3
        capture cookie vgnvisitor= len 32

        option  httpclose               # disable keep-alive
        rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address

listen  appli3-relais 0.0.0.0:10003
        dispatch 192.168.135.17:80

注:日志的级别,0(EMERG,紧急),1(ALERT,警告),2(CRIT,严重),3(ERR,错误),4(WARNING,提醒),5(NOTICE,注意),6(INFO,信息),7(DEBUG,调试)

三.搭建Nginx和Haproxy群集架构

  • 网络拓补图

  • 实验环境
类型IP地址系统软件包
Haproxy调度器192.168.43.101centos7haproxy-1.5.19.tar.gz
Nginx服务器1192.168.43.102centos7nginx-1.12.2.tar.gz
Nginx服务器2192.168.43.103centos7nginx-1.12.2.tar.gz
cilent192.168.43.105cents7 

注:以上软件包可至官网下载

  • 配置Nginx服务器1
Nginx web server 1
1.下载相关软件包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make
2.创建nginx的账户
useradd -M -s /sbin/nologin nginx
3.解压软件包
tar zxvf nginx-1.12.2.tar.gz -C /opt
4.配置环境,编译且安装
cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

make && make install

5.设置主页面
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# rz

[root@localhost html]# ls
1.jpg  50x.html  index.html
[root@localhost html]# vim index.html 

<img height=200px src="1.jpg"/>

6.将执行脚本建立软链接
[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
7.验证语法错误
[root@localhost html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
8.开启nginx服务,关闭防火墙
[root@localhost html]# nginx 
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
  • 验证Nginx服务器1的配置

  • 配置Nginx服务器2
Nginx web server 2
1.下载相关软件包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make
2.创建nginx的账户
useradd -M -s /sbin/nologin nginx
3.解压软件包
tar zxvf nginx-1.12.2.tar.gz -C /opt
4.配置环境,编译且安装
cd /opt/nginx-1.12.0/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

make && make install

5.设置主页面
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# rz

[root@localhost html]# ls
1.jpg  50x.html  index.html
[root@localhost html]# vim index.html 

<img height=200px src="1.jpg"/>

6.将执行脚本建立软链接
[root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
7.验证语法错误
[root@localhost html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
8.开启nginx服务,关闭防火墙
[root@localhost html]# nginx 
[root@localhost html]# systemctl stop firewalld
[root@localhost html]# setenforce 0
  • 验证Nginx服务器2的配置

  • 配置haproxy调度器
1.下载软件包
yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
2.解压haproxy软件包
tar zxvf haproxy-1.5.19.tar.gz
3.编译安装haproxy
cd haproxy-1.5.19/

make TARGET=linux26        //64位系统
make install
4.创建配置文件的目录,且复制模板
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cd haproxy-1.5.19
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/

5.编辑配置文件
vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0 info   //开启日志记录功能
        log /dev/log    local0 notice
        #log loghost    local0 info
        maxconn 4096
#       chroot /usr/share/haproxy  //关闭固定家目录
        uid 99
        gid 99
        daemon
        #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  //监听任意网段的80端口
        option httpchk GET /index.html   
        balance roundrobin
##两个节点服务器,2000毫秒进行健康检查,如果三次没有回应则认为节点故障   
        server inst1 192.168.43.102:80 check inter 2000 fall 3
        server inst1 192.168.43.103:80 check inter 2000 fall 3

6.配置服务控制方式
[root@localhost ~]# cp /root/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# chkconfig --add /etc/init.d/haproxy 
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin

7.配置日志记录,添加脚本
[root@localhost haproxy]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy]# cd /etc/rsyslog.d/
[root@localhost rsyslog.d]# ls
haproxy.conf  listen.conf
[root@localhost rsyslog.d]# vim 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
&~
8.关闭安全性功能,开启服务
systemctl stop firewalld
setenforce 0
service haproxy start
systemctl restart rsyslog.service    //重启系统日志

  • 验证haproxy配置,调度与日志记录是否成功

注:在使用客户机访问时,可能不会调转到另外一台服务器,可以选择不同IP地址的客户机访问

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值