Nginx+Keepalived+Tomcat 源码

第一部分 环境准备
一:Nginx+keepalived服务器两台(调度器,双机热备)
系统:Linux—CentOS7.4
IP地址:192.168.80.100(lvs01)
192.168.80.101(lvs02)
软件需求:nginx安装包(nginx-1.13.9.tar.gz)
Keepalived安装包(keepalived-1.4.2.tar.gz)
二:tomcat服务器两台(服务器池)
系统:Linux—CentOS7.4
IP地址:192.168.80.102(TM01)
192.168.80.103(TM02)
软件需求:java环境jdk包(jdk-8u144-linux-x64.tar.gz)
tomcat安装包(apache-tomcat-8.5.23.tar.gz)

192.168.80.100

1、源码安装Nginx

yum install -y \        //安装编译工具及插件
 gcc \
 gcc-c++ \
 make \
 openssl-devel \
 zlib-devel \

useradd -s /sbin/nologin -M nginx --创建一个不能登录服务器,并且没有家目录的用户

tar vxf nginx-?.tar.gz ---解压nginx软件包

cd nginx-1.13.9 --切换到解压目录里

 ./configure \               //个性化配置
 --user=nginx \
--group=nginx \
--with-file-aio \
 --with-http_flv_module \
 --with-http_stub_status_module \
 --with-http_ssl_module \
 --with-http_gzip_static_module \
 --with-http_realip_module

make && make install --编译且安装

vi /usr/local/nginx/conf/nginx.conf --nginx主配置文件

user nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/nginx.pid;

