如何使用 OpenShift Maven 插件在 OpenShift 上部署 Spring Boot 应用程序

x33g5p2x  于2022-09-28 转载在 Spring  
字(3.4k)|赞(0)|评价(0)|浏览(1525)

本教程将开始使用 OpenShift Maven 插件将Spring Boot(通常是任何 Java 应用程序)部署到 OpenShift/Kubernates

openshift-maven-plugin 是您将 Java 应用程序引入 OpenShift 的最佳朋友。它提供了与 Maven 的紧密集成,并从已经提供的构建配置中受益。这个插件专注于构建 Docker 镜像和创建 Kubernetes 资源描述符。它附带灵活的配置,并支持多种模型来创建资源,例如零配置设置,它允许使用一些自以为是的默认设置快速提升。

对于更高级的要求,XML 配置提供了可以添加到 pom.xml 的附加配置选项。为了充分发挥作用,为了调整创建的所有方面,可以使用外部资源片段和 Dockerfile。

我们现在将演示如何通过在 pom.xml 中添加 OpenShift Maven 插件来将 Spring Boot 2 应用程序部署到 OpenShift

<plugins> 			<plugin> 				<groupId>org.springframework.boot</groupId> 				<artifactId>spring-boot-maven-plugin</artifactId> 			</plugin>  			   <plugin>             <groupId>org.eclipse.jkube</groupId>             <artifactId>openshift-maven-plugin</artifactId>             <version>1.0.0-alpha-1</version>             <configuration>                <resources>                 <labels>                   <all>                     <testProject>spring-boot-sample</testProject>                   </all>                 </labels>               </resources>                <generator>                 <includes>                   <include>spring-boot</include>                 </includes>                 <config>                   <spring-boot>                     <color>always</color>                   </spring-boot>                 </config>               </generator>               <enricher>                 <excludes>                   <exclude>jkube-expose</exclude>                 </excludes>                 <config>                   <jkube-service>                     <type>NodePort</type>                   </jkube-service>                 </config>               </enricher>             </configuration>              <executions>               <execution>                 <goals>                   <goal>resource</goal>                   <goal>build</goal>                 </goals>               </execution>             </executions>           </plugin> 		</plugins>

以下是对其配置的简要说明:

带有 resourceslabels 部分包含应应用于各种对象的标签

生成器和浓缩器的定义用于为 OpenShift 生成特定的应用程序。在我们的例子中,我们将使用 spring-boot 生成器。

生成器添加了指向管理或服务器端口的 Kubernetes liveness 和 readiness 探针,从 application.properties 读取。如果 management.port(用于 Spring Boot 1)或 management.server.port(用于 Spring Boot 2)和 management.ssl.key-store(用于 Spring Boot 1)或 management.server.ssl.key-store(用于 Spring引导 2) 属性在 application.properties 中设置,或者 server.ssl.key-store 属性在 application.properties 中设置,然后探测器自动设置为使用 https。

在其配置中,您可以找到 color 参数,该参数设置为 true 时会强制在 Spring Boot 控制台输出中使用颜色。

OpenShift 上部署示例应用程序所需的只是以下目标

mvn oc:deploy

此目标旨在首先运行资源生成(oc:buildoc:resource)并将它们部署在 OpenShift 上。检查输出是否包含在末尾:

[INFO] OpenShift platform detected [INFO] oc: Using project: spring-boot-sample [INFO] oc: Creating a Service from openshift.yml namespace spring-boot-sample name spring-boot-sample [INFO] oc: Created Service: target/jkube/applyJson/spring-boot-sample/service-spring-boot-sample.json [INFO] oc: Creating a DeploymentConfig from openshift.yml namespace spring-boot-sample name spring-boot-sample [INFO] oc: Created DeploymentConfig: target/jkube/applyJson/spring-boot-sample/deploymentconfig-spring-boot-sample.json [INFO] oc: Creating Route spring-boot-sample:spring-boot-sample host: null [INFO] oc: HINT: Use the command `oc get pods -w` to watch your pods start up

让我们检查应用程序是否已部署在 Pod 中:

$ oc get pods NAME                             READY   STATUS      RESTARTS   AGE spring-boot-sample-1-88ljl       1/1     Running     0          8s spring-boot-sample-1-deploy      0/1     Completed   0          10s spring-boot-sample-s2i-1-build   0/1     Completed   0          81s

完美,现在获取路线名称:

$ oc get routes NAME                 HOST/PORT                                                PATH   SERVICES             PORT   TERMINATION   WILDCARD spring-boot-sample   spring-boot-sample-spring-boot-sample.apps-crc.testing          spring-boot-sample   8080                 None

并测试可用的端点之一:

$ curl -s spring-boot-sample-spring-boot-sample.apps-crc.testing/list | jq [   {     "id": 1,     "name": "frank"   },   {     "id": 2,     "name": "john"   } ]

您可以从 GitHub 检查示例应用程序:https://github.com/fmarchioni/masterspringboot/tree/master/openshift/deploy-openshift

相关文章

微信公众号

最新文章

更多