需求:此项目要求在两台机器中各安装两个tomcat服务做负载均衡
安装步骤:
1.把所需的包下载下来,我用的版本为  openssl-1.1.0f.tar.gz    pcre-8.4.0.tar.bz2ngx_cache_purge-2.3.tar.gz  nginx-1.12.1.tar.gz  keepalived-1.2.20.tar.gz  jdk-8u131-linux-x64.tar.gz  apache-tomcat-7.0.79.tar.gz
2.NGINX的安装
    检查安装nginx的依赖性,nginx的模块需要第三方的支持,检查是否安装下列库:zlib zlib-devel  openssl openssl-devel  pcre  pcre-devel  gcc  (此行要安装的上面的包中不包括的用 yum installzlib* 安装,以此类推)
    1)添加用户和组
    groupadd www
    useradd -g www www
    2)将文件解压后安装
    tar zxvf openssl-1.1.0f.tar.gz
    cd openssl-1.1.0f
    ./config  --prefix=/usr/local/openssl
    make && make install

    tar jxvf pcre-8.4.0.tar.bz2
    cd  pcre-8.4.0
    ./configure --prefix=/usr/local/pcre
    make && make install

    tar zxvf nginx-1.12.1.tar.gz
    cd nginx-1.12.1
  
  ./configure --user=www --group=www  --prefix=/usr/local/nginx --with-pcre=/opt/pcre-8.40  --with-http_ssl_module--with-http_sub_module --with-http_stub_status_module--add-module=/opt/ngx_cache_purge-2.3 --with-openssl=/opt/openssl-1.1.0f      (注:此行中/opt/  路径是我的源码解压的地方)
   
 make && make install
    报错:1).若是报openssl错误。就是要把源路径给带上--with-openssl=/opt/openssl-1.*.*
          2).若是这个错:make -f objs/Makefile
    make[1]: Entering directory `/opt/nginx-1.12.1'
    cd /usr/local/pcre \
    && if [ -f Makefile ]; then make distclean; fi \
    && CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \
    ./configure --disable-shared 
    /bin/sh: line 2: ./configure: 没有那个文件或目录
    make[1]: *** [/usr/local/pcre/Makefile] 错误 127
    make[1]: Leaving directory `/opt/nginx-1.12.1'
    make: *** [build] 错误 2
    解决方法:发现--with-pcre=DIR是设置pcre的源码目录(就是解压后的目录),而不是pcre的安装目录
    重新修改Nginx编译参数
    3).报错: make make[1]: *** [objs/src/core/nginx.o]  
    后来确认是 nginx 与pcre不兼容导致
    nginx-1.12.1与 pcre2-10.21不兼容。后来更换为pcre-8.40便可)
4) 安装完成。修改配置
    vim /usr/local/nginx/conf/nginx.conf
   
 user  www www;
#user  nobody;
worker_processes  4;


error_log  /usr/local/nginx/logs/nginx_error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


pid        /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;


events {
    use epoll;
    worker_connections  65535;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;
    #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;
    tcp_nodelay    on;
    sendfile        on;
    tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    client_body_buffer_size  512k;
    proxy_connect_timeout    1;
    proxy_read_timeout       1;
    proxy_send_timeout       1;
    proxy_buffer_size        16k;
    proxy_buffers            4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;


    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types       text/plain application/x-JavaScript text/css application/xml;
    gzip_vary on;


 (注:若缓存要开启)   #proxy_temp_path  /home/usr/local/www/proxy_temp_dir;
 (注:若缓存要开启)  #proxy_cache_path  /home/usr/local/www/proxy_cache_dir levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;   


    upstream backend_server {
        server   127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
        server   127.0.0.1:9090 weight=1 max_fails=2 fail_timeout=30s;
        #ip_hash;
        }


    server {
        listen       80;
        server_name  localhost;
        index  index.html index.htm;
        root  /usr/local/www/web/ROOT;
        #charset koi8-r;


       #access_log  /usr/local/nginx/logs/host.access.log;


        location / {
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
(注:若缓存要开启)#######    proxy_cache cache_one;
        proxy_cache_valid    200 304 12h;
        proxy_cache_key $host$uri$is_args$args;
        proxy_set_header Host  $host;
        proxy_set_header X-Forwarded-For  $remote_addr;
        proxy_pass http://backend_server;
        expires      1d;
        }

        location ~ .*\.(php|jsp|cgi)?$
        {
                 proxy_set_header Host  $host;
                 proxy_set_header X-Forwarded-For  $remote_addr;
                 proxy_pass http://backend_server;
        }
}
}


5.配置完成。
6. 创建缓存文件夹
mkdir /usr/local/www/proxy_temp_dir
mkdir /usr/local/www/proxy_cache_dir
启动NGINX
/usr/local/nginx/sbin/nginx
关闭NGINX
/usr/local/nginx/sbin/nginx -s stop
重启NGINX
/usr/local/nginx/sbin/nginx -s reload

