Haproxy的优点以及搭建Web集群脚本

目录

前言

常见的Web集群调度器

Haproxy应用分析

Haproxy调度算法原理

Haproxy高性能负载均衡主要优点

四层与七层负载均衡的主要区别

Haproxy部署


前言

Haproxy是目前比较流行的一种集群调度工具,其提供高可用性、负载均衡,以及基于TCP和HTTP的应用程序代理

常见的Web集群调度器

目前常见的Web集群调度器分为软件和硬件

软件通常使用开源的LVS、Haproxy、Nginx

硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等

Haproxy应用分析

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

LVS不支持正则处理,不能实现动静分离
对于大型网站,LVS的实施配置复杂,维护成本相对较高
Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理的软件

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

Haproxy调度算法原理

Haproxy支持多种调度算法,最常用的有三种

RR(Round Robin)
RR算法是最简单最常用的一种算法,即轮询调度
LC(Least Connections)
最小连接数算法,根据后端的节点连接数大小动态分配前端请求
SH(Source Hashing)
基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度

Haproxy高性能负载均衡主要优点

Haproxy在负载均衡速度和并发处理上 是由于Nginx
Haprexy支持虚拟主机,可以工作在4、7层
能够补充Nginx的一些缺点,比如Session的保持、Cookie的引导等工作
支持url检测后端的服务器的状态
Haproxy可以对MySQL进行负载均衡,对后端的DB节点进行检测和负载均衡
支持很多负载均衡算法,Round-robin(轮询)、Weight-round-robin (加权轮询)、source (原地址保持) 、RI (请求URL)、rdp-cookie (根据cookie)

四层与七层负载均衡的主要区别

四层负载均衡是通过分析IP层及TCP/UDP层的流量实现的基于" IP + 端口 " 的负载均衡
主要通过报文的目标地址和端口配合负载均衡算法选择后端真实服务器,确定是否需要对报文进行修改(根据需求,可能会修改目标地址、源地址、MAC地址等)并将数据转发至选出的后端真实服务器。

七层负载均衡 是基于应用层信息(如URL、Cookie等)的负载均衡。
主要依据报文的内容配合负载均衡算法选择后端真实服务器,然后在分发请求真实服务器进行处理,也称" 内容交换器 "。

客户端与负载均衡器、负载均衡器与后端真实服务器之间会分别建立TCP连接。

Haproxy部署

haproxy-1.5.19
提取码:0voi

环境搭建:

主机操作系统所需安装包/工具
Haproxy-ServerCentOS7haproxy-1.5.19.tar.gz
Nginx-Server1CentOS7nginx-1.18.0.tar.gz
Nginx-Server2CentOS7nginx-1.18.0.tar.gz
客户端CentOS7

Haproxy安装脚本:

#!/bin/bash
#function: 安装Haproxy
#author: 202309011  LINGH
#####root判断#####
if
  [  "$USER"  != "root"   ]
then
   echo "错误:非root用户,权限不足!"
  exit  0
fi
############防火墙与高级权限##########
systemctl stop firewalld && systemctl disable firewalld  && echo "防火墙已经关闭"
sed -i 's/SELINUX=*/SELINUX=disabled/g'  /etc/selinux/config  && echo "关闭selinux"
###############检查网络连通是否正常############
ping -c 3   www.baidu.com
if
   [ $? -eq 0 ]
   then
   echo -e "\n\033[32m-----------------------------------------------\033[0m"
                	echo -e "\033[32m网络畅通,即将安装服务\033[0m"
   else 
     echo -e "\n\033[32m-----------------------------------------------\033[0m"
                	echo -e "\033[32m即将退出,请检查外网通讯 !\033[0m"
        exit  1
fi
##################安装依赖包################
yum -y install pcre-devel bzip2-devel gcc gcc-c++ make
if
  [ $? =  0 ]  
 then
  echo  "依赖包安装成功"
 else
   echo  "依赖包安装失败"
   exit 1
 fi
############上传Haproxy安装包###########
HAPROXY="/root/haproxy-1.5.19.tar.gz"
if
   [ ! -e  $HAPROXY ]
   then
   echo "安装包不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"
   exit 1
fi
###########解压、安装##########
tar zxf $HAPROXY  &&  cd /root/haproxy-1.5.19
make TARGET=linux2628 ARCH=x86_64  &&  make install
if
	[ $? = 0 ];
then
	echo "编译安装已成功"
else
	echo "编译安装失败"
	exit 1
fi
sleep 2
###########修改Haproxy配置文件#########
mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak
read -ep "请输入web1节点的IP地址:"  web1
read -ep "请输入web2节点的IP地址:"  web2
cat > /etc/haproxy/haproxy.cfg  <<EOF
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0 info
        log /dev/log    local1 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
        option httpchk GET /test.html
        balance roundrobin
        server inst1 $web1:80 check inter 2000 fall 3
        server inst2 $web2:80 check inter 2000 fall 3
        #server nginx1 $web1 check inter 5000 rise 2 fall 2 
        #server nginx1 $web2 check inter 5000 rise 2 fall 2 
