一、场景需求
![](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)
- 技能知识点
- 对死锁问题的理解
- 文件系统原理:如何用1分钟遍历一个100TB的文件?
- 数据库原理:为什么PrepareStatement性能更好更安全?
- Java Web程序的运行时环境到底是怎样的?
- 你真的知道自己要解决的问题是什么吗?
- 如何解决问题
- 经验分享
- GIT的HTTP方式免密pull、push
- 使用xhprof对php7程序进行性能分析
- 微信扫码登录和使用公众号方式进行扫码登录
- 关于curl跳转抓取
- Linux 下配置 Git 操作免登录 ssh 公钥
- Linux Memcached 安装
- php7安装3.4版本的phalcon扩展
- centos7下php7.0.x安装phalcon框架
- 将字符串按照指定长度分割
- 搜索html源码中标签包的纯文本
- 更换composer镜像源为阿里云
- mac 隐藏文件显示/隐藏
- 谷歌(google)世界各国网址大全
- 实战文档
- PHP7安装intl扩展和linux安装icu
- linux编译安装时常见错误解决办法
- linux删除文件后不释放磁盘空间解决方法
- PHP开启异步多线程执行脚本
- file_exists(): open_basedir restriction in effect. File完美解决方案
- PHP 7.1 安装 ssh2 扩展,用于PHP进行ssh连接
- php命令行加载的php.ini
- linux文件实时同步
- linux下php的psr.so扩展源码安装
- php将字符串中的\n变成真正的换行符?
- PHP7 下安装 memcache 和 memcached 扩展
- PHP 高级面试题 - 如果没有 mb 系列函数,如何切割多字节字符串
- PHP设置脚本最大执行时间的三种方法
- 升级Php 7.4带来的两个大坑
- 不同域名的iframe下,fckeditor在chrome下的SecurityError,解决办法~~
- Linux find+rm -rf 执行组合删除
- 从零搭建Prometheus监控报警系统
- Bug之group_concat默认长度限制
- PHP生成的XML显示无效的Char值27消息(PHP generated XML shows invalid Char value 27 message)
- XML 解析中,如何排除控制字符
- PHP各种时间获取
- nginx配置移动自适应跳转
- 已安装nginx动态添加模块
- auto_prepend_file与auto_append_file使用方法
- 利用nginx实现web页面插入统计代码
- Nginx中的rewrite指令(break,last,redirect,permanent)
- nginx 中 index try_files location 这三个配置项的作用
- linux安装git服务器
- PHP 中运用 elasticsearch
- PHP解析Mysql Binlog
- 好用的PHP学习网(持续更新中)
- 一篇写给准备升级PHP7的小伙伴的文章
- linux 安装php7 -系统centos7
- Linux 下多php 版本共存安装
- PHP编译安装时常见错误解决办法,php编译常见错误
- nginx upstream模块--负载均衡
- 如何解决Tomcat服务器打开不了HOST Manager的问题
- PHP的内存泄露问题与垃圾回收
- Redis数据结构 - string字符串
- PHP开发api接口安全验证
- 服务接口API限流 Rate Limit
- php内核分析---内存管理(一)
- PHP内存泄漏问题解析
- 【代码片-1】 MongoDB与PHP -- 高级查询
- 【代码片-1】 php7 mongoDB 简单封装
- php与mysql系统中出现大量数据库sleep的空连接问题分析
- 解决crond引发大量sendmail、postdrop进程问题
- PHP操作MongoDB GridFS 存储文件,如图片文件
- 浅谈php安全
- linux上keepalived+nginx实现高可用web负载均衡
- 整理php防注入和XSS攻击通用过滤