1. 程式人生 > >nginx+keepalived高可用配置

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