我是 Kubernetes 的新手,我正在努力在新的 Kubernetes 集群中部署应用程序。
目前运行的服务有多个Pod需要相互通信。我正在寻找一种通用的方法来调试问题,而不是深入研究服务的规范,因为问题会变得过于具体。
集群中的 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/
答案 0 :(得分:2)
首先是以下模式:
my-svc.my-namespace.svc.cluster-domain.example
仅适用于 FQDNs of Services,不适用于具有以下形式的 Pods:
pod-ip-address.my-namespace.pod.cluster-domain.example
例如:
172-17-0-3.default.pod.cluster.local
因此,实际上您查询的是关于名为 Service
的 testpod
的 FQDN 而不是 Pod
的 FQDN 的集群 dns。从它已成功解析的事实来看,这样的 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
)Pod
testpod
公开的 Service
(您已被 testpod
Service
成功重定向到它)立>
Pod
,您就试图连接到不正确的端口,这就是服务器拒绝连接的原因我最好的猜测是您的 Pod
实际上侦听不同的端口,例如 80
,但是您通过 ClusterIP
Service
仅指定 {{1} } 值例如作者:
--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
或使用以下 yaml 清单:
kubectl expose pod testpod --port=8080 --targetPort=80
当然,您的 apiVersion: v1
kind: Service
metadata:
name: testpod
spec:
ports:
- protocol: TCP
port: 8080
targetPort: 80
可能与 targetPort
不同,但 80
在这种情况下只能表示一件事:目标 http 服务器(在 connection refused
中运行)拒绝连接到 Pod
端口(很可能是因为它没有监听它)。您没有指定您使用的图像是标准的 8080
网络服务器还是基于您的自定义图像的其他图像。但如果它是 nginx
并且没有进行不同的配置,它会在端口 nginx
上侦听。
为了进一步调试,您可以附加到您的 80
:
Pod
如果 kubectl exec -it testpod --namespace mynamespace -- /bin/sh
命令不存在(最可能的情况)运行:
netstat
然后检查 apt update && apt install net-tools
您的容器侦听的端口。
我希望这可以帮助您解决问题。如有任何疑问,请随时提出。