一、集群规划

服务器角色 主机名 IP地址/VIP 软件
Nginx Master NK1 20.0.20.101/20.0.20.100 Nginx/Keepalived
Nginx Backup NK2 20.0.20.102/20.0.20.100 Nginx/Keepalived
Web Server NK3 20.0.20.103 Tomcat
Web Server NK4 20.0.20.104 Tomcat

#关闭selinux和firewall

#软件版本为:apache-tomcat-7.0.85.tar.gz  jdk-8u151-linux-x64.tar.gz

二、安装配置Tomcat

1.安装JDK并配置环境变量

2.部署Tomcat

[root@NK03 ~]# tar zxf apache-tomcat-7.0.85.tar.gz[root@NK03 ~]# mv apache-tomcat-7.0.85 /usr/local/tomcat

3.配置server.xml

[root@NK03 ~]# cat /usr/local/tomcat/conf/server.xml
  
  
  
  
  
  
  
    
  
  
    
    
    
    #NK4为tomcat2        
          
          
            
            
            
              
                        
            
            
                    
          
          
              
        
            
        
            

4.在web.xml里增加<distributable/>

[root@NK03 ~]# vim /usr/local/tomcat/conf/web.xml
    
............    
        
index.html
        
index.htm
        
index.jsp
    

5.建立测试页面

[root@NK03 ~]# mkdir /usr/local/tomcat/webapps/test[root@NK03 ~]# cat /usr/local/tomcat/webapps/test/index.jsp <%@page language="java"%>              

Session serviced by tomcat

          
                                            Session ID                          <%=session.getId() %>-----NK3                          <% session.setAttribute("abc","abc");%>                                                              Created on                          <%= session.getCreationTime() %>                                

6.复制web.xml到测试文件夹

[root@NK03 ~]# mkdir /usr/local/tomcat/webapps/test/WEB-INF[root@NK03 ~]# cp /usr/local/tomcat/conf/web.xml /usr/local/tomcat/webapps/test/WEB-INF/

7.启动tomcat

[root@NK03 ~]# /usr/local/tomcat/bin/startup.shUsing CATALINA_BASE:   /usr/local/tomcatUsing CATALINA_HOME:   /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME:        /usr/local/jdkUsing CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarTomcat started.

#以上操作在NK4上同步执行,除了配置文件server.xml稍有区别,其他都一样

8.浏览测试页面

image.png

image.png

三、配置Nginx

1.获取Nginx yum源并安装

[root@NK01 ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm[root@NK01 ~]# yum install -y nginx

2.修改配置文件(NK1和NK2相同配置)

[root@NK01 ~]# cat /etc/nginx/nginx.conf user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;events {    worker_connections  1024;}http {    include       /etc/nginx/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  /var/log/nginx/access.log  main;    sendfile       on;    tcp_nopush     on;    tcp_nodelay    on;    keepalive_timeout  65;    server_tokens   off;    gzip  on;    client_body_buffer_size 512k;    proxy_connect_timeout   5;    proxy_send_timeout      60;    proxy_read_timeout      5;    proxy_buffer_size       16k;    proxy_buffers           4 64k;    proxy_busy_buffers_size 128k;    proxy_temp_file_write_size 128k;    upstream test{        server 20.0.20.103:8080 weight=10;        server 20.0.20.104:8080 weight=10;	}	    server {        listen   80;        server_name  20.0.20.101;        charset utf-8;        location / {                proxy_pass	http://test;		proxy_set_header X-Real-IP $remote_addr;		proxy_set_header Host $host;		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;		proxy_set_header X-Forwarded-Proto $scheme;        	}	}    include /etc/nginx/conf.d/*.conf;}

3.启动nginx

[root@NK01 ~]# systemctl start nginx[root@NK01 ~]# systemctl enable nginx

4.测试

image.png

image.png

四、配置keepalived

1.安装keepalived

[root@NK01 ~]# yum -y install keepalived

2.编辑nginx检测脚本

[root@NK01 ~]# cat /usr/local/keepalived/chknginx.sh#!/bin/bashcounter=$(ps -C nginx --no-heading|wc -l)if [ "${counter}" = "0" ]; then    systemctl restart nginx    sleep 2    counter=$(ps -C nginx --no-heading|wc -l)    if [ "${counter}" = "0" ]; then    systemctl stop keepalived    fifi
[root@NK01 ~]# chmod +x /usr/local/keepalived/chknginx.sh

3.修改配置文件

[root@NK01 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalivedglobal_defs {    notification_email {    }    smtp_connect_timeout 30    router_id LVS_DEVEL}vrrp_script check_nginx {    script "/usr/local/keepalived/chknginx.sh"    #nginx检测脚本    interval 3    weight -2}vrrp_instance VI_1 {    state MASTER    #NK2为BACKUP    interface ens192    virtual_router_id 151    priority 100    #NK2为99    advert_int 1    authentication {        auth_type PASS        auth_pass 2222    }    track_script {        check_nginx    #检测脚本名    }    virtual_ipaddress {        20.0.20.100    }}

4.在网卡上添加一个IP地址

image.png

#NK2和NK1配置类似

5.启动keepalived

[root@NK01 ~]# systemctl start keepalived[root@NK01 ~]# systemctl enable keepalived

6.查看IP是否绑定

[root@NK01 ~]# ip add |grep "inet 20"    inet 20.0.20.101/16 brd 20.0.255.255 scope global ens192    inet 20.0.20.100/32 scope global ens192    inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192    [root@NK02 ~]# ip add |grep "inet 20"    inet 20.0.20.102/16 brd 20.0.255.255 scope global ens192    inet 20.0.20.100/16 brd 20.0.255.255 scope global secondary ens192

7.使用虚拟IP浏览测试页面

image.png

image.png

四、故障测试

1.keepalived测试

image.png

终止掉master后切换到了backup

image.png

2.nginx测试

在NK1上终止nginx后,会通过脚本自动启动nginx

image.png

image.png

image.png

3.tomcat测试

终止NK3上的tomcat

image.png

image.png