keepalived的安装和使用

IP配置#

管理IP地址 角色 备注 网卡
192.168.1.114 主调度器(Director) 对外提供VIP服务的地址为192.168.1.88 eth1
192.168.1.205 备用调度器  

eth0

192.168.1.115 RS1    
192.168.1.116 RS2    

 

 

 

 

 #

名词解释:#

主节点:  master

备节点:    backup 

虚拟路由器冗余协议: VRRP(Virtual Route Redundancy Protocol) ,它的出现是为了解决静态路由的单点故障

 

2.安装#

在主调度器和备用调度器进行同样的安装,不同的是配置文件

安装openssl

yum -y install openssl-devel
tar -xvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
make
make install

 

配置规范启动

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
chkconfig keepalived on

 

配置配置文件

新建一个配置文件,默认keepalived启动会去/etc/keepalived目录下寻找配置文件

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

单实例配置#

编辑主调度器的配置文件

复制代码
! Configuration File for keepalived

global_defs {
   notification_email {
     zy5724@163.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_114
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 114
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.88
    }
}
复制代码

从配置文件

复制代码
! Configuration File for keepalived

global_defs {
   notification_email {
     zy5724@163.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_205
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 114
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.88
    }
}
复制代码

注意上面有4个不一样的配置的地方,一般是三个,主服务器的网卡我配置成了eth1。

测试:

主调度器:

当停掉主调度器,再看从调度器,马上会出现VIP

通过ping 192.168.1.88可以看见当主调度器宕机,从调度器立马会接管主调度器,实现了高可用。

 

双主多实例配置#

主调度器

复制代码
! Configuration File for keepalived

global_defs {
   notification_email {
     zy5724@163.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_114
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 114
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.88
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth1
    virtual_router_id 205
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.99
    }
}
主调度器114
复制代码

从调度器

复制代码
! Configuration File for keepalived

global_defs {
   notification_email {
     zy5724@163.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_205
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 114
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.88
    }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth0
    virtual_router_id 205
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.99
    }
}
View Code
复制代码

测试:

主调度器:

从调度器:

 

配置日志路径

   默认的日志是在/var/log/messages中的,我们现在将它配置在“/var/log/keepalived.log”下,注意要关闭selinux

vi /etc/sysconfig/keepalived

#KEEPALIVED_OPTIONS="-D"
KEEPALIVED_OPTIONS="-D -d -S 0"

vi /etc/rsyslog.conf 

添加:

#keepalived
local0.*                                               /var/log/keepalived.log

重启日志服务

/etc/init.d/rsyslog restart

重启keepalive的服务就可以看见日志在指定路径下了。

 

posted @   头痛不头痛  阅读(569)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
点击右上角即可分享
微信分享提示
主题色彩