nginx+keepalived高可用配置

一.环境

应研发需求用两台虚拟机搭建一套nginx+keepalived集群,本篇只做基本配置不进行性能优化。

nginx-1.14.1

keepalived-1.3.5-6

两台centos7虚拟机,192.168.171.8为keepalived主,192.168.171.10为keepalived备

二.nginx

1.安装依赖包

yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.下载安装包

wget http://nginx.org/download/nginx-1.14.1.tar.gz

3.解压并编译安装

tar -zxf nginx-1.14.1.tar.gz
cd nginx-1.14.1
./configure --prefix=/root/nginx   //安装路径随意
make && make install

4.编辑配置文件,进行正、反向代理

##在http{...}中添加下面内容
##upstream
        upstream reverse {
                server 192.168.181.7:80 weight=1;
                server 192.168.171.7:80 weight=1;
}
##reverse proxy 反向代理
        server {
                listen 38080;
                access_log logs/test_reverse_access.log;
                error_log logs/test_reverse_error.log;
                location / {
                        proxy_pass http://reverse;
                        proxy_connect_timeout 60;
                        proxy_send_timeout 60;
                        proxy_read_timeout 60;
        }
}
##forward proxy 正向代理
        server {
                listen 38081;
                #resolver 8.8.8.8;
                resolver 127.0.0.1;
                access_log logs/test_forward_access.log;
                error_log logs/test_forward_error.log;
                location / {
                        proxy_pass http://192.168.171.7:80; //在/etc/hosts中添加了192.168.171.7 192.168.171.7这一行
                        #proxy_pass http://www.baidu.com;
        }
}

5.启动nginx

~/nginx/sbin/nginx

三.keepalived

1.安装

#分别在两台虚拟机上进行安装
yum install -y keepalived

2.修改192.168.171.8上为主的配置文件

#vim /etc/keepalived/keepalived.conf 只留以下内容
global_defs {
   router_id 171_8
}
vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 1
    weight -20
}
vrrp_instance VI_1 {
    state MASTER
    interface eno16777984
    virtual_router_id 51
    priority 100
    vrrp_unicast_bind 192.168.171.8
    vrrp_unicast_peer 192.168.171.10
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.171.44
    }
    track_script {
        chk_nginx
    }
}

3.修改192.168.171.10上为备的配置文件

global_defs {
   router_id 171_10
}
vrrp_script chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 1
    weight -20
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    vrrp_unicast_bind 192.168.171.10
    vrrp_unicast_peer 192.168.171.8
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_nginx
    }
    virtual_ipaddress {
        192.168.171.44
    }
}

4.nginx探测脚本

##两台虚拟机都需要nginx探测脚本,分别放入/etc/keepalived下
#! /bin/bash
if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
/root/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then
        killall keepalived
fi
fi

5.启动

systemctl start keepalived

猜你喜欢

转载自blog.csdn.net/yq90125/article/details/84786810
今日推荐