events {
    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;
        add_header X-Server $hostname;
        server_names_hash_bucket_size 128;
        server_name_in_redirect off;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;

    #keepalive_timeout  0;
    keepalive_timeout  60;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 128k;
        client_max_body_size 512m;
        open_file_cache max=65535 inactive=20s;
        open_file_cache_valid 30s;
        open_file_cache_min_uses 1;

    gzip  on;
        gzip_static on;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_min_length 1024;
        gzip_vary on;
        gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;

server_tokens off;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 512k;
        fastcgi_buffers 6 512k;
        fastcgi_busy_buffers_size 512k;
        fastcgi_temp_file_write_size 512k;
        fastcgi_intercept_errors on;
        client_body_buffer_size 128k;

        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 32k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 2m;
        proxy_ignore_client_abort on;
        proxy_cache_path /usr/local/nginx/cache_temp levels=2:2

keys_zone=cache_temp:128m inactive=30m max_size=2g;
        proxy_cache_valid 200 302 10m;
        include /usr/local/nginx/conf/conf.d/*.conf;

    server {
        listen       80;
        server_name  localhost;

        charset UTF-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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   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;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
 cd /usr/local/nginx/conf/    --切换到nginx配置文件目录中
mkdir conf.d  --创建一个 conf.d的目录
cd conf.d/
vi lvs01.conf          //新建子配置文件
server {
        listen 80;
        server_name lvs01 192.168.80.100;       //服务器名称与IP地址
        index index.html index.jsp;
        root /usr/local/nginx/html;
        access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;
        location ~ .*\.jsp$ {
        index index.jsp;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Client-IP $remote_addr;
        proxy_set_header X-For $proxy_add_x_forwarded_for;
        proxy_pass http://center_pool;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
        proxy_pass http://center_pool;
        }
        location ~ .*\.(js|css)?$ {
        expires 1h;
        proxy_pass http://center_pool;

        }
}

vi pool.conf       //创建服务器池
upstream center_pool {               //默认轮询
        server 192.168.80.102:8080;
        server 192.168.80.103:8080;
}
//制作启动脚本
[root@lvs01 conf.d]# vi /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 99 20
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  reload)
    kill -s HUP $(cat $PIDF)
    ;;
  *)
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
chmod +x /etc/init.d/nginx        //增加执行权限
chkconfig --add nginx             //加入系统管理服务
service nginx start               //启动nginx服务
netstat -anpt | grep 80

Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用

2、源码编译keepalived

yum -y install \         //安装插件
> popt-devel \
> kernel-devel \
> openssl-devel
 tar xvf keepalived-1.4.2.tar.gz   --解压keepalived压缩包
 cd keepalived-1.4.2            --切换到解压目录
 ./configure --prefix=/           --配置
 make && make install        --编译与安装

cp keepalived/etc/init.d/keepalived /etc/init.d/

//以下编辑keepalived配置文件
cd /etc/keepalived/
vi keepalived.conf

    ! Configuration File for keepalived
global_defs {
        route_id NGINX-01
   }
vrrp_script nginx {
        script "/opt/nginx.sh"
        interval 2
        weight -10
}
vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        nginx
        }
    virtual_ipaddress {
        192.168.80.100
    }
}
//注意,主备的优先级配置的相差50

创建脚本文件
判断keepalived进程是否存在,在就启动nginx不在就关闭
vi /opt/nginx.sh

#!/bin/bash 
#Filename:nginx.sh
A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
        /etc/init.d/nginx start
else
        /etc/init.d/nginx stop
fi

chmod +x /opt/nginx.sh --给脚本文件加执行权限

service keepalived start --启动keepalived,会连nginx一块启动

ip addr show dev ens32 --查看漂移地址是否生成
Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用_第1张图片

---------------测试验证------------
[root@lvs01 keepalived]# systemctl stop keepalived    //关闭keepalived服务
[root@lvs01 keepalived]# killall -9 nginx              //关闭nignx服务
[root@lvs01 keepalived]# netstat -anpt | grep 80       //80端口已停止运行

[root@lvs01 keepalived]# systemctl start keepalived     //开启keepalived服务
[root@lvs01 keepalived]# netstat -anpt | grep 80       //nginx随keepalived启动

192.168.80.101

---------------------配置从服务器--------------------------------
1、源码编译安装nginx
从服务器源码安装nginx,和主服务器一样,根据主服务器操作在做一遍
需要修改的地方
Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用

2、源码安装keepalived
操作和主服务器相同,在写到nginx.sh脚本文件时不同,其它的操作一模一样。

#!/bin/bash
#Filename:nginx.sh
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
        /etc/init.d/nginx start
else
        /etc/init.d/nginx stop
fi

chmod +x /opt/nginx.sh --给脚本执行权限

service keepalived start --启动 keepalived

----------测试验证----------
一:查看从服务器状态
[root@lvs02 ~]# ip addr show dev ens33 //查看漂移地址
Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用_第2张图片
//由于主服务器在运行,漂移地址并未同步过来

//当主服务器在运行的时候,从服务器虚拟地址并未生成,nginx服务并未随keepalived启动
二:模拟主服务故障

  1. 主服务器
    Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用

killall 命令需要安装 : yum install psmisc -y
//80端口已不运行
[root@lvs01 ~]# ip addr show dev ens32
Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用_第3张图片
//漂移地址消失,不工作

  1. 从服务器
    [root@lvs02 ~]# ip addr show dev ens32 //查看漂移地址
    Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用_第4张图片
    //漂移地址自动生成
    [root@lvs02 ~]# netstat -anpt | grep 80
    Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用

  2. 主服务器
    
    [root@lvs01 ~]# service keepalived start
    [root@lvs01 ~]# ip addr show dev ens33

[root@lvs01 ~]# netstat -anpt | grep 80

//主服务器已恢复工作

  1. 从服务器
    [root@lvs02 ~]# ip addr show dev ens33

//漂移地址自动移除
[root@lvs02 ~]# netstat -anpt | grep 80
```\

//nginx自动停止服务
//双机热备验证成功

192.168.80.103
提示:操作与192.168.80.102基本一样
需要修改的地方就是把 SERVER AA 改成 SERVER BB j 就可以了
--------测试------
地址栏里输入:192.168.80.188:8080,点击刷新按钮查看是否动态轮询。

#192.168.80.102

第三部分 部署服务器池—搭建Tomcat
//以下在两台tomcat服务器上操作
第一步:部署第一个节点服务器TM01(192.168.80.102)
----------部署java环境,jdk---------

[root@tom01 ~]# tar xvf jdk-8u144-linux-x64.tar.gz //解压jdk
[root@tom01 ~]# cp -r jdk1.8.0_144/ /usr/local/java //创建java源目录
[root@tom01 ~]# vi /etc/profile
最后添加以下内容:
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
保存退出
[root@tom01 ~]# source /etc/profile //立即生效
[root@tom01 ~]# java –version //查看版本

java环境部署完成

----------部署tomcat----------
[root@tom01 ~]# tar xvf apache-tomcat-8.5.23.tar.gz //解压apache-tomcat
[root@tom01 ~]# cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 //创建tomcat源目录
//做个软链接,使tomcat开启与关闭更加方便
[root@tom01 ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@tom01 ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@tom01 ~]# tomcatup //开启tomcat

[root@tom01 ~]# netstat -anpt | grep 8080

Win7下访问默认主页:http://192.168.80.102:8080
Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用_第5张图片
//tomcat部署成功

  1. 服务器池中有两台tomcat服务器,为了便于识别,主页上添加点标记
    [root@tom01 ~]# vi /usr/local/tomcat8/webapps/ROOT/index.jsp
    添加一行内容:
    Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用
    保存退出
    [root@tom01 ~]# tomcatdown
    [root@tom01 ~]# tomcatup //重启
    //再次访问默认主页http://192.168.80.102:8080
    Centos 7 Nginx+Keepalived+Tomcat 源码安装与运用

    192.168.80.103
    提示:操作与192.168.80.102基本一样
    需要修改的地方就是把 SERVER AA 改成 SERVER BB j 就可以了
    --------测试------
    地址栏里输入:192.168.80.188:8080,点击刷新按钮查看是否动态轮询。