Nginx+Keepalived实现负载均衡高可用
2015-06-22 15:45
691 查看
Nginx+Keepalived实现负载均衡高可用
一、环境
5台虚拟机,分别是:
1台测试机(192.168.2.83);
2台nginx/keepalived(192.168.2.235/192.168.2.236);
2台Web Servers(192.168.2.237/192.168.2.238);
注:VIP设置为 192.168.2.229;
二、安装配置Web Server
由于Web Server的安装与配置非常简单,根据自己喜好,安装一个即可;比如:apache、nginx、tomcat等等。在此就不再详述;
三、安装配置Nginx
yum -y install gcc vim lrzsz pcre-devel kernel-devel openssl-devel wget
wget http://nginx.org/download/nginx-1.9.0.tar.gz
tar xzvf nginx-1.9.0.tar.gz
cd nginx-1.9.0
./configure --prefix=/usr/local/nginx --with-debug --with-http_stub_status_module --with-stream --with-http_ssl_module
make;make install
ln -s /usr/local/nginx/sbin/nginx /sbin/
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
编辑nginx主配置文件nginx.conf,内容大致如下:
worker_processes 10;
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"';
sendfile on;
keepalive_timeout 65;
upstream html_pool {
ip_hash;
server 192.168.2.237:80;
server 192.168.2.238:80;
#server 192.168.2.239:80 backup;
#server 192.168.2.240:80 down;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://html_pool; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
注:以上的操作,两台都需要操作;到此,我们的Nginx就安装及配置好了。
四、安装配置Keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz tar xzvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/
#内核参数,根据不同的操作系统,会不一样,注意更改为您服务器上的内核路径即可;
make
make install
ln -s /usr/local/sbin/keepalived /sbin/
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
编辑keepalive.conf主配置文件,内容大致如下:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER/BACKUP #前主后备
interface eth0
virtual_router_id 60
priority 100/80 #前主后备
advert_int 3
authentication {
auth_type PASS
auth_pass 33333
}
virtual_ipaddress {
192.168.2.229
}
track_script {
chk_nginx
}
}
因为主配置文件里有用到一个检测nginx状态的脚本,所以另外需要创建脚本,内容如下:
# cat check_ngix.sh
#!/bin/bash
nginx_start=`ps -C nginx --no-header |wc -l`
if [ $nginx_start = 0 ]; then
#/etc/init.d/nginx start
/usr/local/nginx/sbin/nginx
sleep 3
nginx_status=`ps -C nginx --no-header |wc -l`
if [ $nginx_status = 0 ]; then
/etc/init.d/keepalived stop
fi
fi
chmod +x /etc/keepalived/check_nginx.sh
注:同样,以上的所有操作,需要两台服务器上一样操作,注意更改主备及优先级即可;到此,keepalive的相关安装与配置即OK了;
五、校验及测试
启动keepalived(两台):/etc/init.d/keepalived start
在主服务器上随意把nginx或者keepalived服务关闭,观察服务的可用性;
如果飘到备服务器上了,同样,在备服务器上随意把nginx或者keepalived服务关闭,再观察;
最后需要注意的是,如果服务器上有启用安全软件,比如ipatalbes,一定要让服务器间相互访问,不仅仅是ping通;如:iptables -I INPUT -s 192.168.2.0/24 -j ACCEPT
一、环境
5台虚拟机,分别是:
1台测试机(192.168.2.83);
2台nginx/keepalived(192.168.2.235/192.168.2.236);
2台Web Servers(192.168.2.237/192.168.2.238);
注:VIP设置为 192.168.2.229;
二、安装配置Web Server
由于Web Server的安装与配置非常简单,根据自己喜好,安装一个即可;比如:apache、nginx、tomcat等等。在此就不再详述;
三、安装配置Nginx
yum -y install gcc vim lrzsz pcre-devel kernel-devel openssl-devel wget
wget http://nginx.org/download/nginx-1.9.0.tar.gz
tar xzvf nginx-1.9.0.tar.gz
cd nginx-1.9.0
./configure --prefix=/usr/local/nginx --with-debug --with-http_stub_status_module --with-stream --with-http_ssl_module
make;make install
ln -s /usr/local/nginx/sbin/nginx /sbin/
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
编辑nginx主配置文件nginx.conf,内容大致如下:
worker_processes 10;
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"';
sendfile on;
keepalive_timeout 65;
upstream html_pool {
ip_hash;
server 192.168.2.237:80;
server 192.168.2.238:80;
#server 192.168.2.239:80 backup;
#server 192.168.2.240:80 down;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://html_pool; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
注:以上的操作,两台都需要操作;到此,我们的Nginx就安装及配置好了。
四、安装配置Keepalived
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz tar xzvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/
#内核参数,根据不同的操作系统,会不一样,注意更改为您服务器上的内核路径即可;
make
make install
ln -s /usr/local/sbin/keepalived /sbin/
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
编辑keepalive.conf主配置文件,内容大致如下:
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER/BACKUP #前主后备
interface eth0
virtual_router_id 60
priority 100/80 #前主后备
advert_int 3
authentication {
auth_type PASS
auth_pass 33333
}
virtual_ipaddress {
192.168.2.229
}
track_script {
chk_nginx
}
}
因为主配置文件里有用到一个检测nginx状态的脚本,所以另外需要创建脚本,内容如下:
# cat check_ngix.sh
#!/bin/bash
nginx_start=`ps -C nginx --no-header |wc -l`
if [ $nginx_start = 0 ]; then
#/etc/init.d/nginx start
/usr/local/nginx/sbin/nginx
sleep 3
nginx_status=`ps -C nginx --no-header |wc -l`
if [ $nginx_status = 0 ]; then
/etc/init.d/keepalived stop
fi
fi
chmod +x /etc/keepalived/check_nginx.sh
注:同样,以上的所有操作,需要两台服务器上一样操作,注意更改主备及优先级即可;到此,keepalive的相关安装与配置即OK了;
五、校验及测试
启动keepalived(两台):/etc/init.d/keepalived start
在主服务器上随意把nginx或者keepalived服务关闭,观察服务的可用性;
如果飘到备服务器上了,同样,在备服务器上随意把nginx或者keepalived服务关闭,再观察;
最后需要注意的是,如果服务器上有启用安全软件,比如ipatalbes,一定要让服务器间相互访问,不仅仅是ping通;如:iptables -I INPUT -s 192.168.2.0/24 -j ACCEPT
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- 网站502与504错误分析
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 把Lua编译进nginx步骤方法
- web 应用中常用的各种 cache详解
- window+nginx+php环境配置 附配置搭配说明
- LVS+Keepalived构建高可用负载均衡(测试篇)
- 解析CI即CodeIgniter框架在Nginx下的重写规则
- 将PHP从5.3.28升级到5.3.29时Nginx出现502错误
- 基于Nginx0.8.54+PHP5.3.4+MySQL5.5.8的全新LNMP稳定版架构搭建的VPS
- Nginx(PHP/fastcgi)的PATH_INFO问题
- 如何让Nginx支持中文文件名具体设置步骤
- Nginx upstream的5种权重分配方式分享
- 分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置实例)
- nginx禁止某个IP访问站点的设置方法
- 从Nginx切换到Tengine的步骤分享