OS:Centos 5.7
kernel:2.6.18-274.el5
主:10.10.8.69
備:10.10.8.70
VIP:10.10.8.71
client1:10.10.8.72
client2:10.10.8.73
一,安裝gcc等環境
二,下載下傳軟體,nginx pcre
nginx:nginx-1.1.11.tar.gz
pcre:pcre-8.21.tar.gz
keepalived-1.1.19.tar.gz
三,安裝pcre
解壓
tar zxvf pcre-8.21.tar.gz
cd pcre-8.21
編譯前的配置:
./configure
配置:
make
安裝:
make install
四:安裝nginx:
解壓:
tar zxv nginx-1.1.11.tar.gz
cd nginx-1.1.11
編譯前的配置
./configure --user=www --group=www --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module
make&&make install
配置Nginx:
vi /usr/local/nginx/conf/nginx.conf
user www;
worker_processes 8;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream; sendfile on;
keepalive_timeout 65;
#gzip on;
upstream birdman1.7road.com {
server 10.10.8.72:80;
server 10.10.8.73:80;
}
server {
listen 80;
server_name birdman1.7road.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
proxy_pass http://birdman1.sosoav.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
location ~ ^/(WEB-INF)/ {
deny all;
} error_page 500 502 503 504 /50x.html;
location = /50x.html {
} }
注:10.10.8.72和10.10.8.73為apache後端伺服器。
五:開啟轉發功能:
sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#' /etc/sysctl.conf
sysctl -p
六,安裝keepalived
tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure --prefix=/usr/local/keepalived
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/
(1)主伺服器配置:
vi /etc/keepalived/keepalived.conf
#################################[MASTER]##############################################
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
#define nginx-master-IP
mcast_src_ip 10.10.8.69 #本機IP
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass jesse
}
#define vip
virtual_ipaddress {
10.10.8.71 #為VIP IP10.10.8.71
}
}
####################################################################################
注:設定master的priority為100
(2)備伺服器上配置:
###################################[BACKUP]###########################################
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
#define nginx-backup-ip
mcast_src_ip 10.10.8.70 #本機IP
priority 99
virtual_ipaddress {
10.10.8.71 #為VIP ip
注:設定backup的priority為99
七,啟動keepalived
啟動keepalived和加入開機自動啟動
/etc/init.d/keepalived start
echo "/etc/init.d/keepalived start" >>/etc/rc.local
指令檢查vrrp是否啟動
[root@birdman1 soft]# ip add
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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:04:d9:b0 brd ff:ff:ff:ff:ff:ff
inet 10.10.8.69/24 brd 10.10.8.255 scope global eth0
inet 10.10.8.71/32 scope global eth0
九:監控nginx:此在
vi /shell/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
then
/etc/init.d/keepalived stop
fi
sleep 5
done
nohup /bin/bash /shell/nginx_pid.sh &
如果你正在運作一個程序,而且你覺得在退出帳戶時該程序還不會結束,
那麼可以使用nohup指令。該指令可以在你退出root帳戶之後繼續運作相應的程序.
10.10.8.72和10.10.8.73為後端apache,省略配置
測試:通路http://birdman1.sosoav.com
由于配置client1和client2的内容不同(便于區分),打開http://birdman1.7road.com後顯示内容為
10.10.8.72或10.10.8.73
假設10.10.8.69(主)的nginx挂掉:
1,停掉主上得nginx:
[root@birdman1 ~]# killall nginx
在backup上檢視:
[root@eklfj-2edbdf6c2 ~]# ip add
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
link/ether 00:0c:29:22:31:e3 brd ff:ff:ff:ff:ff:ff
inet 10.10.8.70/24 brd 10.10.8.255 scope global eth0
inet 10.10.8.71/32 scope global eth0
以上可見,backup伺服器上多了IP10.10.8.71