用 Jib 将 Kubernetes 的 Java 应用程序容器化

容器是许多复杂分布式系统的核心。但是,对于 Java 开发人员来说,构建容器是使用 Kubernetes 最常见的一大难题。传统上,Dockerfile 会强制进行容器构建定义,这可能很麻烦、易出错,而且速度很慢。在本次演讲中,我们将介绍一种名为 Jib 的容器图像构建器。Jib 具有声明性 - 它通过从 Maven 或 Gradle 项目推断出它需要哪些内容,来将 Java 应用程序打包到容器中。Jib 速度很快,可重复地构建容器,并允许 Java 开发人员使用他们已经熟悉的构建工具。
展开查看详情

1. A container image builder for Java applications Build containers faster with Jib github.com/GoogleContainerTools/jib

2. Our Team Cloud Tools for Java Appu Goundan Qingyang “Q” Chen @loosebazooka @coollog

3.Containers “Write once, run anywhere” github.com/GoogleContainerTools/jib

4. Building a Java container github.com/GoogleContainerTools/jib

5. Me Java Developer Building website for pet clinic Wants to containerize the backend Wants container on registry ilovejava.io/petclinic-app github.com/GoogleContainerTools/jib

6.github.com/GoogleContainerTools/jib

7.构建Java镜像 github.com/GoogleContainerTools/jib

8.So I read some tutorials

9.FROM ubuntu:14.04 RUN apt-get update && apt-get install -y python-software-properties software-properties-common RUN add-apt-repository ppa:webupd8team/java RUN echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 boolean true"| debconf-set-selections RUN apt-get update && apt-get install -y oracle-java8-installer maven ADD . /usr/local/petclinic RUN cd /usr/local/petclinic && mvn install CMD ["/usr/bin/java", "-cp", "/usr/local/petclinic/target/petclinic-1.0.jar" , "petclinic.WebServer"] github.com/GoogleContainerTools/jib

10.So I read some more tutorials

11.FROM openjdk:8 COPY target/petclinic-*.jar /app.jar ENTRYPOINT java -jar /app.jar github.com/GoogleContainerTools/jib

12.FROM openjdk:8 COPY target/petclinic-*.jar /app.jar ENTRYPOINT java -jar /app.jar Problem: openjdk:8 is 284MB github.com/GoogleContainerTools/jib

13.Some more searching

14.FROM openjdk:8-jre-alpine 82 MB COPY target/petclinic-*.jar /app.jar ENTRYPOINT java -jar /app.jar github.com/GoogleContainerTools/jib

15.Dockerfile best practices github.com/GoogleContainerTools/jib

16.docs.docker.com/develop/develop-images/dockerfile_best-practices github.com/GoogleContainerTools/jib

17..dockerignore ** !target/petclinic-*.jar github.com/GoogleContainerTools/jib

18.Some more tutorials later

19.$ mvn dependencies:copy-dependencies to target/dependencies/ FROM openjdk:8-jre-alpine COPY target/dependencies /app/dependencies COPY target/classes /app/classes ENTRYPOINT java -cp /app/dependencies/*:/app/classes petclinic.WebServer github.com/GoogleContainerTools/jib

20.Some more searching

21. ... <build> <plugins> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.4.8</version> <configuration> <repository>ilovejava.io/petclinic-app</repository> <tag>${project.version}</tag> </configuration> </plugin> </plugins> </build> ... github.com/GoogleContainerTools/jib

22.What did we 1. 2. Write first Dockerfile Reduce image size do? 3. Don’t run installs 4. Use better base image 5. Write .dockerignore 6. Improve incremental speed 7. Switch to use a Maven plugin github.com/GoogleContainerTools/jib

23. cker nd install Do Download a What did we 1. 2. Write first Dockerfile Reduce image size do? 3. Don’t run installs 4. Use better base image 5. Write .dockerignore 6. Improve incremental speed 7. Switch to use a Maven plugin github.com/GoogleContainerTools/jib

24. cker nd install Do Download a What did we 1. 2. Write first Dockerfile Reduce image size Order of layers to optimize for cache hits do? 3. Don’t run installs 4. Use better base image 5. Write .dockerignore 6. Improve incremental speed 7. Switch to use a Maven plugin github.com/GoogleContainerTools/jib

25. cker nd install Do Download a What did we 1. 2. Write first Dockerfile Reduce image size Order of layers to optimize for cache hits do? 3. Don’t run installs 4. Use better base image ilds e bu ag 5. Write .dockerignore st lti- f mu eo 6. Improve incremental speed Us 7. Switch to use a Maven plugin github.com/GoogleContainerTools/jib

26.Understanding Docker cker cache mechanism an nd install Do d quirks Download a What did we 1. 2. Write first Dockerfile Reduce image size Order of layers to optimize for cache hits do? 3. Don’t run installs 4. Use better base image ilds e bu ag 5. Write .dockerignore st lti- f mu eo 6. Improve incremental speed Us 7. Switch to use a Maven plugin github.com/GoogleContainerTools/jib

27.Understanding Docker cker cache mechanism an nd install Do d quirks Download a What did we 1. 2. Write first Dockerfile Reduce image size Order of layers to optimize for cache hits do? 3. Don’t run installs 4. Use better base image ilds e bu aemo n sta g 5. Write .dockerignore cker d lti- to run Do mu rivileges of 6. Improve incremental speed ted p e eleva Us Have 7. Switch to use a Maven plugin github.com/GoogleContainerTools/jib

28.Understanding Docker cker cache mechanism an nd install Do d quirks Download a What did we 1. 2. Write first Dockerfile Reduce image size Order of layers to optimize for cache hits do? 3. Don’t run installs 4. Use better base image ilds e bu aemo n sta g 5. Write .dockerignore cker d lti- to run Do mu rivileges of 6. Improve incremental speed ted p e eleva Us Have 7. Switch to use a Maven plugin saturnism.me/talk/docker-tips-and-tricks github.com/GoogleContainerTools/jib

29.Containerizing with Docker build Project JAR send build Docker Container docker Daemon Image Dockerfile push Registry github.com/GoogleContainerTools/jib