在 Kubernetes 上部署 Spring 启动应用程序:应用程序使用来自环境变量的错误端口 属性

Deploying Spring Boot App on Kubernetes: App uses wrong port property from environment variable

我正在尝试在 Kubernetes (Minikube) 上部署 "Hello world" Spring 启动应用程序。 该应用程序非常简单,只有一种方法,映射到 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

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

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

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

有什么解决办法吗?

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

不,kubernetes 它暴露了 "docker compatible" link env-vars,因为你的 Service 被命名为 server,所以最终成为 SERVER_PORT=tcp://thing:8080 因为它正在尝试成为 "helpful"

解决方案要么给您的 Service 起一个更具描述性的名称,要么 mask-off 违规的 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`