主頁(yè) > 知識(shí)庫(kù) > Nginx實(shí)現(xiàn)高可用集群構(gòu)建(Keepalived+Haproxy+Nginx)

Nginx實(shí)現(xiàn)高可用集群構(gòu)建(Keepalived+Haproxy+Nginx)

熱門(mén)標(biāo)簽:打400電話怎么辦理收費(fèi) 河南防封號(hào)電銷機(jī)器人是什么 10086外包用的什么外呼系統(tǒng) 宿城區(qū)電話機(jī)器人找哪家 福州企業(yè)電銷機(jī)器人排名 怎么找到?jīng)]有地圖標(biāo)注的店 上海申請(qǐng)高400開(kāi)頭的電話 400電話辦理介紹信 麗江真人語(yǔ)音電話外呼系統(tǒng)

1、組件及實(shí)現(xiàn)的功能

Keepalived:實(shí)現(xiàn)對(duì)Haproxy服務(wù)的高可用,并采用雙主模型配置;

Haproxy:實(shí)現(xiàn)對(duì)Nginx的負(fù)載均衡和讀寫(xiě)分離;

Nginx:實(shí)現(xiàn)對(duì)HTTP請(qǐng)求的高速處理;

2、架構(gòu)設(shè)計(jì)圖

3、Keepalived部署

在兩個(gè)節(jié)點(diǎn)上都需要執(zhí)行安裝keepalived,命令如下:

$ yum -y install keepalived

 修改 172.16.25.109 節(jié)點(diǎn)上 keepalived.conf 文件配置,命令如下

$ vim /etc/keepalived/keepalived.conf

修改后的內(nèi)容如下:

! Configuration File for keepalived
global_defs {
   notification_email {
         root@localhost
   }
   notification_email_from admin@lnmmp.com
   smtp_connect_timeout 3
   smtp_server 127.0.0.1
   router_id LVS_DEVEL
}
vrrp_script chk_maintaince_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 1
   weight 2
}
vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 1
    weight 2
}
vrrp_instance VI_1 {
    interface eth0
    state MASTER
    priority 100
    virtual_router_id 125
    garp_master_delay 1
    authentication {
        auth_type PASS
        auth_pass 1e3459f77aba4ded
    }
    track_interface {
       eth0
    }
    virtual_ipaddress {
        172.16.25.10/16 dev eth0 label eth0:0
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master 172.16.25.10"
    notify_backup "/etc/keepalived/notify.sh backup 172.16.25.10"
    notify_fault "/etc/keepalived/notify.sh fault 172.16.25.10"
}
vrrp_instance VI_2 {
    interface eth0
    state BACKUP
    priority 99
    virtual_router_id 126
    garp_master_delay 1
    authentication {
        auth_type PASS
        auth_pass 7615c4b7f518cede
    }
    track_interface {
       eth0
    }
    virtual_ipaddress {
        172.16.25.11/16 dev eth0 label eth0:1
    }
    track_script {
        chk_haproxy
chk_maintaince_down
    }
    notify_master "/etc/keepalived/notify.sh master 172.16.25.11"
    notify_backup "/etc/keepalived/notify.sh backup 172.16.25.11"
    notify_fault "/etc/keepalived/notify.sh fault 172.16.25.11"
}

同理修改 172.16.25.110 節(jié)點(diǎn)上 keepalived.conf 配置,內(nèi)容如下:

! Configuration File for keepalived
global_defs {
   notification_email {
         root@localhost
   }
   notification_email_from admin@lnmmp.com
   smtp_connect_timeout 3
   smtp_server 127.0.0.1
   router_id LVS_DEVEL
}
vrrp_script chk_maintaince_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 1
   weight 2
}
vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 1
    weight 2
}
vrrp_instance VI_1 {
    interface eth0
    state BACKUP
    priority 99
    virtual_router_id 125
    garp_master_delay 1
    authentication {
        auth_type PASS
        auth_pass 1e3459f77aba4ded
    }
    track_interface {
       eth0
    }
    virtual_ipaddress {
        172.16.25.10/16 dev eth0 label eth0:0
    }
    track_script {
        chk_haproxy
chk_maintaince_down
    }
    notify_master "/etc/keepalived/notify.sh master 172.16.25.10"
    notify_backup "/etc/keepalived/notify.sh backup 172.16.25.10"
    notify_fault "/etc/keepalived/notify.sh fault 172.16.25.10"
}
vrrp_instance VI_2 {
    interface eth0
    state MASTER
    priority 100
    virtual_router_id 126
    garp_master_delay 1
    authentication {
        auth_type PASS
        auth_pass 7615c4b7f518cede
    }
    track_interface {
       eth0
    }
    virtual_ipaddress {
        172.16.25.11/16 dev eth0 label eth0:1
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master 172.16.25.11"
    notify_backup "/etc/keepalived/notify.sh backup 172.16.25.11"
    notify_fault "/etc/keepalived/notify.sh fault 172.16.25.11"
}
# vi /etc/keepalived/notify.sh
#!/bin/bash
# Author: Jason.Yu <admin@lnmmp.com>
# description: An example of notify script
#
contact='root@localhost'
notify() {
    mailsubject="`hostname` to be $1: $2 floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
case "$1" in
    master)
        notify master $2
        /etc/rc.d/init.d/haproxy restart
        exit 0
    ;;
    backup)
        notify backup $2 # 在節(jié)點(diǎn)切換成backup狀態(tài)時(shí),無(wú)需刻意停止haproxy服務(wù),防止chk_maintaince和chk_haproxy多次對(duì)haproxy服務(wù)操作;
        exit 0
    ;;
    fault)
        notify fault $2 # 同上
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac

