![](/img/trans.png)
[英]Kubernetes WebSocket service: Connection refused between pods using hostname
[英]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 關於名為testpod
的Service
的 FQDN 而不是關於Pod
的 FQDN。 從它被成功解決的事實來看,這樣的Service
已經存在於您的集群中,但很可能是配置錯誤。 您收到錯誤消息connection refused
的事實可能意味着以下內容:
Service
FQDN testpod.mynamespace.svc.cluster.local
已成功解析(否則您會收到類似curl: (6) Could not resolve host: testpod.default.svc.cluster.local
)testpod
Service
(否則,如果它存在但未在8080
端口上偵聽,則您正在嘗試連接,您將收到timeout
,例如curl: (7) Failed to connect to testpod.default.svc.cluster.local port 8080: Connection timed out
)testpod
Service
暴露的Pod
(你已經被testpod
Service
重定向到它了)Pod
,您就會嘗試連接到錯誤的端口,這就是服務器拒絕連接的原因我最好的猜測是,您的Pod
實際上偵聽不同的端口,例如80
,但是您通過ClusterIP
Service
通過僅指定--port
值來公開它,例如:
kubectl expose pod testpod --port=8080
在這種情況下, --port
( Service
的端口)和--targetPort
( Pod
的端口)都將具有相同的值。 換句話說,您已經創建了如下所示的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.