当前位置:网站首页>案例:使用keepalived+Haproxy搭建Web群集
案例:使用keepalived+Haproxy搭建Web群集
2022-07-26 02:31:00 【大虾好吃吗】
目录
Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx。相比较而言,LVS性能最好,但是搭建相对复杂;Nginx的upstream模块支持群集功能,但是对群集节点健康检查功能不强,性能没有Haproxy好。Haproxy官方网站是http://www.haproxy.org/。
案例前置知识点
HTTP请求 请求方式 GET方式 POST方式 返回状态码 正常的状态码为2××、3×× 异常的状态码为4××、5××
负载均衡常用调度算法
RR(Round Robin):轮询调度 LC(Least Connections):最小连接数 SH(Source Hashing):基于来源访问调度
案例环境
实验目标:使用haproxy搭建web群集,实现负载均衡和高可用,使用keepalived+haproxy实现双机热备和负载均衡。
案例准备:根据下图配置IP地址,漂移地址为192.168.1.100,关闭防火墙、selinux、搭建yum仓库。

搭建Nginx1
[[email protected] ~]# yum -y install pcre-devel zlib-devel
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
[[email protected] ~]# eject
[[email protected] ~]# mount /dev/cdrom /media
mount: /dev/sr0 is write-protected, mounting read-only
[[email protected] ~]# tar zxf /media/nginx-1.12.0.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/nginx-1.12.0/
[[email protected] nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[[email protected] nginx-1.12.0]# make && make install安装后的默认信息如下。
默认安装目录:/usr/local/nginx
默认日志:/usr/local/nginx/logs
监听端口:80
默认web目录:/usr/local/nginx/html
[[email protected] nginx-1.12.0]# cd /usr/local/nginx/html/
[[email protected] html]# echo 111111 > test.html //建立测试页面
[[email protected] html]# /usr/local/nginx/sbin/nginx //启动Nginx
[[email protected] html]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6762/nginx: master 安装完成后,客户端访问http://192.168.1.20/test.html测试。
搭建Nginx2
编译安装的步骤与Nginx1相同,不同之处在于建立的测试网页。
[[email protected] html]# echo 22222 > test.html安装完成后,客户端访问http://192.168.1.30/test.html测试。
编译安装Haproxy
[[email protected] ~]# yum -y install pcre-devel bzip2-devel
[[email protected] ~]# eject
[[email protected] ~]# mount /dev/cdrom /media/
mount: /dev/sr0 is write-protected, mounting read-only
[[email protected] ~]# tar zxf /media/haproxy-1.5.19.tar.gz -C /usr/src
[[email protected] ~]# cd /usr/src/haproxy-1.5.19/
[[email protected] haproxy-1.5.19]# make TARGET=linux26 //64位系统
[[email protected] haproxy-1.5.19]# make installHaproxy服务器配置
建立配置文件
[[email protected] haproxy-1.5.19]# mkdir /etc/haproxy //创建配置文件目录
[[email protected] haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ //将haproxy.cfg文件复制到配置文件目录Haproxy配置项介绍
Haproxy配置文件通常分为三个部分,即global、defaults和listen。global为全局配置,defaults为默认配置,listen为应用组件配置。
global配置项说明如下:
global
log 127.0.0.1 local0 //配置日志记录,local0为日志设备,默认存放到系统日志
log 127.0.0.1 local1 notice //notice为日志级别,通常有24个级别
maxconn 4096 //最大连接数
uid 99 //用户uid
gid 99 //用户giddefaults配置项说明如下:
defaults
log global //定义日志为global配置中的日志定义
mode http //模式为http
option httplog //采用http日志格式记录日志
retries 3 //检查节点服务器失败次数,连续超过3次失败,则认为节点不可用
redispatch //当服务器负载很高时,自动结束当前队列处理较久的连接
maxconn 2000 //最大连接数
contimeout 5000 //连接超时时间
clitimeout 50000 //客户端超时时间
srvtimeout 50000 //服务器超时时间global配置项说明如下:
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.114.56:80 check inter 2000 fall 3 //定义在线节点
server inst2 192.168.114.56:81 check inter 2000 fall 3 backup //定义备份节点根据目前的群集设计,将haproxy.conf配置文件的内容修改如下。
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
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 appli4-backup 0.0.0.0:80 //复制listen配置项模块内容,端口号修改为80
option httpchk GET /index.html //http请求方式改为GET
balance roundrobin
server inst1 192.168.1.20:80 check inter 2000 fall 3 //两个节点服务器的配置
server inst2 192.168.1.30:80 check inter 2000 fall 3 //配置文件中后面的配置全部删除即可- 创建自启动脚本
[[email protected] ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
[[email protected] ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[[email protected] ~]# chmod +x /etc/init.d/haproxy
[[email protected] ~]# chkconfig --add /etc/init.d/haproxy
[[email protected] ~]# /etc/init.d/haproxy start
Starting haproxy (via systemctl): [ OK ]安装keepalived
安装ipvsadm和keepalived包。
[[email protected] ~]# mount /dev/cdrom /media
mount: /dev/sr0 is write-protected, mounting read-only
[[email protected] ~]# yum -y install ipvsadm keepalived配置keepalived.cof
做haproxy+keepalived群集,只需要配置修改热备实例,指定主备调度器,名称,VIP地址,优先级和漂移地址即可。
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
......//省略部分内容
router_id R1 //主调度器名称
}
vrrp_instance VI_1 {
state MASTER //热备状态
interface ens33 //VIP地址物理接口
virtual_router_id 51
priority 100 //优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100 //漂移地址
}
}
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# systemctl enable keepalivedHaproxy+keepalived备份调度器安装配置与之大致相同,唯一需要注意的是keepalived配置时,分清主、备调度器配置(指定主备调度器,名称,VIP地址,优先级和漂移地址)。按照上面配置继续配置haproxy2+keepalived2。
案例测试
客户端打开浏览器访问http://192.168.1.100/test.html,刷新两次浏览器查看网页内容测试负载均衡(此时应能看到11111和22222两个网页)。断开web1,在次访问刷新两次测试高可用(此时应只能看到22222)。


断掉keepalived主调度器网络,继续访问http://192.168.1.100/test.html,访问成功即keepalived负载均衡成功(此时应能看到11111和22222两个网页)。

Haproxy日志
修改原有的配置文件中关于日志配置的选项,在global项目中配置。修改配置后保存并重启Haproxy。
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
log /dev/log local0 info
log /dev/log local0 notice
[[email protected] ~]# /etc/init.d/haproxy restart //重启
Restarting haproxy (via systemctl): [ OK ]为了便于管理,将Haproxy相关的配置独立定义到haproxy.conf并放到/etc/rsyslog.d/下,rsyslog启动时会自动加载此目录下的所有配置文件。
[[email protected] ~]# vim /etc/rsyslog.d/haproxy.conf
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~
[[email protected] ~]# systemctl restart rsyslog在客户端访问后,可以使用下面命令及时查看Haproxy的访问请求日志信息。
[[email protected] ~]# tail -f /var/log/haproxy/haproxy-info.log
Jul 23 23:08:35 localhost haproxy[43674]: 192.168.1.200:62207 [23/Jul/2022:23:08:35.157] appli4-backup appli4-backup/inst2 0/0/1/1/2 200 240 - - ---- 1/1/0/1/0 0/0 "GET /test.html HTTP/1.1"
Jul 23 23:08:35 localhost haproxy[43674]: 192.168.1.200:62207 [23/Jul/2022:23:08:35.159] appli4-backup appli4-backup/inst1 198/0/5/1/204 200 241 - - ---- 2/2/0/1/0 0/0 "GET /test.html HTTP/1.1"Haproxy的参数优化
| 参数 | 参数说明 | 优化建议 |
|---|---|---|
| maxconn | 最大连接数 | 此参数根据应用的实际使用情况进行调整,推荐使用10240,同时"defaults"中的最大连接数的值不能超过"global"段中的定义 |
| daemon | 守护进程模式 | Haproxy可以使用非守护进程模式启动,生产环境建议使用守护进程模式启动 |
| nbproc | 负载均衡的并发进程数 | 建议与当前服务器CPU核数相等或为其2倍 |
| retries | 重试次数 | 参数主要用于对群集节点的检查,如果节点多,并且发量大,设置为2次或3次;在服务器节点不多的情况下,可以设置5次或6次 |
| option http-server-close | 主动关闭http请求选项 | 建议在生产环境中使用此项选项,避免由于timeout时间设置过长导致http连接堆积 |
| timeout http-keep-alive | 长时间超时时间 | 此选项设置长连接超时时间,具体参考应用自身特点设置,可以设置为10s |
| timeout http-request | HTTP请求超时时间 | 建议将此时间设置为5~10s,增加http连接释放速度 |
| timeout client | 客户端超时时间 | 如果访问量过大,节点响应慢,可以将此时间设置短一些,建议设置为1min左右就可以了 |
边栏推荐
- [steering wheel] how to transfer the start command and idea: VM parameters, command line parameters, system parameters, environment variable parameters, main method parameters
- 获取时分秒
- [C] Explain language file operation in detail
- 【方向盘】使用IDEA的60+个快捷键分享给你,权为了提效(重构篇)
- 墨天轮高分技术文档分享——数据库安全篇(共48个)
- Pipnet: face key point detection for natural scenes "pixel in pixel net: directions efficient facial landmark detection in the wild"
- 1.软件测试-----软件测试的基本概念
- Ggplot2 learning summary
- prometheus+process-exporter+grafana 监控进程的资源使用
- [pure theory] Yolo v4: optimal speed and accuracy of object detection
猜你喜欢

Stack Title: the longest absolute path of a file

DFS Niuke maze problem
![[C]详解语言文件操作](/img/12/4affa1d3fb3e4ee126e1c1e3872d9b.png)
[C]详解语言文件操作
![[reading notes] user portrait methodology and engineering solutions](/img/5e/916853accf3a5af237f7f114855437.jpg)
[reading notes] user portrait methodology and engineering solutions

【方向盘】启动命令和IDEA如何传递:VM参数、命令行参数、系统参数、环境变量参数、main方法参数

(PC+WAP)织梦模板蔬菜水果类网站
![[xxl-job] xxl-job learning](/img/2c/d3872983e4228a3ef52a9d1bef836e.png)
[xxl-job] xxl-job learning

prometheus+blackbox-exporter+grafana 监控服务器端口及url地址

National standard gb28181 protocol video platform easygbs message pop-up mode optimization

Wechat applet decryption and unpacking to obtain source code tutorial
随机推荐
获取时分秒
How to speed up matrix multiplication
Illustration of the insertion process of b+ tree
Overview of pytoch's API
Information System Project Manager - Chapter 10 communication management and stakeholder management examination questions over the years
Yum install MySQL FAQ
scipy.sparse.vstack
Games101 review: rasterization
Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
020-024 polymorphism review
[xxl-job] xxl-job learning
Prove that perfect numbers are even
Uni app cross domain configuration
图解B+树的插入过程
[C] Explain language file operation in detail
LeetCode_动态规划_中等_264.丑数 II
ES6 advanced - inherit parent class attributes with constructors
The third question of leetcode 302 weekly Games -- query the number with the smallest k after cutting the number
prometheus+redis-exporter+grafana 监控redis服务
HLS Experiment 1 -- multiplier