在兩個(gè)節(jié)點(diǎn)上執(zhí)行 keepalived 啟動(dòng)命令,命令如下:

  $ service keepalived start

4、Haproxy部署

在兩個(gè)節(jié)點(diǎn)上都需要執(zhí)行安裝 HAProxy,命令如下:

 $ yum -y install haproxy

修改 172.16.25.109 和 172.16.25.110 節(jié)點(diǎn)上 haproxy.cfg 文件配置(兩節(jié)點(diǎn)配置文件內(nèi)容一致),命令如下:

 $ vim /etc/haproxy/haproxy.cfg

配置文件內(nèi)容如下:

global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user         haproxy
    group       haproxy
    daemon # 以后臺(tái)程序運(yùn)行;
defaults
    mode                   http # 選擇HTTP模式,即可進(jìn)行7層過(guò)濾;
    log                     global
    option                  httplog # 可以得到更加豐富的日志輸出;
    option                  dontlognull
    option http-server-close # server端可關(guān)閉HTTP連接的功能;
    option forwardfor except 127.0.0.0/8 # 傳遞client端的IP地址給server端,并寫(xiě)入“X-Forward_for”首部中;
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 30000
listen stats
    mode http
    bind 0.0.0.0:1080 # 統(tǒng)計(jì)頁(yè)面綁定1080端口;
    stats enable # 開(kāi)啟統(tǒng)計(jì)頁(yè)面功能;
    stats hide-version # 隱藏Haproxy版本號(hào);
    stats uri     /haproxyadmin?stats # 自定義統(tǒng)計(jì)頁(yè)面的訪問(wèn)uri;
    stats realm   Haproxy\ Statistics # 統(tǒng)計(jì)頁(yè)面密碼驗(yàn)證時(shí)的提示信息;
    stats auth    admin:admin # 為統(tǒng)計(jì)頁(yè)面開(kāi)啟登錄驗(yàn)證功能;
    stats admin if TRUE # 若登錄用戶驗(yàn)證通過(guò),則賦予管理功能;
frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    acl url_static       path_beg       -i /static /p_w_picpaths /javascript /stylesheets
    acl url_static       path_end       -i .jpg .jpeg .gif .png .css .js .html
    use_backend static_servers if url_static # 符合ACL規(guī)則的,請(qǐng)求轉(zhuǎn)入后端靜態(tài)服務(wù)器
    default_backend dynamic_servers # 默認(rèn)請(qǐng)求轉(zhuǎn)入后端動(dòng)態(tài)服務(wù)器
backend static_servers
    balance roundrobin
    server imgsrv1 192.168.0.25:80 check maxconn 6000 # 靜態(tài)服務(wù)器,可配置多臺(tái),還可設(shè)置權(quán)重weight;
