一、内容介绍:
(1) 本章目的:
将一个自行开发的java程序webpay-api,制作为Docker自定义镜像,并且进行部署。
(2) 实验环境:
物理机:VMware 虚拟机 centos 7.8 x64
Docker:Docker version 20.10.17, build 100c701
(3) 基础镜像: ubuntu:latest
二、制作步骤:
主要是,前期程序文件准备和上传(略:java程序文件), 后续镜像文件构建,以及最后容器部署和容器检查。
三、步骤细节:
Part 1: 制作镜像文件
step 1:
制作镜像文件
mkdir -p /home/src/webpay-api/ubuntu-v/
cat <<EOF> /home/src/webpay-api/ubuntu-v/webpay-api.Dockerfile
FROM ubuntu:latest
MAINTAINER tian.dong <tiandong19860806@126.com>
# 下载资源:
# https://github.com/sgerrand/alpine-pkg-glibc/releases
# https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-2.35-r0.apk
# htTPS://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-bin-2.35-r0.apk
# https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-i18n-2.35-r0.apk
# 基础镜像版本
# FROM alpine:latest === 体积: 6 MB
# FROM centos:7 === 体积: 204 MB
# FROM ubuntu:latest === 体积: 72 MB
# 使用RUN,不用CMD的原因,执行顺序 RUN (build) < CMD (starting) < ENTRYPOINT (started)
# 并且,在RUN命令中,所有VOLUMN或docker -v path:path的外部挂载点,都是处于尚未就绪的状态;只有ENTRYPOINT时刻,才会挂载外部存储介质完毕。
# ==============================================================================================================
# step 1: 初始化依赖环境
# RUN mkdir -p {/home/env/jdk,/home/app/webpay/webpay-api,/home/log/webpay/webpay-api} # 如果是-v参数外部挂载目录,则不需要和不能手动创建
VOLUME ["/home/env/jdk", "/home/app/webpay/webpay-api", "/home/log/webpay/webpay-api"]
COPY app/* /home/app/webpay/webpay-api/
# ADD jdk/jdk-12.0.33-x64_bin.tar.gz /home/env/jdk/
RUN chmod x /home/app/webpay/webpay-api/*.sh && \
sh -c /home/app/webpay/webpay-api/install-jdk.sh && \
sh -c /home/app/webpay/webpay-api/install-app.sh
# ==============================================================================================================
# step 2: 安装和启动程序
# 开放端口
EXPOSE 9080
# 启动程序命令
ENTRYPOINT ["/bin/bash", "-c", "/home/app/webpay/webpay-api/startup.sh"]
# ==============================================================================================================
EOF
step 2:
执行构建镜像文件
cd /home/src/webpay-api/ubuntu-v/
# *.dockerfile文件路径参数,后面的"."符号一定不要忘记。
docker build -f ./webpay-api.Dockerfile . -t app-webpay-api-ubuntu:2.0.0
# step 3:
检查镜像文件结果
docker images
查看docker文件的层大小
docker history {image_id}
输出结果,如下:
## [root@CNT7XVM01 ~]# docker images
## REPOSITORY TAG IMAGE ID CREATED SIZE
## app-webpay-api-ubuntu 2.0.0 fd15a5030735 12 seconds ago 72.8MB
## app-webpay-api-openjdk 1.0.0 9e3a1aab7e37 3 days ago 314MB
## adoptopenjdk/openjdk8 jre8u352-b05-ea-ubuntu-nightly 960f904eb606 7 weeks ago 224MB
## alpine latest c059bfaa849c 13 months ago 5.59MB
## ubuntu latest ba6acccedd29 14 months ago 72.8MB
## [root@CNT7XVM01 ~]#
## [root@CNT7XVM01 ~]# docker history app-webpay-api-ubuntu:2.0.0
## IMAGE CREATED CREATED BY SIZE COMMENT
## fd15a5030735 26 minutes ago /bin/sh -c #(nop) EXPOSE 9080 0B
## 98364f223752 26 minutes ago /bin/sh -c chmod x /home/app/webpay/webpay-… 0B
## 79e8702ae883 26 minutes ago /bin/sh -c #(nop) COPY file:ab6bb0813fdd174f… 133B
## 26a3c2dc515c 26 minutes ago /bin/sh -c #(nop) VOLUME [/home/env/jdk /ho… 0B
## 33a5031d5553 26 minutes ago /bin/sh -c #(nop) MAINTAINER tian.dong <tia… 0B
## ba6acccedd29 14 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
## <missing> 14 months ago /bin/sh -c #(nop) ADD file:5d68d27cc15a80653… 72.8MB
## [root@CNT7XVM01 ~]#
Part 2: 启动容器实例
step 1:
创建物理机本地路径:准备挂载docker容器上面
mkdir -p {/home/env/jdk,/home/app/webpay/webpay-api,/home/log/webpay/webpay-api}
# 授权docker账号的文件路径访问权限
chmod -R a x /home/env/jdk && \
chown -R docker:docker /home/env/jdk
#
chmod -R a x /home/app/webpay/webpay-api && \
chown -R docker:docker /home/app/webpay/webpay-api
#
chmod -R a x /home/log/webpay/webpay-api && \
chown -R docker:docker /home/log/webpay/webpay-api
step 2:
查询物理机的账号docker的uid:后面启动容器要说明用户参数,
必须用uid,不能使用username。
cat /etc/passwd | grep docker
# 输出结果,如下:
## [root@CNT7XVM01 ~]# cat /etc/passwd | grep docker
## docker:x:1001:1001::/home/docker:/bin/bash
## [root@CNT7XVM01 ~]#
step 3:
启动容器,声明启动用户、挂载目录、命令环境
# -d:将容器在后台运行
# -t: 使用虚拟终端
推荐方式:指定用户 docker
# 方式1:推荐方式:指定用户 docker
docker run -td -u 1001:1001 -p 9080:9080 -v /home/env/jdk:/home/env/jdk \
-v /home/app/webpay/webpay-api:/home/app/webpay/webpay-api \
-v /home/log/webpay/webpay-api:/home/log/webpay/webpay-api \
app-webpay-api-ubuntu:2.0.0 \
/bin/bash
# 或者,
# 方式2:测试方式:默认用户 root 【不推荐】
docker run -td -p 9080:9080 -v /home/env/jdk:/home/env/jdk \
-v /home/app/webpay/webpay-api:/home/app/webpay/webpay-api \
-v /home/log/webpay/webpay-api:/home/log/webpay/webpay-api \
app-webpay-api-ubuntu:2.0.0 \
/bin/bash
# 输出结果,如下:
## [root@CNT7XVM01 ~]# docker run -td -p 9080:9080 -v /home/env/jdk:/home/env/jdk \
## > -v /home/app/webpay/webpay-api:/home/app/webpay/webpay-api \
## > -v /home/log/webpay/webpay-api:/home/log/webpay/webpay-api \
## > app-webpay-api-ubuntu:2.0.0 \
## > /bin/bash
## 39d9249adda772e7657aaf77000c6ce40e5db85ac471c1a5374b4e0477683638
## [root@CNT7XVM01 ~]#
## [root@CNT7XVM01 ~]# docker ps -a
## CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
## 39d9249adda7 app-webpay-api-ubuntu:2.0.0 "/bin/bash -c /home/…" 6 seconds ago Up 4 seconds 0.0.0.0:9080->9080/tcp, :::9080->9080/tcp angry_leavitt
## [root@CNT7XVM01 ~]#
## [root@CNT7XVM01 ~]# netstat -nltp
## Active Internet connections (only servers)
## Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
## tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 978/sshd
## tcp 0 0 0.0.0.0:9080 0.0.0.0:* LISTEN 11373/docker-proxy
## tcp6 0 0 :::9100 :::* LISTEN 981/node_exporter
## tcp6 0 0 :::22 :::* LISTEN 978/sshd
## tcp6 0 0 :::9080 :::* LISTEN 11379/docker-proxy
## tcp6 0 0 ::1:25 :::* LISTEN 1187/master
## [root@CNT7XVM01 ~]#
# Part 3: 进入容器内部
step 1:
进入容器内部,查看目录挂载是否成功
# 进入容器内部
docker exec -it -u 1001:1001 39d9249adda7 /bin/bash
# 或者
docker exec -it 39d9249adda7 /bin/bash
# 输出结果,如下:
## [root@CNT7XVM01 ~]# docker exec -it 39d9249adda7 /bin/bash
## root@39d9249adda7:/#
## root@39d9249adda7:/# hostname
## 39d9249adda7
## root@39d9249adda7:/#
## root@39d9249adda7:/# ls -al /home/env/jdk/jdk-12/
## total 20
## drwxrwxrwx. 9 1001 1001 107 Nov 28 14:11 .
## drwxrwxrwx. 3 1001 1001 20 Nov 28 14:11 ..
## drwxrwxrwx. 2 1001 1001 4096 Nov 28 14:11 bin
## drwxrwxrwx. 5 1001 1001 123 Nov 28 14:11 conf
## drwxrwxrwx. 3 1001 1001 132 Nov 28 14:11 include
## drwxrwxrwx. 2 1001 1001 4096 Nov 28 14:11 jmods
## drwxrwxrwx. 72 1001 1001 4096 Nov 28 14:11 legal
## drwxrwxrwx. 5 1001 1001 4096 Nov 28 14:11 lib
## drwxrwxrwx. 3 1001 1001 18 Nov 28 14:11 man
## -rwxrwxrwx. 1 1001 1001 1231 Feb 21 2019 release
## root@39d9249adda7:/#
## root@39d9249adda7:/# ls -al /home/app/webpay/webpay-api/
## total 87288
## drwxrwxrwx. 3 1001 1001 106 Dec 29 07:22 .
## drwxr-xr-x 3 root root 24 Dec 30 04:03 ..
## drwxr-xr-x. 3 1001 1001 24 Dec 29 02:27 conf
## -rwxr-xr-x. 1 1001 1001 455 Dec 29 07:20 docker-compose.yml
## -rwxr-xr-x. 1 1001 1001 134 Dec 29 07:15 install-all.sh
## -rwxr-xr-x. 1 1001 1001 1831 Dec 26 06:33 startup.sh
## -rwxr-xr-x. 1 1001 1001 89369065 Dec 19 04:19 webpay-api.jar
## root@39d9249adda7:/#
step 2: 进入容器,查看安装的环境和应用启动情况
java --vesion
ps -ef | grep java
# 输出结果,如下:
## root@39d9249adda7:/# java --version
## java 12 2019-03-19
## Java(TM) SE Runtime Environment (build 12 33)
## Java HotSpot(TM) 64-Bit Server VM (build 12 33, mixed mode, sharing)
## root@39d9249adda7:/#
## root@39d9249adda7:/# ps -ef | grep java
## root 27 1 12 04:42 pts/0 00:00:55 /home/env/jdk/jdk-12/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -server -Xmx2048m -Xms512m -XX:MaxGCPauseMillis=20 -XX: UseG1GC -XX:MetaspaceSize=256m -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX:MaxMetaspaceFreeRatio=80 -Dserver.port=9080 -Dlogging.file.name=/home/log/webpay/webpay-api/webpay-api.log -XX:HeapDumpPath=/home/log/webpay/webpay-api/HeapDumpOnOutOfMemoryError/ -Dapp.cache.type=redis -Dapp.database.type=postgresql -Dapp.payment.channel=alipay -Dglobal.deploy.app.api=true -Dspring.profiles.active=dev -jar /home/app/webpay/webpay-api/webpay-api.jar
## root 203 161 0 04:50 pts/1 00:00:00 grep --color=auto java
## root@39d9249adda7:/#
## root@39d9249adda7:/#
step 3: 在外部测试和访问部署的应用服务
提醒,如果docker部署的虚拟机没有开放程序端口9080,记得要自己开放一下。
开放防火墙端口,命令如下:
# 开放端口 9080
firewall-cmd --zone=public --add-port=9080/tcp --permanent
# 保存设置:永久保存
firewall-cmd --reload
# 查询开放的端口
firewall-cmd --zone=public --list-ports
访问应用监控接口
访问业务查询接口
Step 4:
对部署的docker 应用服务,执行jemeter压力测试。
对比VM虚拟机部署和Docker方式部署(同一台机器,资源参数和应用并发参数相同),两者性能测试结果差别如下:相差10%。
(1) VM 虚拟机: TPS = 9644 / s
(2) Docker: TPS = 8671 / s
VM 虚拟机,压测结果 TPS = 9644 /S
Docker 容器,压测结果 TPS = 8671 /S
,