keepalived+haproxy搭建web群集
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,haproxy配置文件详解参考:haproxy环境如下:一、准备:1、下载haproxy 软件包,haproxy 提取码: 9it62、web 网站可以使用Apache、Nginx、搭建都可以,这里为
千家信息网最后更新 2025年02月02日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安全错误
数据库的锁怎样保障安全
海口派正软件开发公司
软考网络安全工程师考纲
数据库压缩算法代码
服务器怎么恢复outlook
学软件开发自学教材
软件开发辞退补偿
管理mysql数据库
sql数据库实例教程
网络安全监管领域
苹果手机软件开发者未受信用
上海软件开发税务筹划怎么做
河北it 软件开发编程
数据库管理语言主要有哪四种
软件开发需求要素
dnf服务器80m版本
怎么用命令重启达梦数据库服务
分布式服务器什么意思
共同服务器
服务器被用于犯法
有案底可以做软件开发
嘉定区网络技术咨询供应商家
如何给小孩普及网络安全
数据库db和mdb的区别
学软件开发自学教材
谷歌服务器什么时候能上市
兄弟连3无法连接视频服务器
思迅软件数据库有重复值
网络安全事件进行
重庆服务器机柜供应商
联想塔式服务器怎么用