在Kubernetes上部署Spring Boot App:App使用来自环境变量的错误端口属性 [英] Deploying Spring Boot App on Kubernetes: App uses wrong port property from environment variable

查看:423
本文介绍了在Kubernetes上部署Spring Boot App:App使用来自环境变量的错误端口属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Kubernetes(Minikube)上部署"Hello world" Spring Boot应用程序. 该应用程序确实非常简单,只是一种方法,它映射到GET资源上.我什至没有指定端口.

I am trying to deploy a "Hello world" Spring Boot app on Kubernetes (Minikube). The app is really simple, just one method, which is mapped on a GET resource. I even do not specify a port.

我现在正尝试在Minikube上部署该应用程序,并使用服务使其可用:

I am now trying to deploy the app on Minikube, and making it available using a Service:

kind: Service
apiVersion: v1
metadata:
  name: server
spec:
  selector:
    app: server
  ports:
  - protocol: TCP
    port: 8080
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server
spec:
  selector:
      matchLabels:
        app: server
  replicas: 3
  template:
    metadata:
      labels:
        app: server
    spec:
      containers:
        - name: server
          image: kubernetes-server:latest
          imagePullPolicy: Never
          ports:
            - name: http
              containerPort: 8080

如果我使用此配置启动部署(即先启动服务,然后启动部署),则在启动期间Pod会失败. 在日志中,我可以找到以下消息:

If I start the deployment using this configuration (i. e. the service is started first, then the Deployment), the pods fail during startup. In the logs, I can find the following message:

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target         
org.springframework.boot.autoconfigure.web.ServerProperties@42f93a98 failed:

    Property: server.port
    Value: tcp://10.98.151.181:8080
    Reason: Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'port'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [java.lang.Integer]

注意:如在Minikube仪表板中所见,10.98.151.181是服务的群集IP.

Note: 10.98.151.181 is the cluster IP of the Service, as a can see in the Minikube dashboard.

如果我首先触发实际的部署,则该应用程序将成功启动,然后,我可以启动该服务. 但是,官方文档建议先启动该服务,然后再启动: https ://kubernetes.io/docs/concepts/configuration/overview/#services

If I first trigger the actual Deployment, the app starts successfully, and after that, I can start the Service. However, the official documentation recommends to start the service first, and after that the deplyoment: https://kubernetes.io/docs/concepts/configuration/overview/#services

对我来说,似乎Service设置了一个属性 server.port 作为环境变量,并且在Service之后启动的Spring Boot应用程序意外地将其解释为Spring server .port .

For me, it looks like the Service sets a property server.port as environment variable, and the Spring Boot app, which start after the Service, interprets that accidentially as the Spring server.port.

有什么办法解决这个问题吗?

Any ideas how to solve that?

推荐答案

对我来说,该服务似乎将属性server.port设置为环境变量

For me, it looks like the Service sets a property server.port as environment variable

不,kubernetes公开了与docker兼容" 链接环境变量,因为您的Service被命名为server,最终由于它正试图变得有帮助"而成为SERVER_PORT=tcp://thing:8080

No, kubernetes it is exposing "docker compatible" link env-vars which, because your Service is named server, end up being SERVER_PORT=tcp://thing:8080 because it is trying to be "helpful"

解决方案是给您的Service一个更具描述性的名称,或者掩盖令人讨厌的env-var:

The solution is either give your Service a much more descriptive name, or mask-off the offending env-var:

containers:
- name: server
  env:
  - name: SERVER_PORT
    value: ''  # you can try the empty string,
    # or actually place the port value with
    # value: '8080'
    # ensure it is a **string** and not `value: 8080`

这篇关于在Kubernetes上部署Spring Boot App:App使用来自环境变量的错误端口属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