backend dynamic_servers
    balance source # 對(duì)于動(dòng)態(tài)請(qǐng)求利用source調(diào)度算法,可一定程度上實(shí)現(xiàn)session保持;但最好利用cookie綁定的方式實(shí)現(xiàn)session保持
    server websrv1 192.168.0.35:80 check maxconn 1000 # 動(dòng)態(tài)服務(wù)器,可配置多臺(tái),還可設(shè)置權(quán)重weight;

兩個(gè)節(jié)點(diǎn)執(zhí)行啟動(dòng)服務(wù),命令如下:

$ service haproxy start

5、Nginx部署

yum -y groupinstall “Development tools”
yum -y groupinstall “Server Platform Development”
yum install gcc openssl-devel pcre-devel zlib-devel
groupadd -r nginx
useradd -r -g nginx -s /sbin/nologin -M nginx
tar xf nginx-1.4.7.tar.gz
cd nginx-1.4.7
mkdir -pv /var/tmp/nginx
./configure \

  --prefix=/usr \

  --sbin-path=/usr/sbin/nginx \

  --conf-path=/etc/nginx/nginx.conf \

  --error-log-path=/var/log/nginx/error.log \

  --http-log-path=/var/log/nginx/access.log \

  --pid-path=/var/run/nginx/nginx.pid  \

  --lock-path=/var/lock/nginx.lock \

  --user=nginx \

  --group=nginx \

  --with-http_ssl_module \

  --with-http_flv_module \

  --with-http_stub_status_module \

  --with-http_gzip_static_module \

  --http-client-body-temp-path=/var/tmp/nginx/client/ \

  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

  --http-scgi-temp-path=/var/tmp/nginx/scgi \

  --with-pcre
make && make install

配置服務(wù)腳本

vi /etc/init.d/nginx # 配置服務(wù)腳本
#!/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:      /etc/sysconfig/nginx
# pidfile:     /var/run/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/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' -`
   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 +x /etc/init.d/nginx # 復(fù)***務(wù)腳本執(zhí)行權(quán)限
vi /etc/nginx/nginx.conf # 編輯主配置文件
worker_processes  2;
error_log  /var/log/nginx/nginx.error.log;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  xxrenzhe.lnmmp.com;
        access_log  /var/log/nginx/nginx.access.log  main;
        location / {
            root   /www/lnmmp.com;
            index  index.php index.html index.htm;
        }
        error_page  404              /404.html;
        error_page  500 502 503 504  /50x.html;
        location = /50x.html {
            root   /www/lnmmp.com;
        }
        location ~ \.php$ {
            root           /www/lnmmp.com;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
vi /etc/nginx/fastcgi_params # 編輯fastcgi參數(shù)文件
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

啟動(dòng)服務(wù)

service nginx configtest # 服務(wù)啟動(dòng)前先驗(yàn)證配置文件是否正確
service nginx start
ps -ef |grep nginx # 檢查nginx進(jìn)程,尤其是worker進(jìn)程是否與worker_processes值一致
ss -antupl |grep 80 # 檢查服務(wù)端口是否啟動(dòng)

6、訪問(wèn)驗(yàn)證

Haproxy 統(tǒng)計(jì)頁(yè)面測(cè)試

動(dòng)靜分離測(cè)試

高可用測(cè)試

到此 Nginx高可用集群構(gòu)建(Keepalived+Haproxy+Nginx)介紹完成。

到此這篇關(guān)于Nginx實(shí)現(xiàn)高可用集群構(gòu)建(Keepalived+Haproxy+Nginx)的文章就介紹到這了,更多相關(guān)Nginx 高可用集群內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標(biāo)簽:朝陽(yáng) 荊門(mén) 運(yùn)城 面試通知 隴南 遵義 雞西 連云港

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Nginx實(shí)現(xiàn)高可用集群構(gòu)建(Keepalived+Haproxy+Nginx)》,本文關(guān)鍵詞  Nginx,實(shí)現(xiàn),高可用,高,可用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Nginx實(shí)現(xiàn)高可用集群構(gòu)建(Keepalived+Haproxy+Nginx)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Nginx實(shí)現(xiàn)高可用集群構(gòu)建(Keepalived+Haproxy+Nginx)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章