centos 部署多个tomcat并systemd控制

centos 部署多个tomcat并systemd控制

tomcat包是绿色文件,所以以包的方式部署win和linux集群的方式是一样的,唯一需要更改的就是server.xml端口(保障端口不冲突),就实现了集群。

我的脚本:

bash <(curl https://gitee.com/ffing/onekey/raw/master/onekey.sh)

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-83.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

第一种方法:快捷方式

win集群:复制N个包,修改N个server.xml端口,创建N对快捷方式到桌面(start.bat、shutodwn.bat)

linux集群:复制N个包,修改N个server.xml端口,创建N对软链接到/usr/bin下(start.sh、shutdown.sh)

这种快捷方式部署的方式,效率很高,什么也不用配置,创建快捷方式就OK,但后期管理就很不方便了(譬如上个zabbix,脚本就需要再根据情况编写)

第二种方法:配置环境变量

配置环境变量,就可以实现systemd进行控制。

流程:

  • 下载安装tomcat
  • 配置环境变量
  • 更新tomcat变量
  • 修改tomcat访问端口
  • 防火墙放行端口
  • 创建systemd服务文件
  • 测试

本次集群以3个tomcat8为例:

(为了不影响原有tomcat,分别命名tomcat1、tomcat2、tomcat3)

  • tomcat1:/usr/local/tomcat-group/tomcat-8_1(路径及变量加_1,端口81xx)
  • tomcat2:/usr/locat/tomcat-group/tomcat-8_2(路径及变量加_2,端口82xx)
  • tomcat3:/usr/local/tomcat-group/tomcat-8_3(路径及变量加_3,端口83xx)

1.下载tomcat,安装到/usr/local

(我前面有详写:centos安装tomcat

官网下载:http://tomcat.apache.org/

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-68.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

下载tomcat,并复制2个tomcat包,并重命名

#安装wget(如已安装可以忽略)
yum -y install wget
#下载tomcat并重命名为tomcat-8.tar.gz(地址可能失效,请官网更新)
wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.73/bin/apache-tomcat-8.5.73.tar.gz --no-check-certificate -O tomcat-8.tar.gz
#解压(解压后的文件名需要更改为tomcat-8)
mkdir ./tomcat-8 && tar -xvf tomcat-8.tar.gz  -C ./tomcat-8 --strip-components 1
#创建/usr/local/tomcat-group目录
mkdir /usr/local/tomcat-group
#重命名为tomcat8_1
cp -r tomcat-8 /usr/local/tomcat-group/tomcat-8_1
#复制出第二个tomcat-8_2
cp -r tomcat-8 /usr/local/tomcat-group/tomcat-8_2
#复制出第三个tomcat-8_3
cp -r tomcat-8 /usr/local/tomcat-group/tomcat-8_3

2.配置环境变量

注:tomcat需java环境请提前安装!

编辑/etc/profile(如仅限本用户使用,修改家目录下.bash_profile)加如下变量

#tomcat_1环境变量
export TOMCAT_HOME_1=/usr/local/tomcat-group/tomcat-8_1
export CATALINA_HOME_1=$TOMCAT_HOME_1
export CATLINA_BASE_1=$TOMCAT_HOME_1

#tomcat_2环境变量
export TOMCAT_HOME_2=/usr/local/tomcat-group/tomcat-8_2
export CATALINA_HOME_2=$TOMCAT_HOME_2
export CATLINA_BASE_2=$TOMCAT_HOME_2

#tomcat_3环境变量
export TOMCAT_HOME_3=/usr/local/tomcat-group/tomcat-8_3
export CATALINA_HOME_3=$TOMCAT_HOME_3
export CATLINA_BASE_3=$TOMCAT_HOME_3

环境变量生效:

source /etc/profile

3.更新tomcat变量

将刚配置的环境变量加入到对应的tomcat*/bin/catalina.sh中

tomcat1:vi /usr/local/tomcat-group/tomcat-8_1/bin/catalina.sh

在#/bin/bash下追加环境变量:

#tomcat_1环境变量
export TOMCAT_HOME_1=/usr/local/tomcat-group/tomcat-8_1
export CATALINA_HOME_1=$TOMCAT_HOME_1
export CATLINA_BASE_1=$TOMCAT_HOME_1

tomcat2:vi /usr/local/tomcat-group/tomcat-8_2/bin/catalina.sh

在#/bin/bash下追加环境变量:

#tomcat_2环境变量
export TOMCAT_HOME_2=/usr/local/tomcat-group/tomcat-8_2
export CATALINA_HOME_2=$TOMCAT_HOME_2
export CATLINA_BASE_2=$TOMCAT_HOME_2

tomcat3:vi /usr/local/tomcat-group/tomcat-8_3/bin/catalina.sh

在#/bin/bash下追加环境变量:

#tomcat_3环境变量
export TOMCAT_HOME_3=/usr/local/tomcat-group/tomcat-8_3
export CATALINA_HOME_3=$TOMCAT_HOME_3
export CATLINA_BASE_3=$TOMCAT_HOME_3

4.修改tomcat访问端口

修改server.xml文件,保障端口不冲突,才可以每个tomcat正常访问。

https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-81.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0

tomcat1:编辑 /usr/local/tomcat-group/tomcat-8_1/conf/server.xml

8005端口:改为8105 | 8080端口:改为8180 | 8009端口:改为8109

sed -i 's/port="8080"/port="8180"/g' /usr/local/tomcat-group/tomcat-8_1/conf/server.xml
sed -i 's/port="8005"/port="8105"/g' /usr/local/tomcat-group/tomcat-8_1/conf/server.xml
sed -i 's/port="8009"/port="8109"/g' /usr/local/tomcat-group/tomcat-8_1/conf/server.xml

tomcat2:编辑 /usr/local/tomcat-group/tomcat-8_2/conf/server.xml

8005端口:改为8205 | 8080端口:改为8280 | 8009端口:改为8209

sed -i 's/port="8080"/port="8280"/g' /usr/local/tomcat-group/tomcat-8_2/conf/server.xml
sed -i 's/port="8005"/port="8205"/g' /usr/local/tomcat-group/tomcat-8_2/conf/server.xml
sed -i 's/port="8009"/port="8209"/g' /usr/local/tomcat-group/tomcat-8_2/conf/server.xml

tomcat3:编辑 /usr/local/tomcat-group/tomcat-8_3/conf/server.xml

8005端口:改为8305 | 8080端口:改为8380 | 8009端口:改为8309

sed -i 's/port="8080"/port="8380"/g' /usr/local/tomcat-group/tomcat-8_3/conf/server.xml
sed -i 's/port="8005"/port="8305"/g' /usr/local/tomcat-group/tomcat-8_3/conf/server.xml
sed -i 's/port="8009"/port="8309"/g' /usr/local/tomcat-group/tomcat-8_3/conf/server.xml

5.防火墙放行端口

放行3个tomcat对应的端口


#放行tomcat1:8180
firewall-cmd --add-port=8180/tcp --zone=public --permanent
#放行tomcat2:8280
firewall-cmd --add-port=8280/tcp --zone=public --permanent
#放行tomcat3:8380
firewall-cmd --add-port=8380/tcp --zone=public --permanent
#重新加载防火墙
firewall-cmd --reload

6.创建systemd服务文件

创建systemd服务文件,实现systemctl控制

创建3个tomcat服务文件,tomcat1.service、tomcat2.service、tomcat3.service,分别对应各自的路径文件。

tomcat1:vi /usr/lib/systemd/system/tomcat1.service

[Unit]
Description=tomcat-8_1
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/tomcat-group/tomcat-8_1/bin/startup.sh
ExecStop=/usr/local/tomcat-group/tomcat-8_1/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=ture
Restart=always
 
[Install]
WantedBy=multi-user.target

tomcat2:vi /usr/lib/systemd/system/tomcat2.service

[Unit]
Description=tomcat-8_2
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/tomcat-group/tomcat-8_2/bin/startup.sh
ExecStop=/usr/local/tomcat-group/tomcat-8_2/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=ture
Restart=always
 
[Install]
WantedBy=multi-user.target

tomcat3:vi /usr/lib/systemd/system/tomcat3.service

[Unit]
Description=tomcat-8_3
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/tomcat-group/tomcat-8_3/bin/startup.sh
ExecStop=/usr/local/tomcat-group/tomcat-8_3/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=ture
Restart=always
 
[Install]
WantedBy=multi-user.target

重新加载服务

systemctl daemon-reload

7.测试

为了方便区分3个tomcat内容,分别向第二个,和第三个tomcat主页加入自己tomcat名称

#第一个tomcat主页,body标签下加入“这是tomcat1”
sed -i '/<body>/a 这是tomcat1' /usr/local/tomcat-group/tomcat-8_1/webapps/ROOT/index.jsp
#第二个tomcat主页,body标签下加入“这是tomcat2”
sed -i '/<body>/a 这是tomcat2' /usr/local/tomcat-group/tomcat-8_2/webapps/ROOT/index.jsp
#第三个tomcat主页,body标签下加入“这是tomcat3”
sed -i '/<body>/a 这是tomcat3' /usr/local/tomcat-group/tomcat-8_3/webapps/ROOT/index.jsp

重启3个tomcat:

#懒人一次重启3个(当然你也可以单独启动每一个,进行测试)
systemctl restart tomcat1 tomcat2 tomcat3
https://qnimg.ffing.cn/wp-content/uploads/2021/11/image-86.png?imageView2/0/q/75|watermark/1/image/aHR0cHM6Ly9xbmltZy5mZmluZy5jbi9mbl9sb2dvLnBuZw==/dissolve/55/gravity/SouthEast/dx/0/dy/0
good good study, day day up!

发表评论

textsms
account_circle
email

centos 部署多个tomcat并systemd控制
tomcat包是绿色文件,所以以包的方式部署win和linux集群的方式是一样的,唯一需要更改的就是server.xml端口(保障端口不冲突),就实现了集群。 我的脚本: bash <(curl https:/…
扫描二维码继续阅读
2021-11-18