nginx+負載均衡+keepalive+雙機熱備

最近搭建keepalived雙機熱備, 在網上博採衆之所長, 整理一下本身的筆記!html

在新搭建的centos6.x下最好下載一下阿里雲的源配置:linux

  centos:http://mirrors.aliyun.com/help/centosnginx

一、備份

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backupc++

二、下載新的CentOS-Base.repo 到/etc/yum.repos.d/

CentOS 5

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repoweb

或者vim

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repocentos

CentOS 6

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo緩存

或者tomcat

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repobash

CentOS 7

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

或者

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

三、以後運行yum makecache生成緩存

 

===============================================================================================================================

  準備2臺電腦來作這個實驗:


  192.168.232.137        web服務器  keepalived nginx
  192.168.232.138        web服務器  keepalived nginx

  虛擬IP (VIP):192.168.232.17

  虛擬IP (VIP):192.168.232.18

 

 

nginx下載地址:  http://nginx.org/en/download.html

keepallived下載地址:   http://www.keepalived.org/download.html

 

在137上:(ngnix和keepalived須要的依賴環境)

  安裝依賴:

      su - root

       yum -y install gcc gcc+ gcc-c++

  OpenSSL: yum install -y openssl openssl-*

  pcre庫:  yum install -y pcre pcre-devel

  zlib庫:   yum install -y zlib zlib-devel

       yum -y install lrzsz

          yum -y install openssh-clients

       yum -y install libssl-dev libnl libnl-devel

       yum -y install popt popt-devel

  安裝內核:   

       yum -y install kernel kernel-devel*

 

===================================================================================================================================

 

1.  安裝keepalived

/usr/local/下:  wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

          tar -zxvf keepalived-1.2.2.tar.gz  

        cd keepalived-1.2.2  
        ./configure  --prefix=/usr/local/keepalived
        make  
        make install   

拷貝相應的文件

1.拷貝執行文件

  cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

2.將init.d文件拷貝到etc下,加入開機啓動項

  cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

3.將keepalived文件拷貝到etc下,加入網卡配置

  cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

4.建立keepalived文件夾

  mkdir -p /etc/keepalived

5.將keepalived配置文件拷貝到etc下

  cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

6.添加可執行權限

  chmod +x /etc/init.d/keepalived

 

2.啓動keepalived

啓動:service keepalived start

中止:service keepalived stop

重啓:service keepalived restart

 

3.  配置日誌文件

1.將keepalived日誌輸出到local0:

  vim /etc/sysconfig/keepalived

  KEEPALIVED_OPTIONS="-D -d -S 0"

2.在/etc/rsyslog.conf裏添加:

  local0.*  /var/log/keepalived.log

3.從新啓動keepalived和rsyslog服務:

  service rsyslog restart

  service keepalived restart

4. 打開防火牆的通信地址

iptables -A INPUT -d 224.0.0.18 -j ACCEPT

/etc/rc.d/init.d/iptables save

 ===================================================================================================================================

1.  安裝nginx

/usr/local/下:   wget http://nginx.org/download/nginx-1.10.3.tar.gz

        tar -zxvf linux-nginx-1.10.2.tar.gz   

        mkdir -p /var/temp/nginx    

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

        make
        make install
        whereis nginx

2.啓動nginx

cd /usr/local/nginx/sbin/
./nginx

./nginx -s quit

查看進程: ps -ef|grep nginx 

3.測試nginx

service iptables stop

chkconfig iptables off

訪問: 192.168.200.137

4.配置nginx

配置虛擬主機
反向代理
負載均衡
upstream tomcat_server_pool{
server 192.168.101.5:8080 weight=10;
server 192.168.101.6:8080 weight=10;
}

server {
listen 80;
server_name aaa.test.com;
location / {
proxy_pass http://tomcat_server_pool;
index index.jsp index.html index.htm;
}
}

負載均衡特色:
轉發
故障移除
恢復添加
高可用(keepalived + nginx實現主備)

 

配置高可用

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.200.1 #指定smtp服務器地址
#smtp_connect_timeout 30 #指定smtp鏈接超時時間
router_id LVS_DEVEL #運行keepalived機器的一個標識
}

vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh" ##監控腳本
interval 2 ##時間間隔,2秒
weight 2 ##權重
}

vrrp_instance VI_1 {
state MASTER #標示狀態爲MASTER 備份機爲BACKUP
interface eth0 #設置實例綁定的網卡
virtual_router_id 51 #同一實例下virtual_router_id必須相同
priority 100 #MASTER權重要高於BACKUP 好比BACKUP爲99
advert_int 1 #MASTER與BACKUP負載均衡器之間同步檢查的時間間隔,單位是秒
authentication { #設置認證
auth_type PASS #主從服務器驗證方式
auth_pass 1111
}
track_script {
check_nginx ##監控腳本
}
virtual_ipaddress { #設置vip
#192.168.200.16 #能夠多個虛擬IP,換行便可
192.168.200.17
}
}

vrrp_instance VI_2 {
state BACKUP
interface eth1
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.18
}
}

---------------------------------

vim /etc/keepalived/check_nginx.sh

#!/bin/bash
# 若是進程中沒有nginx則將keepalived進程kill掉
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx進程 把值賦給變量A
if [ $A -eq 0 ];then ## 若是沒有進程值得爲 零
service keepalived stop ## 則結束 keepalived 進程
fi

chmod a+x /etc/keepalived/check_nginx.sh

配置nginx和keepalived開機啓動:

https://www.cnblogs.com/whatmiss/p/7091220.html

 

vim /etc/init.d/nginx

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

-----------------------------------------------------------------

chmod a+x /etc/init.d/nginx

chmod a+x /etc/init.d/keepalived

[root@localhost keepalived]# vim /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

/etc/init.d/nginx start

sleep 1

/etc/init.d/keepalived start

高可用測試:

先啓動兩臺tomcat --->再啓動兩臺nginx-->後啓動兩臺keepalived

1.初始狀態

vip192.168.200.17綁定在主nginx的eth0上(同時是vip192.168.200.18的備nginx)

/sbin/ip add show eth0

vip192.168.200.18綁定在主nginx的eth1上(同時是vip192.168.200.17的備nginx)。

/sbin/ip add show eth1

從而實現雙主熱備....

======================================================================================================================================

附錄:

加入開機啓動:

chkconfig --add keepalived       #添加時必須保證/etc/init.d/keepalived存在

chkconfig keepalived on

添加完可查詢系統服務是否存在:chkconfig --list

相關文章
相關標籤/搜索