keepalived+haproxy搭建web群集

haproxy配置文件详解参考:haproxyhtml

环境以下:linux

2.png

1、准备:
web

一、下载haproxy 软件包,haproxy   提取码: 9it6 redis

二、web 网站可使用Apache、Nginx、搭建均可以,这里为了方便我就直接使用系统盘带的httpd服务了。算法

web网站的搭建可参考:基于 Linux 安装 web 服务及基本配置基于 Centos 7 搭建Nginxvim

三、配置防火墙放行流量后端

四、我这里使用的所有是centos 7系统,注意,该环境不是生产环境,如果在生产环境中,确定还有后端存储来存放网页文件,web服务器读取存储服务器上的网页返回给客户端。这样才可保证网页内容的一致性。centos

2、开始搭建:
bash

一、配置主服务器:服务器

[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 $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 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             # 重启服务使配置生效

二、配置从服务器:

[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

截图03.png

截图04.png

固然,在实际生产环境中网页是同样的,这里我为了验证出效果,因此作了两个不一样的测试文件。