在 NginX 上启用 Keepalived
前言:配置 Keepalived 最初用于配合 LVS 实现负载均衡,LVS是 Linux Virtual Server 的缩写,拥有三种常有IP负载技术和八种常用调度算法。发展到后来 Keepalived 加入了VRRP协议,即Virtual Router Redundancy Protocol,以至于也可以配合 NginX,实现其负载均衡的高可用。补充一点,Keepalived 对于单纯的IP漂移更简单,如果对资源服务有控制(如存储) Heartbeat 更适合。
VRRP协议介绍
Keepalived 有两大用途:failover 和 healthcheck。由于 Keepalived 只用于实现服务器级别的接管,所以当 NginX 宕机时它可以用无可奈何来形容。基于其特性,我们先来说说VRRP协议:
- 用于解决单点故障。
- 通过竞选机制的协议来分配路由。
- 使用 IP 多播的方式实现通信。
- Master 发包,Backup 收包,当 Backup 收不到包时,接管 Master 服务。
- 使用了加密协议。
Keepalived安装
首先,需要我们安装一下相关的依赖:
# yum install -y ipenssl-devel popt* kernel-devel
懒癌发作一步到位:
yum install keepalived
显然,如果想使用最新版的话,就必须自己手动编译安装了(省略了解压环节):
# ln -s /usr/src/kernels/[kernels version]/ /usr/src/linux
# ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/[kernels version]
# make && make install
规范启动
设置开机启动,Centos7中可以使用
systemctl
实现:# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# chkconfig keepalived on
默认情况下,keepalived 每次启动会去
/etc/keepalived
目录下寻找配置文件# mkdir /etc/keepalived
# vi /etc/keepalived/keepalived.conf
然后,终于可以启动了:
# /etc/init.d/keepalived start
这时候我们可以用正则查看一下进程,确保 Keepalived 已经跑起来了。
# ps -ef | grep keepalived
配置模板
配置模板的文件目录位于
/etc/keepalived/keepalived.conf
仔细看看其实配置文件也不难,这里有一些代码片段。
- 全局定义模块
global_defs {
notification_email {
wileysuA@cubat.cn
wileysuB@cubat.cn
wileysuC@cubat.cn //alarm by send e-mail
}
notification_email_from alarm@cubat.cc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL // as an unique identity in local area network
- VRRP协议实例
vrrp_instance VI_1 {
state MASTER // The hireachy of server
interface eth0
virtual_router_id 51 // Only for instance
priority 100 // The priority must larger 50 than BACKUP
advert_int 1 // Interval of heartbeats
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16 // Configures as netmask, such as 10.0.0.11/24. By default, it is 32.
192.168.200.17
192.168.200.18
}
}
因为Keepalived和LVS高度紧密,所以剩下的信息都用于配置LVS。这份笔记只记录了Keepalived和NginX的配合,因此对于配置文件的讲解到此为止。印象里有一些LVS的学习笔记,哪天找个时间整理出来。
使用方法
- 添加IP地址
# ip addr add 10.0.0.11/24 dev eth0
- 拷贝配置文件
# scp keepalived.conf 10.0.0.9:/etc/keepalived/
- 修改备节点配置文件(关键点)
1. router_id
2. state
3. priority
- 启动Keepalived
# /etc/init.d/keepalived start
评论
发表评论