7.安装jdk (网上查询)
配置环境变量:vim  /etc/profile
export JAVA_HOME="/usr/local/jdk1.8.0"  (注:根据自己安装的路径来)
export CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
export PATH=".:$PATH:$JAVA_HOME/bin "
source /etc/profile    

8.安装tomcat 
tar zxvf apache-tomcat-7.0.79.tar.gz
mkdir  /usr/loca/tomcat1
cp -r apache_tomcat-7.0.79/*   /usr/local/tomcat1
cp -r apache_tomcat-7.0.79/*   /usr/local/tomcat2    (因为要安装两个服务)
 修改TOMCAT配置文件/usr/local/tomcat2/conf/server.xml

点击(此处)折叠或打开

  1. 将以下内容
  2.     <Server port="8005" shutdown="SHUTDOWN">...
  3.     <Connector port="8080" protocol="HTTP/1.1" ...
  4.     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


点击(此处)折叠或打开

  1. 修改为
  2.     <Server port="9005" shutdown="SHUTDOWN">...
  3.     <Connector port="9090" protocol="HTTP/1.1" ...
  4.     <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />

(注:在shutdown.sh 也要加入下面的行)
1)修改tomcat配置启动文件/usr/local/tomcat1/bin/startup.sh
在第二行加入: export  CATALINA_HOME=/usr/local/tomcat1

2)修改TOMCAT配置启动文件/usr/local/tomcat2/bin/startup.sh
在 第二行加入: export CATALINA_HOME=/usr/local/tomcat2

9.tomcat安装完成

10.安装keepalived
tar zxvf keepalived-1.2.20.tar.gz

cd keepalived-1.2.6

./configure --prefix=/usr/local/keepalived 

make

make install


cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

chmod +x /etc/init.d/keepalived


修改/etc/init.d/keepalived 

寻找大约15行左右的  . /etc/sysconfig/keepalived, 修改为: 
. /usr/local/keepalived/etc/sysconfig/keepalived, 即指向正确的文件位置

同时在上述行下添加以下内容(将keepavlied主程序所在路径导入到环境变量PATH中):

export PATH="$PATH:/usr/local/keepalived/sbin"


修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数 
KEEPALIVED_OPTIONS="-D  -f  /usr/local/keepalived/etc/keepalived/keepalived.conf"


4. 经过以上修改,keepalived基本安装即可完成,启动测试之: 
service keepalived restart


5. 切勿忘记将此服务设置为开机启动

chkconfig keepalived on


启动提示“Starting keepalived: keepalived: error while loading sharedlibraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory”

用find / -name libssl.so.1.0.0

系统显示路径在/usr/local/lib64下面

用命令  ldconfig  /usr/local/lib64  

此时重启启动成功。


(备注:可以用“ldd 可执行程序名” ldd /usr/local/keepalived/sbin/keepalived  会显示动态链接库)


虚拟IP在两机器中添加:(与tomcat服务器在同一网段)

Ifconfig查看网卡名然后操作

Ifconfig eth0:0 192.168.2.33 netmask 255.255.255.0


配置主从模式:

节点A的关键配置: 
节点A上的配置文件/usr/local/keepalived/etc/keepalived/keepalived.conf

global_defs {

notification_email {

root@localhost

}

notification_email_from root@local host

smtp_server localhost

smtp_connect_timeout 30

router_id  NodeA

}

vrrp_instance VI_1 {

state MASTER   #指定A节点为主节点 备用节点上设置为BACKUP即可

interface eth0   #绑定虚拟IP的网络接口

virtual_router_id 51  #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组

priority 100   #主节点的优先级(1-254之间),备用节点必须比主节点优先级低

advert_int 1   #组播信息发送间隔,两个节点设置必须一样

authentication {   #设置验证信息,两个节点必须一致

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {   #指定虚拟IP, 两个节点设置必须一样

192.168.200.16/24

}

}

默认的配置文件中,竟然没有子网掩码,从而导致使用了默认子网掩码255.255.255.255,如果导致无法从其它机器访问虚拟IP(keepalived虚拟IP无法ping通)。


按同样的方法配置节点B并修改配置文件,可将A节点的配置文件复制到B节点,并修改以下几项: 
router_id  NodeB

state   BACKUP

priority   99

其它项不必修改。

加入系统服务随机启动:chkconfig  keepalived  on


11.所有服务安装完成。可在网上查询把各服务器加入系统服务,随机启动。


为了,在网页中有所区分。把显示的页面改为以下配置:
增加一个Test.war 文件 放入到 /usr/share/tomcat/webapps目录下。

index.jsp文件如下

index.jsp文件如下vim /path/tomcat/webapps/index.jsp

TomcatA

12-10 03:18