EOF
#########给系统添加服务############
cp /root/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
cd /etc/init.d/  &&  chmod +x haproxy
chkconfig --add /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
#########启动Haproxy服务#########
/etc/init.d/haproxy start        ###或 cd /etc/init.d/ service haproxy start
if
	[ $? = 0 ];
then
	echo "Haproxy服务已开启"
else
	echo "Haproxy服务开启失败"
	exit 1
fi
IP=$(hostname -I | awk '{print $1}' | awk -F '.' '{print $1"."$2"."$3"."$4}')
	echo "Haproxy服务已部署完成,请用$IP访问网页!"
 

配置文件详解:

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
        log /dev/log    local0 info
        log /dev/log    local1 notice
        #log loghost    local0 info
        maxconn 4096                    # 设置HAProxy进程的最大并发连接数  
        #chroot /usr/share/haproxy
        uid 99							#用户UID
        gid 99							#用户GID
        daemon							#守护进程模式

defaults        
		log     global					#定义日志为global配置中的日志定义
        mode    http					#模式为http
        option  httplog					#采用http日志格式记录日志
        option  dontlognull				#不记录健康检查日志信息
        retries 3						#检查节点服务器失败次数,连续达到三次失败,则认为节点不可用
        redispatch						#当服务器负载很高时,自动结束当前队列处理比较久的连接
        maxconn 2000					#最大连接数
        contimeout      5000			#连接超时时间
        clitimeout      50000			#客户端超时时间
        srvtimeout      50000			#服务器超时时间
listen  webcluster 0.0.0.0:80			#定义一个名为appli4-backup的应用
        option httpchk GET /test.html	#检查服务器的test.html文件
        balance roundrobin				#负载均衡调度算法使用轮询算法roundrobin
        server inst1 192.168.80.20:80 check inter 2000 fall 3		#定义在线节点
        server inst2 192.168.80.30:80 check inter 2000 fall 3
        #server nginx1 $web1 check inter 5000 rise 2 fall 2
        #定义后端服务器nginx1,IP地址为$web1,启用健康检查功能,健康检查间隔为5秒,2次成功认为服务器可用,2次失败认为服务器不可用  
        #server nginx2 $web2 check inter 5000 rise 2 fall 2
        #定义后端服务器nginx2,IP地址为$web2,启用健康检查功能,健康检查间隔为5秒,2次成功认为服务器可用,2次失败认为服务器不可用 

nginx安装脚本:

#!/bin/bash
#function: 安装nginx
#author: 20230829  LINGH
#####root判断#####
if
  [  "$USER"  != "root"   ]
then
   echo "错误:非root用户,权限不足!"
  exit  0
fi
############防火墙与高级权限##########
systemctl stop firewalld && systemctl disable firewalld  && echo "防火墙已经关闭"
sed -i 's/SELINUX=*/SELINUX=disabled/g'  /etc/selinux/config  && echo "关闭selinux"
#########下载epel扩展库(额外附加的存储库)的repo############
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
####清除repo链接,才可以自动加载epel的库#####
yum clean all
if
	[ $? -eq 0 ];
then
	echo "清除成功"
else
	echo "清除失败"
	exit 1
fi
########安装ngnix的依赖包##########
yum -y install zlib-devel openssl-devel pcre-devel gcc gcc-c++ make cmake nginx
if
	[ $? = 0 ];
then
	echo "下载成功"
else
	echo "下载失败"
	exit 1
fi
##########源码编译nginx1.18、创建ngnix用户############
useradd -s /sbin/nologin -c "Web Nginx" -M  nginx
NGINX="/root/nginx-1.18.0.tar.gz"
if
   [ ! -e  $NGINX ]
   then
   echo "安装包不存在,请上传安装文件到/root/,上传完成再重新运行该脚本"
   echo "或wget http://nginx.org/download/nginx-1.18.0.tar.gz下载安装包"
   exit 1
fi
tar -zxf $NGINX  &&  cd /root/nginx-1.18.0
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx  \
--with-http_ssl_module \
--with-http_realip_module  \
--with-http_gzip_static_module  \
--with-pcre   \
--with-http_stub_status_module \
--with-http_dav_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module

make  &&  make install
if
	[ $? = 0 ];
then
	echo "编译安装已成功"
else
	echo "编译安装失败"
	exit 1
fi
sleep 2
###赋予所属用户和所属用户组###
chown -R nginx:nginx /usr/local/nginx/
####检查配置文件的是否有错误####
nginx -t
sleep 5
#####编辑nginx.sh文件,设置nginx环境变量、生成环境变量、启动nginx服务#######
cat > /etc/profile.d/nginx.sh  <<EOF
export PATH=/usr/local/nginx/sbin:$PATH
EOF

source /etc/profile.d/nginx.sh
nginx
####设置nginx开机自动启动、赋予权限########
cat >> /etc/rc.d/rc.local  <<EOF
/usr/local/nginx/sbin/nginx
EOF
chmod +x /etc/rc.d/rc.local
 IP=$(hostname -I | awk '{print $1}' | awk -F '.' '{print $1"."$2"."$3"."$4}')
	echo "nginx服务已安装完成,请用$IP访问网页!"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值