linux as 5.3 nginx与keepalived做自动切换

有两台机器

mater ip  192.168.1.20

backup ip 192.168.1.22

对外的ip为192.168.10.2

在mater 和backup机器上都安装

nginx

wgetftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz
tar zxvf pcre-7.9.tar.gz
cd pcre-7.7/
./configure --enable-utf8 --enable-pcregrep-libbz2 --enable-pcregrep-libz
make && make install
cd ../

3、编译安装Nginx
wgethttp://sysoev.ru/nginx/nginx-0.7.64.tar.gz
tar zxvf nginx-0.7.64.tar.gz
cd nginx-0.7.64/
./configure --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 ../

 

echo "192.168.1.20" > /var/www/index.html
echo "192.168.1.22" > /var/www/index.html

4、备份默认nginx.conf配置文件
mv /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.old

 

5、配置nginx(两个nginx配置一样!)

vim /usr/local/nginx/conf/nginx.conf
user    www www;
worker_processes    1;
error_log    logs/error.log    notice;
pid                logs/nginx.pid;
events {
        worker_connections    1024;
}
http {
        include             mime.types;
        default_type    application/octet-stream;
        sendfile                on;
        tcp_nopush         on;
        keepalive_timeout    65;
        gzip    on;
        server {
                listen             80;
                server_name    localhost;
                index     index.html index.htm;
                root        /var/www;
                error_page     500 502 503 504    /50x.html;
                location = /50x.html {
                        root     html;
                }
        }
}

 


[root@mylinux keepalived-1.1.19]# vi /opt/nginx_pid.sh
#!/bin/bash
# varsion 0.0.2
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                
                fi
fi
 

keepalive

wgethttp://www.keepalived.org/software/keepalived-1.1.20.tar.gz
tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure --prefix=/usr/local/keepalived

./configure --prefix=/usr/local/keepalived/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.11.3.el5-i686/
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/

vi keepalived.conf

vrrp_script. chk_http_port {
                script. "/opt/nginx_pid.sh"         ###监控脚本
                interval 2                             ###监控时间
                weight 2                                ###目前搞不清楚
}
vrrp_instance VI_1 {
        state MASTER                            ### 设置为 主
        interface eth0                             ### 监控网卡    
        virtual_router_id 51                    ### 这个两台服务器必须一样
        priority 101                                 ### 权重值 MASTRE 一定要高于 BAUCKUP
        authentication {
                     auth_type PASS             ### 加密
                     auth_pass eric                ### 加密的密码,两台服务器一定要一样,不然会出错
        }
        track_script. {
                chk_http_port                     ### 执行监控的服务
        }
        virtual_ipaddress {
             192.168.10.2                          ###    VIP 地址
        }
}

 

在backup

vi keepalived.conf

vrrp_script. chk_http_port {
                script. "/opt/nginx_pid.sh"         ###监控脚本
                interval 2                             ###监控时间
                weight 2                                ###目前搞不清楚
}
vrrp_instance VI_1 {
        state BACKUP                           ### 设置为 主
        interface eth0                             ### 监控网卡    
        virtual_router_id 51                    ### 这个两台服务器必须一样
        priority 100                                ### 权重值 MASTRE 一定要高于 BAUCKUP
        authentication {
                     auth_type PASS             ### 加密
                     auth_pass eric                ### 加密的密码,两台服务器一定要一样,不然会出错
        }
        track_script. {
                chk_http_port                     ### 执行监控的服务
        }
        virtual_ipaddress {
             192.168.10.2                          ###    VIP 地址
        }
}

 

  测试

首先单独对nginx测试看是否工作正常,(更改hosts文件来测试!)

其次对keepalived进行测试,停掉nginx master上的keepalived,看backup服务器是否接管!

你可能感兴趣的:(linux as 5.3 nginx与keepalived做自动切换)