Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10606
  • 博文数量: 3
  • 博客积分: 137
  • 博客等级: 入伍新兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-13 13:47
文章分类
文章存档

2012年(3)

我的朋友

分类: LINUX

2012-08-31 18:13:18

Enviroment

NginxServer01 192.168.2.165/24
NginxServer02 192.168.2.166/24
Virtual_IP_01 192.168.2.168/24
Virtual_IP_02 192.168.2.169/24
Client(For test) 192.168.2.100/24

1 Install nginx on NginxServer01 and NginxServer02, config nginx.conf and iptables to make sure nginx is OK.

omit

NginxServer01:

vhost                     abc.com
/etc/hosts                192.168.2.165 abc.com

NginxServer02:

vhost                     abc.com
/etc/hosts                192.168.2.166 abc.com

2 Install keepalived

cd /usr/local/src
tar zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived
make
make install

3 config keepalived as a service, it will be avaliable to control 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/

service keepalived {start|stop|reload|restart|condrestart|status}

4 mode1: one master + one backup (use Virtual_IP_01 192.168.2.168 )

mkdir /etc/keepalived
cd /etc/keepalived
ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf keepalived.conf
vim /etc/keepalived/keepalived.conf

NginxServer01---keepalived.conf  (MASTER)
! Configuration File for keepalived
global_defs {
    notification_email {
       

          }
    notification_email_from Alexandre.Cassen@firewall.loc
    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
    mcast_src_ip 192.168.2.165
#NginxServer01’s IP 
    priority 100               
#master’s priority should large than backup’s 
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
        }
    virtual_ipaddress {
        192.168.2.168         
#Virtual_IP_01 192.168.2.168 
        }
}
NginxServer02---keepalived.conf  (BACKUP)
! Configuration File for keepalived
global_defs {
    notification_email {
       

          }
    notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0 
    virtual_router_id 51
    mcast_src_ip 192.168.2.166
#NginxServer02’s IP 
    priority 90                #master’s priority should large than backup’s 
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
        }
    virtual_ipaddress {
        192.168.2.168         
#Virtual_IP_01 192.168.2.168
        }
}

service keepalived start

5 use nginx_pid.sh to monitor nginx’s process

vim /opt/nginx_pid.sh

#!/bin/bash
while  :
do
    nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ];then
  /usr/local/webserver/nginx/sbin/nginx -c /usr/local/webserver/nginx/conf/nginx.conf
  sleep 5
  nginxpid=`ps -C nginx --no-header | wc -l`
  echo $nginxpid
          if [ $nginxpid -eq 0 ];then
          /etc/init.d/keepalived stop
         fi
fi
sleep 5
done

run nginx_pid.sh

cd /opt/
nohup /bin/bash /opt/nginx_pid.sh &
 

tail –f /opt/nohup.out 

check whether the Virtual IP has worked on the MASTER nginx server

ip addr

shutdown the nginx of NginxServer01, check whether the Virtual IP starts on the BACKUP nginx server

6 test by a third computer

6.1 edit /etc/hosts, add

192.168.2.168 abc.com

6.2 access abc.com, use url =

6.3 shutdown NginxServer01, still can be access

6.3 start NginxServer01, shutdown NginxServer02, is still ok

another model

NginxServer01---keepalived.conf  (MASTER 192.168.2.168; BACKUP 192.168.2.169)

! Configuration File for keepalived
global_defs {
    notification_email {
  acassen@firewall.loc
          }
  notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
  smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth4
    virtual_router_id 51
    mcast_src_ip 192.168.2.165
#此处是Nginx1服务器的IP!
    priority 100
#Nginx1服务器的优先级!数值越大,优先级越高!
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
        }
    virtual_ipaddress {
        192.168.2.168
#此处为virtual IP1!
        }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth4
    virtual_router_id 52
    mcast_src_ip 192.168.2.165
#此处是Nginx1服务器的IP!
    priority 90
#Nginx1服务器的优先级!数值越大,优先级越高!
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
        }
    virtual_ipaddress {
        192.168.2.169
#此处为virtual IP2!
        }
}

 
NginxServer02---keepalived.conf  (BACKUP 192.168.2.168; MASTER 192.168.2.169)

! Configuration File for keepalived
global_defs {
    notification_email {
  acassen@firewall.loc
          }
  notification_email_from Alexandre.Cassen@firewall.loc
    smtp_server 127.0.0.1
  smtp_connect_timeout 30
    router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth5
    virtual_router_id 51
    mcast_src_ip 192.168.2.166
#此处是Nginx2服务器的IP!
    priority 90
#Nginx2服务器的优先级!数值越大,优先级越高!
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
        }
    virtual_ipaddress {
        192.168.2.168
#此处为virtual IP1!
        }
}

vrrp_instance VI_2 {
    state MASTER
    interface eth5
    virtual_router_id 52
    mcast_src_ip 192.168.2.166
#此处是Nginx2服务器的IP!
    priority 100
#Nginx2服务器的优先级!数值越大,优先级越高!
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass chtopnet
        }
    virtual_ipaddress {
        192.168.2.169
#此处为virtual IP2!
        }
}

 

阅读(512) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~