haproxy负载均衡

haproxy负载均衡

一、配置准备

全局配置

关闭防火墙、上下文

复制代码
[root@everyone ~]# systemctl stop firewalld
[root@everyone ~]# setenforce 0

haproxy端

复制代码
[root@hap ~]# yum install -y haproxy

web端

复制代码
[root@web1/web2 ~]# yum install -y nginx
[root@web1 ~]# echo web1 > /usr/share/nginx/html/index.html
[root@web2 ~]# echo web2 > /usr/share/nginx/html/index.html
[root@web1/web2 ~]# systemctl start nginx

二、haproxy 负载均衡(基于nginx)

haproxy端

修改配置文件

复制代码
[root@hap ~]# cd /etc/haproxy/
[root@hap haproxy]# vim haproxy.cfg 

启动服务

复制代码
[root@hap haproxy]# systemctl   start haproxy.service

检验状态

成功

三、KHN Triad

使用 keepalive 监控 haproxy 实现 nginx 的 负载均衡

准备阶段

2台 安装 keepalive 与 haproxy 服务 并改名为 khn-1 与 khn-2

2台 安装 nginx 并修改 默认访问文件

配置文件

KHN端

knh-1端
keepalived配置文件
复制代码
[root@khn-1 ~]# vim /etc/keepalived/keepalived.conf
脚本编写
复制代码
[root@khn-1 ~]# vim  /etc/keepalived/check_khn_nginx.sh
[root@khn-1 ~]# chmod +x /etc/keepalived/check_khn_nginx.sh
复制代码
[root@khn-1 ~]# vim  /etc/keepalived/add_dnat.sh
[root@khn-1 ~]# chmod +x /etc/keepalived/add_dnat.sh
复制代码
[root@khn-1 ~]# vim /etc/keepalived/remove_dnat.sh
[root@khn-1 ~]# chmod +x  /etc/keepalived/remove_dnat.sh
kaproxy配置文件
复制代码
[root@khn-1 ~]# vim /etc/haproxy/haproxy.cfg 
khn-2端
keepalived配置文件
复制代码
[root@khn-2 ~]# vim /etc/keepalived/keepalived.conf
脚本编写
复制代码
[root@khn-2 ~]# vim  /etc/keepalived/check_khn_nginx.sh
[root@khn-2 ~]# chmod +x /etc/keepalived/check_khn_nginx.sh
复制代码
[root@khn-2 ~]# vim  /etc/keepalived/add_dnat.sh
[root@khn-2 ~]# chmod +x /etc/keepalived/add_dnat.sh
复制代码
[root@khn-2 ~]# vim /etc/keepalived/remove_dnat.sh
[root@khn-2 ~]# chmod +x  /etc/keepalived/remove_dnat.sh
haproxy文件
复制代码
[root@khn-2 ~]# vim /etc/haproxy/haproxy.cfg 

启动服务

复制代码
[root@khn-1/khn-2 ~]# systemctl start haproxy
[root@khn-1/khn-2 ~]# systemctl start keepalived
查看

故障模拟

停止 khn-1 的 haproxy 服务

khn-1端

keepalived 检测 haproxy 服务停止 所以自己也停止服务

khn-2端

keepalived 检测到 khn-1 的keeplived服务停止,自己接替为主进行服务

此时访问

依旧正常

相关推荐
day day day ...2 分钟前
MyBatis条件误写引发的查询条件污染分析与防范
java·服务器·tomcat
TechMasterPlus23 分钟前
Linux U-Boot 与内核启动流程深度解析:从上电到 Shell 的完整之旅
linux·运维·服务器
大白菜和MySQL26 分钟前
Linux下dhcp服务搭建
linux·运维·服务器
SPC的存折38 分钟前
1、MySQL故障排查与运维案例
linux·运维·服务器·数据库·mysql
Run_Teenage39 分钟前
Linux:认识信号,理解信号的产生和处理
linux·运维·算法
Deitymoon1 小时前
linux——TCP服务器获取客户端IP地址
linux·服务器·tcp/ip
CDN3601 小时前
高防服务器磁盘 / CPU 爆满?攻击引流与资源扩容实战
运维·服务器·网络协议
小贾要学习1 小时前
【Linux】应用层自定义协议与序列化
linux·服务器·c++·json
white-persist1 小时前
【vulhub spring CVE-2018-1270】CVE-2018-1270 Spring Messaging 远程命令执行漏洞 完整复现详细分析解释
java·服务器·网络·数据库·后端·python·spring
freewlt1 小时前
OpenClaw 工作流自动化实战:打造你的智能定时任务中心
运维·servlet·自动化