CentOS7编译安装Keepalived2.0.19

 实验环境:centos7

节点1:10.15.192.21

节点2:10.15.192.22

vip地址:10.15.192.23

1、下载文件

cd /usr/local/src
wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz
# 官网地址 https://www.keepalived.org/download.html

2、解压文件

tar xzf keepalived-2.0.19.tar.gz
cd keepalived-2.0.19

3、安装依赖

yum -y install libnl libnl-devel openssl-devel

 4、初始化配置

cd keepalived-2.0.19/
./configure --prefix=/usr/local/keepalived

 5、编译安装

make && make install

6、配置

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /etc/init.d/

 节点1配置文件如下,注意两个节点mcast_src_ip 和 priority 值

cat keepalived.conf
! Configuration File for keepalived
 
global_defs {
    router_id hf-ayd-web-balance-01
    script_user root
    enable_script_security
}
 
 
vrrp_script chk_nginx {
       script "/etc/keepalived/nginx_check.sh"
       interval 2
       weight -20
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 151
    mcast_src_ip 10.15.192.21
    priority 100
    advert_int 1
    nopreempt
 
    
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
   
    virtual_ipaddress {
        10.15.192.23
    }
 
    track_script {
       chk_nginx
    }
}

 节点2配置文件如下

cat keepalived.conf
! Configuration File for keepalived
 
global_defs {
    router_id hf-ayd-web-balance-02
    script_user root
    enable_script_security
}
 
 
vrrp_script chk_nginx {
       script "/etc/keepalived/nginx_check.sh"
       interval 2
       weight -20
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 151 
    mcast_src_ip 10.15.192.22
    priority 90
    advert_int 1
    nopreempt
 
    
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
   
    virtual_ipaddress {
        10.15.192.23
    }
 
    track_script {
       chk_nginx
    }
}

  

7、管理服务

systemctl start keepalived #启动
systemctl restart keepalived #重启
systemctl stop keepalived #关闭
systemctl status keepalived # 状态

  

常见问题一:keepalived配置nopreempt参数无效解决方法

nopreempt    #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高  
master不能设置nopreempt
解决方案是:不设置master,全部设置成backup,这样大家都是backup,就都能添加nopreempt,即使原本成为master的LB坏掉重新修好之后也不会抢占master。
通常如果master服务死掉后backup会变成master,但是当master服务又好了的时候 master此时会抢占VIP,这样就会发生两次切换对业务繁忙的网站来说是不好的。所以我们要在配置文件加入 nopreempt 非抢占,但是这个参数只能用于state 为backup,故我们在用HA的时候最好master 和backup的state都设置成backup 让其通过priority来竞争

   

常见问题二:编译安装keepalived时提示以下错误

make
 cd . && /bin/sh /data/keepalived-2.0.19/missing automake-1.15 --foreign Makefile
/data/keepalived-2.0.19/missing: line 81: automake-1.15: command not found
WARNING: 'automake-1.15' is missing on your system.
         You should only need it if you modified 'Makefile.am' or
         'configure.ac' or m4 files included by 'configure.ac'.
         The 'automake' program is part of the GNU Automake package:
         <http://www.gnu.org/software/automake>
         It also requires GNU Autoconf, GNU m4 and Perl in order to run:
         <http://www.gnu.org/software/autoconf>
         <http://www.gnu.org/software/m4/>
         <http://www.perl.org/>
make: *** [Makefile.in] Error 127

 解决方法如下:

yum install automake -y
autoreconf -ivf
再次执行make

  

编译报错问题安装参考:https://blog.csdn.net/arackethis/article/details/42222905

部署参考:https://www.cnblogs.com/gray13/p/11690590.html

原文地址:https://www.cnblogs.com/caidingyu/p/12012833.html