前言
keepalived的负载均衡功能默认是通过lvs实现的.实际上,也可以配合其他的负载均衡软件一起工作,例如nginx
准备
node1:192.168.10.201
node2:192.168.10.202
node3:192.168.10.203
node4:192.168.10.204
vip:192.168.10.200
其中node1和node2作为director,需事先安装keepalived和nginx软件包;node3和node4作为real server,只需开启web服务
确保所有服务器关闭selinux和防火墙或做好相关的配置,时间同步;node1和node2做好时间同步,并进行公私钥认证,以及确认主机名无误
实验
- 主从模型
1.配置nginx反向代理(node1,node2)
]# vim /etc/nginx/nginx.conf
http {
...
upstream webservers {//定义反向代理规则
server 192.168.10.203:80 weight=1;
server 192.168.10.204:80 weight=1;
}
...
server {
...
location / {//应用已有规则
proxy_pass http://webservers/;
}
...
}
}
...
]# systemctl start nginx
2.配置keepalived(node1,node2)
]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id node1//node2修改为node2
vrrp_skip_check_adv_addr
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER//node2修改为BACKUP
interface ens33
virtual_router_id 51
priority 100//node2修改为99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.200/24
}
}
]# systemctl start keepalived
这样事实上就已经完成了简单的主从模型,但仍有较多缺陷:keepalived尚未完善日志信息;keepalived无从得知nginx服务是否启动,并依据nginx是否启动切换主备;nginx在切换web服务器时无法保存用户session等等…
下面开始一步步解决.首先是keepalived的日志信息:
]# vim /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived//keepalived环境文件的位置
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
]# vim /etc/sysconfig/keepalived
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp -P Only run with VRRP subsystem.
# --check -C Only run with Health-checker subsystem.
# --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs -I Dont remove IPVS topology on daemon stop.
# --dump-conf -d Dump the configuration data.
# --log-detail -D Detailed log messages.
# --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS="-D -S 3 -D -S 0"//添加日志,日志级别为0
]# vim /etc/rsyslog.conf
local0.* /var/log/keepalived.log
]# systemctl restart rsyslog
]# systemctl restart keepalived
这样,keepalived日志文件就保存在/var/log/keepalived.log中.接下来需要修改keepalived配置文件以监控nginx
]# vim /etc/keepalived/keepalived.conf
vrrp_script chk_nginx {
script "/usr/bin/killall -0 nginx &> /dev/null"
interval 1
weight -10
}
vrrp_instance VI_1 {
...
track_script {
chk_nginx
}
}
修改后,当nginx不在运行时,keepalived就会检测到,降低自己的优先级而成为BACKUP状态.但这样的话,nginx进程必须通过管理员手动开启,可以再次修改keepalived配置文件使其启动开启nginx服务
]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
...
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
]# vim /etc/keepalived/notify.sh
#!/bin/bash
vip=192.168.10.200
contact='root@localhost'
notify() {
mailsubject="`hostname` to be $1: $vip floating"
mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
master)
notify master
systemctl restart nginx
exit 0
;;
backup)
notify backup
systemctl restart nginx
exit 0
;;
fault)
notify fault
exit 0
;;
*)
echo 'Usage: `basename $0` {master|backup|fault}'
exit 1
;;
esac
这样,即使我们手动关闭nginx,keepalived也会通过调用脚本的方式再次开启nginx进程.但倘若通过其它方式(比如关闭nginx的同时立刻开启httpd占用80端口)使nginx无法开启,那样keepalived也无能为力
- 双主模型
双主模型中的director各自都是主节点和备节点.即两个节点用另一个vip运行第二个vrrp实例,这样可以充分利用资源,也可以减轻单台vrrp服务器的压力
]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_2 {//新的vrrp实例
state BACKUP
interface ens33
virtual_router_id 60
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.10.100/24
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
这样,客户端便可以通过192.168.10.100和192.168.10.200两个地址(dns轮询)访问web服务器.倘若一台director服务器宕机,这两个ip也可以迅速转移到另一台director服务器
问题:倘若web服务需要session绑定,这种双主模型明显无法满足需求
解决:在后端单独设立session服务器,保存所有的用户session信息
其它
在现实环境中,往往不需要keepalived对nginx做监控,而是用专业的监控软件,如zabbix单独对服务进行监控
问题:当director连接的使缓存服务器而不是真实服务器,应该用什么调度算法以提升缓存命中率
解决:可能是dh算法.依据url地址选取不同的缓存服务器:对url哈希,然后以缓存服务器的个数为被除数进行取模,从而平均分配给所有缓存服务器.
缺点:当一个缓存服务器宕机,将导致所有分配规则全被打乱而需重新计算,此时后端服务器因无法承受巨大访问量而极易宕机
解决:采用哈希环算法.将0~2^32-1构成一个环,将缓存服务器平均分配在环的不同位置.将url哈希后的数值以2^32为被除数进行取模,计算后的数值一定在该环的不同位置.数值顺时针旋转遇到的第一个服务器便是选定的服务器.这样就算一台缓存服务器宕机,也只有另一台缓存服务器受影响.
缺点:在多次宕机后,哈希环中的各个缓存服务器可能会分布不均衡,即哈希环偏移
解决:在哈希环算法的基础上采用虚拟节点.即一台缓存服务器生成多个(1w,10w)虚拟服务器,均匀分布在哈希环的不同位置上,这样即使一台服务器宕机,发生的偏移幅度也很小