(负载均衡)nginx+keepalive

yum -y install gcc pcre-devel zlib-devel openssl-devel popt-devel
wget http://nginx.org/download/nginx-1.8.0.tar.gz 
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
tar zxvf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local
make&&make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
echo "/etc/init.d/keepalived start" >> /etc/rc.local

Nginx配置文件

http {
    upstream imgserver{
        ip_hash;
        server 183.131.54.25:80 weight=1 max_fails=2 fail_timeout=3;
        server 183.131.54.26:80 weight=1 max_fails=2 fail_timeout=3;
    }
    server {
        location /
        {
           proxy_pass http://imgserver;
        }
    }
}    

keepalived配置文件

yum -y install nmap
vi /opt/chk_nginx.sh
#!/bin/bash
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
#echo 'date' >> 1.txt
nmap localhost -p $PORT | grep "$PORT/tcp open"
#echo $?
if [ $? -ne 0 ];then
    $NGINX -s stop
    $NGINX
    sleep 3
    nmap localhost -p $PORT | grep "$PORT/tcp open"
    [ $? -ne 0 ] && /etc/init.d/keepalived stop
fi
vi /etc/keepalived/keepalived.conf
主服务器配置完后运行
service keepalived start开启主查看ip addr是否绑定主服务器是否接管 
service keepalived status查看是否运行 
service keepalived stop关闭主查看ip addr是否绑定从服务器是否接管

! Configuration File for keepalived

global_defs {
   router_id NGINX_UPSTREAM
}

vrrp_script chk_http_port {
    script "/opt/chk_nginx.sh"
    interval 1
    weight -2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

track_script {
    chk_http_port
}
从服务器配置完后运行service keepalived start
! Configuration File for keepalived

global_defs {
   router_id NGINX_UPSTREAM
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98 #每个从服务器数字要改变
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值