系统环境:Centos 7.6
最近研究学习了下Docker,借此机会用docker来搭建实现了下tomcat集群,以此加深对docker的认识。以此文记录相关的步骤细节及心得。
1、安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2 epel-release
yum install -y docker
systemctl start docker
2、创建日志、配置文件目录
提前创建相应的日志文件目录和配置文件目录,用于挂载到相应的容器目录中。
[root@localhost ~]# mkdir /data/tomcatA/{catalina_logs,app_logs,conf,bin} -pv
mkdir: created directory `/data'
mkdir: created directory `/data/test'
mkdir: created directory `/data/test/catalina_logs'
mkdir: created directory `/data/test/app_logs'
mkdir: created directory `/data/test/conf'
mkdir: created directory `/data/test/bin'
[root@localhost ~]# mkdir /data/tomcatB/{catalina_logs,app_logs,conf,bin} -pv
mkdir: created directory ‘/data/tomcatB’
mkdir: created directory ‘/data/tomcatB/catalina_logs’
mkdir: created directory ‘/data/tomcatB/app_logs’
mkdir: created directory ‘/data/tomcatB/conf’
mkdir: created directory ‘/data/tomcatB/bin’
3、创建应用目录
用于存放相应的tomcat应用项目。
[root@localhost ~]# mkdir /usr/local/apps/{tomcatA,tomcatB} -pv
[root@localhost ~]# vim /usr/local/apps/tomcatA/index.jsp
<%@ page language="java" %>
TomcatA
TomcatA.example.com
Session ID
<% session.setAttribute("example.com","example.com"); %>
<%= session.getId() %>
Created on
<%= session.getCreationTime() %>
[root@localhost ~]# vim /usr/local/apps/tomcatB/index.jsp
<%@ page language="java" %>
TomcatB
TomcatB.example.com
Session ID
<% session.setAttribute("example.com","example.com"); %>
<%= session.getId() %>
Created on
<%= session.getCreationTime() %>
[root@localhost ~]# mkdir /usr/local/apps/tomcatA/WEB-INF/
[root@localhost ~]# mkdir /usr/local/apps/tomcatB/WEB-INF/
[root@localhost ~]# vim /usr/local/apps/tomcatA/WEB-INF/web.xml
#分别在tomcatA、tomcatB的应用目录的WEB-INF的web.xml中添加此参数块。
Welcome to Tomcat
Welcome to Tomcat
4、复制创建相应的tomcat 配置文件
[root@localhost ~]# docker pull tomcat
#临时起一个tomcat容器,复制相应的配置文件,docker run 命令的--rm 选项可以在容器退出的时候直接删除容器
[root@localhost ~]# docker run --name tomcat-test --rm -it docker.io/tomcat /bin/bash
root@79b965c5e312:/usr/local/tomcat#
#新起一个ssh终端复制相应的配置文件
[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/conf/server.xml /data/tomcatA/conf/
[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/conf/server.xml /data/tomcatB/conf/
[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/bin/catalina.sh /data/tomcatA/bin/
[root@localhost ~]# docker cp tomcat-test:/usr/local/tomcat/bin/catalina.sh /data/tomcatB/bin/
5、配置运行Docker tomcat容器
调整docker tomcat的配置文件:
[root@localhost ~]# vim /data/tomcatA/bin/catalina.sh
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx512m -Duser.timezone=Asia/Shanghai"
[root@localhost ~]# vim /data/tomcatB/bin/catalina.sh
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms512m -Xmx512m -Duser.timezone=Asia/Shanghai"
[root@localhost ~]# vim /data/tomcatA/conf/server.xml
....
#指定项目包的路径
.......
[root@localhost ~]# vim /data/tomcatB/conf/server.xml
....
#指定项目包的路径
.......
#docker run 参数选项解释
#--memory:指定容器运行的内存大小
#-d:以后台方式运行容器
#-v:指定挂载到容器中的目录,格式:宿主机目录:容器中的目录
#-p:指定容器映射的端口,格式:宿主机端口:容器端口
[root@localhost ~]# docker run -p 8080:8080 --name tomcatA --memory 512M -d -v /data/tomcatA/catalina_logs/:/usr/local/tomcat/logs/ -v /data/tomcatA/app_logs/:/usr/local/logs -v /usr/local/apps/:/usr/local/tomcat/webapps/ -v /data/tomcatA/conf/server.xml:/usr/local/tomcat/conf/server.xml -v /data/tomcatA/bin/catalina.sh:/usr/local/tomcat/bin/catalina.sh docker.io/tomcat
[root@localhost ~]# docker run -p 8081:8080 --name tomcatB --memory 512M -d -v /data/tomcatB/catalina_logs/:/usr/local/tomcat/logs/ -v /data/tomcatB/app_logs/:/usr/local/logs -v /usr/local/apps/:/usr/local/tomcat/webapps/ -v /data/tomcatB/conf/server.xml:/usr/local/tomcat/conf/server.xml -v /data/tomcatB/bin/catalina.sh:/usr/local/tomcat/bin/catalina.sh docker.io/tomcat
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2754623ea1de docker.io/tomcat "catalina.sh run" 2 minutes ago Up 37 seconds 0.0.0.0:8080->8080/tcp tomcatA
3ab68e00ebdc docker.io/tomcat "catalina.sh run" 2 minutes ago Up 51 seconds 0.0.0.0:8081->8080/tcp tomcatB
6、配置tomcat集群
#修改相应tomcat的配置信息,在host配置段中添加cluster配置段
[root@localhost ~]# vim /data/tomcatA/conf/server.xml
[root@localhost ~]# vim /data/tomcatB/conf/server.xml
#重启Tomcat容器
[root@localhost ~]# docker restart tomcatA
tomcatA
[root@localhost ~]# docker restart tomcatB
tomcatB
7、验证docker部署的tomcat集群
[root@localhost ~]# yum install -y nginx
#查看相应的容器所使用的IP信息,docker inspect 可以查看当前运行的容器的相关信息,包括挂载的目录路径,网络IP信息等,--format可指定显示相匹配的json信息
[root@localhost ~]# docker inspect --format '{{.NetworkSettings}}' tomcatA
{ f602619757b50d5eefb76e816511589d11051e2414e0a30aaf2fa0db07b4d062 172.17.42.1 0 false 172.17.0.8 16 0 02:42:ac:11:00:08 17f8cc9318b27eac597aafdd3277209bb71b6b26bed7bae5ce684eddf14f9f6f map[] map[8080/tcp:[{0.0.0.0 8080}]] /var/run/docker/netns/2cfd1be49d4e [] []}
[root@localhost ~]# docker inspect --format '{{.NetworkSettings}}' tomcatB
{ ebb88aaba5eb3fecd1da103269fbb91ea52f5e07eecb9cc6823d2305a152dde6 172.17.42.1 0 false 172.17.0.6 16 0 02:42:ac:11:00:06 17f8cc9318b27eac597aafdd3277209bb71b6b26bed7bae5ce684eddf14f9f6f map[] map[8080/tcp:[{0.0.0.0 8081}]] /var/run/docker/netns/f34a7eb231d6 [] []}
#docker run 的--ip 指定容器IP的选项,需要在用户自建的docker网络才能生效,此处我用的是默认的docker网络,因此无法指定相应的容器IP。
[root@localhost ~]# vim /etc/nginx/nginx.conf
#在http配置段中添加下述配置
upstream tomcat {
server 172.17.0.8:8080 max_fails=1 fail_timeout=3s;
server 172.17.0.6:8080 max_fails=1 fail_timeout=3s;
}
server {
listen 80;
server_name test.tomcat.com
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#配置防火墙放开80端口的访问,注意docker需要系统防火墙来配置相应的转发规则,所以不能把防火墙停用。
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
[root@localhost ~]# firewall-cmd --list-all
#启动nginx
[root@localhost ~]# service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# service nginx start
Starting nginx: [ OK ]
测试访问页面: