046- multitasking problem solving google jib

This is insist on technical writing programs (including translation) of 46, set a small target 999, a minimum of two per week.

This article explains when using google jib packaged application if the underlying image is openjdk the alpine series, after the successful operation can not be used jmap, jstack, arthas and other tools to work properly, the following error message

[ERROR] Start arthas failed, exception stack trace:
com.sun.tools.attach.AttachNotSupportedException: Unable to get pid of LinuxThreads manager thread
at sun.tools.attach.LinuxVirtualMachine.(LinuxVirtualMachine.java:86)
at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:78)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:250)
at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:72)
at com.taobao.arthas.core.Arthas.(Arthas.java:25)
at com.taobao.arthas.core.Arthas.main(Arthas.java:99)
[ERROR] attach fail, targetPid: 1
复制代码

Before writing the  build Docker (based on Google jib) using Maven to accelerate and simplify  finally have mentioned, but did not begin in detail.

google jib and Dragonfly series

Brief google jib and Ali Dragonfly

Ali dragonfly and google jib in fact, are similar problems, how to maximize reuse of dependence. However, the two ideas, Dragonfly is based on the mirror does not change, similar ideas cdn recent data source cache, it can not pull the mirror repeated from the public network to a LAN such, and because the use of p2p transmission, it is possible to effectively prevent the stand-alone hotspot played bandwidth. (D7y mainly use p2p to solve the problem of large file transfers, image transfer for only incidentally a function of the hand), d7y is how to solve the manufactures to optimize the transmission.

google jib idea is to invade the build process, play a sense of ownership, I build, I optimized. For example, in use gradle, maven build, is the largest volume or JDK jre, followed by various tripartite jar package, the web is again the static resource (resource available to those not within the scope of fresh plug document), and these things they often do not change frequently, and often part of the source code changes, then separates it from using the cache layer docker, not only save space, but also save bandwidth, but also save time.

Google jib configuration

settings.xml increase auth configuration

settings.xml increase auth certification of registry server, password can use  Maven password Encryption  , it is not recommended to store passwords in plain text

<settings>
  ...
  <servers>
    ...
    <server>
      <id>MY_REGISTRY</id>
      <username>MY_USERNAME</username>
      <password>{MY_SECRET}</password>
    </server>
  </servers>
</settings>
复制代码

anjia0532/openjdk-8-alpine-lib的Dockerfile

FROM openjdk:8-jdk-alpine

ARG ARTHAS_VERSION="3.1.3"
ARG MIRROR=false

ENV MAVEN_HOST=http://repo1.maven.org/maven2 \
    ALPINE_HOST=dl-cdn.alpinelinux.org \
    MIRROR_MAVEN_HOST=http://maven.aliyun.com/repository/public \
    MIRROR_ALPINE_HOST=mirrors.tuna.tsinghua.edu.cn

# if use mirror change to aliyun mirror site
RUN if $MIRROR; then MAVEN_HOST=${MIRROR_MAVEN_HOST} ;ALPINE_HOST=${MIRROR_ALPINE_HOST} ; sed -i "s/dl-cdn.alpinelinux.org/${ALPINE_HOST}/g" /etc/apk/repositories ; fi

RUN \
    # https://github.com/docker-library/openjdk/issues/76
    apk add --no-cache tini

RUN \
    # change to GMT+0800 timezone
    apk add --no-cache tzdata && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" >  /etc/timezone

RUN  \
    # download & install arthas
    wget -qO /tmp/arthas.zip "${MAVEN_HOST}/com/taobao/arthas/arthas-packaging/${ARTHAS_VERSION}/arthas-packaging-${ARTHAS_VERSION}-bin.zip" && \
    mkdir -p /opt/arthas && \
    unzip /tmp/arthas.zip -d /opt/arthas && \
    rm /tmp/arthas.zip

# Tini is now available at /sbin/tini
ENTRYPOINT ["/sbin/tini", "--"]
复制代码

Build command

docker build --build-arg MIRROR=true . -t anjia0532/openjdk-8-alpine-lib
复制代码

Modify pom.xml

<project>
    <groupId>com.example.module</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>demo</name>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>1.6.1</version>
                <configuration>
                    <from>
                        <image>anjia0532/openjdk-8-alpine-lib</image>
                    </from>
                    <to>
                        <image>${project.artifactId}:${project.version}</image>
                    </to>
                    <container>
                        <entrypoint>
                            <shell>/sbin/tini</shell>
                            <option>--</option>
                        </entrypoint>
                        <args>
                            <arg>java</arg>
                            <arg>-cp</arg>
                            <arg>/app/resources/:/app/classes/:/app/libs/*</arg>
                            <arg>com.example.application</arg>
                        </args>
                        <ports>
                            <port>8080</port>
                            <port>8563</port><!-- arthas 端口 -->
                        </ports>
                        <environment>
                            <_JAVA_OPTIONS>-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/</_JAVA_OPTIONS>
                        </environment>
                    </container>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

The main function is tini, need entrypoint portion disposed /sbin/tini -- However, according to the document  github.com/GoogleConta...  the

ENTRYPOINT ["java", jib.container.jvmFlags, "-cp", "/app/resources:/app/classes:/app/libs/*", jib.container.mainClass]
CMD [jib.container.args]
复制代码

Application jib constructed, will start using the default commands into entrypoint part, will overwrite baseimage in writing entryoint, if defined in the pom in the case, will override the default startup command jib, we can only put the project will start args section, but they can not use jvmFlags, default cp, the default main type inference, you must explicitly declare themselves.

Generate Mirror

mvn compile -Pprod jib:build  # 构建并推送到目标registry上,构建主机不需要安装docker
mvn compile -Pprod jib:dockerBuild # 构建到本机docker daemon
mvn compile -Pprod jib:buildTar # 构建并生成tar.gz,可以使用docker load --input target/jib-image.tar 导入
复制代码

If you are jenkins, etc. CICD scene, so that each developed modify pom inconvenient, can be constructed using the command line.

mvn compile com.google.cloud.tools:jib-maven-plugin:1.6.1:dockerBuild \
    -Djib.to.image=myregistry/myimage:latest \
    -Djib.to.auth.username=$USERNAME \
    -Djib.to.auth.password=$PASSWORD \
    -Djib.container.environment=key1="value1",key2="value2" \
    -Djib.container.args=arg1,arg2,arg3
复制代码

Other

Whether the above example is java maven or gradle type applications, for nodejs or golang, jib official also supports user-based jib core write your own plug-ins build

Recruitment small ads

Jinan little friends to vote resume ah welcome to join us , do things together.
Long-term recruitment, Java programmers, engineers, big data, operation and maintenance engineers, front-end engineers.

Reference material

Guess you like

Origin juejin.im/post/5d889a2bf265da03c9273875