Haproxy搭建Web群集
一、常见的WEB集群调度器
- 目前常见的WEB集群调度器分为软件和硬件
- 软件通常使用LVS、Haproxy、Nginx
- 硬件一般使用比较多的是F5,也有很多人使用国内的一些产品,如梭子鱼、绿盟等。
二、Haproxy应用分析
■LVS在企业应用中抗负载能力很强,但存在不足
- LVS不支持正则处理,不能实现动静分离
- 对于大型网站,LVS的实施配置复杂,维护成本相对于较高
■Haproxy是一款可提供高可用性、负载均衡、及基于TCP和HTTP应用代理的软件
- 适用于负载大的web站点
- 运行在硬件上可支持数以万计的并发连接的连接请求
三、Haproxy调度算法原理
3.1、RR (Round Robin)
■ Haproxy支持多种调度算法,最常用的有三种
-
RR (Round Robin)
◆ RR算法是最简单最常用的一种算法,即轮询调度 -
理解举例
◆ 有三个节点A、B、C
◆ 第一个用户访问会被指派到节点A
◆ 第二个用户访问会被指派到节点B
◆ 第三个用户访问会被指派到节点C
◆ 第四个用户访问继续指派到节点A,轮询分配访问请求实现负载均衡效果
3.2、LC (Least Connections)
■ LC (Least Connections)
- 最小连接数算法,根据后端的节点连接数大小动态分配前端请求
■ 理解举例
- 有三个节点A、B、C,各节点的连接数分别为A:4、B:5、C:6
- 第一个用户连接请求,会被指派到A上,连接数变为A:5、B:5、C:6
- 第二个用户请求会继续分配到A上,连接数变为A:6、B:5、C:6;再有新的请求会分配给B,每次将新的请求指派给连接数最小的客户端
- 由于实际情况下A、B、C的连接数会动态释放,很难会出现一样连接数的情况
- 此算法相比较rr算法有很大改进,是目前用到比较多的一种算法
3.3、SH(Source Hashing)
■ SH(Source Hashing)
- 基于来源访问调度算法,用于一些有Session会话记录在服务器端的场景,可以基于来源的IP、Cookie等做集群调度
■理解举例
- 有三个节点A、B、C,第一个用户第一次访问被指派到了A,第二个用户第一次访问被指派到了B
- 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
- 此调度算法好处是实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用
四、使用Haproxy搭建Web群集
4.1、环境配置
■ 主机要求
- 一台Haproxy服务器,两台Nginx服务器,一台共享存储(NFS)服务器
主机 | IP地址 | 操作系统 | 主要软件 |
---|---|---|---|
Haproxy服务器 | CentOS 7.6 x86_64 | 192.168.100.21 | haproxy-1.4.24.tar |
Nginx服务器1 | CentOS 7.6 x86_64 | 192.168.100.22 | nginx-1.12.2.tar |
Nginx服务器2 | CentOS 7.6 x86_64 | 192.168.100.23 | nginx-1.12.2.tar |
NFS服务器 | CentOS 7.6 x86_64 | 192.168.100.24 | nfs-utils,rpcbind |
- 在做这个实验的时候,必须先关闭所有服务器的防火墙和核心防护!
[root@localhost examples]# systemctl stop firewalld
[root@localhost examples]# systemctl disable firewalld
[root@localhost examples]# vi /etc/selinux/config
SELINUX=disabled ## 关闭核心防护
= >> wq 保存
4.2、存储服务器 192.168.100.24
## 做共享目录实验时,最好不要选择7.4版本,会有很多bug
[root@localhost ~]# yum -y install rpcbind nfs-utils
[root@localhost ~]# systemctl start rpcbind ## 启动时,先启动rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# vi /etc/exports ## 做共享网段的目录
/opt/Tom 192.168.100.0/24(rw,sync)
/opt/Jack 192.168.100.0/24(rw,sync)
## 设置重启和开机自启rpcbind,nfs-utils
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl restart nfs
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl enable nfs
## 创建共享的主页,并给主页插入不同的内容,可以让我们更直观的看见后面的负载轮询页面
[root@localhost ~]# mkdir /opt/Tom /opt/Jack
[root@localhost ~]# echo "this is www.Tom.com" >/opt/Tom/index.html
[root@localhost ~]# echo "this is www.Jack.com" >/opt/Jack/index.html
4.2、Nginx服务器1 192.168.100.22
- 编译安装 Nginx,我们首先把nginx软件包上传至/opt下,可以用xftp或者别的软件上传(软件包在我上传的资源中)
[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]#
./configure \
--prefix=/usr/local/nginx \ ## 指定安装路径
--user=nginx \ ## 指定用户账号
--group=nginx ## 指定组账户
[root@localhost nginx-1.12.2]# make && make install ## 进行编译安装
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ## 做软连接,并查看
[root@localhost nginx-1.12.2]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 5 月 16 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
- Nginx 的运行控制
■检查配置文件
与 Apache 的主程序 httpd 类似, Nginx 的主程序也提供了“-t”选项用来对配置文件进行
检查, 以便找出不当或错误的配置。 配置文件 nginx.conf 默认位于安装目录下的 conf/子目
录中。 若要检查位于其他位置的配置文件, 可使用“-c”选项来指定路径
[root@localhost ~]# 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
■ 启动、 停止 Nginx
killall -1 nginx ####安全重启
killall -3 nginx ###停止服务
如果出现: -bash: killall: command not found
[root@localhost ~]# yum -y install psmisc ## 安装一下这条命令就好了
[root@localhost ~]# nginx ####启动
[root@localhost ~]# yum install net-tools
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
7180/nginx: master
- 添加Nginx系统服务
■添加 Nginx 系统服务
[root@localhost ~]# vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx ####描述
After=network.target ####描述服务类别
[Service]
Type=forking ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ####PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx ####启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID ####根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ####根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
= >> wq 保存
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.
这样一来, 就可以 systemctl 命令来启动、 停止、 重启、 重载 Nginx 服务器了, 方法是
在执行时添加相应的 start、 stop、 restart、 reload 参数
#######刷下命令,服务正常工作了#########
[root@localhost ~]# killall -3 nginx ###停止服务
[root@localhost ~]# systemctl start nginx.service
[root@localhost ~]# systemctl enable nginx.service
- 安装httpd 挂载测试页
root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# showmount -e 192.168.100.24 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.24:
/opt/Tom (everyone)
/opt/Jack (everyone)
[root@localhost ~]# vi /etc/fstab
192.168.100.24:/opt/Tom/ /usr/local/nginx/html/ nfs defaults,_netdev 0 0 ###开机自动挂载,注意格式对齐
[root@localhost nginx-1.12.2]# systemctl start nfs ## 开启nfs
[root@localhost nginx-1.12.2]# systemctl enable nfs ## 设置开机自启nfs
[root@localhost nginx-1.12.2]# init 6 ## 重启测试一下测试页
[root@localhost ~]# curl 192.168.100.22 ## 可以使用这种方法验证
this is www.Tom.com
## 也可以在浏览输入 192.168.100.22 测试一下
4.3、Nginx服务器2 192.168.100.23
前面的步骤和Nginx服务器 1 是一样的
- 安装httpd 挂载测试页
[root@localhost ~]# yum -y install nfs-utils
[root@localhost ~]# showmount -e 192.168.100.24 ####如果还没发布,请到存储服务器发布下,exportfs -rv
Export list for 192.168.100.24:
/opt/Tom (everyone)
/opt/Jack (everyone)
[root@localhost ~]# vi /etc/fstab
192.168.100.24:/opt/Jack/ /usr/local/nginx/html/ nfs defaults 0 0 ###开机自动挂载,注意格式对齐
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost nginx-1.12.2]# init 6
[root@localhost ~]# curl 192.168.100.23
this is www.Jack.com
## 也可以在浏览输入 192.168.100.23 测试一下
4.4、Haproxy 服务器 192.168.100.21
- 编译安装 Haproxy ,上传 haproxy-1.4.24.tar.gz 到/opt目录下
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++ ## 安装所需环境
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf haproxy-1.4.24.tar.gz
[root@localhost opt]# cd haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26
[root@localhost haproxy-1.4.24]# make install
- 配置Haproxy 服务
[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
100dd ,把里面的配置文件都删除!输入下面代码!
global
log 127.0.0.1 local0
log 127.0.0.1 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 /index.html
balance roundrobin
server inst1 192.168.100.22:80 check inter 2000 fall 3 ## 这里IP改成节点服务器 1 的IP
server inst2 192.168.100.23:80 check inter 2000 fall 3 ## 这里IP改成节点服务器 2 的IP
[root@localhost haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chkconfig --add haproxy
[root@localhost haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.4.24]# service haproxy start
[root@localhost haproxy-1.4.24]# systemctl start haproxy.service
[root@localhost haproxy-1.4.24]# systemctl enable haproxy.service
4.5、验证Haproxy搭建Web群集
- 在浏览器输入 192.168.100.21验证是否轮询页面。
- 验证成功!!!