簡體   English   中英

同一集群中 Kubernetes pod 之間的連接被拒絕

[英]Connection Refused between Kubernetes pods in the same cluster

我是 Kubernetes 的新手,我正在努力在新的 Kubernetes 集群中部署應用程序。

目前,運行的服務有多個需要相互通信的 pod。 我正在尋找關於調試問題的 go 的一般方法,而不是進入服務的指定,因為問題將變得過於具體。

集群中的 pod 拋出錯誤: err="Get \"http://testpod.mynamespace.svc.cluster.local:8080/": dial tcp 10.10.80.100:8080: connect: connection refused"兩個 pod 都是在同一個集群中。

調試此問題的最佳步驟是什么?

我試過運行: kubectl exec -it testpod --namespace mynamespace -- cat /etc/resolv.conf這會返回: search mynamespace.svc.cluster.local svc.cluster.local cluster.local us-east-2.compute.internal我在這里找到的: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

首先,以下模式:

my-svc.my-namespace.svc.cluster-domain.example

僅適用於Services 的 FQDN ,不適用於具有以下形式的Pod

pod-ip-address.my-namespace.pod.cluster-domain.example

例如:

172-17-0-3.default.pod.cluster.local

所以實際上你正在查詢集群 dns 關於名為testpodService的 FQDN 而不是關於Pod的 FQDN。 從它被成功解決的事實來看,這樣的Service已經存在於您的集群中,但很可能是配置錯誤。 您收到錯誤消息connection refused的事實可能意味着以下內容:

  1. 您的Service FQDN testpod.mynamespace.svc.cluster.local已成功解析(否則您會收到類似curl: (6) Could not resolve host: testpod.default.svc.cluster.local
  2. 您已成功訪問您的testpod Service (否則,如果它存在但未在8080端口上偵聽,則您正在嘗試連接,您將收到timeout ,例如curl: (7) Failed to connect to testpod.default.svc.cluster.local port 8080: Connection timed out
  3. 你已經到達了由testpod Service暴露的Pod (你已經被testpod Service重定向到它了)
  4. 但是一旦到達Pod ,您就會嘗試連接到錯誤的端口,這就是服務器拒絕連接的原因

我最好的猜測是,您的Pod實際上偵聽不同的端口,例如80 ,但是您通過ClusterIP Service通過僅指定--port值來公開它,例如:

kubectl expose pod testpod --port=8080

在這種情況下, --portService的端口)和--targetPortPod的端口)都將具有相同的值。 換句話說,您已經創建了如下所示的Service

apiVersion: v1
kind: Service
metadata:
  name: testpod
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

而且您可能應該以這種方式公開它:

kubectl expose pod testpod --port=8080 --targetPort=80

或使用以下 yaml 清單:

apiVersion: v1
kind: Service
metadata:
  name: testpod
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80

當然,您的targetPort可能與80不同,但在這種情況下connection refused僅意味着一件事:目標 http 服務器(在Pod中運行)拒絕連接到8080端口(很可能是因為它沒有在監聽它)。 您沒有指定您使用的是什么圖像,無論是標准的nginx網絡服務器還是基於您的自定義圖像的東西。 但是,如果它是nginx並且沒有進行不同的配置,它會在端口80上進行偵聽。

為了進一步調試,您可以附加到您的Pod

kubectl exec -it testpod --namespace mynamespace -- /bin/sh

如果netstat命令不存在(最有可能的情況)運行:

apt update && apt install net-tools

然后使用netstat -ntlp檢查您的容器偵聽的端口。

我希望這可以幫助您解決問題。 如有任何疑問,請隨時提問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM