Eaglebaochenggood.blog.chinaunix.net

我一直在走,至少还有条路,也好于无路可走…

  • 博客访问: 831020
  • 博文数量: 165
  • 博客积分: 1440
  • 博客等级: 少尉
  • 技术积分: 2921
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 23:47
  • 认证徽章:
个人简介

我一直在走,至少还有条路,也好于无路可走…

文章分类

全部博文(165)

文章存档

2017年(2)

2016年(5)

2015年(13)

2014年(17)

2013年(76)

2012年(51)

微信关注

IT168企业级官微



微信号:IT168qiye



系统架构师大会



微信号:SACC2013

订阅
热词专题

分类: LINUX

一。环境

VIP :192.168.10.8
server1 eth0 192.168.10.5
server2 eth0 192.168.10.2
系统环境:centos6.4 32位 (两台)

二。软件下载:

wget http://nginx.org/download/nginx-1.4.7.tar.gz 
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.34.tar.gz 
wget http://www.keepalived.org/software/keepalived-1.2.12.tar.gz
  yum install -y make apr* autoconf automake curl curl-devel gcc gcc-c++ gtk+-devel zlib-devel 
用SecureCRT 同时连上两台服务器, 右击下面的工具栏,选择“send chat to all sessions ”同时向两个终端发送命令,同时操作两台机器,提高工作效率。
三。两台服务器,同时执行以下命令:
tar -zxvf pcre-8.34.tar.gz
./configure 
 make
 make install

tar -zxvf nginx-1.4.7.tar.gz 
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/home/source/pcre-8.34
make
make install 

启动nginx服务
 /usr/local/nginx/sbin/nginx  -s start
vi /etc/rc.d/init.d/nginx  #添加启动脚本
#!/bin/sh
# nginx - this script starts and stops the nginx daemon
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
sleep 1
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
       
chmod 755 /etc/rc.d/init.d/nginx 
/etc/init.d/nginx start
 chkconfig  --add nginx
chkconfig  nginx on

四。安装配置keepalived

tar -zxvf  keepalived-1.2.12.tar.gz
cd  keepalived-1.2.12
./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/
                                                                          BACK  keepalived
 vim /etc/keepalived/keepalived.conf 
vrrp_script chk_http_port {
                script "/opt/nginx_pid.sh"
                interval 2
                weight 2
            }
bal_defs {
        notification_email {
               1032231418@qq.com
        }
        notification_email_from  1032231418@qq.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
         mcast_src_ip  192.168.10.2
        priority  80
        advert_int 1
        smtp_alert
        authentication {
                auth_type PASS
                auth_pass 123
        }
        virtual_ipaddress {
                192.168.10.8
        }

                                                       MAST    keepalived 
               
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
vrrp_script chk_http_port {
                script "/opt/nginx_pid.sh"
                interval 2
                weight 2
            }
bal_defs {
        notification_email {
               1032231418@qq.com
        }
        notification_email_from  1032231418@qq.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
         mcast_src_ip 192.168.10.5
        priority 100
        advert_int 1
        smtp_alert
        authentication {
                auth_type PASS
                auth_pass 123
        }
        virtual_ipaddress {
                192.168.10.8
        }
[root@localhost ~]# cat /opt/nginx_pid.sh                     监控脚本可以添加到计划任务里面*/1 * * * *   /opt/nginx_pid.sh
#!/bin/bash 
#version 0.0.1 

A=`ps -C nginx --no-header |wc -l` 
if [ $A -eq 0 ];then 
      /usr/local/nginx/sbin/nginx
              sleep 3 
                if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then 
#                       killall keepalived 
                        service  keepalived stop 
fi 
fi 
查看vip是否启动
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    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 state UP qlen 1000
    link/ether 00:e0:81:bb:c2:bb brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.5/24 brd 192.168.10.255 scope global eth0
    inet 192.168.10.8/32 scope global eth0
    inet6 fe80::2e0:81ff:febb:c2bb/64 scope link 
       valid_lft forever preferred_lft forever

五。首先单独对nginx测试看是否工作正常,其次对keepalived进行测试,停掉nginx master上的keepalived,看backup服务器是否接管!

阅读(1235) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册