Search code examples
dockerkubernetesmicroserviceskubectlminikube

Unable to connect to the remote server error when using kubectl and minikube


I have installed the following:

docker version 19.03.8, minikube version 1.8.2, kubectl version 1.15.5.

And created a deployment YAML file that looks like this:

---
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

The pods and deployments start successfully:

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

I can access the IP Address by pinging it, but not via the port where the "Hello World" webpage is supposed to be. Why is this?

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

Solution

  • In short: you are using wrong targetPort: 8080 instead of correct targetPort: 80.

    Reason for that - tutum/hello-world

    Has Apache with a 'Hello World' page listening in port 80.

    You can check it from any 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'
    

    Correct yaml(I also commented last line):

    ---
    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
    

    Result:

    $ 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>    
    ...