web环境准备

               web环境准备

                                     作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.配置Java环境

1>.下载JDK

  博主推荐阅读:
    https://www.cnblogs.com/yinzhengjie/p/12199413.html

2>.解压JDK并创建相关符号链接

[root@tomcat101.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/softwares
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# tar -zxf jdk-8u201-linux-x64.tar.gz -C /yinzhengjie/softwares/
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/
total 0
drwxr-xr-x 3 root root  26 Mar 18 14:04 ./
drwxr-xr-x 3 root root  23 Mar 18 14:04 ../
drwxr-xr-x 7 uucp  143 245 Dec 15  2018 jdk1.8.0_201/
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# ln -sv /yinzhengjie/softwares/jdk1.8.0_201/ /usr/local/jdk        #将咱们安装的版本指定软连接到/usr/local/jdk
'/usr/local/jdk' -> '/yinzhengjie/softwares/jdk1.8.0_201/'
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# ln -sv /usr/local/jdk/bin/java /usr/bin/                   #Jenkins会在/usr/bin目录下找java命令,因此我们需要创建该符号连接
'/usr/bin/java' -> '/usr/local/jdk/bin/java'
[root@tomcat101.yinzhengjie.org.cn ~]#

3>.配置环境变量

[root@tomcat101.yinzhengjie.org.cn ~]# vim /etc/profile.d/jdk.sh
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# cat /etc/profile.d/jdk.sh
export HISTTIMEFORMAT="%F %T `whoami`"
export export LANG="en_US.utf8"
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# source /etc/profile.d/jdk.sh
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
[root@tomcat101.yinzhengjie.org.cn ~]# 

二.部署tomcat服务

1>.下载tomcat

  博主推荐阅读:
    https://www.cnblogs.com/yinzhengjie/p/12199468.html

2>.解压tomcat并创建符号链接

[root@tomcat101.yinzhengjie.org.cn ~]# tar zxf apache-tomcat-8.5.51.tar.gz -C /yinzhengjie/softwares/
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# ln -sv /yinzhengjie/softwares/apache-tomcat-8.5.51/ /yinzhengjie/softwares/tomcat
'/yinzhengjie/softwares/tomcat' -> '/yinzhengjie/softwares/apache-tomcat-8.5.51/'
[root@tomcat101.yinzhengjie.org.cn ~]# 

3>.创建tomcat的相关目录

[root@tomcat101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/tomcat/{appdir,webdir}
mkdir: created directory '/yinzhengjie/data'
mkdir: created directory '/yinzhengjie/data/tomcat'
mkdir: created directory '/yinzhengjie/data/tomcat/appdir'        #用来保存web的压缩包
mkdir: created directory '/yinzhengjie/data/tomcat/webdir'        #用来保存解压后的web目录
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/tomcat/webdir/myapp
mkdir: created directory '/yinzhengjie/data/tomcat/webdir/myapp'
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# vim /yinzhengjie/softwares/tomcat/conf/server.xml 
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# grep appBase /yinzhengjie/softwares/tomcat/conf/server.xml         #编辑配置文件,指定咱们的应用程序路径
      <Host name="localhost"  appBase="/yinzhengjie/data/tomcat/webdir"
[root@tomcat101.yinzhengjie.org.cn ~]# 

4>.创建自定义首页

[root@tomcat101.yinzhengjie.org.cn ~]# vim /yinzhengjie/data/tomcat/webdir/myapp/index.html
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/tomcat/webdir/myapp/index.html
<h1>tomcat101.yinzhengjie.org.cn</h1>
[root@tomcat101.yinzhengjie.org.cn ~]# 

5>.创建www用户并将tomcat的应用程序和数据目录权限授予www用户

[root@tomcat101.yinzhengjie.org.cn ~]# useradd www -u 2020 -m -s /bin/bash 
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# id www
uid=2020(www) gid=2020(www) groups=2020(www)
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# su -l www
www@tomcat101:~$ 
www@tomcat101:~$ pwd
/home/www
www@tomcat101:~$      
www@tomcat101:~$ echo $SHELL
/bin/bash
www@tomcat101:~$ 
www@tomcat101:~$ exit
logout
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat102.yinzhengjie.org.cn ~]# chown www:www /yinzhengjie/softwares/tomcat /yinzhengjie/softwares/apache-tomcat-8.5.51 /yinzhengjie/data/tomcat -R
[root@tomcat102.yinzhengjie.org.cn ~]# 
[root@tomcat102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/tomcat -d
lrwxrwxrwx 1 root root 44 Mar 18 14:26 /yinzhengjie/softwares/tomcat -> /yinzhengjie/softwares/apache-tomcat-8.5.51//
[root@tomcat102.yinzhengjie.org.cn ~]# 
[root@tomcat102.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/apache-tomcat-8.5.51 -d
drwxr-xr-x 9 www www 220 Mar 18 14:24 /yinzhengjie/softwares/apache-tomcat-8.5.51/
[root@tomcat102.yinzhengjie.org.cn ~]# 
[root@tomcat102.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/tomcat -d
drwxr-xr-x 4 www www 34 Mar 18 14:32 /yinzhengjie/data/tomcat/
[root@tomcat102.yinzhengjie.org.cn ~]# 
[root@tomcat102.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/tomcat 
total 0
drwxr-xr-x 4 www  www  34 Mar 18 14:32 ./
drwxr-xr-x 3 root root 20 Mar 18 14:32 ../
drwxr-xr-x 2 www  www   6 Mar 18 14:32 appdir/
drwxr-xr-x 3 www  www  19 Mar 18 14:33 webdir/
[root@tomcat102.yinzhengjie.org.cn ~]# 

6>.使用www用户启动tomcat服务

[root@tomcat101.yinzhengjie.org.cn ~]# su -l www
www@tomcat101:~$ 
www@tomcat101:~$ /yinzhengjie/softwares/tomcat/bin/catalina.sh start
Using CATALINA_BASE:   /yinzhengjie/softwares/tomcat
Using CATALINA_HOME:   /yinzhengjie/softwares/tomcat
Using CATALINA_TMPDIR: /yinzhengjie/softwares/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /yinzhengjie/softwares/tomcat/bin/bootstrap.jar:/yinzhengjie/softwares/tomcat/bin/tomcat-juli.jar
Tomcat started.
www@tomcat101:~$ 
www@tomcat101:~$ tail -100f /yinzhengjie/softwares/tomcat/logs/catalina.out 
18-Mar-2020 14:49:05.926 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/8.5.51
18-Mar-2020 14:49:05.929 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Feb 5 2020 22:26:25 UTC
18-Mar-2020 14:49:05.929 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.51.0
18-Mar-2020 14:49:05.929 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
18-Mar-2020 14:49:05.929 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.15.0-55-generic
18-Mar-2020 14:49:05.930 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
18-Mar-2020 14:49:05.930 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /yinzhengjie/softwares/jdk1.8.0_201/jre
18-Mar-2020 14:49:05.930 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_201-b09
18-Mar-2020 14:49:05.931 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
18-Mar-2020 14:49:05.931 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /yinzhengjie/softwares/apache-tomcat-8.5.51
18-Mar-2020 14:49:05.931 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /yinzhengjie/softwares/apache-tomcat-8.5.51
18-Mar-2020 14:49:05.931 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/yinzhengjie/softwares/tomcat/conf/logging.properties
18-Mar-2020 14:49:05.932 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
18-Mar-2020 14:49:05.932 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
18-Mar-2020 14:49:05.933 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
18-Mar-2020 14:49:05.933 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
18-Mar-2020 14:49:05.933 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
18-Mar-2020 14:49:05.933 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/yinzhengjie/softwares/tomcat
18-Mar-2020 14:49:05.933 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/yinzhengjie/softwares/tomcat
18-Mar-2020 14:49:05.933 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/yinzhengjie/softwares/tomcat/temp
18-Mar-2020 14:49:05.934 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64
:/usr/lib64:/lib64:/lib:/usr/lib]18-Mar-2020 14:49:06.095 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
18-Mar-2020 14:49:06.111 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
18-Mar-2020 14:49:06.121 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1278 ms
18-Mar-2020 14:49:06.150 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
18-Mar-2020 14:49:06.151 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.51
18-Mar-2020 14:49:06.165 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/yinzhengjie/data/tomcat/webdir/myapp]
18-Mar-2020 14:49:06.633 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/yinzhengjie/data/tomcat/webdir/myapp] has finished in [468] ms
18-Mar-2020 14:49:06.636 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
18-Mar-2020 14:49:06.779 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 657 ms
www@tomcat101:~$ tail -100f /yinzhengjie/softwares/tomcat/logs/catalina.out

7>.访问tomcat的webUI

  我有两台服务器按照上述步骤配置JAVA环境和Tomcat环境并使用www启动tomcat服务,然后在浏览器访问以下地址:
    http://tomcat101.yinzhengjie.org.cn:8080/myapp/
    http://tomcat102.yinzhengjie.org.cn:8080/myapp/

  如下图所示,2个tomcat节点均可以访问成功。

8>.编写tomcat的启动脚本

[root@tomcat101.yinzhengjie.org.cn ~]# cat /etc/init.d/tomcat.sh
#!/bin/bash
#
#********************************************************************
#Author:        yinzhengjie
#QQ:             1053419035
#Date:             2019-11-25
#URL:             http://www.cnblogs.com/yinzhengjie
#Description:        The manager tomcat script
#Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
#********************************************************************

JDK_HOME=/usr/local/jdk
CATALINT_HOME=/yinzhengjie/softwares/tomcat
export JDK_HOME CATALINT_HOME
source /etc/profile.d/jdk.sh


start() {
    echo "正在判断服务状态,请稍等..."
    if netstat -an | grep 8080 | grep LISTEN >/dev/null
        then
        echo "Tomcat正在运行中..."
        else
        echo "正在启动Tomcat,请稍等..."
        ${CATALINT_HOME}/bin/catalina.sh start
        sleep 5
        if netstat -an | grep 8080 | grep LISTEN >/dev/null
                then
            PID=`ps -ef | grep tomcat | grep jdk | awk '{print $2}'`
            NUM=`ps -ef | grep tomcat | grep jdk | awk '{print $2}' | wc -l`
            echo "Tomcat 已经成功启动${NUM}个进程,PID为${PID}"
            else
            echo "Tomcat 启动失败,请重新启动!"
        fi
    fi
}

stop() {
    echo "正在判断服务状态,请稍等..."
    if netstat -an | grep 8080 | grep LISTEN >/dev/null
        then
            echo "即将关闭tomcat服务,请稍等..."
            ${CATALINT_HOME}/bin/catalina.sh stop
            sleep 3
            pkill java && kill tomcat
            if netstat -an | grep 8080 | grep LISTEN >/dev/null;then
            PID=`ps -ef | grep -v grep | grep java | awk '{print $2}'`
            NUM=`ps -ef | grep -v "color" | grep tomcat | awk '{print $2}' | wc -l`
            kill -9 ${PID}
            echo "已成功关闭${NUM}个tomcat进程"
            else
            echo "Tomcat服务已被关闭..."
            fi
        else
            echo "Tomcat未启动"
    fi
}

restart() {
    stop
    start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
esac
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# cat /etc/init.d/tomcat.sh
[root@tomcat101.yinzhengjie.org.cn ~]# ll /etc/init.d/tomcat.sh
-rw-r--r-- 1 root root 1983 Mar 18 15:35 /etc/init.d/tomcat.sh
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# ll /etc/init.d/tomcat.sh
-rw-r--r-- 1 root root 1983 Mar 18 15:35 /etc/init.d/tomcat.sh
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# chmod +x /etc/init.d/tomcat.sh
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# ll /etc/init.d/tomcat.sh
-rwxr-xr-x 1 root root 1983 Mar 18 15:35 /etc/init.d/tomcat.sh*
[root@tomcat101.yinzhengjie.org.cn ~]# 
[root@tomcat101.yinzhengjie.org.cn ~]# chmod +x /etc/init.d/tomcat.sh

9>.使用www用户测试tomcat的启动脚本

[root@tomcat101.yinzhengjie.org.cn ~]# su -l www
www@tomcat101:~$ 
www@tomcat101:~$ ss -ntl
State                              Recv-Q                              Send-Q                                                                   Local Address:Port                                                             Peer Address:Port                              
LISTEN                             0                                   128                                                                      127.0.0.53%lo:53                                                                    0.0.0.0:*                                 
LISTEN                             0                                   128                                                                            0.0.0.0:22                                                                    0.0.0.0:*                                 
LISTEN                             0                                   100                                                                                  *:8080                                                                        *:*                                 
LISTEN                             0                                   128                                                                               [::]:22                                                                       [::]:*                                 
LISTEN                             0                                   1                                                                   [::ffff:127.0.0.1]:8005                                                                        *:*                                 
www@tomcat101:~$ 
www@tomcat101:~$ /etc/init.d/tomcat.sh stop
正在判断服务状态,请稍等...
即将关闭tomcat服务,请稍等...
Using CATALINA_BASE:   /yinzhengjie/softwares/tomcat
Using CATALINA_HOME:   /yinzhengjie/softwares/tomcat
Using CATALINA_TMPDIR: /yinzhengjie/softwares/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /yinzhengjie/softwares/tomcat/bin/bootstrap.jar:/yinzhengjie/softwares/tomcat/bin/tomcat-juli.jar
Tomcat服务已被关闭...
www@tomcat101:~$ 
www@tomcat101:~$ ss -ntl
State                               Recv-Q                               Send-Q                                                              Local Address:Port                                                               Peer Address:Port                               
LISTEN                              0                                    128                                                                 127.0.0.53%lo:53                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                       0.0.0.0:22                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                          [::]:22                                                                         [::]:*                                  
www@tomcat101:~$ 
www@tomcat101:~$ 
www@tomcat101:~$ /etc/init.d/tomcat.sh stop
www@tomcat101:~$ ss -ntl
State                               Recv-Q                               Send-Q                                                              Local Address:Port                                                               Peer Address:Port                               
LISTEN                              0                                    128                                                                 127.0.0.53%lo:53                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                       0.0.0.0:22                                                                      0.0.0.0:*                                  
LISTEN                              0                                    128                                                                          [::]:22                                                                         [::]:*                                  
www@tomcat101:~$ 
www@tomcat101:~$ 
www@tomcat101:~$ /etc/init.d/tomcat.sh start
正在判断服务状态,请稍等...
正在启动Tomcat,请稍等...
Using CATALINA_BASE:   /yinzhengjie/softwares/tomcat
Using CATALINA_HOME:   /yinzhengjie/softwares/tomcat
Using CATALINA_TMPDIR: /yinzhengjie/softwares/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /yinzhengjie/softwares/tomcat/bin/bootstrap.jar:/yinzhengjie/softwares/tomcat/bin/tomcat-juli.jar
Tomcat started.
Tomcat 已经成功启动1个进程,PID为3396
www@tomcat101:~$ 
www@tomcat101:~$ ss -ntl
State                              Recv-Q                              Send-Q                                                                   Local Address:Port                                                             Peer Address:Port                              
LISTEN                             0                                   128                                                                      127.0.0.53%lo:53                                                                    0.0.0.0:*                                 
LISTEN                             0                                   128                                                                            0.0.0.0:22                                                                    0.0.0.0:*                                 
LISTEN                             0                                   100                                                                                  *:8080                                                                        *:*                                 
LISTEN                             0                                   128                                                                               [::]:22                                                                       [::]:*                                 
LISTEN                             0                                   1                                                                   [::ffff:127.0.0.1]:8005                                                                        *:*                                 
www@tomcat101:~$ 
www@tomcat101:~$ 
www@tomcat101:~$ /etc/init.d/tomcat.sh start
www@tomcat101:~$ ss -ntl
State                              Recv-Q                              Send-Q                                                                   Local Address:Port                                                             Peer Address:Port                              
LISTEN                             0                                   128                                                                      127.0.0.53%lo:53                                                                    0.0.0.0:*                                 
LISTEN                             0                                   128                                                                            0.0.0.0:22                                                                    0.0.0.0:*                                 
LISTEN                             0                                   100                                                                                  *:8080                                                                        *:*                                 
LISTEN                             0                                   128                                                                               [::]:22                                                                       [::]:*                                 
LISTEN                             0                                   1                                                                   [::ffff:127.0.0.1]:8005                                                                        *:*                                 
www@tomcat101:~$ 
www@tomcat101:~$ jps
3396 Bootstrap
3446 Jps
www@tomcat101:~$ 
www@tomcat101:~$ /etc/init.d/tomcat.sh restart
正在判断服务状态,请稍等...
即将关闭tomcat服务,请稍等...
Using CATALINA_BASE:   /yinzhengjie/softwares/tomcat
Using CATALINA_HOME:   /yinzhengjie/softwares/tomcat
Using CATALINA_TMPDIR: /yinzhengjie/softwares/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /yinzhengjie/softwares/tomcat/bin/bootstrap.jar:/yinzhengjie/softwares/tomcat/bin/tomcat-juli.jar
Tomcat服务已被关闭...
正在判断服务状态,请稍等...
正在启动Tomcat,请稍等...
Using CATALINA_BASE:   /yinzhengjie/softwares/tomcat
Using CATALINA_HOME:   /yinzhengjie/softwares/tomcat
Using CATALINA_TMPDIR: /yinzhengjie/softwares/tomcat/temp
Using JRE_HOME:        /usr/local/jdk
Using CLASSPATH:       /yinzhengjie/softwares/tomcat/bin/bootstrap.jar:/yinzhengjie/softwares/tomcat/bin/tomcat-juli.jar
Tomcat started.
Tomcat 已经成功启动1个进程,PID为3501
www@tomcat101:~$ 
www@tomcat101:~$ ss -ntl
State                              Recv-Q                              Send-Q                                                                   Local Address:Port                                                             Peer Address:Port                              
LISTEN                             0                                   128                                                                      127.0.0.53%lo:53                                                                    0.0.0.0:*                                 
LISTEN                             0                                   128                                                                            0.0.0.0:22                                                                    0.0.0.0:*                                 
LISTEN                             0                                   100                                                                                  *:8080                                                                        *:*                                 
LISTEN                             0                                   128                                                                               [::]:22                                                                       [::]:*                                 
LISTEN                             0                                   1                                                                   [::ffff:127.0.0.1]:8005                                                                        *:*                                 
www@tomcat101:~$ 
www@tomcat101:~$ jps
3549 Jps
3501 Bootstrap
www@tomcat101:~$ 
www@tomcat101:~$ 
www@tomcat101:~$ /etc/init.d/tomcat.sh restart

三.部署keepalived

1>.安装keepalived

[root@jenkins210.yinzhengjie.org.cn ~]# yum -y install keepalived
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
base                                                                                                                                                                               | 3.6 kB  00:00:00     
extras                                                                                                                                                                             | 2.9 kB  00:00:00     
updates                                                                                                                                                                            | 2.9 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.3.5-16.el7 will be installed
--> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.3.5-16.el7.x86_64
--> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.3.5-16.el7.x86_64
--> Processing Dependency: libnetsnmp.so.31()(64bit) for package: keepalived-1.3.5-16.el7.x86_64
--> Running transaction check
---> Package net-snmp-agent-libs.x86_64 1:5.7.2-43.el7_7.3 will be installed
--> Processing Dependency: libsensors.so.4()(64bit) for package: 1:net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64
---> Package net-snmp-libs.x86_64 1:5.7.2-43.el7_7.3 will be installed
--> Running transaction check
---> Package lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                              Arch                                                    Version                                                                          Repository                                                Size
==============================================================================================================================================================================================================================================================================
Installing:
 keepalived                                                           x86_64                                                  1.3.5-16.el7                                                                     base                                                     331 k
Installing for dependencies:
 lm_sensors-libs                                                      x86_64                                                  3.4.0-8.20160601gitf9185e5.el7                                                   base                                                      42 k
 net-snmp-agent-libs                                                  x86_64                                                  1:5.7.2-43.el7_7.3                                                               updates                                                  707 k
 net-snmp-libs                                                        x86_64                                                  1:5.7.2-43.el7_7.3                                                               updates                                                  750 k

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 1.8 M
Installed size: 6.0 M
Downloading packages:
(1/4): lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64.rpm                                                                                                                                                                                       |  42 kB  00:00:02     
(2/4): keepalived-1.3.5-16.el7.x86_64.rpm                                                                                                                                                                                                              | 331 kB  00:00:02     
(3/4): net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64.rpm                                                                                                                                                                                                 | 707 kB  00:00:02     
(4/4): net-snmp-libs-5.7.2-43.el7_7.3.x86_64.rpm                                                                                                                                                                                                       | 750 kB  00:00:02     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                         522 kB/s | 1.8 MB  00:00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:net-snmp-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                                    1/4 
  Installing : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                                                                                                                                                                                      2/4 
  Installing : 1:net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                              3/4 
  Installing : keepalived-1.3.5-16.el7.x86_64                                                                                                                                                                                                                             4/4 
  Verifying  : keepalived-1.3.5-16.el7.x86_64                                                                                                                                                                                                                             1/4 
  Verifying  : 1:net-snmp-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                                    2/4 
  Verifying  : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                                                                                                                                                                                      3/4 
  Verifying  : 1:net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                              4/4 

Installed:
  keepalived.x86_64 0:1.3.5-16.el7                                                                                                                                                                                                                                            

Dependency Installed:
  lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7                                           net-snmp-agent-libs.x86_64 1:5.7.2-43.el7_7.3                                           net-snmp-libs.x86_64 1:5.7.2-43.el7_7.3                                          

Complete!
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# yum -y install keepalived
[root@jenkins215.yinzhengjie.org.cn ~]# yum -y install keepalived
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
base                                                                                                                                                                                                                                                   | 3.6 kB  00:00:00     
extras                                                                                                                                                                                                                                                 | 2.9 kB  00:00:00     
updates                                                                                                                                                                                                                                                | 2.9 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                                                                                                      | 164 kB  00:00:00     
(2/2): updates/7/x86_64/primary_db                                                                                                                                                                                                                     | 6.7 MB  00:00:03     
Resolving Dependencies
--> Running transaction check
---> Package keepalived.x86_64 0:1.3.5-16.el7 will be installed
--> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.3.5-16.el7.x86_64
--> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.3.5-16.el7.x86_64
--> Processing Dependency: libnetsnmp.so.31()(64bit) for package: keepalived-1.3.5-16.el7.x86_64
--> Running transaction check
---> Package net-snmp-agent-libs.x86_64 1:5.7.2-43.el7_7.3 will be installed
--> Processing Dependency: libsensors.so.4()(64bit) for package: 1:net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64
---> Package net-snmp-libs.x86_64 1:5.7.2-43.el7_7.3 will be installed
--> Running transaction check
---> Package lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                              Arch                                                    Version                                                                          Repository                                                Size
==============================================================================================================================================================================================================================================================================
Installing:
 keepalived                                                           x86_64                                                  1.3.5-16.el7                                                                     base                                                     331 k
Installing for dependencies:
 lm_sensors-libs                                                      x86_64                                                  3.4.0-8.20160601gitf9185e5.el7                                                   base                                                      42 k
 net-snmp-agent-libs                                                  x86_64                                                  1:5.7.2-43.el7_7.3                                                               updates                                                  707 k
 net-snmp-libs                                                        x86_64                                                  1:5.7.2-43.el7_7.3                                                               updates                                                  750 k

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 1.8 M
Installed size: 6.0 M
Downloading packages:
(1/4): keepalived-1.3.5-16.el7.x86_64.rpm                                                                                                                                                                                                              | 331 kB  00:00:00     
(2/4): lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64.rpm                                                                                                                                                                                       |  42 kB  00:00:00     
(3/4): net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64.rpm                                                                                                                                                                                                 | 707 kB  00:00:00     
(4/4): net-snmp-libs-5.7.2-43.el7_7.3.x86_64.rpm                                                                                                                                                                                                       | 750 kB  00:00:00     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                                                                                         1.6 MB/s | 1.8 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:net-snmp-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                                    1/4 
  Installing : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                                                                                                                                                                                      2/4 
  Installing : 1:net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                              3/4 
  Installing : keepalived-1.3.5-16.el7.x86_64                                                                                                                                                                                                                             4/4 
  Verifying  : keepalived-1.3.5-16.el7.x86_64                                                                                                                                                                                                                             1/4 
  Verifying  : 1:net-snmp-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                                    2/4 
  Verifying  : lm_sensors-libs-3.4.0-8.20160601gitf9185e5.el7.x86_64                                                                                                                                                                                                      3/4 
  Verifying  : 1:net-snmp-agent-libs-5.7.2-43.el7_7.3.x86_64                                                                                                                                                                                                              4/4 

Installed:
  keepalived.x86_64 0:1.3.5-16.el7                                                                                                                                                                                                                                            

Dependency Installed:
  lm_sensors-libs.x86_64 0:3.4.0-8.20160601gitf9185e5.el7                                           net-snmp-agent-libs.x86_64 1:5.7.2-43.el7_7.3                                           net-snmp-libs.x86_64 1:5.7.2-43.el7_7.3                                          

Complete!
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# yum -y install keepalived

2>."jenkins215.yinzhengjie.org.cn"节点配置keepalived并配置开启自启动

[root@jenkins215.yinzhengjie.org.cn ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
     jason@yinzhengjie.org.cn
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface bond0
    virtual_router_id 51
    nopreempt
    priority 100
    advert_int 1
    virtual_ipaddress {
        172.200.2.254 dev bond0 label bond0:1
    }
}
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# systemctl start keepalived.service 
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# systemctl enable keepalived.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# ifconfig bond0:1
bond0:1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        inet 172.200.2.254  netmask 255.255.255.255  broadcast 0.0.0.0
        ether 00:0c:29:14:f3:05  txqueuelen 1000  (Ethernet)

[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# 

3>."jenkins210.yinzhengjie.org.cn"节点配置keepalived并配置开启自启动

[root@jenkins210.yinzhengjie.org.cn ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     jason@yinzhengjie.org.cn
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface bond0
    virtual_router_id 51
    nopreempt
    priority 50
    advert_int 1
    virtual_ipaddress {
        172.200.2.254 dev bond0 label bond0:1
    }
}
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# systemctl start keepalived.service 
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# systemctl enable keepalived.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# ifconfig bond0:1
bond0:1: flags=5187<UP,BROADCAST,RUNNING,MASTER,MULTICAST>  mtu 1500
        ether 00:0c:29:91:ce:86  txqueuelen 1000  (Ethernet)

[root@jenkins210.yinzhengjie.org.cn ~]# 

四.部署haproxy

1>.安装haproxy

[root@jenkins210.yinzhengjie.org.cn ~]# yum -y install haproxy
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package haproxy.x86_64 0:1.5.18-9.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                          Arch                                                            Version                                                                 Repository                                                     Size
==============================================================================================================================================================================================================================================================================
Installing:
 haproxy                                                          x86_64                                                          1.5.18-9.el7                                                            base                                                          834 k

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  1 Package

Total download size: 834 k
Installed size: 2.6 M
Downloading packages:
haproxy-1.5.18-9.el7.x86_64.rpm                                                                                                                                                                                                                        | 834 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : haproxy-1.5.18-9.el7.x86_64                                                                                                                                                                                                                                1/1 
  Verifying  : haproxy-1.5.18-9.el7.x86_64                                                                                                                                                                                                                                1/1 

Installed:
  haproxy.x86_64 0:1.5.18-9.el7                                                                                                                                                                                                                                               

Complete!
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# yum -y install haproxy
[root@jenkins215.yinzhengjie.org.cn ~]# yum -y install haproxy
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package haproxy.x86_64 0:1.5.18-9.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
 Package                                                          Arch                                                            Version                                                                 Repository                                                     Size
==============================================================================================================================================================================================================================================================================
Installing:
 haproxy                                                          x86_64                                                          1.5.18-9.el7                                                            base                                                          834 k

Transaction Summary
==============================================================================================================================================================================================================================================================================
Install  1 Package

Total download size: 834 k
Installed size: 2.6 M
Downloading packages:
haproxy-1.5.18-9.el7.x86_64.rpm                                                                                                                                                                                                                        | 834 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : haproxy-1.5.18-9.el7.x86_64                                                                                                                                                                                                                                1/1 
  Verifying  : haproxy-1.5.18-9.el7.x86_64                                                                                                                                                                                                                                1/1 

Installed:
  haproxy.x86_64 0:1.5.18-9.el7                                                                                                                                                                                                                                               

Complete!
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# yum -y install haproxy

2>.各节点修改内核参数

[root@jenkins210.yinzhengjie.org.cn ~]# vim /etc/sysctl.d/haproxy.conf
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# cat /etc/sysctl.d/haproxy.conf
net.ipv4.ip_nonlocal_bind = 1
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# sysctl -f /etc/sysctl.d/haproxy.conf
net.ipv4.ip_nonlocal_bind = 1
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# sysctl -q net.ipv4.ip_nonlocal_bind 
net.ipv4.ip_nonlocal_bind = 1
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# vim /etc/sysctl.d/haproxy.conf
[root@jenkins215.yinzhengjie.org.cn ~]# sysctl -q net.ipv4.ip_nonlocal_bind 
net.ipv4.ip_nonlocal_bind = 0
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# vim /etc/sysctl.d/haproxy.conf
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# cat /etc/sysctl.d/haproxy.conf
net.ipv4.ip_nonlocal_bind = 1
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# sysctl -f /etc/sysctl.d/haproxy.conf
net.ipv4.ip_nonlocal_bind = 1
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# sysctl -q net.ipv4.ip_nonlocal_bind 
net.ipv4.ip_nonlocal_bind = 1
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# 

3>.编写配置文件

[root@jenkins215.yinzhengjie.org.cn ~]# systemctl restart haproxy.service
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# systemctl enable haproxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# ss -ntl;
State      Recv-Q Send-Q                                                                                          Local Address:Port                                                                                                         Peer Address:Port              
LISTEN     0      3000                                                                                                        *:5000                                                                                                                    *:*                  
LISTEN     0      3000                                                                                            172.200.2.254:80                                                                                                                      *:*                  
LISTEN     0      128                                                                                                         *:22                                                                                                                      *:*                  
LISTEN     0      128                                                                                                        :::22                                                                                                                     :::*                  
[root@jenkins215.yinzhengjie.org.cn ~]# 
[root@jenkins215.yinzhengjie.org.cn ~]# systemctl restart haproxy.service
[root@jenkins210.yinzhengjie.org.cn ~]# tail -6 /etc/haproxy/haproxy.cfg 
#Add by yinzhengjie
listen myapp
    bind 172.200.2.254:80
    mode tcp
    server 172.200.2.101 172.200.2.101:8080 check
    server 172.200.2.102 172.200.2.102:8080 check
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# systemctl restart haproxy.service 
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# systemctl enable haproxy.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# ss -ntl
State      Recv-Q Send-Q                                                                                          Local Address:Port                                                                                                         Peer Address:Port              
LISTEN     0      3000                                                                                                        *:5000                                                                                                                    *:*                  
LISTEN     0      3000                                                                                            172.200.2.254:80                                                                                                                      *:*                  
LISTEN     0      128                                                                                                         *:22                                                                                                                      *:*                  
LISTEN     0      128                                                                                                        :::22                                                                                                                     :::*                  
[root@jenkins210.yinzhengjie.org.cn ~]# 
[root@jenkins210.yinzhengjie.org.cn ~]# 

原文地址:https://www.cnblogs.com/yinzhengjie2020/p/12495586.html