Centos编译安装keepalived-2.0.19,启动

众所周知,在我们实际部署应用服务的时候,高可用是一个必须满足的条件,这篇文档说下,比较常用的一种手段,通过keepalived来实现高可用。

keepalived实现了VRRP协议,VRRP(Virtual RouterRedundancy Protocol)协议是用于实现路由器冗余的协议,在 Keepalived服务正常工作时,主 Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活看,当主 Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的 IP资源及服务。而当主 Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。

本章中,采用的是keepalived-2.0.19 进行配置的;

  1. 首先下载安装包,可以在官网 https://www.keepalived.org/download.html 或者 keepalived安装包 下载即可。
  2. 安装
tar -zxvf keepalived-2.0.19.tar.gz
cd keepalived-2.0.19
./configure --prefix=/usr/local/keepalived
make
make install
  1. 配置
    安装好后,默认情况下,keepalived会读取/etc/keepalived/keepalived.conf的配置,我们将安装好后的/usr/local/keepalived/etc/keepalived/keepalived.conf连接到/etc/keepalived目录:
mkdir /etc/keepalived
cp keepalived-2.0.19/keepalived/etc/keepalived/keepalived.conf /etc/keepalived

将keepalvied加入系统服务:

#安装包目录下
#这是一个shell脚本
cp keepalived-2.0.19/keepalived/etc//init.d/keepalived /etc/init.d/keepalived
cp keepalived-2.0.19/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

这样就完成了keepalived 的安装。
如果报:

Starting keepalived: /bin/bash: keepalived: command not found

配置

这里用两台搭建,

  • 192.168.199.6
  • 192.168.199.8

192.168.199.6配置如下

! 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 LVS_DEVEL
}
vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 5
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    priority 90
    advert_int 1
    unicast_src_ip 192.168.199.6
    unicast_peer {
        192.168.199.8
    }
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
 virtual_ipaddress {
    192.168.199.100
 }
}

192.168.199.8配置如下

! 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 LVS_DEVEL
}
vrrp_script chk_nginx {
        script "/etc/keepalived/chk_nginx.sh"
        interval 5
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    unicast_src_ip 192.168.199.8
    unicast_peer {
        192.168.199.6
    }
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }


 virtual_ipaddress {
    192.168.199.100
 }
}

两台配置上:
virtual_router_id保持一致,priority不一致

这时候,通过192.168.199.100 虚IP就能够访问了。

启动keepalived:

/etc/init.d/keepalived start

开机自启动:

chkconfig keepalived on
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值