由于业务开始复杂,单一tomcat已经不足以满足业务需求,多tomcat部署起来不方便而且面临域名解析问题,因此开始增加反向代理,由于docker的易用性,便使用docker管理各个应用。

docker 教程(菜鸟学院地址):http://www.runoob.com/docker/docker-container-connection.html

一、安装docker(centos)

Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker 。

通过 uname -r 命令查看你当前的内核版本

uname -r

使用root权限登录 Centos。确保 yum 包更新到最新。

sudo yum update

安装一些必要的系统工具:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

添加软件源信息:

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

更新 yum 缓存:

sudo yum makecache fast

安装 Docker-ce:

sudo yum -y install docker-ce

启动 Docker 后台服务

sudo systemctl start docker

测试运行 hello-world

docker run hello-world

二、用docker安装nginx

拉取nginx镜像

docker pull nginx

等待下载完成后,我们就可以在本地镜像列表里查到 REPOSITORY 为 nginx 的镜像。

[root@VM_72_27_centos nginx]# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 881bd08c0b08 weeks ago 109MB

在/opt下新建nginx文件夹,用来存放配置文件及日志文件等

[root@VM_72_27_centos opt]# cd /opt
[root@VM_72_27_centos opt]# mkdir nginx
[root@VM_72_27_centos opt]# cd nginx/
[root@VM_72_27_centos nginx]# mkdir www
[root@VM_72_27_centos nginx]# mkdir conf
[root@VM_72_27_centos nginx]# mkdir logs
[root@VM_72_27_centos nginx]# pwd
/opt/nginx
[root@VM_72_27_centos nginx]# ls
conf logs www

首先创建一个nginx容器,来测试一下(因为是测试,这里先不映射文件夹)

[root@VM_72_27_centos nginx]# docker run -p : --name nginx-test -d nginx
1c653a1ce10fa2946738ada1f4d0eee25c80aa4024a17b264fd5be70b0a5bb0c
[root@VM_72_27_centos nginx]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1c653a1ce10f nginx "nginx -g 'daemon of…" seconds ago Up seconds 0.0.0.0:->/tcp nginx-test

命令说明:

  • -p 8081:80:将容器的80端口映射到主机的8081端口

  • --name nginx-test:将容器命名为nginx-test

浏览器访问测试一下 http://你的IP:8081/index.html

成功!好了,先关闭这个测试用的容器吧。

[root@VM_72_27_centos nginx]# docker stop nginx-test

后面部署完tomcat后我们再来完成配置nginx的反向代理等功能。

三、用docker安装tomcat

拉取tocmat镜像

[root@VM_72_27_centos nginx]# docker pull tomcat

查看镜像

