1. 需求
如上图所示:
- Hot是应用程序(springboot),打成jar包:Hot.jar
- 利用dockerfile将Hot.jar构建成镜像lgedu/hot:1.0
- 构建Swarm 集群
- 在 Swarm 集群中使用 compose 文件 (docker-compose.yml) 来配置、启动多个服务 包括: Mysql、Redis以及应用程序Hot
2. dockerFile构建镜像
2.1 CentOS镜像模版
vim dockerfile-centos
# 依据哪个镜像创建
From centos:7.6.1810
# 指定容器内部使用语言
ENV LANG="en_US.UTF-8"
ENV LC_ALL="en_US.UTF-8"
# 使用亚洲/上海时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 创建工作目录
RUN mkdir -p /data/apps /data/data /data/logs;ln -s /data/apps /apps
# 安装字体
RUN yum install -y epel-release
RUN yum groupinstall -y "fonts"
RUN yum install -y kde-l10n-Chinese
# 安装openssl等依赖包
RUN yum install -y openssl openssl-devel
RUN yum install -y crontabs cronolog ntp
# 安装数据库依赖
RUN yum install -y mariadb-libs
RUN ln -s /usr/lib64/mysql/libmysqlclient.so.18 /usr/lib64/libmysqlclient_r.so.16
RUN yum install -y gcc cmake
RUN yum install -y lrzsz telnet net-tools file bind-utils less
RUN yum install -y jq xml2; yum clean all
RUN yum install -y expat-devel apr-devel ghostscript ghostscript-devel
# 运行容器时的默认命令
CMD ["/bin/bash"]
构建命令:
docker build -f dockerfile-centos -t lagou/centos/7.6/centos .
docker run --rm -it lagou/centos/7.6/centos
2.2 JDK镜像模板
vim dockerfile-jdk
FROM lagou/centos/7.6/centos
ENV JAVA_HOME="/apps/jdk"
ENV PATH=${JAVA_HOME}/bin:$PATH
ADD ./jdk-8u251-linux-x64.tar.gz /apps/
RUN ln -s /apps/jdk1.8.0_251 /apps/jdk
ADD ./UnlimitedJCEPolicyJDK8/US_export_policy.jar /apps/jdk/jre/lib/security/
ADD ./UnlimitedJCEPolicyJDK8/local_policy.jar /apps/jdk/jre/lib/security/
ADD ./msyhbd.ttf /apps/jdk/jre/lib/fonts/
ADD ./msyh.ttf /apps/jdk/jre/lib/fonts/
CMD ["/bin/bash"]
#下载jce_policy-8.zip并解压到当前目录
wget http://pkgs-linux.cvimer.com/jdk/1.8/jce_policy-8.zip
unzip jce_policy-8.zip
#下载jdk-8u251-linux-x64.tar.gz、msyh.ttf、msyhbd.ttf
wget http://pkgs-linux.cvimer.com/jdk/1.8/jdk-8u251-linux-x64.tar.gz
wget http://pkgs-linux.cvimer.com/fonts/msyh.ttf
wget http://pkgs-linux.cvimer.com/fonts/msyhbd.ttf
#构建
docker build -f dockerfile-jdk -t lagou/centos/7.6/jdk/1.8/jdk .
2.3 Tomcat镜像模版
vim dockerfile-tomcat
FROM lagou/centos/7.6/jdk/1.8/jdk
ENV TOMCAT_HOME="/apps/tomcat"
ENV PATH=${TOMCAT_HOME}/bin:$PATH
RUN yum install -y gcc make expat-devel apr-devel; yum clean all ; rm -rf /var/cache/yum/*
ADD ./apr-1.6.5.tar.gz /root/
RUN cd /root/apr-1.6.5 && ./configure && make && make install && rm -rf /root/apr*
ADD ./apr-util-1.6.1.tar.gz /root/
RUN cd /root/apr-util-1.6.1 && ./configure --with-apr=/usr/local/apr && make && make install && rm -rf /root/apr*
ADD ./apache-tomcat-7.0.93.tar.gz /data/apps/
RUN ln -s /apps/apache-tomcat-7.0.93 /apps/tomcat
RUN cd /apps/tomcat/bin; tar xf tomcat-native.tar.gz; cd /apps/tomcat/bin/tomcat-native-1.2.21-src/native && \ ./configure --with-apr=/usr/local/apr && make && make install
CMD ["/bin/bash"]
构建命令:
#下载包
wget http://pkgs-linux.cvimer.com/apr/1.6.5/apr-1.6.5.tar.gz
wget http://pkgs-linux.cvimer.com/apr/1.6.1/apr-util-1.6.1.tar.gz
wget https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.93/bin/apache-tomcat-7.0.93.tar.gz
docker build -f dockerfile-tomcat -t lagou/centos/7.6/jdk/1.8/tomcat/7.0.9/tomcat .
docker run --rm -it --network host lagou/centos/7.6/jdk/1.8/tomcat/7.0.9/tomcat
#进入Docker
cd apps
cd tomcat/bin
./startup.sh
3. 自定义Hot镜像模板
vi dockerfile-hot
FROM openjdk:11-jdk-oracle
# 设置工作目录,进入到容器中的初始目录,不存在会自动创建
ENV MYPATH /root/springboot
WORKDIR $MYPATH
# 复制jar到工作目录并改名为app.jar
ADD hot.jar app.jar
# 添加容器卷,方便以后的处理,关联宿主机中的目录,不存在会自动创建
VOLUME $MYPATH
#声明镜像内服务所监听的端口
EXPOSE 8080
# 运行jar包
CMD java -jar app.jar
#构建镜像
docker build -f dockerfile-hot -t lgedu/hot:1.0 .
#运行
docker run --rm -it --network host lgedu/hot:1.0
4. swarm集群
docker swarm 集群搭建管理参考:https://blog.csdn.net/rzpy_qifengxiaoyue/article/details/110223434
docke compose 使用参考: https://blog.csdn.net/rzpy_qifengxiaoyue/article/details/110228978
docker-compose.yml 配置如下
version: "3.0"
services:
mysql:
image: mysql:5.7.30
ports:
- 13306:3306
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--default-time-zone=+8:00
environment:
MYSQL_ROOT_PASSWORD: "root"
volumes:
- "/docker/mysql/db:/var/lib/mysql"
deploy:
mode: replicated
replicas: 2
redis:
image: redis:5.0.9
environment:
- TZ=Asia/beijing
ports:
- 6379:6379
volumes:
- /docker/redis/data:/data
deploy:
mode: replicated
replicas: 2
#自定义的hot项目镜像
hot:
image: lgedu/hot:1.0
ports:
- 9090:8080
# hot启动需要依赖mysql,redis
depends_on:
- mysql
- redis
deploy:
mode: replicated
replicas: 2
compose启动
docker-compose up -d
与swarm一起使用启动服务:
docker stack deploy -c docker-compose.yml web
查看服务:
docker stack services web
删除服务:
docker stack down web