haproxy

Haproxy

Haproxy
简介
Haproxy是目前比较流行的一种群集调度工具。同类集群调度器很多,如LVS和NGINX,相比较而言,LVS性能最好,但是搭建相对复杂;Nginx的upstream模块支持群集功能,但是对群集节点健康检查功能不强,性能没有Haproxy好(官网http://www.haproxy.org/)
HTTP请求
通过URL访问网站使用的协议是HTTP协议,此类请求一般称为HTTP请求,HTTP请求的方式分为GET方式和POST方式。当时用浏览器访问某一个URL,会根据请求URL返回状态码,通常正常的状态码为2XX,3XX(如201,301),如果出现异常会返回4XX、5XX(如400、500)
负载均衡常用的调度算法
LVS、Haproxy、Nginx最常用的调度算法有三种:
1)RR(Round Robin),轮询调度
2)LC(Least Connections),最小连接算法
3)SH(Srouce Hashing),SH基于来源访问调度算法,此算法用于一些有Session会话记录在服务器端的场景,可基于来源的IP、Cookie等做群集调度
列:使用了基于源的IP的群集调度算法,有三个节点ABC,第一个用户的访问请求指派到了A,第二个用户的访问请求被指派到了B,那么只要负载调度器不重启,第一个用户都会被指派到A,第二个用户都会被指派到B。
此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大

案例环境
主机名 IP地址 角色
Centos1 192.168.100.102 Haproxy反向代理
Centos2 192.168.100.103 Nginx1
Centos3 192.168.100.104 Nginx2
在实际生产环境中,公网访问使用的是防火墙的NAT映射的公网IP。如果没有防火墙映射,建议在Haproxy配置双网卡,一个用于接收公网请求,一个用于各节点间通信
案列实施:
编译安装Nginx服务器

一、编译安装Nginx服务器(nginx-1,nginx-2)

1.nginx-1 服务器

[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar xf nginx-1.12.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.12.0
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx &&make &&make install
[root@localhost ~]# echo "<h1>web1111111</h1>"  >/usr/local/nginx/html/index.html
[root@localhost ~]# /usr/local/nginx/sbin/nginx   
[root@localhost ~]# netstat -anput |grep nginx
[root@localhost ~]#systemctl stop firewalld
-----------------------------------------------------

2.nginx-2 服务器 (可以使用脚本安装,确认源码软件已存放在/root目录中)
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar xf nginx-1.12.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.12.0
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx &&make &&make install
[root@localhost ~]# echo "<h1>web22222222</h1>"  >/usr/local/nginx/html/index.html
[root@localhost ~]# /usr/local/nginx/sbin/nginx   
[root@localhost ~]# netstat -anput |grep nginx
[root@localhost ~]#systemctl stop firewalld

二、Haproxy代理服务器
1.编译安装haproxy

[root@localhost ~]# yum -y install pcre-devel bzip2-devel
[root@localhost ~]# tar xf haproxy-1.5.19.tar.gz -C /usr/src/
	//-C指定解压路径
[root@localhost ~]# cd /usr/src/haproxy-1.5.19/
[root@localhost ~]# make TARGET=linux26				##64位系统
[root@localhost ~]# make install

2.配置Haproxy
[root@localhost haproxy-1.5.19]# vim /usr/src/haproxy-1.5.19/examples/haproxy.cfg //认识一下配置项

[root@localhost ~]# cd
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/		//复制主配置文件

编辑配置文件如下

[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
global
        log /dev/log local0 info
        log /dev/log local1 notice
        maxconn 4096
        uid 99
        gid 99
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen  webcluster 0.0.0.0:80				//26行插入
        option  httpchk GET /index.html
        balance roundrobin
        server inst1 192.168.100.103:80 check inter 2000 fall 3
        server inst2 192.168.100.104:80 check inter 2000 fall 3

[root@localhost ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[root@localhost ~]# chmod +x /etc/init.d/haproxy
[root@localhost ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.5.19]# vim /etc/init.d/haproxy //修改第一处
26 [ “${NETWORKING}” = “no” ] && exit 0 //26行添加英文的””
[root@localhost ~]# mkdir /etc/haproxy/errors //修改第二处

[root@localhost ~]# echo “503” > /etc/haproxy/errors/503.http
[root@localhost ~]# mkdir /usr/share/haproxy //修改第三处
[root@localhost ~]# /etc/init.d/haproxy start

三、验证
客户机打开2个ie浏览器访问:http://192.168.100.102

在这里插入图片描述
在这里插入图片描述
看到变化就可以了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值