zoukankan      html  css  js  c++  java
  • 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
     
     
  • 相关阅读:
    cannot resolve symbol 'XXX'
    jwt单点登入
    空3
    Hibernate持久化,生命周期
    Hibernate主键生成策略
    Hibernate常用api以及增删改查
    Hibernate配置流程
    Hibernate定义
    Git总结
    spring整合MQ
  • 原文地址:https://www.cnblogs.com/ncore/p/9919862.html
Copyright © 2011-2022 走看看