一、前言
nginx進程基于于Master+Slave(worker)多進程模型,自 身具有非常穩定的子進程管理功能。在Master進程分配模式下,Master進程永遠不進行業務處理,只是進行任務分發,從而達到Master進程的存 活高可靠性,Slave(worker)進程所有的業務信號都 由主進程發出,Slave(worker)進程所有的超時任務都會被Master中止,屬于非阻塞式任務模型。
Keepalived是Linux下面實現VRRP 備份路由的高可靠性運行件。基于Keepalived設計的服務模式能夠真正做到主服務器和備份服務器故障時IP瞬間無縫交接。二者結合,可以構架出比較穩定的軟件lb方案。
二、nginx+keepalived架構實戰
采用兩臺服務器做nginx主備,后端采用兩個realserver(可以隨意擴展),數據庫采用mysql主從(這里就不說主從的配置了!)
1、?架構圖如下:

Server? | ip |
nginx master? | 192.168.1.108 |
nginx backup | 192.168.1.110 |
vip | 192.168.1.100 |
real server1 | 192.168.1.105 |
real server2 | 192.168.1.103 |
2、?安裝配置
(1)、安裝keepalived(在nginx的mater和backup都安裝)
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./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
cd /etc/keepalived/
tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./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
cd /etc/keepalived/
配置nginx master的keepalived配置文件
[root@zhang1 keepalived]# cat /etc/keepalived/keepalived.conf
bal_defs {
notification_email {
jimo291@gmail.com
}
notification_email_from jimo291@gmail.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id test1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
smtp_alert
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.1.100
}
}
配置nginx backup的keepalived配置文件
[root@zhang1 keepalived]# cat /etc/keepalived/keepalived.conf
bal_defs {
notification_email {
jimo291@gmail.com
}
notification_email_from jimo291@gmail.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id test2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 80
advert_int 1
smtp_alert
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.1.100
}
}
啟動keepalived!,查看虛擬IP是否綁定!
[root@zhang1 ~]# /etc/rc.d/init.d/keepalived start
Starting keepalived:?????????????????????????????????????? [?OK?]
[root@zhang1 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:65:b4:ed brd ff:ff:ff:ff:ff:ff
inet 192.168.1.108/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.100/32 scope global eth0
inet6 fe80::20c:29ff:fe65:b4ed/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:65:b4:f7 brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
紅色部分顯示IP已經加載過來了!
3、?nginx的安裝和配置
1、創建供Nginx使用的組和帳號:
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
2、編譯安裝rewrite模塊支持包
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure
make && make install
cd ../
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure
make && make install
cd ../
3、編譯安裝Nginx
wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz
tar zxvf nginx-0.7.64.tar.gz
cd nginx-0.7.64/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module
make && make install
cd ../
wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz
tar zxvf nginx-0.7.64.tar.gz
cd nginx-0.7.64/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module
make && make install
cd ../
4、備份默認nginx.conf配置文件
mv /usr/local/nginx/conf/nginx.conf? /usr/local/nginx/conf/nginx.old
mv /usr/local/nginx/conf/nginx.conf? /usr/local/nginx/conf/nginx.old
5、配置nginx(兩個nginx配置一樣!)
[root@linux4 keepalived]# cat /usr/local/nginx/conf/nginx.conf
userwww???? www;
worker_processes 8;
pid/var/run/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections????? 5120;
}
http
{
include?????????? mime.types;
default_type?application/octet-stream;
charset?gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush???????? on;
keepalive_timeout 60;
tcp_nodelay on;
gzip on;
gzip_min_length?1k;
gzip_buffers???????? 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types?????????? text/plain application/x-javascript text/css application/xml;
gzip_vary on;
upstream srtweb {
server?192.168.1.105:80;
server?192.168.1.103:80;
}
server {
listen 80;
server_name www.carl.com;
location /{
proxy_pass????? http://srtweb;
proxy_set_header Host?? $host;
proxy_set_header X-Real-IP????? $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
4、?測試
首先單獨對nginx測試看是否工作正常,(更改hosts文件來測試!)
其次對keepalived進行測試,停掉nginx master上的keepalived,看backup服務器是否接管!
本文出自 “第三方” 博客,請務必保留此出處http://2526575.blog.51cto.com/2516575/592218