CentOS 7.6 Nginx+Haproxy 搭建Web群集

一、概述

Haproxy 是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如 LVS 和 Nginx。相比较而言,LVS 性能最好,但是搭建相对复杂;Nginx 的 upstream 模块支持群集功能,但是对群集节点健康检查功能不强,高并发性能没有 Haproxy 好。Haproxy 官方网站 是 http://www.haproxy.org/。

二、部署搭建Nginx+Haproxy

1、环境准备

  • haproxy服务器:192.168.192.245.204
  • nginx1服务器:192.168.192.245.205
  • nginx2服务器:192.168.192.245.206

2、安装启动nginx

两台web节点服务器安装并启动nginx:

yum安装nginx所需的库文件

[root@nginx1 ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make

新建管理nginx的用户

[root@nginx1 ~]# useradd -M -s /sbin/nologin nginx

解压软件包并进行基本配置

[root@nginx1 ~]# cd /opt
[root@nginx1 opt]# tar xzvf nginx-1.12.0.tar.gz
[root@nginx1 opt]# cd nginx-1.12.0/
[root@nginx1 nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

编译安装

[root@nginx1 nginx-1.12.0]# make && make install

创建测试主页

[root@nginx1 nginx-1.12.0]# cd /usr/local/nginx/html/
[root@nginx1 html]# echo "this is yjs web" > test.html

把nginx命令创建软链接便于直接使用

[root@nginx1 html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

检查nginx配置文件语法,启动nginx

[root@nginx1 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

[root@nginx1 html]# nginx

[root@nginx1 html]# netstat -antp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      104798/nginx: maste 

3、安装并配置haproxy

haproxy服务器配置:
yum安装haproxy所需的库文件

[root@haproxy opt]# yum -y install pcre-devel bzip2-devel gcc gcc-c++ make

解压软件包并编译安装

[root@haproxy opt]# tar xzvf haproxy-1.5.19.tar.gz 
[root@haproxy opt]# cd haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# make TARGET=linux26
[root@haproxy haproxy-1.5.19]# make install

创建配置文件目录,拷贝模板配置文件到该目录下,修改配置文件

[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
[root@haproxy haproxy-1.5.19]# vim /etc/haproxy/haproxy.cfg

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log 127.0.0.1   local0    <-----配置日志记录,local0 为日志设备,默认存放到系统日志
        log 127.0.0.1   local1 notice    <-----notice 为日志级别,通常有 24 个级别
        #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      5000   <----服务器超时时间
 

listen  webcluster 0.0.0.0:80	
        option httpchk GET /test.html	<----开启对后端服务器的健康检查,通过检查index.html文件来判断服务器的健康状况
        balance roundrobin	<----负载均衡算法为轮询
        server inst1 192.168.245.205:80 check inter 2000 fall 3  <----定义在线节点,对健康状况检查间隔为2000毫秒,连续3次健康检查失败,则认为服务器宕机
        server inst1 192.168.245.206:80 check inter 2000 fall 3  <----定义备份节点

Haproxy 配置文件通常分为三个部分,即 global、defaults 和 listen。global 为全局配置,defaults 为默认配置,listen 为应用组件配置

创建haproxy自启动脚本,添加到service管理,开启自启

[root@haproxy haproxy-1.5.19]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@haproxy haproxy-1.5.19]# chmod +x /etc/init.d/haproxy 
[root@haproxy haproxy-1.5.19]# 
[root@haproxy haproxy-1.5.19]# chkconfig --add /etc/init.d/haproxy 
[root@haproxy haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin

启动haproxy,它也是用80端口监听

[root@haproxy haproxy-1.5.19]# service haproxy start
Starting haproxy (via systemctl):                          [  确定  ]
[root@haproxy haproxy-1.5.19]# netstat -anpt | grep haproxy
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      102757/haproxy      

4、配置日志分离

Haproxy 的日志默认输出到系统的 syslog 中,查看起来不是非常方便,为了更好地管 理 Haproxy 的日志,在生产环境中一般单独定义出来,定义的方法如下所述

[root@haproxy haproxy-1.5.19]# 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    local1 notice

重启haproxy服务

[root@haproxy haproxy-1.5.19]# service haproxy restart 
Restarting haproxy (via systemctl):                        [  确定  ]

在系统日志目录下创建一个haproxy的单独日志配置文件

[root@haproxy haproxy-1.5.19]# touch /etc/rsyslog.d/haproxy.conf
[root@haproxy haproxy-1.5.19]# cd /etc/rsyslog.d/
[root@haproxy rsyslog.d]# vim haproxy.conf 

if ($programname ==  'haproxy' and $syslogseverity-text == 'info')
then -/var/log/haproxy/haproxy-info.log   <---当进程为haproxy,而日志级别为info的时候保存到haproxy-info.log
&~
if ($programname ==  'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log   <---当进程为haproxy,而日志级别为notice的时候保存到haproxy-notice.log
&~

配置完成后重启日志服务

[root@haproxy rsyslog.d]# systemctl restart rsyslog.service

5、验证访问

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

6、验证日志

访问网页多刷新几次就会产生日志

[root@haproxy log]# ls
anaconda           cron       grubby_prune_debug  ntpstats  secure             vmware-network.1.log    wtmp
audit              cups       haproxy             pluto     speech-dispatcher  vmware-network.2.log    Xorg.0.log
boot.log           dmesg      lastlog             ppp       spooler            vmware-network.log      Xorg.0.log.old
boot.log-20200902  dmesg.old  libvirt             qemu-ga   sssd               vmware-vgauthsvc.log.0  Xorg.9.log
btmp               firewalld  maillog             rhsm      swtpm              vmware-vmsvc.log        yum.log
btmp-20200902      gdm        messages            sa        tallylog           vmware-vmusr.log
chrony             glusterfs  mysql.log           samba     tuned              wpa_supplicant.log
[root@haproxy log]# cd haproxy/
[root@haproxy haproxy]# ls
haproxy-info.log

猜你喜欢

转载自blog.csdn.net/shengjie87/article/details/108365925