nginx + keepalived achieve high availability Case

Homework:
What is the split brain: think each other's server to its knees, and resources on both sides, leading to what can not be accessed.
. 1, the NFS the DRBD + Heartbeat +
2, + Nginx high availability keepalived
Experimental: nginx + keepalived highly available
1, prepared four virtual machines, two tomcat, two Nginx
2, the same two tomcat configuration, test page
installed JDK
[the root @ localhost ~] # 8u191 the tar-XF-Linux JDK -x64.tar.gz
[the root @ localhost ~] # Music Videos jdk1.8.0_191 / usr / local / Java
[the root @ localhost ~] # VI / etc / Profile
last added
Export the JAVA_HOME = / usr / local / Java
Export the PATH the PATH $ =: $ the JAVA_HOME / bin
[the root @ localhost ~] # Source / etc / Profile
mounting Tomcat
[the root @ localhost ~] # the tar-XF Apache Tomcat-8.5.16.tar.gz
[the root @ localhost ~] # Music Videos Tomcat-8.5.16-the Apache / usr / local / Tomcat
[root @ localhost ~] # /usr/local/tomcat/bin/startup.sh
tomcat1 test page:
[root @ localhost ~] # echo "111111"> / usr /local/tomcat/webapps/ROOT/index.jsp
Tomcat2测试页:
[root@localhost ~]# echo “222222” > /usr/local/tomcat/webapps/ROOT/index.jsp
 
2、两台nginx配置完全相同
[root@localhost ~]# tar xf xf nginx-1.15.9.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.15.9
[root@localhost~]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre
[root@localhost~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
[root@localhost~]# nginx
[root@localhost~]# vi /usr/local/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    upstream tomcat_server {
        server 192.168.200.112:8080 weight=1;
        server 192.168.200.113:8080 weight=1;
    }
    server {
        listen       192.168.200.254:80;
        server_name  localhost;
        charset utf-8;
        access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm index.jsp;
            proxy_pass http://tomcat_server;
            proxy_set_header Host $http_host;
        }
    }
}
[root@localhost~]# killall -9 nginx
[root@localhost~]# nginx
 
4、keepalived配置
[root@localhost~]# yum -y install keepalived
[root@localhost~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   {notification_email
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   the router_id of the LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
vrrp_script chk_http_port {# nginx detection services are running. There are many ways, such as process, script testing, etc.
    script "/root/nginx.sh" # script here by monitoring
    once every two seconds detection interval 2 #
    weight -5 # priority change of script resulting in detection failure (script returns non-zero) then the priority -5
    Fall # 2 detects two consecutive failures are considered to determine the true failure. It will reduce the priority (1-255) with a weight
    Rise 1 # 1 Detection successful even if successful. But does not modify the priority
}
 
vrrp_instance VI_1 {
    the BACKUP State
    interface ens32
    virtual_router_id 51 is
    priority 90
    advert_int. 1
    authentication {
        AUTH_TYPE the PASS
        AUTH_PASS 1111
    }
    virtual_ipaddress {
        192.168.200.254
    }
track_script {# perform monitoring service.
   chk_http_port # quoted VRRP script that specified in the vrrp_script part of the name.
}
}
 
Detection nginx script
# / bin / the bash!
Counter = $ (PS -C nginx --no-heading | WC -l)
IF [ "$ {} counter" = "0"]; the then
    / usr / local / Nginx / sbin / Nginx
    SLEEP 2
    counter = $ (PS -C Nginx --no-heading | WC -l)
    if [ "${counter}" = "0" ]; then
        systemctl stop keepalived
    fi
fi
[root@localhost~]#  chmod +x /root/nginx.sh
[root@localhost~]#  systemctl restart keeplived

Guess you like

Origin www.cnblogs.com/elin989898/p/11943692.html