2 keepalived作用
Keepalived主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。 3 keepalived配置文件
Keepalived是一个相对轻量级的服务,而且与LVS配合的非常好,它只有一个配置文件,主要包括global_defs、static_ipaddress、static_routes、VRRP_script、VRRP_instance和virtual_server的设置,而这个这些配置又可分为三大类:全局配置,VRRP的配置以及LVS配置。 1)全局配置
global_defs { # Block id
notification_email {
admin@example1.com # 指明接收keepalived信息的邮箱
...
}
notification_email_from admin@example.com # 邮箱名
smtp_server 127.0.0.1 # 收邮件的主机地址
smtp_connect_timeout 30 # integer, seconds
router_id my_hostname # 一个标记该主机的字符串,可以不是主机名,建议使用主机名
vrrp_mcast_group4 224.0.0.18 # 多播组地址, 默认是 224.0.0.18
vrrp_mcast_group6 ff02::12 # ipv6多播组地址, 默认是 ff02::12
enable_traps # 启用SNMP陷阱,可以不启用
}2)VRRP配置
VRRPD配置包括三个类:VRRP同步组(synchroization group)、VRRP实例(VRRP Instance)、VRRP脚本
******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.
vrrp_instance inside_network {
state MASTER #
interface eth0 #
use_vmac # Use VRRP Virtual MAC. #
vmac_xmit_base # VMAC interface
dont_track_primary #
track_interface {
eth0
eth1
...
}
mcast_src_ip # 多播组源ip
unicast_src_ip
unicast_peer {
...
}
lvs_sync_daemon_interface eth1
garp_master_delay 10 # secs, default 5
virtual_router_id 51
priority 100 # 优先级
advert_int 1
authentication { # 认证信息模块
auth_type PASS
auth_pass 1234 # 认证密码,建议使用"openssl rand -hex 4"生成8位随机密码
}
virtual_ipaddress { # vrrp实例
/ brd dev scope label # 地址定义格式
172.18.14.77/24 dev eth1 # 地址+interface
172.18.14.66/24 dev eth2 label eth2:1
172.18.14.55/24 # 也可以直接写地址即可
}
virtual_ipaddress_excluded {
/ brd dev scope
/ brd dev scope
...
}
virtual_routes { # 虚拟网关,在配置中可不定义
src 192.168.100.1 to 192.168.109.0/24 via 192.168.200.254 dev eth1
192.168.110.0/24 via 192.168.200.254 dev eth1
192.168.111.0/24 dev eth2
192.168.112.0/24 via 192.168.100.254 or 192.168.113.0/24 via 192.168.200.254 or 192.168.100.254 dev eth1
blackhole 192.168.114.0/24
}
nopreempt
nopreempt
preempt_delay 300 # waits 5 minutes
debug # Debug level, not implemented yet.
# notify scripts, alert as above
notify_master |
notify_backup |
notify_fault |
notify |
smtp_alert
}3)LVS配置
如果你没有配置LVS+keepalived,那么无需配置这段区域,如果你用的是nginx来代替LVS,这无需配置这项,这里的LVS配置是专门为keepalived+LVS集成准备的。它是用keepalived的配置文件来代替ipvsadm来配置LVS,并不是利用ipvsadm来配置,这样会方便很多,一个配置文件搞定这些,维护也方便。这里LVS配置也有两个配置:一个是集群的定义,即VIP指定;二是添加真实服务器地址。
************Virtual server(s)***********
fwmark #防火墙标记,如果你是基于防火墙标记的LVS需要设置这一项
virtual_server group string { # 配置虚拟服务器,可以是virtual_server IP port | virtual_server fwmark int
delay_loop # delay timer for service polling
lb_algo rr|wrr|lc|wlc|lblc|sh|dh # LVS scheduler
ops # Enable One-Packet-Scheduling for UDP (-O in ipvsadm)
lb_kind NAT|DR|TUN # LVS forwarding method
persistence_timeout # LVS persistence timeout, sec
persistence_granularity # LVS granularity mask (-M in ipvsadm)
protocol TCP # Only TCP is implemented
ha_suspend # If VS IP address is not set,suspend healthchecker's activity
virtualhost # VirtualHost string for HTTP_GET or SSL_GET,eg virtualhost www.firewall.loc
alpha
omega
quorum
hysteresis
quorum_up |
quorum_down |
sorry_server # RS to add when all realservers are down
real_server {
weight # relative weight to use, default: 1
notify_up |
notify_down |
HTTP_GET|SSL_GET { # 如果是http则选择HTTP_GET,https时选择SSL_GET
url { # 做做健康性检查时访问的url,可以定多个url
digest # A url to test can have multiple entries here
status_code # status code returned in the HTTP headereg status_code 200
}
delay_before_retry # 失败后判断为宕机的延时
connect_ip # 连接IP地址
connect_port # 连接端口
bindto
bind_port
connect_timeout # 连接超时时间
fwmark
warmup
}
TCP_CHECK { # 基于TCP_CHECK的健康性检测模块定义
connect_ip
connect_port
bindto
bind_port
connect_timeout
fwmark
warmup
}
SMTP_CHECK { # 基于邮件服务的健康性检查模块
host {
connect_ip
connect_port
bindto
bind_port
connect_timeout
fwmark
}
retry
delay_before_retry
helo_name |
warmup
}