keepalived+haproxy搭建web群集

haproxy配置文件详解参考:haproxy

环境如下:

2.png

一、准备:

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.1

global
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
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen 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 KaTeX parse error: Expected 'EOF', got '&' at position 72: …proxy-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 keepalived
global_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值