首先理解上图:一个高访问量的站点(每天100万PV),可以使用以上的服务器组成配置。该站点总共由7台服务器组成,图片文件服务器负责整个站点的图片存储(当然高访问量的站点当然需要cnd图片加速了),两台数据库服务器形成主从备份保证了数据运算的速度,同
首先理解上图:一个高访问量的站点(每天100万PV),可以使用以上的服务器组成配置。该站点总共由7台服务器组成,图片文件服务器负责整个站点的图片存储(当然高访问量的站点当然需要cnd图片加速了),两台数据库服务器形成主从备份保证了数据运算的速度,同时保证了当其中一台服务器宕机后,还有一台数据库服务器保证系统的运行,两台web服务器保证了系统的快速响应请求,同时其中一台web服务器宕机后还有一个web服务器提供服务。两台
nginx代理服务负责转发用户的请求,问题是,两台
nginx有两个ip,怎么才能保证一台nginx服务器宕机后,另一台服务器自动接手服务呢?keepalived提供的VRRP虚拟路由服务就可以解决这个问题。
一、安装必要的软件包,以保证nginx和keepalived能安装:
yum -y install gcc gcc-c++ make
yum -y install
wget
#安装下载工具
yum -y install
pcre-devel
#安装nginx的依赖软件包
yum -y install
openssl-devel
#安装keepalived依赖软件包
yum -y install popt-devel
#安装keepalived依赖软件包
二、安装nginx
wget http://nginx.org/download/nginx-1.2.8.tar.gz
#下载nginx
tar -zxf nginx-1.2.8.tar.gz
#解压nginx
cd nginx-1.2.8
groupadd www
#添加www用户组
useradd -g www www
#新添加www用户,且用户组隶属于www用户组
./configure --user=www --group=www --prefix=/usr/local/nginx
--with-http_stub_status_module --with-http_ssl_module
make && make install
#编译,--with-http_stub_status_module
--with-http_ssl_module这两个模块必须开启
编写nginx启动脚本
cat /etc/init.d/nginx
#!/bin/bash
#
# chkconfig: - 85 15
# description: Nginx is a World Wide Web server.
# processname: nginx
nginx=/usr/local/nginx/sbin/nginx #nginx执行路径
cOnf=/usr/local/nginx/conf/nginx.conf
#nginx配置文件路径
case \$1 in
start)
echo -n "Starting Nginx"
\$nginx -c \$conf
echo " done"
;;
stop)
echo -n "Stopping Nginx"
killall -9 nginx
echo " done"
;;
test)
\$nginx -t -c \$conf
;;
reload)
echo -n "Reloading Nginx"
ps auxww | grep nginx | grep master | awk
'{print \$2}' | xargs kill -HUP
echo " done"
;;
restart)
killall -9 nginx
echo -n "Stopping Nginx"
\$nginx -c \$conf
echo -n "Starting Nginx"
echo " done"
;;
show)
ps -aux|grep nginx
;;
*)
echo -n "Usage: \$0
{start|restart|reload|stop|test|show}"
;;
esac
EOF
chmod +x /etc/init.d/nginx
service nginx start
chkconfig nginx on
service nginx start
在浏览器浏览你的nginx看看是否启动。
三、安装keepalived-1.2.0
wget http://keepalived.org/software/keepalived-1.2.0.tar.gz
tar -zxf keepalived-1.2.0.tar.gz
cd keepalived-1.2.0
./configure --prefix=/usr/local/keepalived
make && make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived
/etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived
/etc/init.d/
mkdir /etc/keepalived
编写keepalived的配置
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script chk_http_port {
script "/etc/keepalived/nginx_pid.sh"
#nginx监控脚本路径,改脚本可以把死掉的nginx进程重新启动。
interval 2
weight 2
}
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
#状态,位主,从机使用BACKUP
interface eth0
#把那块网卡映射寻ip
virtual_router_id 51
#虚拟路由id号,这个和从keepalived机子一样。
priority 100
#权重,那个权重高,那个就是主的,所以MASTER的权重必须高于BACKUP
advert_int 1
authentication {
auth_type PASS
#主从机子通信的加密方式,两台必须一样
auth_pass 123456
#主从机子通信的加密密码,两台必须一样
}
track_script {
chk_http_port
#监控脚本,chk_http_port对应的是第3行的vrrp_script
chk_http_port,
}
virtual_ipaddress {
192.168.1.250
#虚拟IP
}
EOF
service keepalived start
ip addr
看看是否有以下内容:如果内看到以下内容就表明你keepalived配置正确了,然后你用192.168.137.120这个虚拟ip是否和192.168.137.110这个ip看到的是一样的,如果是一样就表明可以了
编写nginx的监控脚本:
cat /etc/keepalived/nginx_pid.sh
#!/bin/bash
while :
do
nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 5
if [ $nginxpid -eq 0
];then
/etc/init.d/keepalived stop
fi
fi
sleep 5
done
EOF
四、经过以上的配置,虚拟路由是可以启动了,当主虚拟路由机宕机后,从虚拟路由机后接手。下面来配置nginx的转发代理。
nginx的配置如下:
user www www;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
client_max_body_size 300m;
sendfile
on;
tcp_nopush
on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_temp_file_write_size 128k;
keepalive_timeout 90;
tcp_nodelay
on;
server_tokens
off;
gzip
on;
gzip_min_length 1k;
gzip_http_version
1.1;
gzip_comp_level
2;
gzip_types text/plain application/x-Javascript text/css
application/xml;
gzip_vary
on;
include vhost/*.conf;
}