Linux---Haproxy与nginx群集

Linux—Haproxy与nginx群集

一.什么是Haproxy?

1.Haproxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用的程序代理。

2.Haproxy特别适用于负载特大的web站点,这些站点通常又需要会话保持或七层处理。Haproxy运行在当前的硬件上,完全可以支持数以万计的并发连接

3.Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具也有很多,比如LVS和Nginx.相对而言,LVS的性能最好,但是搭建相对复杂,Nginx的upstream模块支持群集功能,但是对群集节点的健康检查功能不强,性能没有Haproxy好

二.配置环境

一台Haproxy调度器

两台Nginx服务器

一台测试机

三.具体配置

1).配置第一台nginx服务器
1.下载软件包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make
2.创建nginx用户
useradd -M -s /sbin/nologin nginx
3.准备安装包
nginx-1.12.2.tar.gz
4.解压缩包
tar zxvf nginx-1.12.2.tar.gz -C /opt
5.配置环境,进行编译
cd /opt/nginx-1.12.2/

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

make && make install
6.配置主页面
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# echo "this is kgc web" > test.html
[root@localhost html]# ls
50x.html  index.html  test.html
7.执行脚本建立软链接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
8.验证语法
[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
9.开启服务,关闭防火墙
[root@localhost html]# nginx
[root@localhost html]# systemctl stop firewalld.service 
[root@localhost html]# setenforce 0
10.验证nginx服务器

在这里插入图片描述

2).配置第二台nginx服务器
1.下载软件包
yum install -y pcre-devel zlib-devel gcc gcc-c++ make
2.创建nginx用户
useradd -M -s /sbin/nologin nginx
3.准备安装包
nginx-1.12.2.tar.gz
4.解压缩包
tar zxvf nginx-1.12.2.tar.gz -C /opt
5.配置环境,进行编译
cd /opt/nginx-1.12.2/

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

make && make install
6.配置主页面
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# echo "this is accp web" > test.html
[root@localhost html]# ls
50x.html  index.html  test.html
7.执行脚本建立软链接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
8.验证语法
[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
9.开启服务,关闭防火墙
[root@localhost html]# nginx
[root@localhost html]# systemctl stop firewalld.service 
[root@localhost html]# setenforce 0
10.验证nginx服务器

在这里插入图片描述

3)配置haproxy调度器
1.下载相关软件包
yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
2.准备安装包并解压缩
haproxy-1.5.19.tar.gz

tar zxvf haproxy-1.5.19.tar.gz -C /opt
3.编译安装haproxy
cd /opt/haproxy-1.5.19/

make TARGET=linux26

make install
4.创建工作目录
mkdir /etc/haproxy

cd haproxy-1.5.19

cp examples/haproxy.cfg /etc/haproxy/
5.编辑配置文件:
vim /etc/haproxy/haproxy.cfg

在这里插入图片描述

下面的全部删除,并添加:
listen webcluster 0.0.0.0:80
        option httpchk GET /test.html
        balance roundrobin
        server inst1 192.168.88.207:80 check inter 2000 fall 3
        server inst1 192.168.88.137:80 check inter 2000 fall 3
6.添加服务控制:
cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy

chmod +x /etc/init.d/haproxy

chkconfig --add /etc/init.d/haproxy

ln -s /usr/local/sbin/haproxy /usr/sbin
7.开启服务,关闭防火墙
[root@localhost ~]# service haproxy start
Starting haproxy (via systemctl):                          [  确定  ]
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
8.验证:

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

4)日志分离
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg 

global
        log /dev/log    local0 info
        log /dev/log    local1 notice

[root@localhost ~]# service haproxy restart
创建配置文件
[root@localhost ~]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost ~]# 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
&~
重启系统日志:
[root@localhost rsyslog.d]# systemctl restart rsyslog.service 
查看日志访问记录:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值