.
├── docker-compose.yaml
├── nginx
│ ├── Dockerfile
│ └── nginx-1.14.2
│ ├── nginx.conf
│ ├── logs
└── webapp
├── apache-tomcat-8.5.41
|—— Dockerfile
├── jdk1.8.0_191
├── logs
└── webapps
└── ROOT
└── index.jsp
实验环境没有做数据库的部署
user nginx;
worker_processes 2;
worker_cpu_affinity 01 10;
events {
worker_connections 65535;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
upstream example {
server tomcat1:8080;
server tomcat2:8080;
server tomcat3:8080;
}
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 60;
client_header_buffer_size 4k;
#gzip on;
gzip on;
gzip_buffers 32 4k;
gzip_comp_level 4;
gzip_http_version 1.0;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1024;
gzip_types text/plain application/x-javascript text/css application/xml;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
access_log /usr/local/nginx/logs/host_access.log;
error_log /usr/local/nginx/logs/host_error.log;
location ~ \.(html|css|js|jpg|png|gif)$ {
root /opt/webapp/ROOT;
}
location / {
proxy_pass http://example;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
index.jsp测试索引文件内容
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.net.InetAddress"
pageEncoding="utf-8"%>
Nginx+Tomcat负载均衡
<%
InetAddress addr = InetAddress.getLocalHost();
out.println("主机地址:"+addr.getHostAddress());
out.println("主机名:"+addr.getHostName());
%>
cd nginx/
Dockerfile内容
FROM centos
MAINTAINER Ren
RUN yum -y install pcre-devel openssl-devel net-tools gcc gcc-c++ zlib zlib-devel \
make openssl
ADD ./nginx-1.14.2 /tmp/nginx-1.14.2
WORKDIR /tmp
RUN cd /tmp/nginx-1.14.2 \
&& ./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module \
&& make && make install
RUN mkdir -p /usr/local/nginx/logs \
&& groupadd -g 1001 nginx \
&& useradd -g 1001 -u 1001 -s /sbin/nologin -M nginx
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
构建
docker build -t webapp/nginx .
cd ../webapp/
Dockerfile 内容
FROM centos
MAINTAINER ren
RUN mkdir -p /usr/local/webapp
ADD apache-tomcat-8.5.41 /usr/local/webapp/tomcat
ADD jdk1.8.0_191 /usr/local/webapp/jdk
ENV JAVA_HOME /usr/local/webapp/jdk
ENV CATALINA_HOME /usr/local/webapp/tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
EXPOSE 8080
CMD ["/usr/local/webapp/tomcat/bin/catalina.sh","run"]
构建
docker build -t repostry/centos_tomcat .
docker-compose.yaml 文件内容
# version 1
#version: '1'
tomcat1:
image: repostry/centos_tomcat
# ports:
# - "8080:8080"
volumes:
- ./webapp/logs:/usr/local/webapp/tomcat/logs
- ./webapp/webapps:/usr/local/webapp/tomcat/webapps
environment:
- TZ=Asia/Shanghai
tomcat2:
image: repostry/centos_tomcat
# ports:
# - "8080:8080"
volumes:
- ./webapp/logs:/usr/local/webapp/tomcat/logs
- ./webapp/webapps:/usr/local/webapp/tomcat/webapps
environment:
- TZ=Asia/Shanghai
tomcat3:
image: repostry/centos_tomcat
# ports:
# - "8080:8080"
volumes:
- ./webapp/logs:/usr/local/webapp/tomcat/logs
- ./webapp/webapps:/usr/local/webapp/tomcat/webapps
environment:
- TZ=Asia/Shanghai
nginx:
image: webapp/nginx
ports:
- "80:80"
- "443:443"
links:
- tomcat1:tomcat1
- tomcat2:tomcat2
- tomcat3:tomcat3
volumes:
- ./nginx/logs:/usr/local/nginx/logs
- ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
- ./webapp/webapps/:/opt/webapps/ROOT
environment:
- TZ=Asia/Shanghai
# depends_on:
# - tomcat1
# - tomcat2
# - tomcat3
启动服务
[root@docker ~]# docker-compose up -d
Creating root_tomcat2_1 ... done
Creating root_tomcat1_1 ... done
Creating root_tomcat3_1 ... done
Creating root_nginx_1 ... done
查看服务状态
[root@docker ~]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------------
root_nginx_1 /usr/local/nginx/sbin/ngin ... Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
root_tomcat1_1 /usr/local/webapp/tomcat/b ... Up 8080/tcp
root_tomcat2_1 /usr/local/webapp/tomcat/b ... Up 8080/tcp
root_tomcat3_1 /usr/local/webapp/tomcat/b ... Up 8080/tcp
如有错误 查看日志
[root@docker ~]# docker-compose logs -f --tail=100 tomcat1
Attaching to root_tomcat1_1
tomcat1_1 | 29-May-2019 15:59:12.593 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.41
tomcat1_1 | 29-May-2019 15:59:12.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: May 4 2019 09:17:16 UTC
tomcat1_1 | 29-May-2019 15:59:12.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.41.0
tomcat1_1 | 29-May-2019 15:59:12.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
tomcat1_1 | 29-May-2019 15:59:12.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-862.el7.x86_64
tomcat1_1 | 29-May-2019 15:59:12.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
tomcat1_1 | 29-May-2019 15:59:12.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/webapp/jdk/jre
tomcat1_1 | 29-May-2019 15:59:12.596 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_191-b12
[root@docker ~]# docker-compose logs nginx
Attaching to root_nginx_1
浏览器登录测试
刷新
每次刷新的主机名和IP 都不同 表名已经成功了
参考: https://blog.csdn.net/u011781521/article/details/80466451