keepalived+nginx+tomcat搭建高效能web伺服器叢集

來源:互聯網
上載者:User

標籤:keepalived+nginx+tomcat 高效能 高可用 web伺服器叢集

使用keepalived+nginx+tomcat搭建高效能web伺服器叢集,系統採用centos6.9,前端用nginx做反向 Proxy實現負載平衡,同時結合keepalived對nginx實現高可用,後端使用兩台tomcat做動態jsp解析,實現了動靜分離。

650) this.width=650;" title="繪圖1.png" src="https://s3.51cto.com/wyfs02/M02/96/A6/wKioL1kj-nGAzXu2AAERmQK4AlI941.png-wh_500x0-wm_3-wmp_4-s_1240282532.png" alt="wKioL1kj-nGAzXu2AAERmQK4AlI941.png-wh_50" />

搭建環境


準備四台伺服器 
vip: 192.168.75.130
master: 192.168.75.131 (安裝nginx做反向 Proxy實現負載勻衡,結合keepalived實現高可用)
backup: 192.168.75.132 (同上)
web1: 192.168.75.151 (安裝nginx和tomcat實現動靜分離)
web2: 192.168.75.152 (同上)
這裡為節省服器,nginx和tomcat和裝在了一台服務上,也可以分開配置。


配置步驟如下:


四台伺服器分別安裝好nginx
安裝方法參見:LNMP環境搭建


web1,web2上分別安裝好tomcat

安裝方法參見:Tomcat8.5的安裝配置

web1, web2上分別啟動nginx和tomcat
service nginx start
service tomcat start


在master和backup上配置nginx:

vi /usr/local/nginx/conf/nginx.conf

加入或修改以下內容:

    upstream web_nginx {
        #ip_hash;
        server 192.168.75.151;
        server 192.168.75.152;
    }

upstream web_tomcat {
    server 192.168.75.151:8080;
    server 192.168.75.152:8080;
}

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass      http://web_nginx/;
            proxy_set_header Host   $host;
            proxy_set_header X-Real-IP      $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location ~ \.jsp$ {
            proxy_pass   http://web_tomcat;
        }

分別在master和backup上啟動nginx服務

service nginx start


在master和backup上分別安裝keepalived
yum install -y keepalived


配置master
vi /etc/keepalived/keepalived.conf
清空原文,加入如下內容:
! Configuration File for keepalived

global_defs {
   notification_email {
     #[email protected]
     #[email protected]
     #[email protected]
   }
   #notification_email_from [email protected]
   #smtp_server 192.168.200.1
   #smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_http_port {
    script "</dev/tcp/127.0.0.1/80"
    interval 1
    weight -2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.75.130
    }
    track_script {
        chk_http_port
    }
}


配置backup
scp 192.168.75.131:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

vi /etc/keepalived/keepalived.conf
backup的設定檔只需要修改
state MASTER  -> state BACKUP
priority 100 -> priority 99


最後,分別在master和backup上啟動keepalived服務,先主後備
/etc/init.d/keepalived start



keepalived+nginx+tomcat搭建高效能web伺服器叢集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.