【问题标题】:Deploying Spring Boot App on Kubernetes: App uses wrong port property from environment variable在 Kubernetes 上部署 Spring Boot 应用程序:应用程序使用环境变量中的错误端口属性
【发布时间】:2018-08-29 20:49:57
【问题描述】:

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

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

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 在启动期间会失败。 在日志中,我可以找到以下消息:

***************************
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]

注意:10.98.151.181 是 Service 的集群 IP,可以在 Minikube 仪表板中看到。

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

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

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

【问题讨论】:

    标签: spring-boot kubernetes minikube


    【解决方案1】:

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

    不,kubernetes 它正在公开“docker 兼容”link env-vars,因为您的 Service 被命名为 server,最终会成为 SERVER_PORT=tcp://thing:8080,因为它试图“提供帮助”

    解决方案是给您的 Service 一个更具描述性的名称,或者屏蔽掉有问题的 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`
    

    【讨论】:

      猜你喜欢
      • 2021-03-12
      • 2019-04-01
      • 1970-01-01
      • 2021-09-27
      • 2020-03-11
      • 1970-01-01
      • 2022-10-19
      • 2018-05-21
      • 2017-09-30
      相关资源
      最近更新 更多