docker配置nginx做反向代理管理tomcat应用

由于业务开始复杂,单一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 :在运行的容器中执行命令。

详细见: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
::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性能优化推荐:https://blog.csdn.net/lifetragedy/article/details/7708724

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

猜你喜欢

转载自www.cnblogs.com/yanglei-xyz/p/10600707.html