[root@VM_72_27_centos nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest dd6ff929584a weeks ago 463MB
nginx latest 881bd08c0b08 weeks ago 109MB
hello-world latest fce289e99eb9 months ago .84kB

我的tomcat之前有3个应用,分别为hsz、tdl、cv,我把他们移动到 /opt/webapps 下

[root@VM_72_27_centos webapps]# pwd
/opt/webapps
[root@VM_72_27_centos webapps]# cp -rf /opt/apache-tomcat-9.0./hostapps/* /opt/webapps/
[root@VM_72_27_centos webapps]# ls
cv hsz tdl

将hsz映射到tomcat-hsz下,端口映射为8100;将tdl映射到tomcat-tdl下,端口映射为8101;将cv映射到tomcat-cv下,端口映射为8102

[root@VM_72_27_centos webapps]# docker run --name tomcat-hsz -p : -v /opt/webapps/hsz:/usr/local/tomcat/webapps/ROOT -d tomcat
ae36a1f321aedb5e86eb449fc034bab8a11982eed22261dae136eb49e1659d10
[root@VM_72_27_centos webapps]# docker run --name tomcat-tdl -p : -v /opt/webapps/tdl:/usr/local/tomcat/webapps/ROOT -d tomcat
a4e006d8b3931df3bbc50d7e19ccc732423413b813873bb2f7e7398dcf2df193
[root@VM_72_27_centos webapps]# docker run --name tomcat-cv -p : -v /opt/webapps/cv:/usr/local/tomcat/webapps/ROOT -d tomcat
9da57d8ce7d65b95c22bf578b86017e3f4eecc601eeddb5e63e0ae3b42e648ee
[root@VM_72_27_centos webapps]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9da57d8ce7d6 tomcat "catalina.sh run" seconds ago Up seconds 0.0.0.0:->/tcp tomcat-cv
a4e006d8b393 tomcat "catalina.sh run" seconds ago Up seconds 0.0.0.0:->/tcp tomcat-tdl
ae36a1f321ae tomcat "catalina.sh run" seconds ago Up seconds 0.0.0.0:->/tcp tomcat-hsz
1c653a1ce10f nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:->/tcp nginx-test

分别访问三个地址,测试成功!

-p参数说明:

创建docker容器时,如果不使用 -p : ,而是使用 -P (如: docker run --name tomcat-test2 -P -d tomcat ),则会随机指定一个端口来映射到容器默认端口(例如tomcat默认8080,nginx默认80),如使用 -p 127.0.0.1:: ,则只允许宿主机访问docker容器。更多-p参数的说明,可参考https://www.jianshu.com/p/2b424c3bf0f7

四、配置nginx反向代理,转发到Tomcat服务器

上面我们创建了 nginx-test 的测试容器。我们进入容器内部查看一下nginx的默认配置。

[root@VM_72_27_centos ~]# docker exec -it nginx-test /bin/bash
root@1c653a1ce10f:/# cat /etc/nginx/
conf.d/ fastcgi_params koi-utf koi-win mime.types modules/ nginx.conf scgi_params uwsgi_params win-utf
root@1c653a1ce10f:/# cat /etc/nginx/nginx.conf user nginx;
worker_processes ; error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid; events {
worker_connections ;
} http {
include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on;
#tcp_nopush on; keepalive_timeout ; #gzip on; include /etc/nginx/conf.d/*.conf;
}
root@1c653a1ce10f:/# exit

docker exec :在运行的容器中执行命令。

-it :-i -t写在一起了。

  • -i :即使没有附加也保持STDIN 打开

  • -t :分配一个伪终端

/bin/bash :在运行的容器中执行命令。

详细见:http://www.runoob.com/docker/docker-exec-command.html

最下面 include /etc/nginx/conf.d/*.conf; ,我们看见它把conf.d下面的所有conf文件都引入了,因此我们把宿主机的conf.d映射到容器中。

创建conf.d文件夹

[root@VM_72_27_centos ~]# cd /opt/nginx/conf/
[root@VM_72_27_centos conf]# mkdir conf.d
[root@VM_72_27_centos conf]# ls
conf.d
[root@VM_72_27_centos conf]# cd conf.d

创建配置文件之前,我们需要知道每个Tomcat容器的IP地址(以 tomcat-tdl 为例)。

[root@VM_72_27_centos conf.d]# docker exec -it tomcat-tdl /bin/bash
root@336e633dbf8d:/usr/local/tomcat# cat /etc/hosts
127.0.0.1 localhost
:: localhost ip6-localhost ip6-loopback
fe00:: ip6-localnet
ff00:: ip6-mcastprefix
ff02:: ip6-allnodes
ff02:: ip6-allrouters
172.17.0.2 336e633dbf8d
root@336e633dbf8d:/usr/local/tomcat# exit
exit

我们看到最后一行,docker为容器创建的IP为172.17.0.2。

创建反向代理配置文件cv.conf、hsz.conf、tdl.conf。

tdl.conf:

upstream tdl  {
server 172.17.0.2:;
} server {
listen ;
server_name tdl.yanglei.xyz; location / {
proxy_pass http://tdl;
index index.html;
}
}

cv.conf:

upstream cv  {
server 172.17.0.3:;
} server {
listen ;
server_name cv.yanglei.xyz; location / {
proxy_pass http://cv;
index index.html;
}
}

hsz.conf:

upstream hsz  {
server 172.17.0.4:;
} server {
listen ;
server_name hsz.yanglei.xyz; location / {
proxy_pass http://hsz;
index index.html;
}
}

创建nginx反向代理容器

[root@VM_72_27_centos conf.d]# cd /opt/nginx/
[root@VM_72_27_centos nginx]# docker run -p : --name nginx-proxy -v $PWD/www:/www -v $PWD/conf/conf.d:/etc/nginx/conf.d -v $PWD/logs:/wwwlogs -d nginx

查看所有容器,浏览器访问测试,成功。

[root@VM_72_27_centos nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
336e633dbf8d tomcat "catalina.sh run" hours ago Up hours 0.0.0.0:->/tcp tomcat-tdl
e360ea0f9d91 nginx "nginx -g 'daemon of…" hours ago Up hours 0.0.0.0:->/tcp nginx-proxy
9da57d8ce7d6 tomcat "catalina.sh run" days ago Up days 0.0.0.0:->/tcp tomcat-cv
ae36a1f321ae tomcat "catalina.sh run" days ago Up days 0.0.0.0:->/tcp tomcat-hsz
1c653a1ce10f nginx "nginx -g 'daemon of…" days ago Up hours 0.0.0.0:->/tcp nginx-test

停止并删除测试nginx的容器 nginx-test

[root@VM_72_27_centos nginx]# docker stop nginx-test
nginx-test
[root@VM_72_27_centos nginx]# docker rm nginx-test
nginx-test
[root@VM_72_27_centos nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
336e633dbf8d tomcat "catalina.sh run" hours ago Up hours 0.0.0.0:->/tcp tomcat-tdl
e360ea0f9d91 nginx "nginx -g 'daemon of…" hours ago Up hours 0.0.0.0:->/tcp nginx-proxy
9da57d8ce7d6 tomcat "catalina.sh run" days ago Up days 0.0.0.0:->/tcp tomcat-cv
ae36a1f321ae tomcat "catalina.sh run" days ago Up days 0.0.0.0:->/tcp tomcat-hsz

后记:这里的Tomcat和nginx都没有做性能优化,也没有做https反向代理。我们知道了docker的常规使用方法,自己写一个server.xml,每个Tomcat都映射这个配置文件就好了,https只需要增加一个443端口的反向代理就好了。如果多tomcat做集群,可以在nginx的配置文件中使用 ip_hash 来使每个IP固定到特定tomcat(当然,考虑到以后做分布式,以及单服务器挂掉等特殊情况,最好使用redis来管理session)。

Tomcat性能优化推荐:https://blog.csdn.net/lifetragedy/article/details/7708724

使用Spring-Session整合Redis共享Session:https://blog.csdn.net/qq_35830949/article/details/79995318

docker配置nginx做反向代理管理tomcat应用的更多相关文章

  1. 在ubuntu上面配置nginx实现反向代理和负载均衡

    上一篇文章(http://www.cnblogs.com/chenxizhang/p/4684260.html),我做了一个实验,就是利用Visual Studio,基于Nancy框架,开发了一个自托 ...

  2. nginx做反向代理并防盗链

    nginx做反向代理真的非常简单,只需设置location+proxy_pass即可. 防盗链配置有些复杂,需要注意的地方: 在防盗链的location中需要再设置一下proxy_pass(在这里走了 ...

  3. 【Nginx】使用Nginx做反向代理时,关于被代理服务器相应的超时设置

    > 参考的优秀文章 Module ngx_http_proxy_module > 设置等待被代理服务器的最大响应时间 使用Nginx做反向代理时,因被代理服务器因业务确实复杂,需时较久,往 ...

  4. Nginx做反向代理总是被系统kill

    公司使用Nginx做反向代理,以前都挺正常的,最近不知怎么回事总是无端被系统kill,而在nginx错误日志中也没有信息输出. 网上查了很多资料,也没什么靠谱的回答,唯一觉得有点关联的就是linux ...

  5. 使用nginx做反向代理

    很多同学喜欢用nginx做反向代理访问某些网站,原因大家都懂的,今天老高记录一下如何使用nginx做反向代理以及如何配置和优化nginx的反向代理. 准备工作 首先,你需要一个稳定的国外的便宜的VPS ...

  6. CentOS 7 安装Nginx做反向代理

    题记 须要使用nginx的反向代理功能,測试环境为centos+NGINX 1.8.0. 跳过一些繁琐的问题,直接记录核心 步骤 (1)centos 安装在VM中.因此须要注意网络连接问题 (2)安装 ...

  7. wsl 2 unbuntu 部署 asp.net core 使用 nginx 做反向代理,调试文件上传失败

    继上一篇 asp.net core 3.1多种身份验证方案,cookie和jwt混合认证授权 的公司内部项目上线后发现文件上传功能有问题. 上传的文件超过50M以后前端就报错了,没有状态返回,也没有响 ...

  8. 用nginx做反向代理来访问防外链图片

    用nginx做反向代理来访问防外链图片 女儿的博客从新浪搬到wordpress后,发现原来博客上链接的新浪相册的图片都不能访问了,一年的博客内容,一个个去重新上传图片,修正链接也是个大工程.还是得先想 ...

  9. 利用nginx做反向代理解决前端跨域问题

    最近朋友再群里提了一个问题,他们公司给他提供了一个获取数据的接口,在浏览器访问这个接口能获取到json数据,但是放在项目里使用ajax就产生了跨域问题,一般这个需要提供接口的后台方面需要做跨域处理,但 ...

随机推荐

  1. python7 数据类型的相互转化 字符编码

    复习 1.深浅拷贝    ls = [1, 'a', [10]]    值拷贝:直接赋值 ls1 = ls, ls中的任何值发生改变,ls1中的值都会随之改变    浅拷贝:通过copy()方法 ls ...

  2. CMake快速入门

    参考: https://www.hahack.com/codes/cmake/ 1. 单目标文件 main.c #include <stdio.h> #include <stdlib ...

  3. Servlet学习笔记(1)

    Servlet:Sun公司制订的一种用来扩展Web服务功能的组间规范. 第1部分 C/S和B/S介绍 1 C/S Client Server 客户端 服务器程序: 客户端需要单独开发,用户需要下载并安 ...

  4. python3 练手实例3 摄氏温度与华氏温度转换

    def wd(): w=input('请输入一个摄氏温度或者一个华氏温度,如,34c/C or 34f/F:') if w[-1] in ['c','C']: w=float(w[:-1]) hs=1 ...

  5. TERADATA SQL学习随笔<一>

    此博客内容简介及目录 http://www.cnblogs.com/weibaar/p/6644261.html 最近在TERADATA环境学习SQL.在这里记录一下学习中查过的知识点,作为备案. 目 ...

  6. 【题解】狼抓兔子—BZOJ1001。

    (胡扯时间)今天炒鸡无聊就打算BZOJ开始从第一道题开始顺着打,这样未来一段时间内也就有事干了.结果发现A+B切掉后就遭遇了一个"小小"的瓶颈(真不友好. 好了说题说题.看题第一眼 ...

  7. 记事本:一些js案例以及DOM和BOM

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. redis批量灌库

    需求:将批量数据灌入redis中 如果通过代码形式将数据灌入redis中,效率比较低,以下将根据redis的特性进行快速的批量灌库 环境:centos7 将数据整理成规定格式的文件,比如: SET k ...

  9. 理解 Linux 的硬链接与软链接【转】

    转自:https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/index.html 从 inode 了解 Linux 文件 ...

  10. 【medium】78. Subsets

    求集合不重复的子集: 下面python的写法很好啊! class Solution(object): def subsets(self, nums): """ :type ...