Quickly build an image of SpringBoot project based on Fabric8 and publish it to k8s detailed tutorial

Preface

In the prevailing cloud computing ecosystem, Docker has led the trend of containerization technology since its release. Docker is very suitable for managing a single container. But if our distributed application system is composed of multiple containers, as the system iteratively evolves, more and more containers and containerized applications are used, and they are divided into numbers due to high concurrency and high availability. Hundreds of parts are likely to make management and layout very difficult. We urgently need to group containers to provide services such as networking, storage, security, and telemetry across all containers, so Kubernetes came into being.

Introduction to Kubernetes

Kubernetes, also known as k8s or "kube" for short, is an open source platform that can automatically implement Linux container operations. It can help users save many manual deployment and expansion operations in the application containerization process. In other words, you can gather multiple groups of hosts running Linux containers together, and Kubernetes can help you manage these clusters easily and efficiently. Moreover, these clusters can deploy hosts across public, private, or hybrid clouds. Therefore, for cloud-native applications that require rapid expansion (such as real-time data stream processing with Apache Kafka), Kubernetes is an ideal hosting platform.

The traditional way of publishing SpringBoot applications to K8s

As the technology of k8s continues to mature and gradually becomes the mainstream of application management, as far as SpringBoot applications are concerned, how to release the application to the k8s platform requires the following steps:

image-20200718225935393

In these steps, the team needs to have the ability to build Docker images and k8s configuration capabilities, which is more troublesome to implement, and needs to manage docker configuration and k8s configuration separately. For developers or colleagues responsible for launching, publishing applications is not a one-click be able to get something, say we'll do agility, we'll do DevOps, continuous integration, Docker以及k8s都是好技术,但是过于麻烦且不简单的步骤,确实是大部分公司或者项目想转型DevOps的门槛。and later jenkinsappeared, the emergence of automated assembly line so I can not help but think 第一次工业革命, has really improved productivity, we finally do not have to every time ssh, sftp, docker build, kubectl apply...

惊å '† ï¼ ??

工业革命Is it enough to rely on scripts for automated execution for the first time ?

è¿˜ä¸ ?? 够

We need to further reduce the difficulty of SpringBoot project construction of Docker configuration and k8s configuration, so that even a developer who is not very good at docker image construction and k8s application construction can quickly realize the release of a project command. In this era, I call it As the "Second Industrial Revolution."

The key tool of this era is ----Fabric8

Introduction to Fabric8

Fabric is the word I face every day, who is engaged in blockchain, and occasionally when I visit github, I meet a friend Fabric8 who is similar to him.

What is Fabric8?

Fabric8 is an open source integrated development platform that provides continuous release for microservices based on Kubernetes and Jenkins .

Using fabric, you can easily create, compile, deploy, and test microservices through Continuous Delivery pipelines , and then run and manage them through Continuous Improvement and ChatOps .

The Fabric8 microservice platform provides:

Text introduction

In this article, based on the maven plugin provided by fabric8, the springboot application will be quickly published to k8s through the pom.xml file configuration.

practice

Environmental preparation

System environment/dependency version Remarks
Ubuntu16.04 16.04 Development/compilation environment system
Java 1.8
SpringBoot 2.1
Maven 3.3.9
k8s 1.16.1
fabric8-maven-plugin 4.0.0 https://github.com/fabric8io/fabric8-maven-plugin
docker 18.04

Engineering configuration

First of all, fabric8-maven-plugin, as a maven plug-in, has no introduction to the application code, so basically all springboot engineering applications can directly add the maven plug-in configuration in the POM to achieve the application of a command to be published to k8s.

Next, I will talk about the default configuration method of fabric8-maven-plugin , the xml-based configuration method , and the external file configuration method to help us publish the Springboot application directly to k8s.

Default configuration

