keepalived安装教程

时间:2025-03-31 09:21:22

安装

1、yum方式

安装依赖包

安装依赖包
[root@localhost ~]# yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
[root@localhost ~]# yum install -y keepalived

​ 初始化及启动

[root@localhost ~]# systemctl start keepalived   //启动keepalived
[root@localhost ~]# systemctl enable keepalived  //加入开机启动keepalived
[root@localhost ~]# systemctl restart keepalived  //重新启动keepalived
[root@localhost ~]# systemctl status keepalived   //查看keepalived状态

2、源码包安装

1、配置
[root@master src]# pwd
/usr/local/src
[root@master src]# wget /software/keepalived-2.2.
[root@master src]# tar xvf keepalived-2.2.
[root@master src]# cd keepalived-2.2.
[root@master keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived
[root@master keepalived-2.2.7]# make && make install
1、去 keepalived 源码目录下拷贝启动脚本到/etc//下
[root@bogon keepalived-2.2.7]# pwd
/usr/local/src/keepalived-2.2.7
[root@bogon keepalived-2.2.7]# cp /opt/keepalived-2.2.7/keepalived/etc//keepalived /etc//keepalived
[root@bogon keepalived-2.2.7]# ll /etc// | grep keepalived
-rwxr-xr-x  1 root root  1308 82 14:15 keepalived
2、修改/etc//keepalived启动脚本

/etc//keepalived

默认配置:

pidfile: /var/run/
config: /etc/keepalived/

mv  /usr/local/keepalived/etc/keepalived/  /etc/keepalived/
2、修改其权限并开机启动
  1. 修改权限:chmod 755 /etc//keepalived
  2. 加为系统服务:chkconfig --add keepalived
  3. 开机启动:chkconfig keepalived on
  4. 查看开机启动的服务:chkconfig --list
3、备注:keepalived服务控制

systemctl enable #设置开机自动启动
systemctl disable #取消开机自动启动
systemctl start #启动服务
systemctl restart #重启服务
systemctl stop #停止服务
systemctl status #查看服务状态

4、附:另一种添加为系统服务的方式
1、复制相应文件到相应目录下
# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[root@bogon /]# cp /usr/local/keepalived/etc/sysconfig/keepalived  /etc/sysconfig/keepalived 

# 将keepalived主程序加入到环境变量(安装目录下)
[root@bogon /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived

# keepalived启动脚本(源码目录下),放到/etc//目录下就可以使用service命令便捷调用
[root@bogon /]# cp /usr/local/src/keepalived-1.3.5/keepalived/etc//keepalived  /etc//keepalived

# 将配置文件放到默认路径下
[root@bogon /]# mkdir /etc/keepalived
[root@bogon /]# cp /usr/local/keepalived/etc/keepalived/ /etc/keepalived/
2、设置开机启动
# 设置开机启动
[root@bogon /]# chkconfig keepalived on

[root@bogon /]# service keepalived start|stop|restart
5、附、安装过程中的问题
1、修改启动脚本 /etc//keepalived 后,未生效

对于此问题一般的解决方法是执行如下命令,重新加载脚本。

[root@bogon sbin]# systemctl daemon-reload

以上方法大多时候会生效,但也有例外。此时,可以试试如下方法:

删除路径 /usr/lib/systemd/system/ 下相应的启动脚本

[root@bogon /]# chkconfig --del keepalived
[root@bogon /]# cd /usr/lib/systemd/system/
[root@bogon system]# ls | grep keepalived

[root@bogon system]# rm 
rm:是否删除普通文件 ""?y

然后重新注册为系统服务:

[root@bogon system]# chkconfig --add keepalived

2、a configured resource limit was exceeded.

执行 systemctl start 后出现如下错误:

[root@bogon sbin]# systemctl start 
Job for  failed because a configured resource limit was exceeded. See "systemctl status " and "journalctl -xe" for details.

这个问题在网上找了半天也没找可以解决的方法,但是把 问题1 解决之后 问题2 就没再出现

配置

主节点

vim /etc/keepalived/

! Configuration File for keepalived
#全局定义模块
#配置邮件信息
global_defs {
   smtp_connect_timeout 30 #超时时间
   router_id LVS_DEVEL_01  #路由id  #此处注意router_id为负载均衡标识,在局域网内应该是唯一的。
}


#VRRP实例定义块
vrrp_instance VI_1 {
    state MASTER  #状态只有MASTER和BACKUP两种,并且要大写,MASTER为工作状态,BACKUP是备用状
    interface enp0s3 #网卡名称
    virtual_router_id 51 #虚拟路由标识,同一个vrrp_instance的MASTER和BACKUP的
    priority 100 #优先级,同一个vrrp_instance的MASTER优先级必须比BACKUP高。
    advert_int 1 #MASTER 与BACKUP 负载均衡器之间同步检查的时间间隔,单位为秒。
    authentication {
        auth_type PASS #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,通常使用的类型为PASS,\
        auth_pass 1111 #据说AH 使用时有问题。验证密码为明文,同一vrrp 实例MASTER 与BACKUP 使用相同的密码才能正常通信。
    }
    virtual_ipaddress {  #虚拟ip地址,可以有多个地址,每个地址占一行,不需要子网掩码,同时这个ip 必须与我们在lvs 客户端设定的vip 相一致!
   		@ip  //虚拟ip
    }
}

备份节点

vim /etc/keepalived/

! Configuration File for keepalived
#全局定义模块
#配置邮件信息
global_defs {
   smtp_connect_timeout 30 #超时时间
   router_id LVS_DEVEL_02  #路由id  #此处注意router_id为负载均衡标识,在局域网内应该是唯一的。
}


#VRRP实例定义块
vrrp_instance VI_1 {
    state BACKUP  #状态只有MASTER和BACKUP两种,并且要大写,MASTER为工作状态,BACKUP是备用状
    interface enp0s3 #网卡名称
    virtual_router_id 51 #虚拟路由标识,同一个vrrp_instance的MASTER和BACKUP的
    priority 99 #优先级,同一个vrrp_instance的MASTER优先级必须比BACKUP高。
    advert_int 1 #MASTER 与BACKUP 负载均衡器之间同步检查的时间间隔,单位为秒。
    authentication {
        auth_type PASS #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,通常使用的类型为PASS,\
        auth_pass 1111 #据说AH 使用时有问题。验证密码为明文,同一vrrp 实例MASTER 与BACKUP 使用相同的密码才能正常通信。
    }
    virtual_ipaddress {  #虚拟ip地址,可以有多个地址,每个地址占一行,不需要子网掩码,同时这个ip 必须与我们在lvs 客户端设定的vip 相一致!
      	@ip //虚拟ip
    }
}

systemctl {start|stop|reload|restart|condrestart|status} keepalived

systemctl start keepalived

ip a 可以看到虚拟 ip 在主服务器上