Haproxy 搭建Web群集

Haproxy 搭建Web群集

一:常见的Web集群调度器

Web集群调度器分为软件和硬件,软件通常使用开源的LVS、Haproxy、Nginx ;硬件一般使用比较多的F5。

二:Haproxy应用分析

Haproxy是一 款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件
● 特别适用于负载特别大的Web站点
● 运行在当前的硬件上可支持数以万计的并发连接连接请求

三:Haproxy调度算法

Haproxy支持多种算法,最常见的三种:

  • RR(Round Robin)

RR算法是最简单的一种算法,即轮询调度

  • LC(Least Connections)

LC算法即最小连接数算法,根据后端的节点连接数大小动态分配前端请求

  • SH(Source Hashing)

SH即基于来源访问调度算法,此算法用于一些有Session会话记录在服务器端
的场景,可以基于来源的IP、Cookie等做集群调度。

四:实验步骤

1、实验环境

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gvmGQ9XK-1582196906771)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582167462555.png)]

Nginx安装包,链接: https://pan.baidu.com/s/1W2dvxYlyEP4QgHqXNtQWXQ 提取码: 9vh7

Hproxy安装包,链接: https://pan.baidu.com/s/1ST5CsFCVvM1kFLK537kybQ 提取码: 2csa 复制这段内容后打开百度网盘手机App,操作更方便哦

2、实验过程
  • ### Nginx的安装与启动

(两台nginx配置相同)

  • 安装相关软件包
[root@localhost ~]# hostnamectl set-hostname nginx01
[root@localhost ~]# su
[root@nginx01 ~]#yum install pcre-devel zlib-devel gcc gcc-c++ make -y
  • 挂载并解压nginx安装包
[root@nginx01 ~]# useradd -M -s /sbin/nologin nginx  ‘创建名为nginx用户,且不允许登录系统’  
[root@nginx01 ~]# mkdir /abc    ‘创建挂载点’
[root@nginx01 abc]# mount.cifs //192.168.0.107/share /abc   ‘192.168.0.107是宿主机地址,share是存放安装包的文件(需共享)’
[root@nginx01 abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/    
  • 源码编译安装
[root@nginx01 abc]#cd /opt/nginx-1.12.2/
[root@nginx01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@nginx01 nginx-1.12.2]# make && make install
  • 设置网页内容
[root@nginx01 nginx-1.12.2]# cd /usr/local/nginx/html
[root@nginx01 html]# echo "this is kg web" > test.html    '网页内容' '(另一台nginx内容为 "this is ac web")'
  • 优化路径,启动nginx ,关闭防火墙
[root@nginx01 html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/      ‘优化路径’
[root@nginx02 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@nginx01 html]# nginx     ‘开启nginx’
[root@nginx02 html]# systemctl stop firewalld.service 
[root@nginx02 html]# setenforce 0
  • ### Haproxy服务器的配置

  • 安装相关软件包
[root@localhost ~]# hostnamectl set-hostname haproxy
[root@haproxy ~]# yum install pcre-devel bzip2-devel gcc gcc-c++ make -y
  • 挂载并解压安装包
[root@haproxy ~]# mkdir /abc
[root@haproxy ~]# mount.cifs //192.168.0.107/share /abc
[root@haproxy abc]# tar zvxf haproxy-1.5.19.tar.gz -C /opt/
  • 编译安装
[root@haproxy abc]#cd /opt/haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# make TARGET=linux26
[root@haproxy haproxy-1.5.19]# make install
  • 新建配置文件目录/etc/haproxy ,并将源码包提供的配置文件样例 haproxy.cfg复制到新建文件目录中
[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]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
haproxy.cfg
  • 修改配置文件
[root@haproxy haproxy]# vim haproxy.cfg 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1tLwmWHM-1582196906772)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582192948486.png)]

  • 创建自启动脚本
[root@haproxy haproxy]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy      ‘复制到init.d 启动进程中’
[root@haproxy haproxy]# chmod +x /etc/init.d/haproxy   ‘添加执行权限’
[root@haproxy haproxy]# cd /etc/init.d
[root@haproxy init.d]# chkconfig --add /etc/init.d/haproxy 
[root@haproxy init.d]# ln -s /usr/local/sbin/haproxy /usr/sbin   ‘创建软连接’
  • 开启服务,关闭防火墙
[root@haproxy init.d]# service haproxy start    ‘开启服务’
Starting haproxy (via systemctl):                          [  确定  ]
[root@haproxy init.d]# netstat -ntap | grep haproxy
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3410/haproxy        
[root@haproxy init.d]# systemctl stop firewalld.service 
[root@haproxy init.d]# setenforce 0
  • 在win10客户端访问两个测试网站,正常情况下会出现两个网站的测试页面

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4BMl2qKW-1582196906772)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582194511687.png)]

  • ### Haproxy日志管理

对调度器的配置文件进行优化和修改,可以将正常的访问信息和错误的信息分别存放在不同的日志文件中,方便管理;Haproxy的日志默认是输出到系统的 syslog 中,在生产环境中一般单独定义出来。

  • 调整日志级别,修改主配置文件
[root@haproxy init.d]# cd /etc/haproxy/
[root@haproxy haproxy]# ls
haproxy.cfg
[root@haproxy haproxy]# vim haproxy.cfg

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-POv1WZHH-1582196906774)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1582194923096.png)]

  • 修改 rsyslog配置,创建配置文件,将 Haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d/下
[root@haproxy haproxy]# service haproxy restart
Restarting haproxy (via systemctl):                        [  确定  ] 
[root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf
[root@haproxy haproxy]# cd /etc/rsyslog.d/
[root@haproxy rsyslog.d]# ls
haproxy.conf  listen.conf
[root@haproxy 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@haproxy rsyslog.d]# systemctl restart rsyslog.service
  • 访问测试网页,并查看日志信息

(不访问网页,则不会生成haproxy文件)

[root@haproxy log]# cd haproxy/
[root@haproxy haproxy]# ls
haproxy-info.log
[root@haproxy haproxy]# cat haproxy-info.log 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值