LVS+keepalived集群

host1 主调度器,host4辅调度器,host2,host3是服务器

基本配置:服务器host2和host3,不需要vip,start keepalived时会自动生成vip,[root@host1 ~]# ipvsadm -A -t 172.25.254.10:80 -s rr
[root@host1 ~]# ipvsadm -a -t 172.25.254.10:80 -r 172.25.254.2:80 -g
[root@host1 ~]# ipvsadm -a -t 172.25.254.10:80 -r 172.25.254.3:80 -g
[root@host1 ~]# /etc/init.d/ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ]
调度器vip不用自己手动添加,
这里写图片描述

host2,host3服务器需要

arptables    -A IN -d 172.25.254.10 -j DROP 
arptables    -A OUT -s 172.25.254.10 -j mangle --mangle-ip-s 172.25.254.2  #host3最后ip为172.25.254.3

1:安装源码包编译需要的工具。并下载keepalived的源码包。 (两个节点都需要做)

gcc 和 openssl-devel
这里写图片描述

2:解压并且编译keepalived的源码包

 tar zxf keepalived-1.4.1.tar.gz  ##解压
 ./configure --prefix=/usr/local/keepalived --with-init=SYSV ##进入解压后的目录中编译
make
make install

这里写图片描述

3:连接或者复制配置文件到正常目录

cp  /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ##往掠配置文件
cp -r /usr/local/keepalived/etc/keepalived/ /etc  ##配置主目录
cd /usr/local/keepalived/etc/rc.d/init.d                   
chmod +x keepalived
cp -r /usr/local/keepalived/sbin/keepalived  /usr/sbin/  ##keepalived命令
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/   ##服务启动命令

MASTER主机

安装yum install openssh-clients.x86_64
scp  -r /usr/local/keepalived/ 备机:/usr/local/

BACKUP辅机

yum -y install ipvsadm*
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
/etc/init.d/keepalived start
/etc/init.d/keepalived stop

4主调度器文件配置:

[root@host1 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
   root@localhost     ###接受报警的邮箱
   }
   notification_email_from keepalived@localhost  ##邮件发送者
   smtp_server 127.0.0.1    ##
   smtp_connect_timeout 30  ##连接smtp的超时时间
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER   #备机需要改为BACKUP
    interface eth1  #高可用的检测网络接口
    virtual_router_id 51  #主和备机的id必须一样,且在0~255
    priority 100   #主机的优先级,备机应该此值小点
    advert_int 1  #主备之间的通告时间间隔秒数
    authentication {
        auth_type PASS #设置验证类型
        auth_pass 1111  #设置验证密码
    }
    virtual_ipaddress {
        172.25.254.10  #VIP
    }
}

virtual_server 172.25.254.10 80 {
    delay_loop 6   #每隔4秒查询realserver的状态
    lb_algo rr   #rr论叫算法
    lb_kind DR   #LVS的DR模式
    #persistence_timeout 50  #保持会话使用的时常ftp服务类型
    protocol TCP  ##tcp协议

    real_server 172.25.254.2 80 {   ##RS
        weight 1  #权重
        TCP_CHECK {  #realserver的状态检测设置部分
            connect_timeout 3  #3秒无响应超时
            retry 3    #重试次数
            delay_before_retry 3   #重试间隔
       }
   }
    real_server 172.25.254.3 80 {
        weight 1  
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
       } 
   }   
}

###5辅调度器文件配置

[root@host4 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.25.254.10
    }
}

virtual_server 172.25.254.10 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    #persistence_timeout 50
    protocol TCP

    real_server 172.25.254.2 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
       }
   }
    real_server 172.25.254.3 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            delay_before_retry 3
       } 
   }  
}

6完成测试:

这里写图片描述

@高可用的解决方案:

主调度器在使用中,辅调度器开启,
这里写图片描述
这里写图片描述

当主调度器host1 down掉,备用调度器会接替工作

这里写图片描述
这里写图片描述

@健康检查机制,host2关闭http服务

这里写图片描述
日志和邮件查看相关信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值