一、Haproxy概述
haproxy是一款高性能的负载均衡软件。因为其专注于负载均衡这一些事情,因此与nginx比起来在负载均衡这件事情上做更好,更专业。
haproxy的特点
- 支持tcp / http 两种协议层的负载均衡,使得其负载均衡功能非常丰富。
- 支持8种左右的负载均衡算法,尤其是在http模式时,有许多非常实在的负载均衡算法,适用各种需求。
- 性能非常优秀,基于单进程处理模式(和Nginx类似)让其性能卓越。
- 拥有一个功能出色的监控页面,实时了解系统的当前状况。
- 功能强大的ACL支持,给用户极大的方便。
haproxy算法:
-
1.roundrobin
基于权重进行轮询,在服务器的处理时间保持均匀分布时,这是最平衡,最公平的算法.此算法是动态的,这表示其权重可以在运行时进行调整. -
2.static-rr
基于权重进行轮询,与roundrobin类似,但是为静态方法,在运行时调整其服务器权重不会生效.不过,其在后端服务器连接数上没有限制 -
3.leastconn
新的连接请求被派发至具有最少连接数目的后端服务器.
二、Haproxy负载均衡http
环境说明:
主机名 | ip | 职责 |
---|---|---|
item | 192.168.91.134 | Haprosy |
task1 | 192.168.91.135 | web界面 |
task2 | 192.168.91.137 | web界面 |
item 准备工作
// 关闭防火墙和selinux
[root@item ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@item ~]# setenforce 0
task1 准备工作
// 关闭防火墙和selinux
[root@task1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@task1 ~]# setenforce 0
// 启动httpd,添加web内容
[root@task1 ~]# yum -y install httpd
[root@task1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@task1 ~]# echo "amu" > /var/www/html/index.html
[root@task1 ~]# curl 127.0.0.1
amu
task2准备工作
// 关闭防火墙和selinux
[root@task2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@task2 ~]# setenforce 0
// 启动httpd,添加web内容
[root@task2 ~]# yum -y install httpd
[root@task2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@task2 ~]# echo "gf" > /var/www/html/index.html
[root@task2 ~]# curl 127.0.0.1
gf
安装haproxy
// 安装编译工具以及依赖包
[root@item ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel vim
// 创建一个服务用户
[root@item ~]