keepalived+Nginx高可用負載均衡

說明:準備兩臺虛擬機:202.207.178.6和202.207.178.7html

前提:node

因爲是高可用服務,首先配置高可用服務實現的一些基本條件nginx

1)節點名稱必須跟uname -n命令的執行結果一致vim

node1:服務器

# hostname node1ssh

# vim /etc/sysconfig/networkide

HOSTNAME=node1oop

node2:測試

# hostname node2ui

# vim /etc/sysconfig/network

HOSTNAME=node2

2)節點之間必須經過ssh互信通訊

[root@node1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''

[root@node1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@202.207.178.7

[root@node2 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''

[root@node2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@202.207.178.6

3)集羣各節點之間時間必須同步

使用ntp服務器同步時間

ntpdate ip(配置了ntp服務的主機地址)

4)配置本地解析:

[root@node1 ~]# vim /etc/hosts

202.207.178.6 node1

202.207.178.7 node2

[root@node1 ~]# scp /etc/hosts node2:/etc/

1、安裝配置Nginx

node1:

一、首先添加用戶nginx,實現以之運行nginx服務進程

# groupadd -r -g 108 nginx

# useradd -r -g 108 -u 108 nginx

二、將下載好的軟件包解壓並安裝(我這裏是nginx-1.4.7.tar.gz)

# tar xf nginx-1.4.7.tar.gz

# cd nginx-1.4.7

接着開始編譯和安裝:

# ./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 \

--with-file-aio

# make && make install

報錯時可能要求安裝以下包,按需安裝便可!

# yum -y install pcre-devel

# yum -y install gcc

# yum -y install openssl-devel

三、爲nginx提供SysV init腳本:


新建文件/etc/rc.d/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


五、添加至服務管理列表,並讓其開機自動啓動:

# chkconfig --add nginx

# chkconfig nginx on

六、提供訪問測試頁面

# vim /usr/html/index.html

改成以下內容:

<h1>node1 202.207.178.6</h1>

七、然後就能夠啓動服務並測試了:

# service nginx start

node2:

配置與node1基本相同,爲了顯示效果,只要把訪問頁面改成以下內容便可:

<h1>node2 202.207.178.7</h1>

2、安裝配置keepalived

node1:

一、安裝keepalived

# tar xf keepalived-1.2.1.tar.gz

# cd keepalived-1.2.1

# ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-642.11.1.el6.i686/

# make && make install

注意:

可能須要安裝此包:

# yum install popt-devel

發現/usr/src/kernels/爲空,須要安裝內核源碼

# yum install kernel-devel

二、配置keepalived

1)設置一個變量

# DIR=/usr/local/

2)執行以下語句,將keepalived的配置文件放置在/etc下相關目錄中

# cp $DIR/etc/rc.d/init.d/keepalived  /etc/rc.d/init.d/

# cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived

# cp $DIR/sbin/keepalived /usr/sbin/

3)爲keepalived提供配置文件

# cd /etc/keepalived/

# vim keepalived.conf(添加如下內容)

global_defs {

  notification_email {

 2663154088@qq.com

  }

  notification_email_from 2663154088@qq.com

  smtp_server 127.0.0.1

  smtp_connect_timeout 30

  router_id LVS_DEVEL

}

# VIP1

vrrp_instance VI_1 {

state BACKUP

interface eth0

lvs_sync_daemon_inteface eth0

virtual_router_id 151

#定義優先級

priority 100

advert_int 5

   #非搶佔,定義此選項,能夠使主節點從宕機恢復到正常                     後,不會搶佔從節點上的資源,增長服務在線時間!

   nopreempt

authentication {

auth_type PASS

auth_pass 2222

}

virtual_ipaddress {

202.207.178.4

}

}

virtual_server 202.207.178.4 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

persistence_timeout 60

protocol TCP

real_server 202.207.178.6 80 {

weight 100

notify_down /data/sh/nginx.sh

TCP_CHECK {

connect_timeout 10

nb_get_retry 3

delay_before_retry 3

connect_port 80

}

}

}

4)啓動服務

# /etc/init.d/keepalived restart

# ps -ef | grep keepalived

此時發現服務已經啓動!

# tail -fn 100 /var/log/messages

此時發現一直滾屏,是缺乏一個模塊,下面加載此模塊

# modprobe ip_vs

# /etc/init.d/keepalived restart

# tail -fn 100 /var/log/messages

此時一切正常

node2:

配置同主節點,只是在配置文件中修改如下幾項便可:

priority 90

real_server 202.207.178.7 80

3、配置相關操做,並測試

一、在主從節點上分別添加以下文件並授予執行權限,實如今主nginx宕機時中止keepalived,實

     現主從角色實現切換

# vim /data/sh/nginx.sh

/etc/init.d/keepalived stop

# chmod +x /data/sh/nginx.sh

二、在主節點中止nginx服務,進行訪問測試

此時訪問http://202.207.178.4/ 能夠訪問到從節點上的nginx服務!


                            歡迎批評指正!

相關文章
相關標籤/搜索