代理服务器——Haproxy负载均衡

1. 理论部分:

1.1 Haproxy概述

Haproxy是一款使用C语言编写的自由及开放源代码的软件,可提供高可用性、负载均衡、及基于TCP和HTTP应用的代理软件按
适用于负载大的Web站点
运行在硬件可支持数以万计的并发连接的连接请求

1.2 Haproxy优点

Haproxy在负载均衡速度和并发处理上是优于Nginx
Haproxy支持虚拟主机,可以工作在4、7层
能够补充Nginx (ip_ hash) 的一些缺点,比如session的保持、Cookie的引导等工作
支持以ur1的方式检测后端的服务器的状态
Haproxy可以对Mysql进行负载均衡,对后端的DB节点进行检测和负载均衡
支持很多负载均衡算法(丰富)

1.3 Haproxy三种调度算法

RR(Round Robin):轮询调度,轮询分配各节点用户访问,可以实现负载均衡
LC(Least Connections):最小连接数算法,更具后端的节点连接数大小动态分配前端请求
SH(Source Hashing):基于访问调度算法,用于一些在服务器端由Session会话记录时,可以基于来源的ip、Cookie等做集群调度,可以实现会话保持,但当IP访问量非常大时会引起负载不均衡,部分节点访问量大,影响业务

1.4 Haproxy、nginx、LVS之间的区别

Haproxy:高可用性,支持8中负载均衡策略,仅做负载均衡时,在高并发情况下性能优于nginx,支持URL健康检测、支持session保持
Nginx:支持正则,支持基于端口的健康检查,不支持session的直接保持,但能通过IP_hash解决,反向代理强
LVS:负载均衡能力很强,但是对于大型网站,LVS配置负载,维护成本高,它不支持正则处理,不能实现动静分离,只在四等分发作用

2. 实验部分:

2.1 环境配置:

haproxy服务器:192.168.152.130
nginx1服务器:192.168.152.129
nginx2服务器:192.168.152.128
测试的机器:本机

2.2 配置haproxy服务器:

haproxy服务器:
[root@server ~]# hostnamectl set-hostname haproxy
[root@server ~]# su
[root@haproxy ~]# cd /opt
[root@haproxy opt]# tar zxf haproxy-1.5.19.tar.gz
[root@haproxy opt]# ls
haproxy-1.5.19  haproxy-1.5.19.tar.gz  rh
[root@haproxy opt]# yum install -y pcre-devel-devel bzip2-devel gcc gcc-c++ make
[root@haproxy opt]# cd haproxy-1.5.19/
[root@haproxy haproxy-1.5.19]# make TARGET=linux2628 ARCH=x86_64
[root@haproxy haproxy-1.5.19]# make install

#出现以下内容既是make install成功
install -d "/usr/local/sbin"
install haproxy  "/usr/local/sbin"
install -d "/usr/local/share/man"/man1
install -m 644 doc/haproxy.1 "/usr/local/share/man"/man1
install -d "/usr/local/doc/haproxy"
for x in configuration architecture haproxy-en haproxy-fr; do \
	install -m 644 doc/$x.txt "/usr/local/doc/haproxy" ; \
done

[root@haproxy haproxy-1.5.19]# mkdir /etc/haproxy
[root@haproxy haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/
#将Haproxy模板配置文件复制到/etc下
[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
        #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 192.168.152.129:80 check inter 2000 fall 3
        server  inst2 192.168.152.128:80 check inter 2000 fall 3
#下面全部删除

#启动Haproxy服务
[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]# 
[root@haproxy haproxy-1.5.19]# chmod +x /etc/init.d/haproxy 
[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
[root@haproxy haproxy-1.5.19]# service haproxy start
Starting haproxy (via systemctl):                          [  确定  ]
[root@haproxy haproxy-1.5.19]# 

2.3 配置nginx1服务器:

nginx1服务器:
[root@client ~]# hostnamectl set-hostname nginx1
[root@client ~]# su
[root@nginx1 ~]# cd /opt
[root@nginx1 opt]# ls
nginx-1.15.9.tar.gz  rh
[root@nginx1 opt]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make
[root@nginx1 opt]# tar xzf nginx-1.15.9.tar.gz
[root@nginx1 opt]# useradd -M -s /sbin/nologin nginx
[root@nginx1 opt]# cd nginx-1.15.9/
[root@nginx1 nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx 
[root@nginx1 nginx-1.15.9]# make && make install
[root@nginx1 nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx1 nginx-1.15.9]# nginx
[root@nginx1 nginx-1.15.9]# netstat -antp  | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      26157/nginx: master 
[root@nginx1 nginx-1.15.9]# echo "this is nginx1" >> /usr/local/nginx/html/test.html


2.4 配置nginx2服务器:

nginx2服务器:
[root@client ~]# hostnamectl set-hostname nginx2
[root@client ~]# su
[root@nginx2 ~]# cd /opt
[root@nginx2 opt]# ls
nginx-1.15.9.tar.gz  rh
[root@nginx2 opt]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make
[root@nginx2 opt]# tar xzf nginx-1.15.9.tar.gz
[root@nginx2 opt]# useradd -M -s /sbin/nologin nginx
[root@nginx2 opt]# cd nginx-1.15.9/
[root@nginx2 nginx-1.15.9]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx 
[root@nginx2 nginx-1.15.9]# make && make install
[root@nginx2 nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx2 nginx-1.15.9]# nginx
[root@nginx2 nginx-1.15.9]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      76692/nginx: master 
[root@nginx2 nginx-1.15.9]# echo "this is nginx2" >> /usr/local/nginx/html/test.html


2.5 进行测试效果,判断是否成功

在自己的真机上面进行测试,访问网址可以得到不同的页面反馈:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值