keepalived+Nginx负载均衡高可用
安装环境:Centos6.5 x86_64系统最小化安装实验环境:
Nginx1:172.16.35.206
Nginx2:172.16.35.81
http1:172.16.35.249
http2:172.16.35.75
VIP:172.16.35.211
拓扑图:
背景:
既然有了Lvs+keepalived这样高性能的组合,那为何还要有Nginx+keepalived呢,keepalived的初衷就是为了Lvs而设计的,我们都知道Lvs是一个四层的负载均衡设备,虽然有着高性能的优势,但同时它却没有后端服务器的健康检查机制,keepalived为lvs设计了一系列的健康检查机制TCP_CHECK,UDP_CHECK,HTTP_GET等。同时lvs也可以自己写健康检查脚步。或者结合ldirectory来实现后端检测。但是固LVS始终无法摆脱它是一个四层设备,无法对上层协议进行解析。Nginx不一样,Nginx是一个七层的设备可以对七层协议进行解析,可以对一些请求进行过滤,还可以对请求结果进行缓存。这些都是Nginx独有的优势。但是keepalived并没有为Nginx提供健康检测。需要自己去写一些脚步来进行健康检测。关于Lvs+keepalived的构建可参考我的另一篇博文:
http://forlinux.blog.51cto.com/8001278/1404278
一:安装配置Nginx(结合gperftool来优化内存使用tcmalloc替代glibc来管理内存)
给Nginx1和Nginx2分别安装nginx服务,并配置upstream
#!/bin/bash
groupadd -r nginx
useradd -r -g nginx nginx
yum install gcc gcc-c++ openssl-devel pcre-devel wget vim automake autoconf -y
wget http://nginx.org/download/nginx-1.4.7.tar.gz
#这个地址可能有的时候无法解析到域名下载不到这个包,需要自己去下载包然后安装安装步骤一步一步安装
#wget http://mirror.yongbok.net/nongnu/libunwind/libunwind-1.1.tar.gz
wget http://gperftools.googlecode.com/files/gperftools-2.1.tar.gz
# libunwind install
tar -xvf libunwind-1.1.tar.gz
cd libunwind-1.1
CFLAGS=-fPIC ./configure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
cd ..
# gperftools install
tar -xvf gperftools-2.1.tar.gz
cd gperftools-2.1
./configure
make && make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
cd ..
# nginx install
tar zxvf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-google_perftools_module \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-pcre
make && make install
# Setup
mkdir /tmp/tcmalloc
chmod 0777 /tmp/tcmalloc
#简单的一个nginx优化
cat >> /etc/sysctl.conf/usr/local/nginx/html/index.html
四:测试最终的结果。
基本功能测试:
关闭了nginx后开vip是否漂移。
vip没有漂移之前:
关闭了Nginx后VIP进行了漂移
备用机器抢占VIP的过程
关闭Keepalived,看VIP偏移。
注意事项:
1.keeaplived的配置文件要注意书写格式
keepalived,默认不检查配置文件的语法问题。这是个很头疼的问题,我就遇见过好几次因为配置文件格式的问题搞了几个小时才解决。要避免从word中粘贴配置文件内容。一下总结下配置文件格式的一些问题:
1.VI_1 { 类似于这样的形式,关键字和{之间需要留空格
2.检测脚本要有权限。
2.检测脚本也可以使用如下脚本:更具有健壮性。
#!/bin/bash
N=`ps -C nginx --no-header|wc -l`
if [ $N -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 1
if [ `ps -C nginx --no-header|wc -l` -eq 0 ];then
service keepalived stop
fi
fi
页:
[1]