使用 Helm Charts 管理 Kubernetes 上的 Spring Boot 应用程序

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

在本教程中,我们将学习如何在 Kubernetes 上部署 Spring Boot 应用程序并使用 Helm 图表对其进行管理。

Helm ChartKubernetes 集群的应用程序包管理器。图表是描述一组相关 Kubernetes 资源的文件的集合。单个图表可用于部署一些简单的东西,例如 memcached pod 或完整的应用程序堆栈。

通过使用 Helm Charts,您可以简化集群的管理,而无需记住复杂的 Kubernetes 命令来管理 Kubernetes 资源。

安装 Helm

有几种方法可以安装 Helm。以下命令将作为脚本安装:

$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3  $ chmod 700 get_helm.sh  $ ./get_helm.sh

我们将使用 MiniKube 来启动我们的 Kubernetes 集群。请参考本教程了解如何开始使用 Minikube:如何通过 3 个简单的步骤在 Kubernetes 上部署 Spring Boot 应用程序

启动 MiniKube

$ minikube start

完成后,验证默认和 kube-system 服务是否可用:

$ minikube service list |-------------|------------|--------------|-----| |  NAMESPACE  |    NAME    | TARGET PORT  | URL | |-------------|------------|--------------|-----| | default     | kubernetes | No node port | | kube-system | kube-dns   | No node port | |-------------|------------|--------------|-----|

太好了,您的 Kubernetes 集群现已启动并运行。

然后,确保您使用 Minikube docker 存储库:

$ eval $(minikube docker-env)

创建你的第一个 Helm 图表

为了创建我们的第一个 Helm 图表,您可以运行“helm create”命令,后跟图表名称:

helm create springbootdemo

将生成一组 YAML 文件。

springboot   |   |- Chart.yaml    # Information about your chart   |   |- values.yaml   # The default values for your templates   |   |- charts/       # Charts that this chart depends on   |   |- templates/    # The template files

.Chart.yaml 保存有关图表的元数据(名称、版本和描述)

  • values.yaml 保存图表模板的默认值
  • 图表目录包含任何依赖图表。除非您是从组件图表构建图表,否则它可以为空。
  • templates 目录包含您用于通过 kubectl cli 推送的所有 yaml 文件(部署、rbac、服务等文件)

那么我们如何配置 helm 来使用我们自己的 Spring Boot 应用呢?简而言之,我们必须提供镜像名称(可在 DockerHub 或您自己的存储库中获得)和运行服务的端口。

第一个更改将在 values.yaml 中:

image:   repository: kharulp12/spring_hello_rest_api #just an example Spring Boot image   pullPolicy: IfNotPresent   # Overrides the image tag whose default is the chart appVersion.   tag: ""

然后,在 /templates/deployment.yaml 中设置适当的端口:

ports:             - name: http               containerPort: 8080               protocol: TCP

在将图表推送到集群之前,Helm 允许您进行试运行以模拟安装/升级:

helm install --dry-run --debug <path to chart directory>

然后,您可以使用“helm install”命令安装您的应用程序:

helm install springbootdemo springbootdemo NAME: springbootdemo LAST DEPLOYED: Sat Dec 19 10:33:41 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: 1. Get the application URL by running these commands:   export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=springboot,app.kubernetes.io/instance=springbootdemo" -o jsonpath="{.items[0].metadata.name}")   export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")   echo "Visit http://127.0.0.1:8080 to use your application"   kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT

然后,使用命令“helm list”检查图表是否可用:

[francesco@fedora target]$ helm list -a NAME            	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART                 	APP VERSION springbootdemo  	default  	1       	2020-12-19 10:33:41.613142795 +0100 CET	deployed	springboot-0.1.0      	1.16.0

并验证 Pod 是否也可用:

[francesco@fedora target]$ kubectl get all NAME                                    READY   STATUS         RESTARTS   AGE pod/springdemo-79bbc497d5-tj455         1/1     Running        1          15h

使用 JKube Maven 插件部署 Helm Charts

还有一种更简单的方法可以生成和部署 Helm 图表。您可以使用 JKube Maven 插件从您的 Maven 项目设置中创建 Helm Chart。

我们将部署以下应用程序,其中包括一个简单的 Hello World 控制器:

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
  @RequestMapping("/")
  public String index() {
    return "Greetings from Spring Boot with YAML files!!";
  }
}

Maven 插件设置将包含在配置文件中:

