Keepalived:编译安装与配置简明手册

环境

依赖

安装开发工具集(涵盖了gcc,c++,make等编译工具),以及Keepalived依赖包。

# Centos 6.x
yum groupinstall "Development Tools" -y
yum install libnl-devel libnfnetlink-devel -y

# Centos 7.x
yum group install "Development Tools" -y
yum install libnl-devel libnfnetlink-devel -y

编译安装

# 下载解压
wget http://www.keepalived.org/software/keepalived-1.4.1.tar.gz
tar xzf keepalived-1.4.1.tar.gz
cd keepalived-1.4.1

# 预编译配置、编译、安装
./configure --prefix=/opt/soft/keepalived
make
make install

# 软链命令到PATH中,或者自定义PATH配置也可以。
ln -s  /opt/soft/keepalived/*bin/* /usr/sbin/

# 从源码包中/源码包中/源码包中 cp 启动脚本
# Centos 6.x
cp keepalived/etc/init.d/keepalived /etc/init.d/

# 修改启动文件中的配置,或者cp sysconfig/keepalived 到系统环境中
# . /etc/sysconfig/keepalived    //找到此处,修改为下面的内容
# . /opt/soft/keepalived/etc/sysconfig/keepalived
cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/
service keepalived status

# Centos 7.x
cp keepalived/keepalived.service /usr/lib/systemd/system
systemctl status keepalived

简单用于故障转移配置

这里我是基于阿里云环境配置的,云端用多播会有问题,所以这里配置的单播模式。
IP 地址分配情况如下:

  • MASTER:10.1.1.1
  • BACKUP:10.1.1.2
  • Virtual ip:10.1.1.100

MASTER 配置

! Configuration File for keepalived

global_defs {
   notification_email {
     opsarno@qq.com
   }
   notification_email_from 1-1-1@arno.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id 1-1-1
}

vrrp_script chk_sshd {
    script "killall -0 sshd"
    interval 2
    weight -4
    fall 2
    rise 2
}

vrrp_instance my_v1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    unicast_src_ip 10.1.1.1
    unicast_peer {
        10.1.1.2
    }
    authentication {
        auth_type PASS
        auth_pass my_v1_pass
    }
    virtual_ipaddress {
        10.1.1.100
    }
    track_script {
       chk_sshd
    }
}

BACKUP 配置

! Configuration File for keepalived

global_defs {
   notification_email {
     opsarno@qq.com
   }
   notification_email_from 1-1-2@arno.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id 1-1-2
}

vrrp_script chk_sshd {
    script "killall -0 sshd"
    interval 2
    weight -4
    fall 2
    rise 2
}

vrrp_instance my_v1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    unicast_src_ip 10.1.1.2
    unicast_peer {
        10.1.1.1
    }
    authentication {
        auth_type PASS
        auth_pass my_v1_pass
    }
    virtual_ipaddress {
        10.1.1.100
    }
    track_script {
       chk_sshd
    }
}
阅读 4.1k

推荐阅读

运维开发交流专栏

3 人关注
3 篇文章
专栏主页