适合开发的构建fabric8-maven-plugin

在项目过程中越来越多的出现在开发阶段就需要把部分微服务直接做容器化发布,然后自己的代码还需要和这些发布后的微服务进行调用的开发过程,这个阶段基本的需求是需要把代码快速的容器化并发布,这种情况下就比较适合fabric8-maven-plugin插件,只需要修改pom文件,然后通过mvn fabric8:deploy就可直接部署到openshift平台上,简单高效。

fabric8-maven-plugin提供了直接从mvn打包直接生成配置然后部署到openshift的过程,从而把这个过程集成到开发平台,便于在开发环境中快速的部署服务。

plugin提供了好几种部署模式,主要包含Generator,XML configuration,Dockerfile,Docker compose等,详细可参考官方网站

http://maven.fabric8.io

1.基于Generator

基于一个base images,然后将构建的组件放入并运行的配置,所有的配置都在xml中,如下:

pom.xml

ericdeMacBook-Pro:openshift-tomcat-local ericnie$ cat 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/maven-v4_0_0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.rabbit</groupId>
<artifactId>ROOT</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>openshift-tomcat Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ROOT</finalName>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.</version>
<configuration>
<generator>
<includes>
<include>webapp</include>
</includes>
<config>
<webapp>
<from>172.30.1.1:/openshift/s2i-tomcat:latest</from>
<server>tomcat</server>
<targetDir>/tomcat/webapps/</targetDir>
<cmd>/tomcat/bin/catalina.sh run</cmd>
<ports></ports>
<name>172.30.1.1:/myproject/fabric-generator-tomcat</name>
<alias>tomcat-svc</alias>
</webapp>
</config>
</generator>
<authConfig>
<username>admin</username>
<password>admin</password>
<useOpenShiftAuth>true</useOpenShiftAuth>
</authConfig>
<enricher>
<config>
<fmp-controller>
<name>tomcat-service</name>
</fmp-controller>
</config>
</enricher>
<env>
<MYSQL_USER>product</MYSQL_USER>
<MYSQL_PASSWORD>password</MYSQL_PASSWORD>
</env>
</configuration>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/Users/ericnie/minishift/openshift-tomcat-local/src</outputDirectory>
<resources>
<resource>
<directory>/Users/ericnie/minishift/openshift-tomcat-local/target</directory>
<includes>
<include>ROOT.war</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

2.基于Dockerfile

基于Dockerfile实现构建,更多的镜像的构建在Dockerfile

pom.xml

ericdeMacBook-Pro:openshift-tomcat-local ericnie$ cat 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/maven-v4_0_0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.rabbit</groupId>
<artifactId>openshift-tomcat</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>openshift-tomcat Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ROOT</finalName>
<plugins>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>3.5.</version>
<configuration>
<images>
<image>
<name>172.30.1.1:/myproject/fabrictomcat</name>
<alias>tomcat-svc</alias>
<build>
<dockerFileDir>/Users/ericnie/minishift/openshift-tomcat-local/src</dockerFileDir>
</build>
</image>
</images>

<authConfig>
<username>admin</username>
<password>admin</password>
<useOpenShiftAuth>true</useOpenShiftAuth>
</authConfig>
<enricher>
<config>
<fmp-controller>
<name>tomcat-service</name>
</fmp-controller>
</config>
</enricher>
<env>
<MYSQL_USER>product</MYSQL_USER>
<MYSQL_PASSWORD>password</MYSQL_PASSWORD>
</env>
</configuration>
<executions>
<execution>
<id>fmp</id>
<goals>
<goal>resource</goal>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>/Users/ericnie/minishift/openshift-tomcat-local/src</outputDirectory>
<resources>
<resource>
<directory>/Users/ericnie/minishift/openshift-tomcat-local/target</directory>
<includes>
<include>ROOT.war</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

目录结构

ericdeMacBook-Pro:openshift-tomcat-local ericnie$ tree .
.
├── README.md
├── ROOT.war
├── config
│   └── catalina.sh
├── pom.xml
└── src
├── Dockerfile
├── ROOT.war
└── main
├── fabric8
│   └── svc.yml
└── webapp
├── WEB-INF
│   └── web.xml
└── index.jsp

Dockerfile

ericdeMacBook-Pro:src ericnie$ cat Dockerfile
FROM 172.30.1.1:/openshift/s2i-tomcat:latest # Maintainer
MAINTAINER Eric Nie # deploy app
ADD ROOT.war /tomcat/webapps/ CMD ["/tomcat/bin/catalina.sh","run"]

