【问题标题】:Kubernetes pod times out connecting to serviceKubernetes pod 连接到服务超时
【发布时间】:2022-06-16 04:48:34
【问题描述】:

我在当前的 Kubernetes minikube 设置中遇到问题,无法让 pod 连接到 ClusterIP 服务。我当前的设置环境如下所示:

OS: Rocky Linux 8 Guest Hosted with VMware on a Windows 10 Machine
VMware has 'Virtualize Intel VT-x/EPT or AMD-V/RVI' enabled
Minikube (v1.24.0) is running with docker (Docker version 20.10.11, build dea9396) as its driver

为了隔离问题,我开始使用这个简单的golang hello world image。简单来说,如果你wget url:8080你会下载一个index.html。

在本地构建映像后,我创建了一个 pod:

kubectl run hello --image=hello --port=8080 --labels='app=hello'

吊舱运转良好,我可以执行它。在吊舱内,如果我运行:

wget localhost:8080 或 wget 172.17.0.3:8080

我得到了预期的输出:

converted 'http://172.17.0.3:8080' (ANSI_X3.4-1968) -> 'http://172.17.0.3:8080' (UTF-8)
--2022-01-09 20:15:44--  http://172.17.0.3:8080/
Connecting to 172.17.0.3:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13 [text/plain]
Saving to: 'index.html'

index.html                                   100%[==============================================================================================>]      13  --.-KB/s   in 0s     

2022-01-09 20:15:44 (3.11 MB/s) - 'index.html' saved [13/13]

现在,如果我使用 kubectl expose pod hello --name=hello-service --port=8080 --target-port=8080 公开 pod,则该服务将作为 hello-service 启动并描述它输出以下内容:

Name:              hello-service
Namespace:         default
Labels:            app=hello
Annotations:       <none>
Selector:          app=hello
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.101.73.45
IPs:               10.101.73.45
Port:              <unset>  8080/TCP
TargetPort:        8080/TCP
Endpoints:         172.17.0.3:8080
Session Affinity:  None
Events:            <none>

端口已设置并且端点存在,所以从我读过的所有内容来看,这应该可以工作。所以我执行回到 pod 并尝试 wget 服务,我得到:

root@hello:/go/src/app# wget hello-service:8080
converted 'http://hello-service:8080' (ANSI_X3.4-1968) -> 'http://hello-service:8080' (UTF-8)
--2022-01-09 20:36:06--  http://hello-service:8080/
Resolving hello-service (hello-service)... 10.101.73.45
Connecting to hello-service (hello-service)|10.101.73.45|:8080... failed: Connection timed out.

当我尝试wget 10.101.73.45:8080 时也会发生同样的情况,这当然是有道理的,因为 hello-service 在之前的 wget 中解析到了正确的 IP。

现在,我显然不是 Kubernetes 专家,但接下来的部分对我来说很奇怪。如果我改为使用 nodePort 公开 pod,那么一切都会按您的预期进行。使用以下定义文件:

apiVersion: v1
kind: Service
metadata:
  name: hello-service
spec:
  selector:
    app: hello
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080
    nodePort: 31111
  type: NodePort

我可以从 nodePort 访问 pod。一个简单的wget 192.168.49.2:31111,我得到了预期的输出:

--2022-01-09 15:00:48--  http://192.168.49.2:31111/
Connecting to 192.168.49.2:31111... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13 [text/plain]
Saving to: ‘index.html’

index.html                                   100%[============================================================================================>]      13  --.-KB/s    in 0s      

2022-01-09 15:00:48 (3.05 MB/s) - ‘index.html’ saved [13/13]

无论如何,我的业余智慧到此为止。几天来试图找到类似的问题,我们不仅仅是“哦,您没有正确标记您的容器”或“您的港口清单中有错字”,但运气不佳。我认为这种情况非常独特,足以保证其发布。

【问题讨论】:

    标签: kubernetes minikube


    【解决方案1】:

    我发现基于 minikube 的服务不允许 pod 重定向回自身,从而导致超时。通过将副本增加到 2 个,service-url curl 不再超时,因为可以访问兄弟 pod。

    【讨论】:

      猜你喜欢
      • 2018-09-03
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 2018-08-18
      • 2016-04-16
      • 2020-08-26
      相关资源
      最近更新 更多