<?xml version="1.0" encoding="UTF-8"?><profile>
          
   <id>kubernetes</id>
          
   <build>
               
      <plugins>
                    
         <plugin>
                         
            <groupId>org.eclipse.jkube</groupId>
                         
            <artifactId>kubernetes-maven-plugin</artifactId>
                         
            <version>${jkube.version}</version>
                          
            <configuration>
                              
               <resources>
                                   
                  <labels>
                                        
                     <all>
                                             
                        <testProject>spring-boot-with-yaml-label-for-all</testProject>
                                           
                     </all>
                                      
                  </labels>
                                 
               </resources>
                              
               <generator>
                                   
                  <config>
                                        
                     <spring-boot>
                                             
                        <color>always</color>
                                           
                     </spring-boot>
                                      
                  </config>
                                 
               </generator>
                            
            </configuration>
                          
            <executions>
                              
               <execution>
                                   
                  <phase>package</phase>
                                   
                  <goals>
                                        
                     <goal>resource</goal>
                                        
                     <goal>build</goal>
                                        
                     <goal>helm</goal>
                                      
                  </goals>
                                 
               </execution>
                            
            </executions>
                       
         </plugin>
                  
      </plugins>
             
   </build>
        
</profile>

使用此插件,您可以使用以下命令生成具有默认值的 helm 图表:

mvn k8s:resource k8s:helm -Pkubernetes

这个插件的伟大之处在于它使用一些默认值自动为 Spring Boot 应用程序设置正确的值:

[INFO] k8s: spring-boot: Using Docker image quay.io/jkube/jkube-java-binary-s2i:0.0.8 as base / builder [INFO] k8s: Using resource templates from /home/git/jkube-master/quickstarts/maven/spring-boot-helm/src/main/jkube [INFO] k8s: jkube-controller: Adding a default Deployment [INFO] k8s: jkube-service: Adding a default service 'spring-boot-helm' with ports [8080] [INFO] k8s: jkube-healthcheck-spring-boot: Adding readiness probe on port 8080, path='/actuator/health', scheme='HTTP', with initial delay 10 seconds [INFO] k8s: jkube-healthcheck-spring-boot: Adding liveness probe on port 8080, path='/actuator/health', scheme='HTTP', with initial delay 180 seconds [INFO] k8s: jkube-revision-history: Adding revision history limit to 2

Helm 文件将在 target/jkube 目录下生成:

tree target/jkube/ target/jkube/ ├── helm │   └── spring-boot-helm │       └── kubernetes │           ├── Chart.yaml │           ├── LICENSE │           ├── README.md │           ├── templates │           │   ├── password-secret.yaml │           │   ├── spring-boot-helm-deployment.yaml │           │   └── spring-boot-helm-service.yaml │           └── values.yaml └── password-secret.yml

另一方面,如果您更喜欢在一个命令中构建应用程序并生成 helm 文件,只需执行:

在 Minikube 运行时,执行以下命令:

$ mvn -Pkubernetes clean package

然后,使用以下命令安装 Helm 图表:

$ helm install spring-boot-helm target/jkube/helm/spring-boot-helm/kubernetes/

检查 helm chart 是否可用:

helm list -a NAME            	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART                 	APP VERSION spring-boot-helm	default  	2       	2020-12-19 11:25:04.621514079 +0100 CET	deployed	spring-boot-helm-1.0.2

现在在浏览器上运行服务:

$ minikube service spring-boot-helm

您将在屏幕上看到:“来自 Spring Boot 的带有 YAML 文件的问候!!”

太好了,您刚刚使用 Helm 图表在 Kubernetes 上部署了一个 Spring Boot 应用程序

请注意,有一个影响某些 JDK 的已知问题:https://bugs.openjdk.java.net/browse/JDK-8236039

发生这种情况时,客户端会抛出异常:

“javax.net.ssl.SSLHandshakeException:扩展名 (5) 不应出现在证书请求中”

发生这种情况是因为 JDK 11 及更高版本支持可能导致上述错误的 TLS 1.3。

您可以通过将属性 -Djdk.tls.client.protocols=TLSv1.2 设置为 JVM args 以使其使用 1.2 来解决此问题。作为替代方案,更新到最新版本 od JDK(在 JDK 15 中已完全解决)。

在 Kubernetes 上享受 Spring Boot!

您可以在以下位置找到此 Maven 项目的源代码:https://github.com/eclipse/jkube/tree/master/quickstarts/maven/spring-boot-helm

相关文章

微信公众号

最新文章

更多