制作带有java监控命令的镜像

在生产环境中,经常有docker容器的CPU消耗太高而导致宿主机的CPU占用率攀升,引起AWS CloudWatch的警报邮件等,另外在批量重启容器的时候,也会经常遇到短暂对宿主机的CPU消耗过高的情况,现阶段准备再原来的容器中新增java的监控命令。具体是在镜像中包含如:jps、jstat和ps等命令的容器。

寻找包含jstat的openjdk

可以通过以下命令查找哪些openjdk版本包中含需要的命令。

yum provides "*jstat" --enablerepo=epel
yum provides "*jps" --enablerepo=epel
yum provides "*ps" --enablerepo=epel

最终选择你需要的版本,这里我选择的是java-1.8.0-openjdk-devel-1.8.0.181-3.b13.el7_5.x86_64,在这个版本包中包含所需的java监控命令。

(一步到位, 一个Dockerfile搞定所有)

制作非缩减版镜像

Dockfile

FROM docker.io/centos:7.4.1708
# author
MAINTAINER longsl <583297550@qq.com>
# install jdk including jps,jstat,ps command.
RUN yum -y update && yum -y install java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64 && yum -y install java-1.8.0-openjdk-devel-1.8.0.191.b12-0.el7_5.x86_64
# add group and user in OS
RUN groupadd -g 2003 kpgame && useradd -u 2003 -g 2003 -s /sbin/nologin -d /dev/null -M kpgame
# add tomcat gzfile
ADD tomcat8.tar /usr/local/tomcat/
ADD kp.tar /usr/local/tomcat/webapps/kp/
# set env
ADD https://s3.amazonaws.com/resource.pinball-heroes.com/dockerfiles/common/tomcat/setenv.sh /usr/local/tomcat/bin
RUN chown -R kpgame:kpgame /usr/local/tomcat/
ENV CATALINA_HOME /usr/local/tomcat
ENV CATALINA_BASE /usr/local/tomcat
ENV PATH ${PATH}:${JAVA_HOME}/bin::$CATALINA_HOME/lib:$CATALINA_HOME/bin
# run container with base path:/usr/local/tomcat/bin/catalina.sh
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

build image

#! /bin/bash
sudo docker build -t longsl/kp .

进入容器内部

sudo docker exec -it KP_KP_SERVER_s8009_kp_29 /bin/bash

# 通过镜像的方式直接启动容器并进入
sudo docker run -it ec2-18-221-193-240.us-east-2.compute.amazonaws.com/kp-longsl /bin/bash

镜像大小

ec2-18-221-193-240.us-east-2.compute.amazonaws.com/kp   latest   4870cc895b20   3 hours ago   499 MB

(分步完成)

制作openjdk+tomcat8 Image(第一步)

上面那种是一次构建完成所有步骤,在我们的集群环境中,可能存在项目包的版本更新,所有需要制作一个基础的jdk+tomcat的基础版本,然后在这个镜像之上进行项目的版本迭代。

Dockerfile

FROM docker.io/centos:7.4.1708
# author
MAINTAINER longsl <583297550@qq.com>
# install jdk including jps,jstat,ps command.
RUN yum -y install java-1.8.0-openjdk-devel-1.8.0.181-3.b13.el7_5.x86_64
# add group and user in OS
RUN groupadd -g 2003 kpgame && useradd -u 2003 -g 2003 -s /sbin/nologin -d /dev/null -M kpgame
# add tomcat gzfile
ADD tomcat8.tar.gz /usr/local/tomcat/

build image

sudo docker build -t longsl/tomcat:8.0 .

制作镜像(第二步)

Dockerfile

# kpserver dockerfile
FROM ec2-18-221-193-240.us-east-2.compute.amazonaws.com/tomcat
MAINTAINER dingxing <303159963@qq.com>
ADD https://s3.amazonaws.com/resource.pinball-heroes.com/dockerfiles/common/tomcat/setenv.sh /usr/local/tomcat/bin
ADD kp.tar /usr/local/tomcat/webapps/kp/
RUN chown -R kpgame:kpgame /usr/local/tomcat/
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

build image

sudo docker build -t ec2-18-221-193-240.us-east-2.compute.amazonaws.com/kp:P11_1_V1.05.longsl .

镜像大小

