一.keepalived介绍


Keepalived is a routing software written in C. The main goal of this project is to provide simple and robust facilities for loadbalancing and high-availability to Linuxsystem and Linux based infrastructures. Loadbalancing framework relies on well-knownand widely used Linux Virtual Server (IPVS)kernel module providing Layer4 loadbalancing. Keepalived implements a set of checkersto dynamically and adaptively maintain and manage loadbalanced server pool accordingtheir health. On the other hand high-availability is achieved by VRRP protocol. VRRP is afundamental brick for router failover. In addition, Keepalived implements a set ofhooks to the VRRP finite state machine providing low-level and high-speed protocolinteractions. Keepalived frameworks can be used independently or all together toprovide resilient infrastructures.

keepalived 是用c写的路由软件,主要目的是为基于linux的设备提供一个简单强健的,能实现负载均衡和高可用功能的工具."负载均衡"框架基于"IPVS",能调用一系列的"checker" 动态的维护管理负载均衡服务器和后端的RS."高可用"是基于"VRRP"协议的.两者可同时使用.

二.keepalived应用


1.负载均衡(主要是ipvs),同时不依赖ipvsadm这个包

2.高可用,自动failover(主要用于不具备health aware的服务,如nginx)

3.自定义脚本,主要是因为keepalived提供了一种机制,可以在server状态改变时执行相应的策略.至于策略是什么,归用户自定义(可以用来煮咖啡)

4.动态,静态路由


至于如何实现的,网上大把教程


三.keepalive配置文件


KEEPALIVED.CONF(5) KEEPALIVED.CONF(5)

NAME
/etc/keepalived/keepalived.conf - configuration file for keepalived

DESCRIPTION
keepalived.conf is the configuration file which describes all the
keepalived keywords. keywords are placed in hierachies of blocks (and
subblocks), each layer being delimited by ’{’ and ’}’ pairs.

(配置文件是用{}括起来的多级块)

Comments start with ’#’ or ’!’ to the end of the line and can start
anywhere in a line.
(用#或!来注释,没有多行注释)
TOP HIERACHY
GLOBAL CONFIGURATION

VRRP CONFIGURATION

LVS CONFIGURATION


GLOBAL CONFIGURATION
contains subblocks of Global defination and static routes
全局设定包括"全局定义"和"静态路由"


GLOBAL DEFINATION
global_defs # Block id
{
notification_email # To:
{
[email protected] (邮件接收者)
...
}
# From: from address that will be in header
notification_email_from [email protected] (发送者)
smtp_server 127.0.0.1 # IP
smtp_connect_timeout 30 # integer, seconds
router_id my_hostname # string identifying the machine,标示本机的字符串
# (doesn’t have to be hostname).
enable_traps # enable SNMP traps
}



STATIC ROUTES
keepalived can configure static addresses and routes. These addresses
are NOT moved by vrrpd, they stay on the machine. If you already have
IPs and routes on your machines and your machines can ping each other,
you don't need this section.

The syntax is the same as for virtual addresses and virtual routes.

static_ipaddress
{
192.168.1.1/24 dev eth0 scope global
...
}

static_routes
{
192.168.2.0/24 via 192.168.1.100 dev eth0
...
}


VRRD CONFIGURATION
contains subblocks of VRRP synchronization group(s) and VRRP
instance(s)

vrrd配置包括"VRRP同步组"和"vrrp实例"
VRRP synchronization group(s)
#string, name of group of IPs that failover together
vrrp_sync_group VG_1 {
group {
inside_network # name of vrrp_instance (below)
outside_network # One for each moveable IP.
...
}

# notify scripts and alerts are optional
#
# filenames of scripts to run on transitions
# can be unquoted (if just filename) 监测脚本若接收参数,需用括号引用
# or quoted (if has parameters)
# to MASTER transition
notify_master /path/to_master.sh
# to BACKUP transition
notify_backup /path/to_backup.sh
# FAULT transition
notify_fault "/path/fault.sh VG_1"

# for ANY state transition.
# "notify" script is called AFTER the(notify脚本优先级低于nitofy_开头的)
# notify_* script(s) and is executed
# with 3 arguments provided by keepalived
# (ie don’t include parameters in the notify line).
# arguments
# $1 = "GROUP"|"INSTANCE"
# $2 = name of group or instance
# $3 = target state of transition
# ("MASTER"|"BACKUP"|"FAULT")
notify /path/notify.sh

# Send email notifcation during state transition,
# using addresses in global_defs above.
smtp_alert
}


VRRP instance(s)
describes the moveable IP for each instance of a group in
vrrp_sync_group. Here are described two IPs (on inside_network and on
outside_network), on machine "my_hostname", which belong to the group
VG_1 and which will transition together on any state change.
#You will need to write another block for outside_network.
vrrp_instance inside_network {
# Initial state, MASTER|BACKUP
# As soon as the other machine(s) come up,
# an election will be held and the machine
# with the highest "priority" will become MASTER.
# So the entry here doesn’t matter a whole lot.
state MASTER (这句话其实不重要,重要的是优先级)

# interface for inside_network, bound by vrrp
interface eth0(表明vrrp绑定在哪个设备)

# Use VRRP Virtual MAC.
use_vmac (虚拟mac地址,不需要设定,kp会自动分配一个)

# Ignore VRRP interface faults (default unset)
dont_track_primary (忽略vrrp设备错误)

# optional, monitor these as well.
# go to FAULT state if any of these go down.
track_interface { (跟踪设备,若出错则进入"fault"状态)
eth0
eth1
...
}

#default IP for binding vrrpd is the primary IP
#on interface. If you want to hide location of vrrpd,
#use this IP as src_addr for multicast vrrp packets.
#(since it’s multicast, vrrpd will get the reply
#packet no matter what src_addr is used).
#optional
mcast_src_ip (定义多播地址)

# Binding interface for lvs syncd
lvs_sync_daemon_interface eth1 (把lvs功能绑定在特定网卡)

# delay for gratuitous ARP after transition to MASTER
garp_master_delay 10 # secs, default 5 (进入MASTER状态后10秒发送"garp"广播)

# arbitary unique number 0..255
# used to differentiate multiple instances of vrrpd
# running on the same NIC (and hence same socket).
virtual_router_id 51 (虚拟路由ID,可借此自动分配"VMAC")

# for electing MASTER, highest priority wins.
# to be MASTER, make 50 more than other machines.
priority 100(你懂的-_-)

# VRRP Advert interval, secs (use default)
advert_int 1 (广播 间隔)
authentication { # Authentication block
# PASS||AH (认证方式,明码和IPSEC,推荐使用明码)
# PASS - Simple Passwd (suggested)
# AH - IPSEC (not recommended))
auth_type PASS
# Password for accessing vrrpd.
# should be the same for all machines.
# Only the first eight (8) characters are used.
auth_pass 1234

#addresses add|del on change to MASTER, to BACKUP.
#With the same entries on other machines,
#the opposite transition will be occuring.
virtual_ipaddress {
/ brd dev scope label