keepalived+haproxy搭建web群集
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,haproxy配置文件详解参考:haproxy环境如下:一、准备:1、下载haproxy 软件包,haproxy 提取码: 9it62、web 网站可以使用Apache、Nginx、搭建都可以,这里为
千家信息网最后更新 2026年11月03日keepalived+haproxy搭建web群集
haproxy配置文件详解参考:haproxy
环境如下:
一、准备:
1、下载haproxy 软件包,haproxy 提取码: 9it6
2、web 网站可以使用Apache、Nginx、搭建都可以,这里为了方便我就直接使用系统盘带的httpd服务了。
web网站的搭建可参考:基于 Linux 安装 web 服务及基本配置;基于 Centos 7 搭建Nginx;
3、配置防火墙放行流量
4、我这里使用的全部是centos 7系统,注意,该环境不是生产环境,若是在生产环境中,肯定还有后端存储来存放网页文件,web服务器读取存储服务器上的网页返回给客户端。这样才可保证网页内容的一致性。
二、开始搭建:
1、配置主服务器:
[root@haproxy1 /]# yum -y install pcre-devel bzip2-devel keepalived # 安装相关的依赖包和软件包[root@haproxy1 media]# tar zxf haproxy-1.5.19.tar.gz -C /usr/src/[root@haproxy1 media]# cd /usr/src/haproxy-1.5.19/[root@haproxy1 haproxy-1.5.19]# make TARHET=linux26 && make install # 编译安装,TARGET表示64位操作系统[root@haproxy1 haproxy-1.5.19]# mkdir /etc/haproxy # 创建用来存放主配文件的目录[root@haproxy1 haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ # 将编译包中的主配文件复制到配置文件目录,注意啊:这个配置文件是在编译包中,注意看路径[root@haproxy1 /]# vim /etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1global log /dev/log local0 info log /dev/log local0 notice #log loghost local0 info maxconn 4096 #chroot /usr/share/haproxy # 将此行注释掉 uid 99 gid 99 daemon #debug #quietdefaults log global mode http option httplog option dontlognull retries 3 redispatch maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000listen webcluster 0.0.0.0:80 # 将端口号修改为80,webcluster为群集名称,可修改 option httpchk GET /index.html balance roundrobin # 表示采用轮询算法 server web1 192.168.1.20:80 check inter 2000 fall 3 # 两个web节点 server web2 192.168.1.30:80 check inter 2000 fall 3[root@haproxy1 /]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy # 复制服务控制脚本[root@haproxy1 /]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy # 创建链接使命令使用更方便[root@haproxy1 /]# chmod +x /etc/init.d/haproxy # 添加执行权限[root@haproxy1 /]# chkconfig --add /etc/init.d/haproxy # 添加为系统服务[root@haproxy1 /]# /etc/init.d/haproxy start # 启动Starting haproxy (via systemctl): [ OK ][root@haproxy1 /]# 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& ~ [root@haproxy1 /]# systemctl restart rsyslog.service # 重启日志服务
配置 keepalived :
[root@haproxy1 /]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalivedglobal_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS1 # 主从调度器名称区分开}vrrp_instance VI_1 { state MASTER interface ens33 # 修改网卡名称 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 # 填写漂移地址 }}[root@haproxy1 /]# systemctl restart keepalived # 重启服务使配置生效2、配置从服务器:
[root@haproxy2 /]# yum -y install pcre-devel bzip2-devel keepalived[root@haproxy2 media]# tar zxf haproxy-1.5.19.tar.gz -C /usr/src/[root@haproxy2 media]# cd /usr/src/haproxy-1.5.19/[root@haproxy2 haproxy-1.5.19]# make TARGET=linux26 && make install[root@haproxy2 haproxy-1.5.19]# mkdir /etc/haproxy[root@haproxy2 haproxy-1.5.19]# scp root@192.168.1.10:/etc/haproxy/haproxy.cfg /etc/haproxy/ # 图个方便直接复制root@192.168.1.10's password: haproxy.cfg 100% 570 0.6KB/s 00:00 [root@haproxy2 /]# cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy # 复制服务控制脚本[root@haproxy2 /]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy[root@haproxy2 /]# chmod +x /etc/init.d/haproxy [root@haproxy2 /]# chkconfig --add /etc/init.d/haproxy [root@haproxy2 /]# /etc/init.d/haproxy start Starting haproxy (via systemctl): [ OK ][root@haproxy2 /]# scp root@192.168.1.10:/etc/rsyslog.d/haproxy.conf /etc/rsyslog.d/root@192.168.1.10's password: haproxy.conf 100% 226 0.2KB/s 00:00 [root@haproxy2 /]# systemctl restart rsyslog.service # 重启服务使配置生效[root@haproxy2 haproxy-1.5.19]# scp root@192.168.1.10:/etc/keepalived/keepalived.conf /etc/keepalived/root@192.168.1.10's password: keepalived.conf 100% 3511 3.4KB/s 00:00 [root@haproxy2 /]# vim /etc/keepalived/keepalived.conf # 修改主配置文件,修改如下几个部分....................... 省略部分 router_id LVS2 # 主从调度器区分ID}vrrp_instance VI_1 { state BACKUP # 状态修改为 BACKUP interface ens33 virtual_router_id 51 priority 90 # 优先级调低 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 # 漂移地址 }}[root@haproxy2 /]# systemctl restart keepalived两个web节点配置(两个配置相同):
[root@web2 /]# yum -y install httpd
[root@web2 /]# echo server2.com > /var/www/html/index.html # 创建测试网页
[root@web2 /]# systemctl start httpd
[root@web2 /]# systemctl enable httpd

当然,在实际生产环境中网页是一样的,这里我为了验证出效果,所以做了两个不同的测试文件。
配置
服务
文件
环境
网页
两个
服务器
系统
名称
生产
编译
主从
内容
地址
日志
是在
目录
网站
脚本
节点
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
咖啡豆
互联网金融科技
广州紫鲸互联网科技有限公司
科技金融是不是互联网金融
互联网科技保险
互联网电商科技
互联网与科技发展
奇酷互联网科技公司
深圳天众互联网科技公司
狮子座互联网科技培训
互联网科技股有哪些
互联网科技创新企业排名
互联网公司科技股
996高科技互联网公司
深圳艾果互联网通讯科技
互联网在线教育科技创业
互联网安防科技感
江苏互联网科技小贷
福仕德教育科技互联网
科技互联网专业务虚会材料
互联网科技
朗新科技与工业互联网
国外互联网科技网站
互联网科技大会案例
武汉健翔互联网络科技有限公司
阿大互联网科技有限公司
上海京穆互联网科技有限公司
山东子淼互联网科技有限公司
张家口科技产业互联网平台
山东亿川互联网科技有限责任公司