Linux安装运行keepalived

Linuxkeepalived大约 2696 字

前置条件

安装gccopenssl-develpopt-devel

yum install -y gcc openssl-devel popt-devel

下载keepalived

wget https://www.keepalived.org/software/keepalived-2.0.18.tar.gz

解压

tar zxvf keepalived-2.0.18.tar.gz

编译安装

进入解压目录

cd keepalived-2.0.18.tar.gz

配置

./configure

编译安装

make && make install

运行前配置

以下所有配置文件都在下载后解压的keepalived-2.0.18文件夹下。

  • 拷贝keepalived执行文件
cp keepalived-2.0.18/keepalived/etc/init.d/keepalived /etc/init.d/
  • 创建keepalived文件夹
mkdir /etc/keepalived
  • 拷贝配置文件
cp keepalived-2.0.18/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  • 拷贝系统配置
cp keepalived-2.0.18/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
  • 拷贝keepalived命令至sbin下
cp /usr/local/sbin/keepalived /usr/sbin/

修改keepalived配置

vim /etc/keepalived/keepalived.conf

配置示例

MASTER

! 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_NGINX_MASTER
   vrrp_skip_check_adv_addr
   #vrrp_strict # 严格遵守vvrp协议,不注释访问不了VIP
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface eth0
    virtual_router_id 51 # 虚拟路由编号,主从要一致
    priority 100 # 优先级,数值越大,获取处理请求的优先级越高,step建议为50
    advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        #192.168.200.16
        #192.168.200.17
        #192.168.200.18
        192.168.153.50/24 # 定义虚拟ip(VIP),可多设,每行一个
    }
}

BACKUP

! 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_NGINX_BACKUP
   vrrp_skip_check_adv_addr
   #vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface eth0
    virtual_router_id 51 # 虚拟路由编号,主从要一致
    nopreempt # 不抢占VIP,MASTER恢复时VIP交给MASTER,只在BACKUP设置
    priority 50 # 优先级,数值越大,获取处理请求的优先级越高,step建议为50
    advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        #192.168.200.16
        #192.168.200.17
        #192.168.200.18
        192.168.153.50/24 # 定义虚拟ip(VIP),可多设,每行一个
    }
}

添加防火墙规则

编辑iptables文件

vim /etc/sysconfig/iptables

接受vrrp(必须配置在REJECT之上)

 -A INPUT -p vrrp -j ACCEPT

启动服务

service keepalived start

查看日志

tail -f /var/log/messages
阅读 552 · 发布于 2019-08-06

————        END        ————

扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看换一批