【问题标题】:Kubernetes pod gives "connection refused" when trying to connect via ClusterIp尝试通过 ClusterIp 连接时,Kubernetes pod 给出“连接被拒绝”
【发布时间】:2020-09-07 00:08:39
【问题描述】:

我正在尝试使用 Spring Gateway 将请求路由到一些暴露 REST API 的 Spring Boot pod。

我有一个这样部署的搜索服务:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: search
  name: search
spec:
  replicas: 1
  selector:
    matchLabels:
      app: search
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: search
    spec:
      containers:
      - image: <ommited>
        name: search-service
        imagePullPolicy: Always
        resources: {}
      imagePullSecrets:
        - name: <ommited>
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: search
  name: search
spec:
  ports:
  - name: stage
    port: 8080
    protocol: TCP
  selector:
    app: search
  type: ClusterIP

如果我使用 ngnix 入口部署,它可以很好地部署并且可以访问。 我想使用 Spring Gateway 作为入口点,所以我将它部署在另一个 pod 中,如下所示:

<ommiting deployment>
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: gateway
  name: gateway
spec:
  ports:
  - name: stage
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: gateway
  type: LoadBalancer

使用已定义的路线:

.route("search", r -> r.path("/search")
                        .uri("http://search:8080/search"))

它也可以很好地部署并且可以从外部访问。

当网关 pod 尝试将请求路由到搜索 pod 时,它得到一个明确的“连接被拒绝”。

{
  "timestamp": "...",
  "path": "/search",
  "status": 500,
  "error": "Internal Server Error",
  "message": "finishConnect(..) failed: Connection refused: search/10.100.130.149:8080",
  "requestId": "a3af8165-1"
}

主机名已正确解析。

还尝试从网关连接到搜索nc

kubectl exec -it gateway-7798cb658-ffzvb -- nc -z search 8080
command terminated with exit code 1

再次正确解析主机,所以预计我在某处错误配置了端口。 我已经验证了搜索 Spring Boot 应用程序在 8080 上侦听,并且 8080 是通过 docker 容器和搜索 pod 公开的。

任何帮助表示赞赏,谢谢!

【问题讨论】:

  • 分享 kubectl describe svc search 的输出
  • 发现一个旧端口映射是问题...正在部署旧配置,搜索 pod 正试图转发到 8082。您的评论让我查看了 svc 描述并找到了问题,所以添加一个答案,所以我可以接受它,你可以获得声誉。

标签: spring spring-boot kubernetes spring-cloud


【解决方案1】:

使用命令kubectl describe svc search 检查search 服务的描述,并验证port 是否与Pod 正在侦听的端口(containerPort) 匹配。您也可以在服务中定义一个targetPort 来明确指向容器正在侦听的端口(@98​​7654326@)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2019-05-06
    • 2016-02-02
    • 1970-01-01
    • 2021-11-16
    • 2019-10-20
    • 1970-01-01
    相关资源
    最近更新 更多