keepalived基础
lvs官方提供了几种lvs高可用构建方案,如下:常用keepalived
http://www.linuxvirtualserver.org/HighAvailability.html
The following examples of building highly available LVS systems are only for reference.
- Using Piranha to build highly available LVS systems
- Using Keepalived to build highly available LVS systems
- Using UltraMonkey to build highly available LVS systems
- Using heartbeat+mon+coda to build highly available LVS systems
- Using heartbeat+ldirectord to build highly available LVS systems
简介
lvs本身不探测后端rs的健康性,由ldirectd辅助完成
lvs本身不保证自己的高可用,结合keepalived的vvrp虚拟路由冗余协议实现高可用
keepalived作用:
- 保证lvs的高可用
- 探测后端节点的健康性,并实现节点动态增删
- 通过配置keepalived,即可实现ipvs规则的配置管理
- keepalived支持多种方法探测后端节点
- 四层端口
- 七层http/https头部
- 其他http报文内容
- 支持调用外部自定义脚本进行探测
vrrp协议
vvrp:virtual router redundancy protocol 虚拟路由冗余协议,本意用来保证路由的高可用,实现逻辑:多个路由节点组成一个虚拟路由节点,某一刻,只有一个主节点起作用,但主节点和其他从节点会不断进行通信,告诉它们朕一日不死,你们都是太子,当主节点挂掉时,其他从节点会争抢出新的主节点,周而复始,从而这个虚拟路由节点就有了高可用的属性!
keepalived程序架构
- checkers:检测后端rs的健康状态,支持多种方法
- tcp
- http
- https
- misc自定义脚本
- ipvs wrapper:该组件用于管理ipvs规则
- system all:提供自定义脚本调用的功能
- vvrp stack:管理vvrp协议的实现
- netlink reflector:管理监控vvrp的 vip地址
- watch dog:监控程序
编译安装
yum可以直接安装;
下为编译安装过程:
1、下载源码包,解压,安装依赖包,编译工具
[root@lvs ~]# yum install -y openssl-devel gcc gcc++
[root@lvs ~]# tar -xf keepalived-1.2.19.tar.gz
[root@lvs ~]# cd keepalived-1.2.19
[root@lvs keepalived-1.2.19]# pwd
/root/keepalived-1.2.19
2、三步走
[root@lvs keepalived-1.2.19]# ./configure --prefix=/usr/local/keepalived-1.2.19
[root@lvs keepalived-1.2.19]# make && make install
[root@lvs keepalived-1.2.19]# ln -sv /usr/local/keepalived-1.2.19/ /usr/local/keepalived
‘/usr/local/keepalived’ -> ‘/usr/local/keepalived-1.2.19/’
# 做软链接方便,版本切换;
3、安装后配置:配置path变量和manpath路径
[root@lvs keepalived-1.2.19]# echo "export PATH=/usr/local/keepalived/bin:$PATH" >> /etc/profile.d/keepalived.sh
[root@lvs keepalived-1.2.19]# source !$
source /etc/profile.d/keepalived.sh
[root@lvs keepalived-1.2.19]# echo "MANPATH /usr/local/keepalived/share/man" >> /etc/man.config
[root@lvs keepalived-1.2.19]# man keepalived.conf
4、安装后目录结构
[root@lvs keepalived]# pwd
/usr/local/keepalived
[root@lvs keepalived]# ll ./*
./bin:
total 44
-rwxr-xr-x 1 root root 43752 Sep 10 17:18 genhash
# bin下有个genhash,健康检测后端rs页面时,用status_code或genhash命令生成的hash值判断检查结果
./etc:
total 0
drwxr-xr-x 3 root root 44 Sep 10 17:18 keepalived
# 主配置,以及参考模版
drwxr-xr-x 3 root root 20 Sep 10 17:18 rc.d
# 服务启动脚本
drwxr-xr-x 2 root root 24 Sep 10 17:18 sysconfig
# 服务脚本启动时读取的参数文件
./sbin:
total 196
-rwx------ 1 root root 200248 Sep 10 17:18 keepalived
# 主程序
./share:
total 0
drwxr-xr-x 5 root root 42 Sep 10 17:18 man
# man手册,有 1 5 8 三个章节,有keepalived.conf和genhash的配置手册说明
5、准备服务脚本、服务脚本参数文件、配置文件,并做适当修改
[root@lvs keepalived]# cp etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# 将自带的服务脚本复制到init.d目录下
# 修改服务脚本
# 将daemon后的keepalived改为编译安装的主程序文件路径,如下:
[root@lvs keepalived]# vim /etc/rc.d/init.d/keepalived
......
start() {
echo -n $"Starting $prog: "
daemon /usr/local/keepalived/sbin/keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
# 添加开启启动项,并设置为开启启动
[root@lvs keepalived]# chkconfig --add keepalived
[root@lvs keepalived]# chkconfig keepalived on
# 复制自带的服务脚本参数文件,到/etc/sysconfig目录下
[root@lvs keepalived]# cp etc/sysconfig/keepalived /etc/sysconfig/
# 复制自带的配置文件到,对应目录
[root@lvs keepalived]# mkdir /etc/keepalived
[root@lvs keepalived]# cp etc/keepalived/keepalived.conf /etc/keepalived/
[root@lvs keepalived]# ll /etc/keepalived/keepalived.conf
ps: 复制自带配置文件到哪个目标路径?自带的服务脚本里指明了默认路径,如下:
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
ps:配置sysconfig文件,可以自定义keepalived程序读取的配置文件,从而不使用默认的配置文件