The default configuration method, as the name implies, is to not do any custom processing, and the implementation is as follows:

  1. Edit pom.xml
 <plugin>
      <groupId>io.fabric8</groupId>
      <artifactId>fabric8-maven-plugin</artifactId>
      <version>${fabric8.maven.plugin.version}</version>
      <executions>
          <execution>
             <goals>
                <goal>resource</goal>
                <goal>build</goal>
                </goals>
            </execution>
     </executions>
                             
    </plugin>

  1. Publish application to k8s

    Generally my operation is

    mvn fabric8:deploy
    

    Then build the mirror and publish the application to k8s and it's done.

    The following is the main log executed by mvn

    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test ---
    [INFO] Building jar: /home/lps/servers/jenkins/workspace/test-pipe1/target/test-2.0.2.RELEASE.jar
    [INFO] 
    [INFO] --- sofa-ark-maven-plugin:1.1.1:repackage (default-cli) @ test ---
    [INFO] 
    [INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ test ---
    [INFO] 
    [INFO] --- fabric8-maven-plugin:4.3.1:build (default) @ test ---
    [INFO] F8: Pulling from java-alpine-openjdk8-jdk
    [INFO] F8: Digest: sha256:e8a8e8f5fcd3b545c48ade9ab5716e9414c6f60a60f07a388ff9fafbd
    [INFO] F8: Status: Downloaded newer image for registry.testdemo.com/java-alpine-openjdk8-jdk:latest
    [INFO] F8: Pulled registry.testdemo.com/java-alpine-openjdk8-jdk:latest in 588 milliseconds
    [INFO] Reading assembly descriptor: /home/lps/servers/jenkins/workspace/test-pipe1/src/main/docker/assembly.xml
    [INFO] Copying files to /home/lps/servers/jenkins/workspace/test-pipe1/target/docker/testdemo/test/2.0.2.RELEASE/build/maven
    [INFO] Building tar: /home/lps/servers/jenkins/workspace/test-pipe1/target/docker/testdemo/test/2.0.2.RELEASE/tmp/docker-build.tar
    [INFO] F8: [testdemo/test:2.0.2.RELEASE]: Created docker-build.tar in 2 seconds 
    [INFO] F8: [testdemo/test:2.0.2.RELEASE]: Built image sha256:486a1
    [INFO] 
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ test ---
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/target/test-2.0.2.RELEASE.jar to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE.jar
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/pom.xml to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE.pom
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/target/classes/META-INF/fabric8/kubernetes.yml to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE-kubernetes.yml
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/target/classes/META-INF/fabric8/openshift.yml to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE-openshift.yml
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/target/test-2.0.2.RELEASE-sources.jar to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE-sources.jar
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/target/classes/META-INF/fabric8/kubernetes.yml to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE-kubernetes.yml
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/target/classes/META-INF/fabric8/openshift.yml to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE-openshift.yml
    [INFO] Installing /home/lps/servers/jenkins/workspace/test-pipe1/target/test-2.0.2.RELEASE-sources.jar to /root/.m2/repository/com/testdemo/test/2.0.2.RELEASE/test-2.0.2.RELEASE-sources.jar
    [INFO] 
    [INFO] <<< fabric8-maven-plugin:4.3.1:deploy (default-cli) < install @ test <<<
    [INFO] 
    [INFO] 
    [INFO] --- fabric8-maven-plugin:4.3.1:deploy (default-cli) @ test ---
    [INFO] F8: Using Kubernetes at https://localhost:6443/ in namespace baas-test with manifest /home/lps/servers/jenkins/workspace/test-pipe1/target/classes/META-INF/fabric8/kubernetes.yml 
    [INFO] F8: Using namespace: baas-test
    [INFO] F8: Using namespace: baas-test
    [INFO] F8: Updating a Service from kubernetes.yml
    [INFO] F8: Updated Service: target/fabric8/applyJson/baas-test/service-test.json
    [INFO] F8: Using namespace: baas-test
    [INFO] F8: Updating ConfigMap from kubernetes.yml
    [INFO] F8: Updated ConfigMap: target/fabric8/applyJson/baas-test/configmap-test-config.json
    [INFO] F8: Using namespace: baas-test
    [INFO] F8: Updating Deployment from kubernetes.yml
    [INFO] F8: Updated Deployment: target/fabric8/applyJson/baas-test/deployment-test.json
    [WARNING] Error reading service account token from: [/var/run/secrets/kubernetes.io/serviceaccount/token]. Ignoring.
    [INFO] F8: HINT: Use the command `kubectl get pods -w` to watch your pods start up
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    
    

    From the above log, we can see that a fabric8:deploy actually has the following steps:

    image-20200718234641209

The above is the specific operation of the default configuration. It is very convenient to configure the pom.xml and execute the mvn command. The execution of the mvn command can be placed on jenkins. And fabric8 comes with 3 basic mirrors, namely:

Mirror name description
fabric8/java-alpine-openjdk8-jdk Based on apline system with java8 basic image
fabric8/java-jboss-openjdk8-jdk Based on jboss system with java8 basic mirror
fabric8/java-centos-openjdk8-jdk Based on centos system with java8 basic image

默认是使用第一个镜像,其他两个以及自定义基础镜像可以通过后面的配置自定义化。

Based on xml configuration

The xml-based configuration method is mainly to realize the customization of docker image construction and k8s release configuration (deployment, service, configmap), as mentioned above, how to use my own image as the base image for application construction

The implementation is as follows:

 <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>fabric8-maven-plugin</artifactId>
                <version>${fabric8.maven.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>resource</goal>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
     <!-- 自定义配置 -->
                <configuration>
                    <enricher>
                        <config>
                            <!-- k8s service 配置 将service开放类型改成NodePort  -->
                            <fmp-service>
                                <type>NodePort</type>
                            </fmp-service>
                        </config>
                    </enricher>
                    <images>
                        <image>
                            <!-- 自定义镜像名 -->
                            <name>lps/${project.artifactId}:${project.version}</name>
						
                            <build>
                                <!-- 自定义基础镜像-->
                                <from>registry.lps.com/fabric-base-jdk8</from>
                                <!-- 自定义文件发布 比如将项目 a-jarwithdepences.jar发布到镜像目录/deployment / -->
                                <assembly>
                                    <descriptor>assembly.xml</descriptor>
                                    <targetDir>/deployments</targetDir>
                                </assembly>

                            </build>
                        </image>
                    </images>

                </configuration>
            </plugin>

Several commonly used configurations are provided above. For more xml configurations, please refer to: https://maven.fabric8.io/#xml-configuration

Configuration based on external files

Based on an external configuration file is an external configuration that uses src/main/fabric8the YAML configuration located in the directory.

The main project catalog is as follows


├── README.md
├── pom.xml
└── src
    ├── main
    │   └── java 
    │   └── resource
    │   └── fabric8
             ├── service.yml 
             ├── deployment.yml 
             ├── configmap.yml 

Mainly src/main/fabric8, the three files in the directory mainly correspond to the configuration of k8s deployment, service, and configmap. The fabric8-maven-plugin will automatically read the files in this directory when building, and generate the configuration according to the file-defined parameters (the plugin itself has default Configuration, custom configuration only needs to modify the custom part and the rest will be automatically generated)

The template is as follows:

deployment.yml

spec:
  replicas: 1
  template:
    spec:
      volumes:
        - name: config
          gitRepo:
            repository: 'https://github.com/jstrachan/sample-springboot-config.git'
            revision: 667ee4db6bc842b127825351e5c9bae5a4fb2147
            directory: .
      containers:
        - volumeMounts:
            - name: config
              mountPath: /app/config
          env:
            - name: KUBERNETES_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
      serviceAccount: ribbon

The configuration of volumes, containers, and serviceAccont are customized above, and the rest of the configuration will be generated according to the default template.

Configuration customization can be achieved in this way, which can be used in combination with xml.

Fabric8 mvn command summary

fabric8:build

Build image

use:

mvn  fabric8:build

fabric8:push

Push mirror

use:

mvn  fabric8:push

To push to a private warehouse, you need to settings.xmlconfigure the warehouse url password in the maven configuration file

example

<?xml version="1.0"?>
<settings>

  <profiles>
    <profile>
      <id>mydocker</id>
      <properties>
          <!-- 配置docker host/仓库地址 通过环境变量获取-->
        <docker.registry>${env.DOCKER_REGISTRY}</docker.registry>
        <docker.url>${env.DOCKER_HOST}</docker.url>
      </properties>
    </profile>
  </profiles>
<!-- 配置用户名密码 -->
   <servers>
       <server>
           <id>mydocker</id>
           <username>jolokia</username>
           <password>jolokia</password>
       </server>
        
  </servers>

  <activeProfiles>
    <activeProfile>mydocker</activeProfile>
  </activeProfiles>
</settings>

fabric8:deploy

Publish application to k8s

1. Set environment variables

export KUBERNETES_TRUST_CERTIFICATES=true \
&& export KUBERNETES_MASTER=${KUBERNETES_MASTER} \
&& export FABRIC8_PROFILES=kubernetes \ 
&& export DOCKER_HOST=${DOCKER_HOST} \
&& export KUBERNETES_NAMESPACE=${KUBERNETES_NAMESPACE} \
  • ${KUBERNETES_MASTER}: The API address of the master in the k8s network, such as: http://localhost:6443
  • ${DOCKER_HOST}: DOCKER service address such as: http://localhost:2375
  • ${KUBERNETES_NAMESPACE}: The namespace of the k8s where the application is published, such as: test

2. Execute the command

 mvn fabric8:deploy

fabric8:resource

src/main/fabric8Generate a k8s configuration file according to the custom yml file in the directory, which is generally used to verify whether the generated yml has an effective custom configuration

 mvn fabric8:resource

to sum up

The fabric8-maven-plugin should be one of the best solutions for publishing springboot projects to k8s at present. It greatly reduces the difficulty of building configuration, and even a single command can be realized from a springboot project to a service running on k8s, so I highly recommend it.

If there is a better plan, you are welcome to comment to the small ones.

Guess you like

Origin blog.csdn.net/qq_28540443/article/details/107437916