22

谷歌助力,快速实现 Java 应用容器化

 5 years ago
source link: http://blog.720ui.com/2019/docker_jib/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Google 在 2018 年下旬开源了一款新的 Java 工具 Jib,可以轻松地将 Java 应用程序容器化。通过 Jib,我们不需要编写 Dockerfile 或安装 Docker,通过集成到 Maven 或 Gradle 插件,就可以立即将 Java 应用程序容器化。

fuuaqun.png!web

开源地址: https://github.com/GoogleContainerTools/jib

一、什么是 Jib

Jib 是一个快速而简单的容器镜像构建工具,它作为 Maven 或 Gradle 的一部分运行,不需要编写 Dockerfile 或运行 Docker 守护进程。它从 Maven 或 Gradle 中构建我们的 Docker 镜像, 并只将发生变更的层(而不是整个应用程序)推送到注册表来节省宝贵的构建时间。现在,我们对 Docker 构建流程和 Jib 构建流程进行对比。Docker 构建流程,如下所示。

AbauiiF.png!web

Jib 构建流程,则是这样的。

A7Zbqem.png!web

二、实战出真知

1. 构建一个简单的 Java 工程

我们编写一个简单的 Java 类。

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        System.out.println("http://blog.720ui.com");
    }
}

紧接着,我们再创建一个 pom.xml 文件。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lianggzone.sample.lib</groupId>
    <artifactId>helloworld-samples</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>
    <name>helloworld-samples</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jib-maven-plugin.version>1.0.2</jib-maven-plugin.version>
        <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
    </properties>
    <dependencies>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Jib -->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>${jib-maven-plugin.version}</version>
                <configuration>
                    <from>
                        <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/oracle_java8</image>
                    </from>
                    <to>
                        <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1</image>
                    </to>
                    <container>
                        <jvmFlags>
                            <jvmFlag>-Xms512m</jvmFlag>
                            <jvmFlag>-Xdebug</jvmFlag>
                        </jvmFlags>
                        <mainClass>com.lianggzone.HelloWorld</mainClass>
                    </container>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

由于默认访问谷歌的 gcr.io 仓库,而国内访问 gcr.io 不稳定会经常导致网络超时,所以笔者使用了国内的阿里云镜像服务,那么就不需要访问谷歌的仓库了。现在,我们执行 mvn compile jib:build 命令进行自动化构建,它会从 <from> 拉取镜像,并把生成的镜像上传到 <to> 设置的地址。这里,笔者还通过 ` 设置了一些 JVM 参数。

mvn compile jib:build

此外,如果”登录失败,未授权”,需要通过 docker login 登录鉴权一下。此外,更好的做法是,你可以考虑在Maven 中放置凭据。

<settings>
  ...
  <servers>
    ...
        <server>
          <id>registry.cn-hangzhou.aliyuncs.com</id>
          <username>你的阿里云账号</username>
          <password>你的阿里云密码</password>
        </server>
  </servers>
</settings>

最后,执行完成后,我们可以在阿里云镜像仓库获取镜像。

iUvm6rf.png!web

大功告成,现在,我们来验证一把。我们通过 docker pull 拉取镜像,并运行。

docker pull registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1
docker run --name jib-helloworld -it registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-helloworld:v1 /bin/bash

执行结果,如下所示。

36fqArR.png!web

2. 构建一个 SpringBoot 的可运行 Jar

我们来一个复杂一些的项目,构建一个 SpringBoot 的项目。关于 SpringBoot 的使用,可以阅读笔者之前的文章: http://blog.720ui.com/columns/springboot_all/ 。现在,我们首先需要搭建一个工程,并创建一个启动类。

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

同时,需要一个 Web 的接口。

@RestController
public class WebController {
    @RequestMapping("/blog")
    public String index() {
        return "http://blog.720ui.com";
    }
}

紧接着,我们再创建一个 pom.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <groupId>com.lianggzone.sample.lib</groupId>
    <artifactId>springboot-samples</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>
    <name>springboot-samples</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jib-maven-plugin.version>1.0.2</jib-maven-plugin.version>
        <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Jib -->
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>${jib-maven-plugin.version}</version>
                <configuration>
                    <from>
                        <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/oracle_java8</image>
                    </from>
                    <to>
                        <image>registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1</image>
                    </to>
                    <container>
                        <jvmFlags>
                            <jvmFlag>-Xms512m</jvmFlag>
                            <jvmFlag>-Xdebug</jvmFlag>
                        </jvmFlags>
                    </container>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

现在,我们执行 mvn compile jib:build 命令进行自动化构建。执行完成后,我们可以在阿里云镜像仓库获取镜像。

IFRjueR.png!web

现在,我们再来验证一把。我们通过 docker pull 拉取镜像,并运行。

docker pull registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1
docker run -p 8080:8080 --name jib-springboot -it registry.cn-hangzhou.aliyuncs.com/lianggzone/jib-springboot:v1 /bin/bash

执行结果,如下所示。

rAJn6z7.png!web

现在,我们访问 http://localhost:8080/blog ,我们可以正常调用 API 接口了。

BfANbin.png!web

3. 构建一个 WAR 工程

Jib 还支持 WAR 项目。如果 Maven 项目使用 war-packaging 类型,Jib 将默认使用 distroless Jetty 作为基础镜像来部署项目。要使用不同的基础镜像,我们可以自定义 <container><appRoot><container> <entrypoint><container> <args> 。以下是使用 Tomcat 镜像的案例。

<configuration>
  <from>
    <image>tomcat:8.5-jre8-alpine</image>
  </from>
  <container>
    <appRoot>/usr/local/tomcat/webapps/ROOT</appRoot>
  </container>
</configuration>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK