首页 > 解决方案 > 使用 kubectl 和 minikube 时无法连接到远程服务器错误

问题描述

我已经安装了以下内容:

docker 版本 19.03.8,minikube 版本 1.8.2,kubectl 版本 1.15.5。

并创建了一个如下所示的部署 YAML 文件:

---
kind: Service
apiVersion: v1
metadata:
  name: hellowworldservice
spec:
  selector:
    app: hello-world
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8081
      # Port to forward to inside the pod
      targetPort: 8080
      # Port accessible outside cluster
      nodePort: 30005
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
  labels:
    app: hello-world
spec:
  replicas: 5
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
        - name: hello-world
          image: tutum/hello-world
          ports:
            - containerPort: 8080

Pod 和部署成功启动:

PS C:\kubernetes> kubectl create -f deployment.yaml
service/hellowworldservice created
deployment.apps/hello-world created
PS C:\kubernetes> kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
hello-world-889b5c84c-98vvx   1/1     Running   0          25s
hello-world-889b5c84c-bs48d   1/1     Running   0          25s
hello-world-889b5c84c-hm6j2   1/1     Running   0          25s
hello-world-889b5c84c-hzqcc   1/1     Running   0          25s
hello-world-889b5c84c-xg5nl   1/1     Running   0          25s
PS C:\kubernetes> kubectl get deployments
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
hello-world   5/5     5            5           40s

我可以通过 ping 访问 IP 地址,但不能通过“Hello World”网页应该所在的端口。为什么是这样?

PS C:\kubernetes> minikube ip
172.17.45.76
PS C:\kubernetes> ping 172.17.45.76

Pinging 172.17.45.76 with 32 bytes of data:
Reply from 172.17.45.76: bytes=32 time<1ms TTL=64
Reply from 172.17.45.76: bytes=32 time<1ms TTL=64
Reply from 172.17.45.76: bytes=32 time<1ms TTL=64
Reply from 172.17.45.76: bytes=32 time=1ms TTL=64

Ping statistics for 172.17.45.76:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 1ms, Average = 0ms
PS C:\kubernetes> curl http://172.17.45.76:30005
curl : Unable to connect to the remote server
At line:1 char:1
+ curl http://172.17.45.76:30005
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

标签: dockerkubernetesmicroserviceskubectlminikube

解决方案


简而言之:您使用的是错误targetPort: 8080而不是正确的targetPort: 80

原因 - tutum/hello-world

Apache 有一个“Hello World”页面在端口 80 中监听。

您可以从任何 pod 进行检查。

 $ kubectl get pod -l app=hello-world -o jsonpath="{.items[0].metadata.name}" | xargs -I % sh -c 'echo == Pod %; kubectl exec -ti % -- netstat -tunaple'
== Pod hello-world-5df464dd44-fcz9g
Unable to use a TTY - input is not a terminal or the right kind of file
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      10/php-fpm.conf)
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1/nginx: master pro


#check all pods
$ kubectl get pods -l app=hello-world | grep hello-world |grep Running | awk '{ print $1 }' | xargs -I % sh -c 'echo == Pod %; kubectl exec -ti % -- netstat -nat'

正确的 yaml(我还评论了最后一行):

---
kind: Service
apiVersion: v1
metadata:
  name: hellowworldservice
spec:
  selector:
    app: hello-world
  ports:
    - protocol: "TCP"
      # Port accessible inside cluster
      port: 8081
      # Port to forward to inside the pod
      targetPort: 80
      # Port accessible outside cluster
      nodePort: 30005
  type: LoadBalancer

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
  labels:
    app: hello-world
spec:
  replicas: 5
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
        - name: hello-world
          image: tutum/hello-world
          ports:
#         - containerPort: 80

结果:

$ minikube service hellowworldservice --url
http://172.17.0.2:30005
$ curl $(minikube service hellowworldservice --url)
...
    <title>Hello world!</title>
...
<body>
    <img id="logo" src="logo.png" />
    <h1>Hello world!</h1>
    <h3>My hostname is hello-world-5df464dd44-tqzbz</h3>    
...

推荐阅读