参考:
本方式参考了:https://github.com/EdSingleton/docker-httpd-mod_jk
另外还有一种方式:https://github.com/Paritosh-Anand/Docker-Httpd-Tomcat
目标:
使用Docker来管理Apache和Tomcat,并且动静分离。
思路:
DockerHub里面没有找到含mod_jk的httpd镜像,需要自建一个Dockerfile构建。然后关联另外一个tomcat容器。httpd放静态,tomcat放动态。
步骤:
1、当前目录创建Dockerfile、mod_jk.conf、workers.properties
Dockerfile
FROM centos:latest
MAINTAINER asc <2046@asc>
RUN yum -y update && yum clean all
RUN yum -y install httpd httpd-devel gcc* make && yum clean all
# Install mod_jk
RUN curl -SL https://archive.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.46-src.tar.gz -o \ntomcat-connectors-1.2.46-src.tar.gz \
&& mkdir -p src/tomcat-connectors \
&& tar xzf \ntomcat-connectors-1.2.46-src.tar.gz -C src/tomcat-connectors --strip-components=1 \
&& cd src/tomcat-connectors/native/ \
&& ./configure --with-apxs=/usr/bin/apxs \
&& make \
&& cp apache-2.0/mod_jk.so /usr/lib64/httpd/modules/ \
&& ./libtool --finish /usr/lib64/httpd/modules/ \
&& cd / \
&& rm -rf src/ \
&& rm -f \ntomcat-connectors-1.2.46-src.tar.gz
# Copy load balancing configurations files
COPY mod_jk.conf /etc/httpd/conf.d/
COPY workers.properties /etc/httpd/conf.d/
EXPOSE 80
CMD ["apachectl", "-k", "start", "-DFOREGROUND"]
mod_jk.conf
LoadModule jk_module modules/mod_jk.so
JkWorkersFile /etc/httpd/conf.d/workers.properties
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel warn
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /* loadbalancer
JkShmFile /var/log/httpd/mod_jk.shm
workers.properties
worker.list=loadbalancer,jk-status
worker.node1.type=ajp13
worker.node1.host=localhost
worker.node1.port=8009
worker.node1.jvm_route=node1
# Activation allows to configure
# whether this node should actually be used
# A: active (use node fully)
# D: disabled (only use, if sticky session needs this node)
worker.node1.activation=A
#worker.node2.type=ajp13
#worker.node2.host=localhost
#worker.node2.port=8009
#worker.node2.jvm_route=node2
# Activation allows to configure
# whether this node should actually be used
# A: active (use node fully)
# D: disabled (only use, if sticky session needs this node)
#worker.node1.activation=D
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=node1
worker.loadbalancer.sticky_session=TRUE
worker.jk-status.type=status
注意:mod_jk.conf、workers.properties,里面的JkMount、worker.node1.host,是可以通过之后的启动映射来修改。
2、构建image
docker build -t asc/apache-jk .
3、准备静态程序、动态程序在本地文件夹
例如/data/project/xxxx/web-ui-api/webapps、/data/project/xxxx/apache/www
4、准备自定义的mod_jk.conf、workers.properties
正如前文所述,可以通过映射自定义配置文件来修改image里的参数,例如JkMount /api* loadbalancer,把请求里面的api开头url全部转发到tomcat。worker.node1.host=192.168.0.111,tomcat地址改为实际地址。
5、启动apache-jk
docker run --name xxxx-apache-jk --rm -d -p 6001:80 -v /data/projects/xxxx/apache/www/:/var/www/html/ -v /data/projects/xxxx/apache/logs:/usr/local/apache2/logs -v /data/projects/xxxx/apache/conf/workers.properties:/usr/local/apache2/conf/workers.properties asc/apache-jk
题外话:因为所有数据都在本地映射保存,所以通过--rm参数来使容器停止后即删除。但如果启动失败想看日志,则去掉这个参数。
6、启动tomcat
docker run --name xxxx-web-ui-api --rm -d -p 6002:8080 -p 8009:8009 -v /data/projects/xxxx/web-ui-api/webapps:/usr/local/tomcat/webapps -v /data/projects/xxxx/web-ui-api/logs:/usr/local/tomcat/logs -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro tomcat:7-jre8
7、完成
访问http://host:6001,可以看到静态首页,使用功能时,api开头请求都转发给了tomcat处理,测试成功。