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服务停止,自己接替为主进行服务

此时访问

依旧正常

相关推荐
深频率1 小时前
补丁还在排队,AI先替服务器挡了一刀
服务器·人工智能·jenkins
3095566662 小时前
股票接口日期参数为什么不起作用:五组请求对照与逐字节比对排查笔记
服务器·数据库·笔记·python·量化·api接口·股票接口
沙老师2 小时前
Linux Purification Plan - More Modification
linux·运维·服务器
Decay__3 小时前
PostgreSQL 高可用与容灾架构 —— 流复制、Patroni、Keepalived+VIP、读写分离与两地三中心
运维·数据库·sql·postgresql
鱼很腾apoc4 小时前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
bufengwangluo13 小时前
动态代理IP是什么?从IP池到自动切换的工作机制解读
服务器·网络·ip
bkspiderx15 小时前
C++符号冲突与内部符号优先策略详解
linux·服务器·c++·符号冲突·内部符号
tz52515 小时前
Superpowers 游戏开发实战应用场景大纲
linux·运维·服务器
倔强的石头_16 小时前
Linux 电脑也能被 UU 远程连进去了:Linux PC 端内测上手,主控和被控我都试了
运维
Decay__20 小时前
PostgreSQL WAL 归档与时间点恢复 —— WAL 连续归档、recovery_target 时点恢复与 pg_rewind
运维·数据库·sql·postgresql