Fork me on GitHub

Tomcat+Nginx+Keepalived 实现主从热备和负载均衡

 

虚拟机 IP 说明
Keepalived+Nginx1[Master] 172.16.228.130 Nginx Server 01
Keeepalived+Nginx[Backup] 172.16.228.131 Nginx Server 02
Tomcat01 172.16.228.128 Tomcat Web Server01
Tomcat02 172.16.228.129 Tomcat Web Server02
VIP 172.16.228.133 虚拟漂移IP
<div id="asf-box">

    <h1>${pageContext.servletContext.serverInfo}(tomcat_01)<%=request.getHeader("X-NGINX")%></h1>

</div>
(tomcat_01)ROOT/index.jsp
<div id="asf-box">

    <h1>${pageContext.servletContext.serverInfo}(tomcat_02)<%=request.getHeader("X-NGINX")%></h1>

</div>
(tomcat_02)ROOT/index.jsp
upstream tomcat {
   server 172.16.228.128:8080 weight=1;
   server 172.16.228.129:8080 weight=1;
}
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    # location / {
    #     root   /usr/share/nginx/html;
    #     index  index.html index.htm;
    # }

    location / {
        proxy_pass http://tomcat;
        proxy_set_header X-NGINX "NGINX-1";
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
nginx_01
upstream tomcat {
   server 172.16.228.128:8080 weight=1;
   server 172.16.228.129:8080 weight=1;
}
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    # location / {
    #     root   /usr/share/nginx/html;
    #     index  index.html index.htm;
    # }

    location / {
        proxy_pass http://tomcat;
        proxy_set_header X-NGINX "NGINX-2";
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
nginx_02
! Configuration File for keepalived

vrrp_script check_nginx {  
 script "/etc/keepalived/check_nginx.sh"
 interval 2  
 weight -20  
}  

global_defs {  
    router_id lb01
}

vrrp_instance VI_1 {  
 state MASTER                  
 interface ens33               
 virtual_router_id 51            
 mcast_src_ip 172.16.228.130  
 priority 250                  
 advert_int 1                                 
 authentication {         
        auth_type PASS
        auth_pass 123456
 }
 track_script {  
        check_nginx  
 }
 virtual_ipaddress {          
        172.16.228.133         
 }
}
(MASTER)keepalived.conf
! Configuration File for keepalived

vrrp_script check_nginx {  
 script "/etc/keepalived/check_nginx.sh"
 interval 2  
 weight -20  
}  

global_defs {  
    router_id lb02
}

vrrp_instance VI_1 {  
 state BACKUP                  
 interface ens33               
 virtual_router_id 51            
 mcast_src_ip 172.16.228.131
 priority 240                
 advert_int 1                  
 nopreempt               
 authentication {         
        auth_type PASS
        auth_pass 123456
 }
 track_script {  
        check_nginx  
 }
 virtual_ipaddress {          
        172.16.228.133         
 }
}
(BACKUP)keepalived.conf
#!/bin/bash
d=`date --date today +%Y%m%d_%H:%M:%S`
n=`ps -C nginx --no-heading|wc -l`
if [ $n -eq "0" ]; then
        systemctl start nginx
        n2=`ps -C nginx --no-heading|wc -l`
        if [ $n2 -eq "0"  ]; then
                echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
                systemctl stop keepalived
        fi
fi
check_nginx.sh
 
 
posted @ 2018-11-07 00:25  Nick.Chung  阅读(421)  评论(0编辑  收藏  举报