新公司,开发用java,跑在tomcat里,一台机器会跑多个tomcat,原来的运维人员是复制了多份tomcat放到目录下。这样做我觉得管理不方便,遂改为superviosr来控制,同时将tomcat本身与工程文件分开放置。即一台主机只有一份tomcat可执行文件,这样也方便以后升级tomcat,下边简要流水账一下,不解释细节,都能看懂。另外,我把安装过程写成了ansible playbook,需要的可以去我的github上下载,https://github.com/polygun2000
1.安装Server JRE
]# mkdir /usr/local/java/
]# tar xvzf server-jre-8u121-linux-x64.tar.gz –strip-components=1 -C /usr/local/java
2.安装supervisor
可以用yum直接安装,epel源中有,不过版本比较低。
推荐的方式是使用pip安装,需要机器能访问internet。
如果不能访问internet,可以在能访问的机器上直接下载源码,复制过来后解压,用python setup.py install方式安装。
注意:supervisor依赖meld3,但是pip安装的meld3有可能版本太低,启动supervisor会报错,可以下载meld3-1.0.2.tar.gz,采用python setup.py install方式手工安装。
]# pip install supervisor
supervisor查找配置文件的顺序如下,其中 $CWD 代表当前工作目录 :
$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf (Supervisor 3.3.0以后引入)
../etc/supervisord.conf (相对于supervisor可执行文件的路径)
../supervisord.conf (相对于supervisor可执行文件的路径)
通常使用/etc/supervisord.conf
创建supervisord的启动脚本,如果是用yum装的就不必了,系统自带了。
]# vi /usr/lib/systemd/system/supervisord.service
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
[Install]
WantedBy=multi-user.target
3.安装tomcat
]# mkdir /usr/local/tomcat
]# groupadd tomcat
]# useradd -s /bin/nologin -d /usr/local/tomcat -M tomcat
]# tar xvzf apache-tomcat-8.0.43.tar.gz –strip-components=1 -C /usr/local/tomcat
]# cd /usr/local/tomcat
]# rm -rf webapps logs temp work
4.准备各独立的tomcat实例目录,这里只举一个website1的例子,更多只需要重复这步即可。
]# mkdir -p /data/apps/website1/{bin,webapps,lib,logs,temp,work}
]# cp -r /usr/local/tomcat/conf /data/apps/website1/
]# cp /usr/local/tomcat/bin/*.sh /data/apps/website1/bin
]# cd /data/apps/website1
]# chmod g+w temp logs work
]# vi /data/apps/website1/bin/setenv.sh
#!/bin/sh
CATALINA_HOME=/usr/local/tomcat CATALINA_BASE=/data/apps/website1/CATALINA_PID=/data/apps/website1/temp/tomcat.pidJAVA_HOME=/usr/local/java
]# chmod 755 /data/apps/website1/bin/setenv.sh
]# vi /data/apps/website1/bin/supervisord-wrapper.sh
#!/bin/bash# Source: http://serverfault.com/questions/425132/controlling-tomcat-with-supervisorfunction shutdown(){date echo “Shutting down Tomcat” unset CATALINA_PID # Necessary in some cases /data/apps/website1/bin/catalina.sh stop }dateecho “Starting Tomcat”. /data/apps/website1/bin/catalina.sh start -security# Allow any signal which would kill a process to stop Tomcattrap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTPecho “Waiting for `cat $CATALINA_PID`”wait `cat $CATALINA_PID`
]# chmod 755 /data/apps/website1/bin/supervisord-wrapper.sh
]# chown -R tomcat.tomcat /data/apps/website1
配置catalina.out日志文件的轮转,以免硬盘被写满
]# vi /etc/logrotate.d/tomcat-website1
/data/apps/website1/logs/catalina.out {copytruncate daily rotate 7 compress missingok size 10M dateext }
5.配置supervisor的program
]# vi /etc/supervisord.d/website1.ini
[program:website1]directory = /data/apps/website1command = /data/apps/website1/bin/supervisord-wrapper.shenvironment=HOME=”/usr/local/tomcat”,USER=”tomcat”stdout_logfile = /data/apps/website1/logs/catalina.outautostart = truestartsecs = 10stopsignal = QUITstopasgroup = trueuser = tomcatredirectstderr = true
6.配置supervisor,打开内置的http server,以便用web方式启动,停止tomcat
]# vi /etc/supervisord.conf,取消如下两行的注释:
;[inet_http_server]
;port=*:9001 ; (ip_address:port specifier, *:port for all iface)
7.启动supervisor,会自动启动website1的tomcat
]# systemctl start supervisord
]# systemctl enable supervisord
可以用pstree命令看到tomcat是被supervisor启动的。
参考文档:
https://blog.tankywoo.com/2014/12/05/supervisor-run-program-as-nonroot.html
https://tomcat.apache.org/tomcat-7.0-doc/RUNNING.txt
http://blog.e-zest.com/how-to-run-multiple-tomcat-instances-on-single-linux-server/
https://dzone.com/articles/running-multiple-tomcat
http://www.ramkitech.com/2011/07/running-multiple-tomcat-instances-on.html
https://www.mulesoft.com/tcat/tomcat-webapp
http://mikenaberezny.com/talks/oscon08/supervisor_as_a_platform.pdf
https://github.com/jasoncorbett/ordered-startup-supervisord/blob/master/ordered_startup_supervisord.py
https://code.tutsplus.com/tutorials/monitor-and-control-applications-using-supervisor-part-2–cms-24236
http://qiita.com/harukasan/items/cfbd75561a1ebf3779c4
http://qiita.com/imunew/items/465521e30fae238cf7d0