由于业务开始复杂, 单一 tomcat 已经不足以满足业务需求, 多 tomcat 部署起来不方便而且面临域名解析问题, 因此开始增加反向代理, 由于 docker 的易用性, 便使用 docker 管理各个应用.
docker 教程 (菜鸟学院地址):http://www.runoob.com/docker/docker-container-connection.html
一, 安装 docker(CentOS)
安装一些必要的系统工具:
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 3 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 8081:80 --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..." 4 seconds ago Up 3 seconds 0.0.0.0:8081->80/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 2 weeks ago 463MB
- nginx latest 881bd08c0b08 3 weeks ago 109MB
- hello-world latest fce289e99eb9 2 months ago 1.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.12/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 8100:8080 -v /opt/webapps/hsz:/usr/local/tomcat/webapps/ROOT -d tomcat
- ae36a1f321aedb5e86eb449fc034bab8a11982eed22261dae136eb49e1659d10
- [root@VM_72_27_centos webapps]# docker run --name tomcat-tdl -p 8101:8080 -v /opt/webapps/tdl:/usr/local/tomcat/webapps/ROOT -d tomcat
- a4e006d8b3931df3bbc50d7e19ccc732423413b813873bb2f7e7398dcf2df193
- [root@VM_72_27_centos webapps]# docker run --name tomcat-cv -p 8102:8080 -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" 6 seconds ago Up 4 seconds 0.0.0.0:8102->8080/tcp tomcat-cv
- a4e006d8b393 tomcat "catalina.sh run" 21 seconds ago Up 20 seconds 0.0.0.0:8101->8080/tcp tomcat-tdl
- ae36a1f321ae tomcat "catalina.sh run" 39 seconds ago Up 38 seconds 0.0.0.0:8100->8080/tcp tomcat-hsz
- 1c653a1ce10f nginx "nginx -g'daemon of..." About an hour ago Up About an hour 0.0.0.0:8081->80/tcp nginx-test
分别访问三个地址, 测试成功!
-p 参数说明:
创建 docker 容器时, 如果不使用 -p 8100:8080 , 而是使用 -P (如: docker run --name tomcat-test2 -P -d tomcat ), 则会随机指定一个端口来映射到容器默认端口 (例如 tomcat 默认 8080,nginx 默认 80), 如使用 -p 127.0.0.1:8100:8080 , 则只允许宿主机访问 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 1;
- error_log /var/log/nginx/error.log warn;
- pid /var/run/nginx.pid;
- events {
- worker_connections 1024;
- }
- 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 65;
- #gzip on;
- include /etc/nginx/conf.d/*.conf;
- }
- root@1c653a1ce10f:/# exit
docker exec : 在运行的容器中执行命令.
-it :-i -t 写在一起了.
-i : 即使没有附加也保持 STDIN 打开
-t : 分配一个伪终端
/bin/bash : 在运行的容器中执行命令.
详细见:
最下面 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
- ::1 localhost ip6-localhost ip6-loopback
- fe00::0 ip6-localnet
- ff00::0 ip6-mcastprefix
- ff02::1 ip6-allnodes
- ff02::2 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:8080;
- }
- server {
- listen 80;
- server_name tdl.yanglei.xyz;
- location / {
- proxy_pass http://tdl;
- index index.HTML;
- }
- }
- cv.conf:
- upstream cv {
- server 172.17.0.3:8080;
- }
- server {
- listen 80;
- server_name cv.yanglei.xyz;
- location / {
- proxy_pass http://cv;
- index index.HTML;
- }
- }
- hsz.conf:
- upstream hsz {
- server 172.17.0.4:8080;
- }
- server {
- listen 80;
- 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 80:80 --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" 25 hours ago Up 25 hours 0.0.0.0:8101->8080/tcp tomcat-tdl
- e360ea0f9d91 nginx "nginx -g'daemon of..." 27 hours ago Up 25 hours 0.0.0.0:80->80/tcp nginx-proxy
- 9da57d8ce7d6 tomcat "catalina.sh run" 2 days ago Up 2 days 0.0.0.0:8102->8080/tcp tomcat-cv
- ae36a1f321ae tomcat "catalina.sh run" 2 days ago Up 2 days 0.0.0.0:8100->8080/tcp tomcat-hsz
- 1c653a1ce10f nginx "nginx -g'daemon of..." 2 days ago Up 46 hours 0.0.0.0:8081->80/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" 25 hours ago Up 25 hours 0.0.0.0:8101->8080/tcp tomcat-tdl
- e360ea0f9d91 nginx "nginx -g'daemon of..." 27 hours ago Up 25 hours 0.0.0.0:80->80/tcp nginx-proxy
- 9da57d8ce7d6 tomcat "catalina.sh run" 2 days ago Up 2 days 0.0.0.0:8102->8080/tcp tomcat-cv
- ae36a1f321ae tomcat "catalina.sh run" 2 days ago Up 2 days 0.0.0.0:8100->8080/tcp tomcat-hsz
后记: 这里的 Tomcat 和 nginx 都没有做性能优化, 也没有做 https 反向代理. 我们知道了 docker 的常规使用方法, 自己写一个 server.xml, 每个 Tomcat 都映射这个配置文件就好了, https 只需要增加一个 443 端口的反向代理就好了. 如果多 tomcat 做集群, 可以在 nginx 的配置文件中使用 ip_hash 来使每个 IP 固定到特定 tomcat(当然, 考虑到以后做分布式, 以及单服务器挂掉等特殊情况, 最好使用 Redis 来管理 session).
Tomcat 性能优化推荐:
使用 Spring-Session 整合 Redis 共享 Session:
来源: http://www.bubuko.com/infodetail-3004016.html