Linux-centos下安裝lvs+keepalived+nginx+tomcat__Linux

來源:互聯網
上載者:User

一、如圖


二、安裝tomcat(jdk安裝參考http://blog.csdn.net/u011254180/article/details/77893457)

一、tar格式的安裝(以apache-tomcat-7.0.52.tar.gz為例)
1)上傳tomcat到linux上
2)解壓Tomcat到/usr/local下
tar -zxvf apache-tomcat-7.0.52.tar.gz -C /usr/local/tomcat
3)開放Linux的對外訪問的連接埠8080
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
/etc/rc.d/init.d/iptables save
4)啟動關閉Tomcat
進入tomcat的bin下啟動:./startup.sh
進入tomcat的bin下關閉:./shutdown.sh


三、安裝nginx(參考http://blog.csdn.net/u011254180/article/details/77897663)

設定檔和之前的一樣

user  nobody nobody;#定義Nginx啟動並執行使用者和使用者組
worker_processes  4; #nginx進程數,建議設定為等於CPU總核心數。
error_log  logs/error.log info;#全域錯誤記錄檔定義類型,[ debug | info | notice | warn | error | crit ]
worker_rlimit_nofile 1024; #一個nginx進程開啟的最多檔案描述符數目,所以建議與ulimit -n的值保持一致。
pid logs/nginx.pid;#進程檔案


#工作模式及串連數上限
events {
use epoll;#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本核心中的高效能網路I/O模型
   worker_connections  1024;#單個進程最大串連數(最大串連數=串連數*進程數)
}


#設定http伺服器,利用它的反向 Proxy功能提供負載平衡支援
http {
    include       mime.types;#副檔名與檔案類型映射表
    default_type  application/octet-stream;#預設檔案類型
#設定負載平衡的伺服器列表
upstream  tomcatxxxcom  {  
     server   192.168.56.200:8080;  
     server   192.168.56.201:8080;  
}
#設定日誌格式
    log_format  www_xy_com  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    sendfile        on;#開啟高效檔案傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出檔案,對於普通應用設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
    keepalive_timeout  65; #長連線逾時時間,單位是秒


    #gzip  on;
#設定虛擬機器主機,預設為監聽80連接埠
    server {
        listen       80;
        server_name  tomcat.xxx.com;#網域名稱可以有多個,用空格隔開


        #charset koi8-r;
#設定本虛擬機器主機的訪問日誌
        access_log  /data/logs/access.log  www_xy_com;
#對 "/" 啟用反向 Proxy 
  location / {
  proxy_pass        http://tomcatxxxcom;  
               proxy_set_header   Host             $host;  
               proxy_set_header   X-Real-IP        $remote_addr;  
               proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        
        #error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

四、安裝lvs(參考http://blog.csdn.net/u011254180/article/details/77898489)

lvs-dr.sh:和之前對比,變化之處就是vip和轉寄的連接埠。

#!/bin/bash
#description:start lvs server
echo "1" >/proc/sys/net/ipv4/ip_forward
 
WEB1=192.168.56.200
WEB2=192.168.56.201
 
VIP1=192.168.56.90
 
/etc/rc.d/init.d/functions
 
case "$1" in
start)
echo "start LVS of directorServer"
#set the Virtual address and sysctl parameter
/sbin/ifconfig eth1:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
#clear ipvs table
/sbin/ipvsadm -C
 
#set LVS
#web apache or tomcat
/sbin/ipvsadm -A -t $VIP1:80 -s rr
/sbin/ipvsadm -a -t $VIP1:80 -r $WEB1:80  -g
/sbin/ipvsadm -a -t $VIP1:80 -r $WEB2:80  -g
 
#run LVS
/sbin/ipvsadm

;;

stop)
echo "close LVS directorserver"
echo "0" >/proc/sys/net/ipv4/ip_forward

/sbin/ipvsadm -C

/sbin/ipvsadm -Z

;;
*)
echo "usage:$0 {start|stop}"
exit 1
esac
lvs-rs.sh:與之前的不同在於修改了vip

#!/bin/sh
#description start realserver
#chkconfig 235 26 26
VIP1=192.168.56.90
/etc/rc.d/init.d/functions
case "$1" in
start)
 
echo "start LVS of realserver"
/sbin/ifconfig lo:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up
 
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
stop)
/sbin/ifconfig lo:0 down
echo "close lvs dirctorserver"
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
;;
*)
echo "usage:$0{start|stop}"
exit 1
esac

五、安裝keepalived(參考 http://blog.csdn.net/u011254180/article/details/77898489)

注意:在用keepalived做tomcat和nginx的熱備時,需要加入realserver的配置。但是做lvs的熱備則不需要配置realserver,因為keepalived有lvs的配置參數。

backup:

! 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_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
lvs_sync_daemon_inteface eth1
    virtual_router_id 51
    priority 100
nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.90
    }
}

virtual_server 192.168.56.90 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    #nat_mask 255.255.255.0
    persistence_timeout 1
    protocol TCP
}
master:

! 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_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
lvs_sync_daemon_inteface eth1
    virtual_router_id 51
    priority 200
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.90
    }
}

virtual_server 192.168.56.90 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    #nat_mask 255.255.255.0
    persistence_timeout 1
    protocol TCP
}
監控指令碼:

wangsf.sh指令檔內容

#!/bin/bash
while true;
do
    A=`ipvsadm -ln | wc -l`
B=`ps -ef|grep keepalived |wc -l`
if [ $A -eq 3 ];then
                echo 'restart lvs!!!!'
                /usr/local/lvs/lvs-dr.sh start
if [ $A -eq 3 ];then
if [ $B -gt 1 ];then
  echo 'lvs dead  !!!! kill keepalived'
  killall keepalived
  break
fi
fi
fi
if [ $A -eq 6 ];then
if [ $B -eq 1 ];then
  echo 'tomcat live  !!!! start keepalived'
  service keepalived start
fi
fi
sleep 3
done


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.