Nginx-keepalived高可用負載均衡

Nginx-keepalived高可用負載均衡


架構
keepalive1:10.1.41.60
keepalive2:10.1.41.61
vrrp:10.1.41.88
nginx1:10.1.41.64
nginx2:10.1.41.65


系統版本
#cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core)


實驗目的:用keepalived實現高可用,用nginx的upstream模塊反向代理後端的web服務器實現負載均衡


keepalvied安裝與配置


安裝前的準備
ln -sv /usr/src/kernels/3.10.0-327.el7.x86_64/ /usr/src/linux
從官網下載keepalived的最新版本,本文以當前最新版本1.3.0爲例。




編譯安裝keepalived
wget http://www.keepalived.org/software/keepalived-1.3.0.tar.gz
tar zxf keepalived-1.3.0.tar.gz  -C  /usr/local
cd  /usr/local/keepalived-1.3.0/
yum install openssl-devel -y
./configure  --prefix=/usr/local/keepalived --sbindir=/usr/sbin --sysconfdir=/etc
make -j4 && make install 




    
在Centos6.4版本以後都可以直接yum安裝keepalived
   
yum install keepalived -y




配置文件的修改




vim /etc/keepalived/keepalived.conf
註釋掉佔時用不到的配置
.,$s/^/#/g  從當前行到結尾的所有行首加上#註釋掉
set nohlsearch




在 keepalive1:10.1.41.60 配置


vim /etc/keepalived/keepalived.conf




! Configuration File for keepalived




global_defs {
   router_id lvs_60
   vrrp_mcast_group4 224.0.0.18
}




vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 90
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass uRINKmfBsok=
    }
    virtual_ipaddress {
        10.1.41.88/24
    }
}








在 keepalive2:10.1.41.61 配置




! Configuration File for keepalived




global_defs {
   router_id lvs61
   vrrp_mcast_group4 224.0.0.18
}




vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 90
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass uRINKmfBsok=
    }
    virtual_ipaddress {
        10.1.41.100/24
    }
}










配置完成後主副節點都啓動keepalived服務
systemctl start keepalived






在keepalive1和keepalive2都安裝nginx




nginx版本:nginx-1.8.1.


yum install gcc openssl-devel pcre-devel zlib-devel  mod_ssl  -y 
獲取nginx編譯安裝包


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


或者直接到網站進行下載 http://nginx.org/en/download.html


# tar xf nginx-1.8.1.tar.gz 


# cd nginx-1.8.1
# groupadd -r nginx
# useradd -r -g nginx -s /sbin/nologin -M nginx


參考官方網站http://nginx.org/en/linux_packages.html#stable 進行編譯安裝 




./configure \--prefix=/etc/nginx \--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.pid \--lock-path=/var/run/nginx.lock \--http-client-body-temp-path=/var/cache/nginx/client_temp \--http-proxy-temp-path=/var/cache/nginx/proxy_temp \--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \--http-scgi-temp-path=/var/cache/nginx/scgi_temp \--user=nginx \--group=nginx \--with-http_ssl_module \--with-http_realip_module \--with-http_addition_module \--with-http_sub_module \--with-http_dav_module \--with-http_flv_module \--with-http_mp4_module \--with-http_gunzip_module \--with-http_gzip_static_module \--with-http_random_index_module \--with-http_secure_link_module \--with-http_stub_status_module \--with-http_auth_request_module \--with-threads






make -j4 && make install




參考官方網站進行nginx腳步編寫 


https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/




Red Hat NGINX Init Script


Red Hat Nginx Init Script Should work on RHEL, Fedora, CentOS. Tested on CentOS 5. 




Save this file as /etc/init.d/nginx 






vim /etc/init.d/nginx


#!/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/rc.d/init.d/nginx


# chmod 755 /etc/init.d/nginx
添加至服務管理列表,並讓其開機自動啓動:
# chkconfig --add nginx
# chkconfig nginx on




而後就可以啓動服務並測試了:
# service nginx start




在keepalive1和keepalive2都進行nginx配置文件的更改,要修改的部分如下


#vim /etc/nginx/nginx.conf


upstream myserver {
       server 10.1.41.64:80 weight=5;
       server 10.1.41.65:80 weight=5;
    }


    server {
        listen       80;
        server_name  localhost;
 
        #charset koi8-r;


        #access_log  logs/host.access.log  main;
        location / {
             proxy_pass http://myserver;
        }




nginx配置文件更改後,檢查配置文件語法
        
nginx -t


對nginx進行優雅重啓


nginx -s reload


把nginx1和nginx2安裝好nginx後分別配置默認網頁


nginx1:10.1.41.64  默認網頁 nginx 41.64
nginx2:10.1.41.65  默認網頁 nginx 41.65




測試結果如下


root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65


在keepalive1(10.1.41.60)關閉keepalived
#systemctl stop keepalived
vrrp(虛擬IP)會自動偏移到keepalive2(10.1.41.61)
下面是在keepalive2上看到的日誌
cat /var/log/messages
Mar  7 22:14:13 centos7 Keepalived_vrrp[7494]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens160 for 10.1.41.88
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:14 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:14 centos7 Keepalived_healthcheckers[7493]: Netlink reflector reports IP 10.1.41.88 added
Mar  7 22:14:19 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:19 centos7 Keepalived_vrrp[7494]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens160 for 10.1.41.88
Mar  7 22:14:19 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:19 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:19 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88
Mar  7 22:14:19 centos7 Keepalived_vrrp[7494]: Sending gratuitous ARP on ens160 for 10.1.41.88


這時我們在進行訪問測試
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64
[root@mynginx ~]# curl 10.1.41.88
nginx 41.65
[root@mynginx ~]# curl 10.1.41.88
nginx 41.64


OK 這樣簡單的nginx-keepalived高可用負載均衡就實現了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章