架构说明:docker结合haproxy、tomcat、redis实现session共享
环境:
宿主机系统:centos7
软件版本:
docker-19.03.12
jdk1.8.0_261
tomcat-8.5.57
redis-5.0.5
说明:以下操作都是基于centos7的基础docker镜像去完成

实现架构图

docker结合nginx、tomcat、redis实现session共享_第1张图片

dockerfile制作centos基础镜像

#目录结构
#centos
[root@localhost system]# tree centos/
centos/
├── build-command.sh
├── CentOS-Base.repo
└── Dockerfile

0 directories, 3 files

#CentOS-Base.repo镜像仓库地址为阿里云镜像下载

#dockerfile文件编写,创建多个用户已备后期制作业务镜像使用
#下载镜像版本请指定特定版本,如果指定latest后期镜像更新会覆盖原来的镜像
[root@localhost centos]# cat Dockerfile 
#!/bin/bash

#centos-base:7.8.2003
From centos:7.8.2003

LABEL chenjianhong [email protected]

RUN rm -f /etc/yum.repos.d/*

ADD  CentOS-Base.repo /etc/yum.repos.d/

RUN yum -y install vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel unzip iproute net-tools iotop && groupadd -g 2001 www && useradd www -u 2001 -g www && groupadd -g 2002 nginx && useradd nginx -u 2002 -g nginx

#主要辨别标识和管理,因为镜像一多自己可能都不会记得
[root@localhost centos]# cat build-command.sh 
#!/bin/bash

docker build -t centos-base:7.8.2003 .

#执行构建,可以看见构建成功
[root@localhost centos]#  bash build-command.sh 
Sending build context to Docker daemon  10.38MB
Step 1/4 : FROM centos-base/jdk-1.8.0:v1
 ---> 755618c0c19d
Step 2/4 : LABEL chenjianhong [email protected]
 ---> Using cache
 ---> 29be464df83a
Step 3/4 : ADD apache-tomcat-8.5.57.tar.gz /app
 ---> Using cache
 ---> be0333a228b9
Step 4/4 : RUN ln -sv /app/apache-tomcat-8.5.57 /app/tomcat
 ---> Using cache
 ---> 0a6136ecde6d
Successfully built 0a6136ecde6d
Successfully tagged centos-base/tomcat:8.5.57

docker结合nginx、tomcat、redis实现session共享_第2张图片
可以看到centos环境的基础镜像制作完成

dockerfile制作jdk镜像

#因为镜像是分层的,所以我们制作镜像是按照基础镜像和业务镜像去区分构建,方便以后管理

#制作jdk镜像
[root@localhost ~]# cd /data/dockerfile/web/jdk
[root@localhost jdk]# pwd
/data/dockerfile/web/jdk

#查看jdk目录树
[root@localhost web]# tree jdk/
jdk/
└── centos
    ├── build-command.sh
    ├── Dockerfile
    ├── jdk-8u261-linux-x64.tar.gz
    └── profile

1 directory, 4 files

#编写Dockerfile文件
#提前准备好jdk软件包
[root@localhost centos]# cat Dockerfile 
#!/bin/bash

FROM centos-base:7.8.2003

LABEL chenjianhong [email protected]

ADD jdk-8u261-linux-x64.tar.gz /usr/local/src/

RUN ln -sv /usr/local/src/jdk1.8.0_261 /usr/local/jdk

ADD profile /etc/profile

#设置jdk环境
ENV JAVA_HOME /usr/local/jdk 
ENV JRE_HOME $JAVA_HOME/jre 
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/ 
ENV PATH $PATH:$JAVA_HOME/bin

#设置时区
RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

#编写profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

#编写build-command.sh 
[root@localhost centos]# cat build-command.sh 
#!/bin/bash

docker build -t centos-base/jdk-1.8.0:v1 .

#构建jdk环境的dockerfile
[root@localhost centos]# bash build-command.sh 
Sending build context to Docker daemon  143.1MB
Step 1/10 : FROM centos-base:7.8.2003
 ---> df9d37a83b7a
Step 2/10 : LABEL chenjianhong [email protected]
 ---> Using cache
 ---> 6b9645dc1b05
Step 3/10 : ADD jdk-8u261-linux-x64.tar.gz /usr/local/src/
 ---> Using cache
 ---> 087df086c9f3
Step 4/10 : RUN ln -sv /usr/local/src/jdk1.8.0_261 /usr/local/jdk
 ---> Using cache
 ---> daf71407fa44
Step 5/10 : ADD profile /etc/profile
 ---> Using cache
 ---> c0e7394ac83c
Step 6/10 : ENV JAVA_HOME /usr/local/jdk
 ---> Using cache
 ---> 640e51cbd53d
Step 7/10 : ENV JRE_HOME $JAVA_HOME/jre
 ---> Using cache
 ---> 29c398041dd3
Step 8/10 : ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
 ---> Using cache
 ---> d59f7a7b5878
Step 9/10 : ENV PATH $PATH:$JAVA_HOME/bin
 ---> Using cache
 ---> acfbd6c8c486
Step 10/10 : RUN rm -rf /etc/localtime && ln    -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
 ---> Using cache
 ---> 755618c0c19d
Successfully built 755618c0c19d
Successfully tagged centos-base/jdk-1.8.0:v1

#运行jdk容器,可以看到正常
[root@localhost centos]# docker run -it --rm centos-base/jdk-1.8.0:v1 bash

#时间正常
[root@a795db598736 /]# date 
Tue Jul 28 09:16:21 CST 2020

#jdk正常
[root@a795db598736 /]# java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)

docker结合nginx、tomcat、redis实现session共享_第3张图片
这边看到jdk环境的镜像制作完成

dockerfile制作tomcat镜像

#tomcat镜像目录结构
[root@localhost tomcat]# tree centos/
centos/
├── apache-tomcat-8.5.57.tar.gz
├── build-command.sh
└── Dockerfile

0 directories, 3 files

#编写dockerfile
[root@localhost centos]# cat Dockerfile 
#!/bin/bash

FROM centos-base/jdk-1.8.0:v1

LABEL chenjianhong [email protected]

ADD apache-tomcat-8.5.57.tar.gz /app

RUN ln -sv /app/apache-tomcat-8.5.57 /app/tomcat

#编写构建脚本
[root@localhost centos]# cat build-command.sh 
#!/bin/bash

docker build -t centos-base/tomcat:8.5.57 .

docker结合nginx、tomcat、redis实现session共享_第4张图片
这样tomcat镜像就制作完成,启动镜像看看
docker结合nginx、tomcat、redis实现session共享
可以看到正常

dockerfile制作redis镜像

说明:因为主要是实现session共享,所以我这边设置的是单机redis

# redis目录树
[root@localhost web]# tree redis/
redis/
├── build-command.sh
├── Dockerfile
├── redis-5.0.5.tar.gz
├── redis_6379.conf
└── run_redis.sh

0 directories, 5 files

#提前准备redis安装包
#准备好redis配置文件,设置redis密码
[root@localhost redis]# egrep -v "^#|^$" redis_6379.conf 
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /app/redis/run/redis_6379.pid
loglevel notice
logfile "/app/redis/logs/redis_6379.log"
requirepass 123456
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
requirepass 123456
maxmemory 1G
dynamic-hz yes

#编写dockerfile文件
[root@localhost redis]# cat Dockerfile 
#!/bin/bash

From centos-base:7.8.2003

LABEL chenjianhong [email protected]

ADD redis-5.0.5.tar.gz /usr/local/src/

RUN cd /usr/local/src/redis-5.0.5 && make PREFIX=/app/redis-5.0.5 MALLOC=libc install && \
    mkdir -p /app/redis-5.0.5/{etc,data,logs,run}

ADD redis_6379.conf /app/redis-5.0.5/etc/

RUN ln -sf /app/redis-5.0.5 /app/redis && groupadd -g 2003 redis && useradd redis -g 2003 -u 2003 && \
    chown -R redis.redis /app

ADD run_redis.sh /app/redis/bin/

EXPOSE 6379

CMD ["/app/redis/bin/run_redis.sh"]

#准备好redis启动脚本
#docker要有守护进程不然会退出
[root@localhost redis]# cat run_redis.sh 
#!/bin/bash

/app/redis/bin/redis-server /app/redis/etc/redis_6379.conf

tail -f /etc/hosts

#编写构建脚本
[root@localhost redis]# cat build-command.sh 
#!/bin/vbash

docker build -t centos-base/redis-5.0.5:v1 .

#执行构建,可以看到构建成功
[root@localhost redis]# bash build-command.sh 
Sending build context to Docker daemon  2.043MB
Step 1/9 : From centos-base:7.8.2003
 ---> df9d37a83b7a
Step 2/9 : LABEL chenjianhong [email protected]
 ---> Using cache
 ---> 6b9645dc1b05
Step 3/9 : ADD redis-5.0.5.tar.gz /usr/local/src/
 ---> Using cache
 ---> ececa702ea34
Step 4/9 : RUN cd /usr/local/src/redis-5.0.5 && make PREFIX=/app/redis-5.0.5 MALLOC=libc install &&     mkdir -p /app/redis-5.0.5/{etc,data,logs,run}
 ---> Using cache
 ---> 72316fd04d7c
Step 5/9 : ADD redis_6379.conf /app/redis-5.0.5/etc/
 ---> Using cache
 ---> 3a5246a1686f
Step 6/9 : RUN ln -sf /app/redis-5.0.5 /app/redis && groupadd -g 2003 redis && useradd redis -g 2003 -u 2003 &&     chown -R redis.redis /app
 ---> Using cache
 ---> c4c2c94570b5
Step 7/9 : ADD run_redis.sh /app/redis/bin/
 ---> Using cache
 ---> de20aabdd502
Step 8/9 : EXPOSE 6379
 ---> Using cache
 ---> 5711e0dab25e
Step 9/9 : CMD ["/app/redis/bin/run_redis.sh"]
 ---> Using cache
 ---> fcdb7f4cdd9f
Successfully built fcdb7f4cdd9f
Successfully tagged centos-base/redis-5.0.5:v1

#启动redis容器
[root@localhost redis]# docker exec -ti -d -p 6379:6379 centos-base/redis-5.0.5:v1

docker结合nginx、tomcat、redis实现session共享
可以看到启动正常

dockerfile制作tomcat环境app1和app2镜像

#基本tomcat基础镜像制作完成,app1和app2基本一致所以不再重复说明,这里以app1为例
[root@localhost tomcat]# tree centos_tomcat-app1-session
centos_tomcat-app1-session
├── app1.tar.gz
├── build-command.sh
├── context.xml
├── Dockerfile
├── run_tomcat.sh
├── server.xml
└── tomcat-redis.tar.gz

0 directories, 7 files
[root@localhost tomcat]# tree centos_tomcat-app2-session
centos_tomcat-app2-session
├── app.tar.gz
├── build-command.sh
├── context.xml
├── Dockerfile
├── run_tomcat.sh
├── server.xml
└── tomcat-redis.tar.gz

0 directories, 7 files

#app1镜像制作
#提前准备好tomcat支持redis库
tomcat支持redis包下载地址:https://github.com/cc-chen/tomcat8.5-redis-session-manager
[root@localhost centos_tomcat-app1-session]# ls tomcat-redis.tar.gz 
tomcat-redis.tar.gz

#修改tomcat的html根路径
[root@localhost centos_tomcat-app1-session]# cat server.xml |grep /data/tomcat/webapps
      

#提前准备代码文件,我这里以目录app1(centos_tomcat-app1-session镜像的目录)和app2(centos_tomcat-app2-session镜像的目录)为例
[root@localhost centos_tomcat-app1-session]# ls app1.tar.gz 
app1.tar.gz

#添加入下配置
[root@localhost centos_tomcat-app1-session]# cat context.xml
   
   

#编写dockerfile
[root@localhost centos_tomcat-app1-session]# cat Dockerfile 
#!/bin/bash

From centos-base/tomcat:8.5.57

LABEL chenjianhong [email protected]

ADD server.xml /app/tomcat/conf/server.xml

ADD tomcat-redis.tar.gz /app/tomcat/lib/

ADD app.tar.gz /data/tomcat/webapps/

ADD context.xml /app/tomcat/conf/context.xml

ADD run_tomcat.sh /app/tomcat/bin/run_tomcat.sh

RUN cd /app/tomcat/lib/tomcat-redis && cp * ../ && \
    rm -rf /app/tomcat/lib/tomcat-redis && chown www.www /data /app -R

expose 8080 8005 8009

CMD ["/app/tomcat/bin/run_tomcat.sh"]

#编写构建脚本
[root@localhost centos_tomcat-app1-session]# cat build-command.sh 
#!/bin/bash

docker build -t centos-base/tomcat-app1-redis:v1 .

#执行构建
[root@localhost centos_tomcat-app1-session]# bash build-command.sh 
Sending build context to Docker daemon  803.8kB
Step 1/10 : From centos-base/tomcat:8.5.57
 ---> 0a6136ecde6d
Step 2/10 : LABEL chenjianhong [email protected]
 ---> Using cache
 ---> b63f86f0da22
Step 3/10 : ADD server.xml /app/tomcat/conf/server.xml
 ---> Using cache
 ---> 652f29e9bf5c
Step 4/10 : ADD tomcat-redis.tar.gz /app/tomcat/lib/
 ---> Using cache
 ---> b44eaab856bf
Step 5/10 : ADD app1.tar.gz /data/tomcat/webapps/
 ---> Using cache
 ---> e30257de9085
Step 6/10 : ADD context.xml /app/tomcat/conf/context.xml
 ---> Using cache
 ---> 6656ab7074c5
Step 7/10 : ADD run_tomcat.sh /app/tomcat/bin/run_tomcat.sh
 ---> Using cache
 ---> cfa7542fc9de
Step 8/10 : RUN cd /app/tomcat/lib/tomcat-redis && cp * ../ &&     rm -rf /app/tomcat/lib/tomcat-redis && chown www.www /data /app -R
 ---> Using cache
 ---> 2f1a93bc27a6
Step 9/10 : expose 8080 8005 8009
 ---> Using cache
 ---> b51391c4c654
Step 10/10 : CMD ["/app/tomcat/bin/run_tomcat.sh"]
 ---> Using cache
 ---> 2e2a8bdca9ac
Successfully built 2e2a8bdca9ac
Successfully tagged centos-base/tomcat-app1-redis:v1

dockerfile制作nginx镜像

#查看目录树
[root@localhost nginx]# tree src_tomcat-redis-session/
src_tomcat-redis-session/
├── build-command.sh
├── Dockerfile
├── nginx-1.18.0.tar.gz
└── nginx.conf

0 directories, 4 files

#准备好nginx安装包

#准备好nginx配置文件
[root@localhost src_tomcat-redis-session]# cat nginx.conf 
user  nginx;
worker_processes  auto;
events {
    worker_connections  1024;
}
http {

upstream tomse { 
      #server 172.17.0.3:8080; 
      #server 172.17.0.3:8080; 
      server 192.168.100.101:8181; 
      server 192.168.100.101:8282;
}
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location /usr/local/html {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       80;
        server_name  www.test.com;
        location / {
        proxy_set_header Host $host;
        proxy_pass http://tomse/;
       } 
  }
}

#编写dockerfile
[root@localhost src_tomcat-redis-session]# cat Dockerfile 
#!/bin/bash

From centos-base:7.8.2003

LABEL chenjianhong [email protected]

ADD nginx-1.18.0.tar.gz /usr/local/src

RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install

ADD nginx.conf /usr/local/nginx/conf/

RUN echo "This is test dockerfile nginx" > /usr/local/nginx/html/index.html && chown -R nginx.nginx /usr/local/nginx/html && ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx && \
    rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

expose 80

CMD ["nginx", "-g", "daemon off;"]

#编写构建脚本
[root@localhost src_tomcat-redis-session]# cat build-command.sh 
#!/bin/bash

docker build -t centos-base/nginx-tomcat-redis:session .

#执行构建
[root@localhost src_tomcat-redis-session]# bash build-command.sh 
Sending build context to Docker daemon  1.046MB
Step 1/8 : From centos-base:7.8.2003
 ---> df9d37a83b7a
Step 2/8 : LABEL chenjianhong [email protected]
 ---> Using cache
 ---> 6b9645dc1b05
Step 3/8 : ADD nginx-1.18.0.tar.gz /usr/local/src
 ---> Using cache
 ---> 2fd4fa3dd583
Step 4/8 : RUN cd /usr/local/src/nginx-1.18.0 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
 ---> Using cache
 ---> bea0e89d650a
Step 5/8 : ADD nginx.conf /usr/local/nginx/conf/
 ---> Using cache
 ---> 0e7a876cc055
Step 6/8 : RUN echo "This is test dockerfile nginx" > /usr/local/nginx/html/index.html && chown -R nginx.nginx /usr/local/nginx/html && ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx &&     rm -rf /etc/localtime && ln   -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
 ---> Using cache
 ---> aa296034f907
Step 7/8 : expose 80
 ---> Using cache
 ---> b1192ae8d84c
Step 8/8 : CMD ["nginx", "-g", "daemon off;"]
 ---> Using cache
 ---> 5b1c307c4cb9
Successfully built 5b1c307c4cb9
Successfully tagged centos-base/nginx-tomcat-redis:session

#启动容器
[root@localhost src_tomcat-redis-session]# docker run -it -d -p 80:80 centos-base/nginx-tomcat-redis:session

docker结合nginx、tomcat、redis实现session共享
启动各业务容器测试
docker结合nginx、tomcat、redis实现session共享

测试
docker结合nginx、tomcat、redis实现session共享_第5张图片
docker结合nginx、tomcat、redis实现session共享_第6张图片
可以看到已经实现的session共享,并且是属于两个不同的业务容器