建立src/main/fabric8文件目录,然后放一个svc.yml

ericdeMacBook-Pro:fabric8 ericnie$ cat svc.yml
apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
ports:
- protocol: TCP
port:
targetPort:
type: ClusterIP

构建输出

ericdeMacBook-Pro:openshift-tomcat-local ericnie$ mvn clean fabric8:deploy  -Dfabric8.mode=kubernetes
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.rabbit:openshift-tomcat >---------------------
[INFO] Building openshift-tomcat Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ openshift-tomcat ---
[INFO] Deleting /Users/ericnie/minishift/openshift-tomcat-local/target
[INFO]
[INFO] >>> fabric8-maven-plugin:3.5.:deploy (default-cli) > install @ openshift-tomcat >>>
[INFO]
[INFO] --- maven-resources-plugin:3.0.:resources (default-resources) @ openshift-tomcat ---
[WARNING] Using platform encoding (UTF- actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/ericnie/minishift/openshift-tomcat-local/src/main/resources
[INFO]
[INFO] --- fabric8-maven-plugin:3.5.:resource (fmp) @ openshift-tomcat ---
[INFO] F8: Running in Kubernetes mode
[INFO] F8: using resource templates from /Users/ericnie/minishift/openshift-tomcat-local/src/main/fabric8
[INFO] F8: fmp-controller: Adding a default Deployment
[INFO] F8: fmp-revision-history: Adding revision history limit to
[INFO] F8: validating /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/openshift/tomcat-service-svc.yml resource
[INFO] F8: validating /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/openshift/tomcat-service-route.yml resource
[INFO] F8: validating /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/openshift/tomcat-service-deploymentconfig.yml resource
[INFO] F8: validating /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/kubernetes/tomcat-service-svc.yml resource
[INFO] F8: validating /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/kubernetes/tomcat-service-deployment.yml resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ openshift-tomcat ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:3.0.:testResources (default-testResources) @ openshift-tomcat ---
[WARNING] Using platform encoding (UTF- actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/ericnie/minishift/openshift-tomcat-local/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ openshift-tomcat ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.:test (default-test) @ openshift-tomcat ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ openshift-tomcat ---
[INFO] Packaging webapp
[INFO] Assembling webapp [openshift-tomcat] in [/Users/ericnie/minishift/openshift-tomcat-local/target/ROOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/ericnie/minishift/openshift-tomcat-local/src/main/webapp]
[INFO] Webapp assembled in [ msecs]
[INFO] Building war: /Users/ericnie/minishift/openshift-tomcat-local/target/ROOT.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO]
[INFO] --- maven-resources-plugin:3.0.:copy-resources (copy-resources) @ openshift-tomcat ---
[WARNING] Using platform encoding (UTF- actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying resource
[INFO]
[INFO] --- fabric8-maven-plugin:3.5.:build (fmp) @ openshift-tomcat ---
[INFO] F8: Building Docker image in Kubernetes mode
[INFO] Building tar: /Users/ericnie/minishift/openshift-tomcat-local/target/docker/172.30.1.1//myproject/fabrictomcat/tmp/docker-build.tar
[INFO] F8: [172.30.1.1:/myproject/fabrictomcat:latest] "tomcat-svc": Created docker-build.tar in milliseconds
[INFO] F8: [172.30.1.1:/myproject/fabrictomcat:latest] "tomcat-svc": Built image sha256:112a4
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ openshift-tomcat ---
[INFO] Installing /Users/ericnie/minishift/openshift-tomcat-local/target/ROOT.war to /Users/ericnie/.m2/repository/com/rabbit/openshift-tomcat/1.0-SNAPSHOT/openshift-tomcat-1.0-SNAPSHOT.war
[INFO] Installing /Users/ericnie/minishift/openshift-tomcat-local/pom.xml to /Users/ericnie/.m2/repository/com/rabbit/openshift-tomcat/1.0-SNAPSHOT/openshift-tomcat-1.0-SNAPSHOT.pom
[INFO] Installing /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/openshift.yml to /Users/ericnie/.m2/repository/com/rabbit/openshift-tomcat/1.0-SNAPSHOT/openshift-tomcat-1.0-SNAPSHOT-openshift.yml
[INFO] Installing /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/openshift.json to /Users/ericnie/.m2/repository/com/rabbit/openshift-tomcat/1.0-SNAPSHOT/openshift-tomcat-1.0-SNAPSHOT-openshift.json
[INFO] Installing /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/kubernetes.yml to /Users/ericnie/.m2/repository/com/rabbit/openshift-tomcat/1.0-SNAPSHOT/openshift-tomcat-1.0-SNAPSHOT-kubernetes.yml
[INFO] Installing /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/kubernetes.json to /Users/ericnie/.m2/repository/com/rabbit/openshift-tomcat/1.0-SNAPSHOT/openshift-tomcat-1.0-SNAPSHOT-kubernetes.json
[INFO]
[INFO] --- fabric8-maven-plugin:3.5.:push (fmp) @ openshift-tomcat ---
[INFO] F8> The push refers to a repository [172.30.1.1:/myproject/fabrictomcat]
44974c8092d7: Pushed
8d197010ad01: Mounted from s2i-tomcat/testlocal
d61c0c9b0850: Mounted from s2i-tomcat/testlocal
1c8610d869b2: Mounted from s2i-tomcat/testlocal
38b2d487d92e: Mounted from s2i-tomcat/testlocal
4b4687331806: Mounted from s2i-tomcat/testlocal
3312e49dd611: Mounted from s2i-tomcat/testlocal
cb96aea742c3: Mounted from s2i-tomcat/testlocal
f1bbaf33b49c: Mounted from s2i-tomcat/testlocal
4b1e8db0189a: Mounted from s2i-tomcat/testlocal
34e7b85d83e4: Mounted from s2i-tomcat/testlocal
[INFO] F8> latest: digest: sha256:643d7211687580ff670f0cf9e4d77d75e0c1d8fb7d715cfc040d99d27bbdbd8e size:
[INFO] F8> Pushed 172.30.1.1:/myproject/fabrictomcat in seconds
[INFO]
[INFO] <<< fabric8-maven-plugin:3.5.:deploy (default-cli) < install @ openshift-tomcat <<<
[INFO]
[INFO]
[INFO] --- fabric8-maven-plugin:3.5.:deploy (default-cli) @ openshift-tomcat ---
[INFO] F8: Using OpenShift at https://192.168.99.100:8443/ in namespace myproject with manifest /Users/ericnie/minishift/openshift-tomcat-local/target/classes/META-INF/fabric8/openshift.yml
[INFO] OpenShift platform detected
[INFO] Using project: myproject
[INFO] Creating a Service from openshift.yml namespace myproject name tomcat-service
[INFO] Created Service: target/fabric8/applyJson/myproject/service-tomcat-service.json
[INFO] Using project: myproject
[INFO] Creating a DeploymentConfig from openshift.yml namespace myproject name tomcat-service
[INFO] Created DeploymentConfig: target/fabric8/applyJson/myproject/deploymentconfig-tomcat-service.json
[INFO] Creating Route myproject:tomcat-service host: null
[INFO] F8: HINT: Use the command `oc get pods -w` to watch your pods start up
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.765 s
[INFO] Finished at: --17T21::+:
[INFO] ------------------------------------------------------------------------

然后查看,发现把dc,image,route,pod,rc和service都建立起来了

ericdeMacBook-Pro:fabric8 ericnie$ oc get all
NAME REVISION DESIRED CURRENT TRIGGERED BY
deploymentconfigs/tomcat-service config NAME DOCKER REPO TAGS UPDATED
imagestreams/fabrictomcat 172.30.1.1:/myproject/fabrictomcat latest minutes ago
imagestreams/kafka 172.30.1.1:/myproject/kafka 2.12-2.0. days ago
imagestreams/zookeeper 172.30.1.1:/myproject/zookeeper 3.4. days ago NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD
routes/tomcat-service tomcat-service-myproject.192.168.99.100.nip.io tomcat-service None NAME READY STATUS RESTARTS AGE
po/tomcat-service--44bv9 / Running 20m NAME DESIRED CURRENT READY AGE
rc/tomcat-service- 20m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/tomcat-service ClusterIP 172.30.154.91 <none> /TCP 20m

比较坑的地方

  • fabric8 maven plugin的版本要注意和openshift匹配
  • 之前把Dockerfile放在项目根目录下,报错,错误信息是io.fabric8:docker-maven-plugin:0.20.0:build failed: A tar file cannot include itself查了一下说是项目根目录下要加一个.dockerignore文件什么的,没理,换了个目录就过去了。
  • image name没写镜像全路径,发现一直往registry.access.redhat.com里push出错

OpenShift应用镜像构建(4) - fabric8-maven-plugin的更多相关文章

  1. OpenShift应用镜像构建(1) S2I tomcat 镜像定制

    参考并感谢https://www.jianshu.com/p/fd3e62263046 在对接项目制作应用镜像的过程中,经常发现避免不了的是需要写Dockerfile,(当然另外一种方式是直接run一 ...

  2. OpenShift应用镜像构建(2) - 链式构建

    Openshift对于应用构建提供了三种模式 从应用的源代码构建并部署,Openshift通过一个S2I的构建过程编译打包并实现发布,具体可以参考 https://www.cnblogs.com/er ...

  3. OpenShift应用镜像构建(3) - Jenkins的流水线构建

    Jenkins方式构建的定位是使用专门的CICD平台. 既支持把JenKins作为一个Pod部署到openshift内部,也支持部署在Openshift集群外部,操作上的区别是 openshift自己 ...

  4. 项目自动构建工具对比(Maven、Gradle、Ant)

    Java世界中主要有三大构建工具:Ant.Maven和Gradle.经过几年的发展,Ant几乎销声匿迹.Maven也日薄西山,而Gradle的发展则如日中天. Maven的主要功能主要分为5点,分别是 ...

  5. Spring Boot Maven Plugin(一):repackage目标

    简介 Spring Boot Maven Plugin插件提供spring boot在maven中的支持.允许你打包可运行的jar包或war包. 插件提供了几个maven目标和Spring Boot ...

  6. Spring Boot Maven Plugin打包异常及三种解决方法:Unable to find main class

    [背景]spring-boot项目,打包成可执行jar,项目内有两个带有main方法的类并且都使用了@SpringBootApplication注解(或者另一种情形:你有两个main方法并且所在类都没 ...

  7. Spring Boot的Maven插件Spring Boot Maven plugin详解

    Spring Boot的Maven插件(Spring Boot Maven plugin)能够以Maven的方式为应用提供Spring Boot的支持,即为Spring Boot应用提供了执行Mave ...

  8. Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin)

    近期在appfuse看到使用webtest-maven-plugin实现Web应用的集成測试,研究了下.感觉很不错.对于Web应用自己主动构建很有帮助,在性能測试之前能够保证Web应用的基本功能工作正 ...

  9. Docker容器 关于镜像构建的安全问题

    写在前面 确保容器中服务与应用安全是容器化演进的关键点.容器安全涉及到应用开发与维护的整个生命周期,本文主要从镜像构建的视角来看docker容器的一些安全问题及应对措施. 一.权限管理 1.避免以容器 ...

随机推荐

  1. mac date命令

    usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd ...

  2. ORM数据库查询操作之基于双下划线的跨表查询

     创建表结构 from django.db import models class Book(models.Model): title=models.CharField(max_length=32) ...

  3. Linux下undefined reference to ‘pthread_create’问题解决 zz

    接触了Linux系统编程中的线程编程模块,可gcc sample.c(习惯把书上的sample代码写进sample.c文件中)出现“undefined reference to ‘pthread_cr ...

  4. poj 1692(动态规划)

    Crossed Matchings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2711   Accepted: 1759 ...

  5. Linux之父Linus的8个趣闻轶事

    博客中的文章均为 meelo 原创,请务必以链接形式注明本文地址 <只是为了好玩:Linux之父林纳斯自传>是一本很古老的书了,2001年就有了中文版,在2014的时候图灵图书又把它重新翻 ...

  6. .net 多播委托的使用方法以及场景,更简单的观察者模式

    首先来说一下什么是多播委托 多播委托就是在委托里定义一个或者多个方法的一个集合 使用方法: public Action actList; //添加方法 public void AddActionMet ...

  7. 前端读者 | Web App开发入门

    本文来自互联网 自Iphone和Android这两个牛逼的手机操作系统发布以来,在互联网界从此就多了一个新的名词 - Web App(意为基于WEB形式的应用程序).业界关于Web App与Nativ ...

  8. java之异常

    package com.text.exception; class Test{ void add(int a,int b) throws Exception { int c; c=a/b; Syste ...

  9. 关于php的session.serialize_handler的问题

    前言 php的session信息是储存在文件中的 session.save_path="" 指定储存的路径 session.save_handler="" 指定 ...

  10. 2015 ACM-ICPC 沈阳站

    题目链接  2015 ACM-ICPC Shenyang Problem A Problem B Problem C Problem D 签到题,所有gcd的倍数都可以被写出来. 那么判断一下这类数的 ...