ec2-18-221-193-240.us-east-2.compute.amazonaws.com/kp  P11_1_V1.05.longsl   8bcfe44ba2e5   9 minutes ago   617 MB

测试centos官方镜像

官方centos7镜像

sudo docker run -it centos:7.5.1804 /bin/bash

sudo docker images | grep centos7.5.1804
docker.io/centos      7.5.1804     fdf13fa91c6e      4 weeks ago      200 MB

官方最小版centos7镜像

操作系统模板缓存是安装在容器中的操作系统模板,然后打包到gzip压缩包中。使用这样的缓存,可以在几分钟内创建新容器。
导入模板并启动容器 模板下载地址

docker import https://download.openvz.org/template/precreated/centos-7-x86_64-minimal.tar.gz longsl/centos-7-x86_64-minimal

sudo docker images | grep centos-7-x86_64-minimal
longsl/centos-7-x86_64-minimal     latest    08001dd5a457    7 minutes ago     435 MB

sudo docker run -it longsl/centos-7-x86_64-minimal /bin/bash

制作精简版镜像(一个Dockerfile搞定所有)

精简版镜像主要缩减的地方:centos不能缩减,jdk可以缩减到只需要最基本的功能。

缩减jdk源文件

#首先下载jre,下载地址是https://www.java.com/en/download/manual.jsp,大概是77M。
wget http://javadl.oracle.com/webapps/download/AutoDL?BundleId=234464_96a7b8442fe848ef90c96a2fad6ed6d1
mv AutoDL\?BundleId\=234464_96a7b8442fe848ef90c96a2fad6ed6d1 jre-8u181-linux-x64.tar.gz

#解压,删除根目录下文本文件,然后删除其他不必要文件。
tar xvcf jre-8u161-linux-x64.tar.gz
#进入目录
cd jre1.8.0_161/
#删除文本文件
rm -rf COPYRIGHT LICENSE README release THIRDPARTYLICENSEREADME-JAVAFX.txt THIRDPARTYLICENSEREADME.txt Welcome.html
#删除其他无用文件
rm -rf  lib/plugin.jar \
           lib/ext/jfxrt.jar \
           bin/javaws \
           lib/javaws.jar \
           lib/desktop \
           plugin \
           lib/deploy* \
           lib/*javafx* \
           lib/*jfx* \
           lib/amd64/libdecora_sse.so \
           lib/amd64/libprism_*.so \
           lib/amd64/libfxplugins.so \
           lib/amd64/libglass.so \
           lib/amd64/libgstreamer-lite.so \
           lib/amd64/libjavafx*.so \
           lib/amd64/libjfx*.so

# 重新打包所有文件(不打包也可以,在Dockerfile里ADD这个目录即可,当前精简完jre目录大小是107M,压缩后是41M)
# 注意这里打出的包解压出来就直接是各个文件模块,不包含外层的文件夹
tar -zcvf jre8.tar.gz *

Dockerfile

FROM docker.io/centos:7.4.1708
# author
MAINTAINER longsl <583297550@qq.com>
# install slim jdk
ADD jre8.tar.gz /usr/local/jdk/
# add group and user in OS
RUN groupadd -g 2003 kpgame && useradd -u 2003 -g 2003 -s /sbin/nologin -d /dev/null -M kpgame
# add tomcat gzfile
ADD tomcat8.tar /usr/local/tomcat/
ADD kp.tar /usr/local/tomcat/webapps/kp/
# set env
ADD https://s3.amazonaws.com/resource.pinball-heroes.com/dockerfiles/common/tomcat/setenv.sh /usr/local/tomcat/bin
RUN chown -R kpgame:kpgame /usr/local/tomcat/
ENV JAVA_HOME /usr/local/jdk/
ENV CATALINA_HOME /usr/local/tomcat
ENV CATALINA_BASE /usr/local/tomcat
ENV PATH ${PATH}:${JAVA_HOME}/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# run container with base path:/usr/local/tomcat/bin/catalina.sh
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

build image

sudo docker build ec2-18-221-193-240.us-east-2.compute.amazonaws.com/kp:kp-slim .

镜像大小

ec2-18-221-193-240.us-east-2.compute.amazonaws.com/kp   kp-slim   3187e2e4a18d   36 minutes ago   458 MB
隐藏边栏