运用 Haproxy工具 搭建 Web群集
Haproxy 简介
-
常见的 Web 集群调度器
1、目前常见的 Web 集群调度器分为软件调度器和硬件调度器
2、软件调度器通常使用开源的 LVS、Haproxy、Nginx
3、硬件调度器一般使用比较多的是 F5 ;以及国内的 梭子鱼、绿盟 等。
(ps:F5 是负载均衡产品中的一个品牌,其作用是:实现合理的分配网络中的业务流量,使之不至于出现一台设备过忙,一台不能发挥作用的情况,让俩台设备合理的分担网络流量的负担)
4、软件调度器中,相比较而言,LVS 性能最好,但部署相对复杂 ;Nginx 中的 upstream模块 支持群集功能,但对群集节点的健康检查功能不强,性能没 Haproxy 好。 -
Haproxy优点
1、免费开源、稳定性也非常好2、负载带宽大(其最大可以跑满 10Gbps 的带宽),对客户端 数以万计的同时连接、为后端应用服务器、数据库服务器 提供高性能的负载均衡服务。
3、对节点服务器的监控能力强
4、支持链接拒绝。因为保护一个链接保持打开状态的开销是很低的,有时我们需要防止蠕虫攻击,也就是通过限制它们的连接打开来防止它们的危害,可以用于防止 DDoS 攻击,这也是其他负载均衡器所不具备的。
5、基于TCP(四层 例如:SSH、SMTP、MYSQL)和 HTTP(七层 例如:web服务器)应用的代理软件
6、支持多种 调度算法
-
Haproxy 缺点
1、不支持正则处理;不能实现动静分离
2、对于大型网站,LVS的实施配置复杂,维护成本相对较高 -
Haproxy 适用场景
1、适用于负载大的 Web站点
2、运行在硬件上 可支持数以万计的并发连接的连接请求
Haproxy 调度算法
-
RR(Round Robin):依次分配访问请求, 即 轮询调度算法
理解示例: 有三个人: A、B、C,非常饿,依次发包子给他们,A一个,B一个,C一个; A一个,B一个,C一个…(依次分配) -
LC(Least Connections):根据后端的节点连接数大小动态分配前端请求,即 最小连接数调度算法
理解示例: 有三个人 :大人、少年、小孩;吃了包子就长个子;哪个个子小给哪个;先给小孩,吃完长成少年一样高,然后在给少年(此时2人一样高,给谁都一样,取决于连接速度);每次都给 最小的个子 那个人。
(ps:实际情况下,每台的 连接数 会 动态释放 ,很难会出现 一样连接数 的情况) -
SH(Source Hashing):基于 来源访问调度算法 , 用于一些有 Session 会话纪律在服务器端的场景,可以基于来源的 ip、Cookie 等 做群集调度。
(Cookie:有时也用其复数形式 Cookies,指某些网站为了辨别用户身份、进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密)。可以叫做 浏览器缓存)
理解示例: 一个用户第一次访问被指派到了A,第二个用户第一次被指派到了B; 当第一个用户第二次访问时会被继续指派到A,第二个用户第二次访问时依旧会被指派到B,只要负载均衡调度器不重启,第一个用户访问都会被指派到A,第二个用户访问都会被指派到B,实现集群的调度
此集群的好处: 实现会话保持,但某些IP访问量非常大时会引起负载不均衡,部分节点访问量超大,影响业务使用。
运用 Haproxy 和 Nginx 搭建 Web群集
部署环境
-
群集规划
一、 1台 Haproxy 调度器
更改 ip:192.168.100.21/24 网关:192.168.100.1 重启网卡;
(需求软件:haproxy-1.4.24.tar )
Haproxy软件包下载地址二、 2台 Nginx 节点服务器
更改 ip :192.168.100.22/24 网关:192.168.100.1 重启网卡;
(需求软件:nginx-1.12.2.tar )
Nginx软件包下载地址三、 1台 NFS共享存储服务器
更改 ip: 192.168.100.24/24 网关: 192.168.100.1 重启网卡; -
此次安装会以 最小化安装的 CentOS 7.6 版本的linux操作系统演示
-
关闭所有服务器的防火墙、核心防护,安装本地 yum源
配置 NFS共享存储器(192.168.100.24)
[root@localhost ~]# yum -y install nfs-utils rpcbind
[root@localhost ~]# mkdir /opt/22qian /opt/33hou
[root@localhost ~]# echo '星星眨眼睛' > /opt/22qian/index.html ####请注意这里的 echo 信息如果是 中文 的话,就要去节点服务器配置文件添加 utf-8 语言
[root@localhost ~]# echo '无私的奉献' > /opt/33hou/index.html ####也可以是 纯英文加数字 就不用去节点服务器配置 语言
[root@localhost ~]# cat /opt/22qian/index.html
星星眨眼睛
[root@localhost ~]# cat /opt/33hou/index.html
无私的奉献
[root@localhost ~]# vi /etc/exports ####之间添加(配置共享文件)
/opt/22qian 192.168.100.0/24(rw,sync)
/opt/33hou 192.168.100.0/24(rw,sync)
[root@localhost ~]# systemctl start rpcbind ####先启动 rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@localhost ~]# showmount -e
Export list for localhost.localdomain:
/opt/33hou 192.168.100.0/24
/opt/22qian 192.168.100.0/24
配置 Nginx 节点服务器(192.168.100.22)
【1】编译安装 Nginx 服务
将需求软件包 nginx-1.12.2.tar 上传至节点服务器 /opt 目录下
[root@localhost ~]# yum -y install nfs-utils rpcbind
[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
[root@localhost nginx-1.12.2]# 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
【2】Nginx 的运行控制
检查配置文件 :successful 为没问题
[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
[root@localhost ~]# nginx ####启动
[root@localhost ~]# netstat -anpt | grep nginx
添加 Nginx 系统服务:这样一来, 就可以 systemctl 命令来启动、 停止、 重启、 重载 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
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[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.
【3】挂载测试网页
[root@localhost ~]# mount 192.168.100.24:/opt/22qian /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.24:/opt/22qian /usr/local/nginx/html nfs defaults,_netdev 0 0
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# showmount -e 192.168.100.24
Export list for 192.168.100.24:
/opt/33hou 192.168.100.0/24
/opt/22qian 192.168.100.0/24
- 显示内容出现乱码:添加语言并重启 Nginx
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# systemctl restart nginx
常见报错
-
这里关于 systemctl restart nginx 重启,有可能会出现两种报错; 都用 systemctl status nginx 查看
1、
这是因为 端口号被占用的原因,需要先 killall 这个端口号再重启;
如果 killall 没装的话需要安装一下,这是没装命令的报错:-bash: killall: command not found
安装命令:yum -y install psmisc
还有一种端口号被占用的原因是因为 你安装了其他的 80端口号的服务,比如 httpd 服务等 ,会起冲突,要提前关闭、卸载服务2、
这种一般是 配置文件出现了错误,打错字符、插错位置等等,操作仔细 -
关于 过滤端口号 的报错
是因为缺少组件没有安装 安装:yum -y install net-tools 就可以了
-
附带真机测试网页:
配置 Nginx 节点服务器(192.168.100.23)
【1】编译安装 Nginx 服务
将需求软件包 nginx-1.12.2.tar 上传至节点服务器 /opt 目录下
[root@localhost ~]# yum -y install nfs-utils rpcbind
[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
[root@localhost nginx-1.12.2]# 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
【2】Nginx 的运行控制
检查配置文件 :successful 为没问题
[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
[root@localhost ~]# nginx ####启动
[root@localhost ~]# netstat -anpt | grep ngin
添加 Nginx 系统服务:这样一来, 就可以 systemctl 命令来启动、 停止、 重启、 重载 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
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[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.
【3】挂载测试网页
[root@localhost ~]# mount 192.168.100.24:/opt/33hou /usr/local/nginx/html/
[root@localhost ~]# vi /etc/fstab
192.168.100.24:/opt/33hou /usr/local/nginx/html nfs defaults,_netdev 0 0
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# systemctl enable nfs
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# showmount -e 192.168.100.24
Export list for 192.168.100.24:
/opt/33hou 192.168.100.0/24
/opt/22qian 192.168.100.0/24
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# systemctl restart nginx
请注意报错,如有!请往上翻
附带真机测试:
配置 Haproxy 调度器(192.168.100.21)
将需求软件包 haproxy-1.4.24.tar.gz 上传到调度器的 /opt目录下
【1】编译安装
[root@localhost ~]# yum -y install nfs-utils rpcbind
[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
【2】配置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
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
server inst2 192.168.100.23:80 check inter 2000 fall 3
[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]# systemctl start haproxy.service
[root@localhost haproxy-1.4.24]# systemctl enable haproxy.service
[root@localhost haproxy-1.4.24]# systemctl start rpcbind
[root@localhost haproxy-1.4.24]# systemctl enable rpcbind
[root@localhost haproxy-1.4.24]# systemctl start nfs
[root@localhost haproxy-1.4.24]# systemctl enable nfs
!!!下面是关于脚本的解释,只作参考,不输出!!!
global:为全局配置
defaults:为默认配置
listen:为应用组件配置
global配置参数
log 127.0.0.1 local0:配置日志记录,配置日志记录,local0为日志设备,默认存放到系统日志
log 127.0.0.1 local1 notice:notice为日志级别,通常有24个级别
maxconn 4096:最大连接数
uid 99:用户uid
gid 99:用户gid
defaults配置项配置默认参数,一般会被应用组件继承,如果在应用组件中没有特别声明,将安装默认配置参数设置
log global:定义日志为global配置中的日志定义
mode http:模式为http
option httplog:采用http日志格式记录日志
option dontlognull :保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
retries 3:检查节点服务器失败连续达到三次则认为节点不可用
maxconn 2000:最大连接数
contimeout 5000:连接超时时间
clitimeout 50000:客户端超时时间
srvtimeout 50000:服务器超时时间
listen配置项目一般为配置应用模块参数
listen appli4-backup 0.0.0.0:10004:定义一个appli4-backup的应用
option httpchk /index.html:检查服务器的index.html文件
option persist :强制将请求发送到已经down掉的服务器
balance roundrobin:负载均衡调度算法使用轮询算法
server inst1 192.168.100.22:80 check inter 2000 fall 3:定义在线节点
server inst2 192.168.100.23:80 check inter 2000 fall 3 backup:定义备份节点
报错
关于这个报错,原因是有可能你没装 nfs-utils、rpcbind 导致的;
真机测试负载均衡
网页输入 Haproxy调度器的 IP地址
到此结束,感谢观看