zoukankan      html  css  js  c++  java
  • OpenShift应用镜像构建(4)

    适合开发的构建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.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.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>ROOT</finalName>
         <plugins>
          <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>fabric8-maven-plugin</artifactId>
            <version>3.5.38</version>
            <configuration>
                <generator>
                   <includes>
              <include>webapp</include>
                   </includes>
                   <config>
                      <webapp>
                <from>172.30.1.1:5000/openshift/s2i-tomcat:latest</from>
                            <server>tomcat</server>
                            <targetDir>/tomcat/webapps/</targetDir>
                            <cmd>/tomcat/bin/catalina.sh run</cmd>
                <ports>8080</ports>
                            <name>172.30.1.1:5000/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.2</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.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.1</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
      <build>
        <finalName>ROOT</finalName>
         <plugins>
          <plugin>
            <groupId>io.fabric8</groupId>
            <artifactId>fabric8-maven-plugin</artifactId>
            <version>3.5.38</version>
            <configuration>
                <images>
                    <image>
                        <name>172.30.1.1:5000/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.2</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:5000/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: 8080
          targetPort: 8080
      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.38:deploy (default-cli) > install @ openshift-tomcat >>>
    [INFO]
    [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ openshift-tomcat ---
    [WARNING] Using platform encoding (UTF-8 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.38: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 2
    [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.2:testResources (default-testResources) @ openshift-tomcat ---
    [WARNING] Using platform encoding (UTF-8 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.4: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 [60 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.2:copy-resources (copy-resources) @ openshift-tomcat ---
    [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 1 resource
    [INFO]
    [INFO] --- fabric8-maven-plugin:3.5.38: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/5000/myproject/fabrictomcat/tmp/docker-build.tar
    [INFO] F8: [172.30.1.1:5000/myproject/fabrictomcat:latest] "tomcat-svc": Created docker-build.tar in 83 milliseconds
    [INFO] F8: [172.30.1.1:5000/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.38:push (fmp) @ openshift-tomcat ---
    [INFO] F8> The push refers to a repository [172.30.1.1:5000/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: 2625
    [INFO] F8> Pushed 172.30.1.1:5000/myproject/fabrictomcat in 2 seconds
    [INFO]
    [INFO] <<< fabric8-maven-plugin:3.5.38:deploy (default-cli) < install @ openshift-tomcat <<<
    [INFO]
    [INFO]
    [INFO] --- fabric8-maven-plugin:3.5.38: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: 2018-10-17T21:29:15+08:00
    [INFO] ------------------------------------------------------------------------

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

    ericdeMacBook-Pro:fabric8 ericnie$ oc get all
    NAME                               REVISION   DESIRED   CURRENT   TRIGGERED BY
    deploymentconfigs/tomcat-service   1          1         1         config
    
    NAME                        DOCKER REPO                              TAGS         UPDATED
    imagestreams/fabrictomcat   172.30.1.1:5000/myproject/fabrictomcat   latest       20 minutes ago
    imagestreams/kafka          172.30.1.1:5000/myproject/kafka          2.12-2.0.0   2 days ago
    imagestreams/zookeeper      172.30.1.1:5000/myproject/zookeeper      3.4.13       2 days ago
    
    NAME                    HOST/PORT                                        PATH      SERVICES         PORT      TERMINATION   WILDCARD
    routes/tomcat-service   tomcat-service-myproject.192.168.99.100.nip.io             tomcat-service   8080                    None
    
    NAME                        READY     STATUS    RESTARTS   AGE
    po/tomcat-service-1-44bv9   1/1       Running   0          20m
    
    NAME                  DESIRED   CURRENT   READY     AGE
    rc/tomcat-service-1   1         1         1         20m
    
    NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
    svc/tomcat-service   ClusterIP   172.30.154.91   <none>        8080/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出错
  • 相关阅读:
    eclipse如何与git 配合工作。
    git托管代码(二)
    PPC2003 安装 CFNET 3.5成功
    我的Window Mobile WCF 項目 第三篇 WM窗体设计
    我的Window Mobile WCF 項目 第一篇Mobile开发和WinForm开发的区别
    我的Window Mobile WCF 項目 第七天
    我的Window Mobile WCF 項目 第二篇 WindowsMobile访问WCF
    WCF 用vs2010 和 vs2008的简单对比测试
    vs2010beta1 和 搜狗输入法 冲突,按下 Ctrl 键就报错,重装搜狗解决
    我的Window Mobile WCF 項目 第六天 (二)
  • 原文地址:https://www.cnblogs.com/ericnie/p/9807374.html
Copyright © 2011-2022 走看看