CentOS7上部署keepalived+Haproxy+httpd

环境:
10.0.0.41 haproxy+keepalived
10.0.0.42 haproxy+keepalived
10.0.0.43 web1(httpd)
10.0.0.44 web2(httpd)

在这里插入图片描述

1、在10.0.0.41和10.0.0.42安装部署:

yum  -y install haproxy keepalived

主keepalived上面的修改:

vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id LVS_R1
}

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 {
        10.0.0.100   #VIP地址
    }
}

virtual_server 10.0.0.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
!    persistence_timeout 50
    protocol TCP

    real_server 10.0.0.41 80 {    #本机IP
        weight 1
        notify_down /etc/keepalived/bb.sh #当80端口关闭执行脚本
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

添加健康检查脚本

echo '#!/bin/bash
systemctl stop keepalived'>/etc/keepalived/bb.sh
chmod +x /etc/keepalived/bb.sh

从keepalived服务器上的修改:

vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id LVS_R2
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33  #对应网卡
    virtual_router_id 51
    priority 99  #权重
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.0.0.100   #VIP地址
    }
}

virtual_server 10.0.0.100 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
!    persistence_timeout 50
    protocol TCP

    real_server 10.0.0.42 80 {    #本机IP
        weight 1
        notify_down /etc/keepalived/bb.sh #当80端口关闭执行脚本
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
}

添加健康检查脚本

echo '#!/bin/bash
systemctl stop keepalived'>/etc/keepalived/bb.sh
chmod +x /etc/keepalived/bb.sh

2、两台haproxy的修改一样,如下所示:

vi /etc/haproxy/haproxy.cfg

最下面添加

#最下面添加
listen web 0.0.0.0:80 #后端配置,http_back名称可自定义
 option httpchk GET /index.html #设置健康检查页面
 balance roundrobin #roundrobin 轮询方式
# 需要转发的ip及端口
 server httpd1 10.0.0.43:80 check inter 2000 rise 3 fall 3 weight 1
 server httpd2 10.0.0.44:80 check inter 2000 rise 3 fall 3 weight 1

注意参数解释:inter 2000 心跳检测时间;rise 3 三次连接成功,表示服务器正常;fall 3 三次连接失败,表示服务器异常; weight 1 权重设置
启动haproxy和keepalived

systemctl restart haproxy  #按顺序启动
systemctl restart keepalived

在这里插入图片描述
3、在10.0.0.43和10.0.0.44安装部署:
10.0.0.43

yum -y install httpd
echo '111111111'>/var/www/html/index.html
systemctl restart httpd

10.0.0.44

yum -y install httpd
echo '22222222'>/var/www/html/index.html
systemctl restart httpd

4、测试
在这里插入图片描述

systemctl stop haproxy

在这里插入图片描述
keepalived切换有延时
成功

发布了142 篇原创文章 · 获赞 249 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/a13568hki/article/details/103937174