一、场景需求 ![](https://upload-images.jianshu.io/upload_images/6954572-d5a8885e69130d98?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 二、Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务的高可用。 Keepalived 以 VRRP 协议为实现基础,用 VRRP 协议来实现高可用性(HA)。VRRP(VirtualRouter Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP 协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器 IP(一个或多个),而在路由器组内部,如果实际拥有这个对外 IP 的路由器如果工作正常的话就是 MASTER,或者是通过[算法](http://lib.csdn.net/base/31)选举产生,MASTER 实现针对虚拟路由器 IP 的各种网络功能, 如 ARP 请求,ICMP,以及数据的转发等;其他设备不拥有该虚拟 IP,状态是 BACKUP,除了接收 MASTER 的VRRP 状态通告信息外,不执行对外的网络功能。当主机失效时,BACKUP 将接管原先 MASTER 的网络功能。VRRP 协议使用多播数据来传输 VRRP 数据,VRRP 数据使用特殊的虚拟源 MAC 地址发送数据而不是自身网卡的 MAC 地址,VRRP 运行时只有 MASTER 路由器定时发送 VRRP 通告信息,表示 MASTER 工作正常以及虚拟路由器 IP(组),BACKUP 只接收 VRRP 数据,不发送数据,如果一定时间内没有接收到 MASTER 的通告信息,各 BACKUP 将宣告自己成为 MASTER,发送通告信息,重新进行 MASTER 选举状态。 ![](https://upload-images.jianshu.io/upload_images/6954572-62a4ef16c8218c94?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 三、方案规划 ![](https://upload-images.jianshu.io/upload_images/6954572-979ad74c9330acb2?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 四、安装 Nginx 1、安装编译 Nginx 所需的依赖包 Connecting to 192.168.1.121:22... Connection established. To escape to local shell, press 'Ctrl+Alt+\]'. Last login: Sat Apr 16 17:55:20 2016 from 192.168.1.61 \[root@edu-proxy--01 ~\]# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel 2、上传 Nginx(nginx-1.6.2.tar.gz)到 /usr/local/src 目录 ![](https://upload-images.jianshu.io/upload_images/6954572-65b1e190a9a5a3c1?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 3、编译安装 Nginx \[root@edu-proxy--01 ~\]# cd /usr/local/src/ \[root@edu-proxy--01 src\]# ls keepalived-1.2.18.tar.gz  nginx-1.6.2.tar.gz \[root@edu-proxy--01 src\]# tar -zxvf nginx-1.6.2.tar.gz \[root@edu-proxy--01 src\]# cd nginx-1.6.2 \[root@edu-proxy--01 nginx-1.6.2\]# ./configure --prefix=/usr/local/nginx \[root@edu-proxy--01 nginx-1.6.2\]# make && make install \[root@edu-proxy--01 nginx-1.6.2\]# vi /usr/local/nginx/conf/nginx.conf user  root; worker\_processes  1; #error\_log  logs/error.log; #error\_log  logs/error.log  notice; #error\_log  logs/error.log  info; #pid        logs/nginx.pid; events { worker\_connections  1024; } http { include       mime.types; default\_type  application/octet-stream; #log\_format  main  '$remote\_addr - $remote\_user \[$time\_local\] "$request" ' \#                  '$status $body\_bytes\_sent "$http\_referer" ' \#                  '"$http\_user\_agent" "$http\_x\_forwarded\_for"'; #access\_log  logs/access.log  main; sendfile        on; #tcp\_nopush     on; #keepalive\_timeout  0; keepalive\_timeout  65; #gzip  on; server { listen       88; server\_name  localhost; #charset koi8-r; #access\_log  logs/host.access.log  main; location / { root   html; index  index.html index.htm; } #error\_page  404              /404.html; \# redirect server error pages to the static page /50x.html error\_page   500 502 503 504  /50x.html; location = /50x.html { root   html; } } } 修改 Nginx 欢迎首页内容(用于后面[测试](http://lib.csdn.net/base/softwaretest),用于区分两个节点的 Nginx): vi /usr/local/nginx/html/index.html 192.168.1.121 中的标题加 1 [Welcome to nginx! 1](https://www.jianshu.com/writer) 192.168.1.122 中的标题加 2 [Welcome to nginx! 2](https://www.jianshu.com/writer) 5、系统防火墙打开对应的端口 88 vi /etc/sysconfig/iptables \-A INPUT -m state --state NEW -m tcp -p tcp --dport 88 -j ACCEPT service iptables restart 6、测试 Nginx 是否安装成功 /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 7、启动 Nginx /usr/local/nginx/sbin/nginx 重启 /usr/local/nginx/sbin/nginx -s reload 8、设置 Nginx 开机启动 vi /etc/rc.local 加入 /usr/local/nginx/sbin/nginx 分别访问两个nginx nginx1 ![](https://upload-images.jianshu.io/upload_images/6954572-dcb19348c3c66c34?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 2.nginx2 ![](https://upload-images.jianshu.io/upload_images/6954572-400dff562bc7530d?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 五、安装 Keepalived ([http://www.keepalived.org/download.html](http://www.keepalived.org/download.html)) 1、上传或下载 keepalived(keepalived-1.2.18.tar.gz)到 /usr/local/src 目录 2、解压安装 cd /usr/local/src tar -zxvf keepalived-1.2.18.tar.gz cd keepalived-1.2.18 ./configure --prefix=/usr/local/keepalived make && make install 3、将 keepalived 安装成[Linux](http://lib.csdn.net/base/linux)系统服务: 因为没有使用 keepalived 的默认路径安装(默认是/usr/local),安装完成之后,需要做一些工作 复制默认配置文件到默认路径 mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ 复制 keepalived 服务脚本到默认的地址 cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/sbin/keepalived /usr/sbin/ ln -s /usr/local/keepalived/sbin/keepalived /sbin/ 设置 keepalived 服务开机启动 chkconfig keepalived on 4、修改 Keepalived 配置文件 注意,如果linux用的网卡是eth0,interface就要写成eth1,用的是eth1就要写成eth1 (1) MASTER 节点配置文件(192.168.1.121) vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global\_defs { router\_id edu-proxy-01 } vrrp\_script chk\_nginx { script "/etc/keepalived/nginx\_check.sh" interval 2 weight -20 } vrrp\_instance VI\_1 { state MASTER interface eth0 virtual\_router\_id 51 mcast\_src\_ip 192.168.1.121 priority 100 nopreempt advert\_int 1 authentication { auth\_type PASS auth\_pass 1111 } track\_script { chk\_nginx } virtual\_ipaddress { 192.168.1.120 } BACKUP节点配置 (192.168.1.122) ! Configuration File for keepalived global\_defs { router\_id edu-proxy-02 } vrrp\_script chk\_nginx { script "/etc/keepalived/nginx\_check.sh" interval 2 weight -20 } vrrp\_instance VI\_1 { state BACKUP interface eth0 virtual\_router\_id 51 mcast\_src\_ip 192.168.1.122 priority 90 advert\_int 1 authentication { auth\_type PASS auth\_pass 1111 } track\_script { chk\_nginx } virtual\_ipaddress { 192.168.1.120 } } 5.编写Nginx状态检测脚本/etc/keepalived/nginx\_check.sh(keepalived.conf中配置) 脚本要求:如果nginx停止运行,尝试启动,如果无法启动则杀死本机的keepalived进程,keepaied将虚拟ip绑定到BACKUP机器上,内容如下 #!/bin/bash A=`ps -C nginx –no-header |wc -l` if \[ $A -eq 0 \];then /usr/local/nginx/sbin/nginx sleep 2 if \[ `ps -C nginx --no-header |wc -l` -eq 0 \];then killall keepalived fi fi 保存后给脚本授邓权限 chmod -x /etc/keepalived/nginx\_check.sh 6.启动keepalived serivce keepalived start 我们在cmd中查看arp ![](https://upload-images.jianshu.io/upload_images/6954572-f93072c4202e7808?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 在edu-proxy-01 \[root@edu-proxy--01 keepalived\]# ip add ![](https://upload-images.jianshu.io/upload_images/6954572-53d6d0b4f3b98084?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 这里我们访问192.168.1.120:88 ![](https://upload-images.jianshu.io/upload_images/6954572-3ae4e6d25fdd43c6?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 7.Keepalived+nginx的高可用测试 (1)关闭192.168.1.121的Nginx,Keepalived会将它重新启动 \[root@edu-proxy--01 keepalived\]# /usr/local/nginx/sbin/nginx -s stop (2)关闭192.168.1.121中的Keepalived,VIP会切换到192.168.1.122中 \[root@edu-proxy--01 keepalived\]# service keepalived stop root@edu-proxy-02 keepalived\]# ip add ![](https://upload-images.jianshu.io/upload_images/6954572-4a0a087a2cb5ec88?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 查看虚拟ip ![](https://upload-images.jianshu.io/upload_images/6954572-0e9c4312f921bb7e?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 说明此时VIP已经漂移到主机192.168.1.122上了 再能过VIP来访问Nginx集群,访问到的也是192.168.1.122 ![](https://upload-images.jianshu.io/upload_images/6954572-f0877ec8bfd77669?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) (3)重新启动192.168.1.121中的Keepalived,VIP又会切回到192.168.1.121中来 \[root@edu-proxy--01 keepalived\]# service keepalived start 查看虚拟机IP ![](https://upload-images.jianshu.io/upload_images/6954572-c74055aa3e4f634c?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ip add ![](https://upload-images.jianshu.io/upload_images/6954572-d82bd57b2bf47805?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Keepalived启动后,网络接口上又会创建出VIP192.168.1.120 ![](https://upload-images.jianshu.io/upload_images/6954572-6b9d46dabe068f3f?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) Keepalived服务管理命令: 停止: service keepalived stop 启动: service keepalived start 重启: service keepalived restart 查看状态: service keepalived status 其他参考资料: keepalived之vrrp\_script总结:http://my.oschina[.NET](http://lib.csdn.net/base/dotnet)/hncscwc/blog/158746 keepalived双机热备实现故障时发送邮件通知:http://www.2cto.com/os/201407/317795.html 基于keepalived实现VIP转移,lvs,nginx的高可用;:[http://www.tuicool.com/articles/eu26Vz](http://www.tuicool.com/articles/eu26Vz)