Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。主要提供loadbalancing(负载均衡)和high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务。
主要讲述一下Keepalived编译和安装出现的问题以及解决方法,本人使用keepalived-1.3.5.tar.gz。
官网源码包下载地址(2017-07-31 Keepalived最新版keepalived-1.3.5.tar.gz):http://www.keepalived.org/download.html
[root@iZ256w2hluuZ local]# tar -zxvf /tmp/keepalived-1.3.5.tar.gz
[root@iZ256w2hluuZ local]# cd keepalived-1.3.5/
[root@iZ256w2hluuZ keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
1. 系统出现警告信息“*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.”,具体日志如下:
Keepalived configuration
------------------------
Keepalived version : 1.3.5
Compiler : gcc
Preprocessor flags :
Compiler flags : -Wall -Wunused -Wstrict-prototypes -Wextra -g -O2
Linker flags :
Extra Lib : -lcrypto -lssl
Use IPVS Framework : Yes
IPVS use libnl : No
IPVS syncd attributes : No
IPVS 64 bit stats : No
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
Use VRRP authentication : Yes
With ip rules/routes : Yes
SNMP vrrp support : No
SNMP checker support : No
SNMP RFCv2 support : No
SNMP RFCv3 support : No
DBUS support : No
SHA1 support : No
Use Debug flags : No
Stacktrace support : No
Memory alloc check : No
libnl version : None
Use IPv4 devconf : No
Use libiptc : No
Use libipset : No
init type : upstart
Build genhash : Yes
Build documentation : No
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
解决方案一:(在线安装)
执行yum命令yum -y install libnl libnl-devel解决上述警告问题
[root@iZ256w2hluuZ keepalived-1.3.5]# yum -y install libnl libnl-devel
解决方案二:(离线安装)
离线安装libnl-devel包,从网上下载libnl-devel-1.1.4-2.el6.x86_64.rpm包,执行rpm -ivh libnl-devel-1.1.4-2.el6.x86_64.rpm命令。
[root@iZ256w2hluuZ other-packages]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libnl-devel-1.1.4-2.el6.x86_64.rpm
####执行wget命令是从另外一台可以连接外网的服务器上下载libnl-devel-1.1.4-2.el6.x86_64.rpm包的命令,可以忽略,只是为大家提供个下载libnl-devel地址####
[root@iZ256w2hluuZ other-packages]# rpm -ivh libnl-devel-1.1.4-2.el6.x86_64.rpm
Preparing... ########################################### [100%]
package libnl-devel-1.1.4-2.el6.x86_64 is already installed
安装完成以后,重新执行configure命令
[root@iZ256w2hluuZ keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
2. 系统出现错误信息“configure: error: libnfnetlink headers missing”,具体日志如下:
checking for nl_socket_modify_cb in -lnl... yes
checking for linux/rtnetlink.h... yes
checking libnfnetlink/libnfnetlink.h usability... no
checking libnfnetlink/libnfnetlink.h presence... no
checking for libnfnetlink/libnfnetlink.h... no
configure: error: libnfnetlink headers missing
解决方案一:(在线安装)
执行yum命令yum install -y libnfnetlink-devel解决上述错误问题
[root@iZ256w2hluuZ keepalived-1.3.5]# yum install -y libnfnetlink-devel
解决方案二:(离线安装)
Linux执行wget下载rpm包(离线安装libnfnetlink包时所需rpm包,执行wget下载libnfnetlink rpm包命令,此步可以忽略,只是为大家提供个下载libnfnetlink地址),具体命令如下:
wget ftp://mirror.switch.ch/mirror/centos/6/os/x86_64/Packages/libnfnetlink-1.0.0-1.el6.x86_64.rpm
wget ftp://mirror.switch.ch/mirror/centos/6/os/x86_64/Packages/libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm
执行rpm -ivh libnfnetlink-1.0.0-1.el6.x86_64.rpm命令安装libnfnetlink-1.0.0-1.el6.x86_64.rpm包,具体如下:
[root@iZ256w2hluuZ other-packages]# rpm -ivh libnfnetlink-1.0.0-1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:libnfnetlink ########################################### [100%]
执行rpm -ivh libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm命令安装libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm包,具体如下:
[root@iZ256w2hluuZ other-packages]# rpm -ivh libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm
Preparing... ########################################### [100%]
1:libnfnetlink-devel ########################################### [100%]
安装完成以后,重新执行configure命令
[root@iZ256w2hluuZ keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived
3. 编译keepalived不会出现上述警告和其他错误,执行make && make install命令进行安装:
[root@iZ256w2hluuZ keepalived-1.3.5]# make && make install
至此Keepalived编译并安装结束,在下一篇文章中会详解介绍Nginx搭配Keepalived的详细配置和使用方法,欢迎大家收藏本站素文宅博客。
使用命令 /usr/local/keepalived/sbin -h 参看各启动参数,启动命令:
[root@iZ256w2hluuZ keepalived-1.3.5]# ./sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf
制作快捷启动,在源文件的目录./keepalived-1.3.5/keepalived/etc/init.d下有两个个快捷启动文件和生成/usr/local/keepalived目录下一个配置文件需要复制,具体执行如下命令:
[root@iZ256w2hluuZ keepalived-1.3.5]# cp ./keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/
[root@iZ256w2hluuZ keepalived-1.3.5]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@iZ256w2hluuZ keepalived-1.3.5]# cp ./keepalived-1.3.5/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
这样就可以执行service keepalived [start | stop | reload | restart ]命令。
启动报“Starting keepalived: /bin/bash: keepalived: command not found”错误,具体日志如下:
[root@yoodb /]# service keepalived start
正在启动 keepalived:/bin/bash: keepalived: command not found
[失败]
解决方案:
[root@yoodb /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@yoodb /]# service keepalived start
正在启动 keepalived: [确定]
[root@yoodb /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
有上述日志信息,表示启动成功。
前面我们已经讲述了Keepalived-1.3.5编译安装和制作快捷启动方式,下面主要说一说如何在两台服务器分布部署Nginx,通过keepalived实现高可用。
准备工作:
Linux操作系统Centos6.5,两台接入服务器公用一个虚拟IP(VIP)123.57.47.154
用途IP
MASTER192.168.1.1
BACKUP192.168.1.2
配置Nginx
两台接入服务器Nginx配置完全相同,配置文件目录/usr/local/nginx/conf/nginx.conf,配置内容如下:
upstream blog.yoodb.com {
server 192.168.1.1;
server 192.168.1.2;
}
server{
listen 80;
server_name blog.yoodb.com;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
配置keepalived
keepalived的配置文件/etc/keepalived/keepalived.conf,注意主、从服务器的keepalived配置相关联有所不同,配置如下:
主Master:
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server smtp.gmail.com
smtp_connect_timeout 30
router_id nginx_master
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
123.57.47.154
}
}
从Backup:
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server smtp.gmail.com
smtp_connect_timeout 30
router_id nginx_backup
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
123.57.47.154
}
}
验证:
先后启动主、从服务器上keepalived,执行/etc/init.d/keepalived start命令
利用ip a命令查看主服务器虚拟IP是否绑定成功
停止主服务器上的keepalived,执行/etc/init.d/keepalived stop命令访问123.57.47.154是否依然能够访问web页面,再次利用ip a命令查看从服务器虚拟IP是否绑定成功。
启动主服务器上的keepalived,看看主服务器能否重新接管虚拟IP。
转载自素文宅博客:
Keepalived-1.3.5编译安装步骤和启动方式详解
Nginx keepalived实现高可用负载均衡详细配置步骤