Port Forward Not Working in MiniKube on Windows 10

6/21/2019

I am setting up kubernetes on my Laptop (Windows 10 OS) to work on the Containers and it's Orchestration. I have created Minikube VM using the below command and it got succeeded.

minikube start --vm-driver hyperv --hyperv-virtual-switch "Primary Virtual Switch"

I am able to Launch Kubernetes and able to launch the Minikube dashboard as well

I started the Kubernetes cluster and deployed nginx app into Cluster. Below are the commands.

kubectl run hello-nginx --image=nginx --port=8020
kubectl expose deployment hello-nginx --type=NodePort --port=8020 --target-port=8020

I am able to view PODs and Services using the below commands.

kubectl get pods
kubectl get services

it works perfect up to here. I am able to view deployment, PODs and Service information in the Minikube dashboard.

When i run the below command to launch the application in the browser, browser is throwing a message as "Resource Not Found" but I am able to view POD and Service information in the MiniKube Dashboard.

minikube service hello-nginx

URL: http://192.168.43.20:32087/

getting exception in a browser

This website could not be found.
Error Code: INET_E_RESOURCE_NOT_FOUND

Below given the deployment YAML file,

{
  "kind": "Deployment",
  "apiVersion": "extensions/v1beta1",
  "metadata": {
    "name": "hello-nginx",
    "namespace": "default",
    "selfLink": "/apis/extensions/v1beta1/namespaces/default/deployments/hello-nginx",
    "uid": "5629038e-93e5-11e9-ad2e-00155d162e0e",
    "resourceVersion": "49313",
    "generation": 1,
    "creationTimestamp": "2019-06-21T05:28:01Z",
    "labels": {
      "run": "hello-nginx"
    },
    "annotations": {
      "deployment.kubernetes.io/revision": "1"
    }
  },
  "spec": {
    "replicas": 1,
    "selector": {
      "matchLabels": {
        "run": "hello-nginx"
      }
    },
    "template": {
      "metadata": {
        "creationTimestamp": null,
        "labels": {
          "run": "hello-nginx"
        }
      },
      "spec": {
        "containers": [
          {
            "name": "hello-nginx",
            "image": "nginx",
            "ports": [
              {
                "containerPort": 8020,
                "protocol": "TCP"
              }
            ],
            "resources": {},
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File",
            "imagePullPolicy": "Always"
          }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "ClusterFirst",
        "securityContext": {},
        "schedulerName": "default-scheduler"
      }
    },
    "strategy": {
      "type": "RollingUpdate",
      "rollingUpdate": {
        "maxUnavailable": 1,
        "maxSurge": 1
      }
    },
    "revisionHistoryLimit": 2147483647,
    "progressDeadlineSeconds": 2147483647
  },
  "status": {
    "observedGeneration": 1,
    "replicas": 1,
    "updatedReplicas": 1,
    "readyReplicas": 1,
    "availableReplicas": 1,
    "conditions": [
      {
        "type": "Available",
        "status": "True",
        "lastUpdateTime": "2019-06-21T05:28:01Z",
        "lastTransitionTime": "2019-06-21T05:28:01Z",
        "reason": "MinimumReplicasAvailable",
        "message": "Deployment has minimum availability."
      }
    ]
  }
}

Below given the replica set YAML file info,

{
  "kind": "ReplicaSet",
  "apiVersion": "extensions/v1beta1",
  "metadata": {
    "name": "hello-nginx-76696c698f",
    "namespace": "default",
    "selfLink": "/apis/extensions/v1beta1/namespaces/default/replicasets/hello-nginx-76696c698f",
    "uid": "562be1e8-93e5-11e9-ad2e-00155d162e0e",
    "resourceVersion": "49310",
    "generation": 3,
    "creationTimestamp": "2019-06-21T05:28:01Z",
    "labels": {
      "pod-template-hash": "76696c698f",
      "run": "hello-nginx"
    },
    "annotations": {
      "deployment.kubernetes.io/desired-replicas": "1",
      "deployment.kubernetes.io/max-replicas": "2",
      "deployment.kubernetes.io/revision": "1"
    },
    "ownerReferences": [
      {
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "name": "hello-nginx",
        "uid": "5629038e-93e5-11e9-ad2e-00155d162e0e",
        "controller": true,
        "blockOwnerDeletion": true
      }
    ]
  },
  "spec": {
    "replicas": 1,
    "selector": {
      "matchLabels": {
        "pod-template-hash": "76696c698f",
        "run": "hello-nginx"
      }
    },
    "template": {
      "metadata": {
        "creationTimestamp": null,
        "labels": {
          "pod-template-hash": "76696c698f",
          "run": "hello-nginx"
        }
      },
      "spec": {
        "containers": [
          {
            "name": "hello-nginx",
            "image": "nginx",
            "ports": [
              {
                "containerPort": 8020,
                "protocol": "TCP"
              }
            ],
            "resources": {},
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File",
            "imagePullPolicy": "Always"
          }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "ClusterFirst",
        "securityContext": {},
        "schedulerName": "default-scheduler"
      }
    }
  },
  "status": {
    "replicas": 1,
    "fullyLabeledReplicas": 1,
    "readyReplicas": 1,
    "availableReplicas": 1,
    "observedGeneration": 3
  }
}

Now I am trying out the Port Forwarding option to route the request to POD, but it is not working.

kubectl port-forward deployment/hello-nginx 8020:8020

I am getting the below exception when i try to access the URL "http://127.0.0.1:8020"

Handling connection for 8020
E0622 01:07:06.306320   18888 portforward.go:331] an error occurred forwarding 8020 -> 8020: error forwarding port 8020 to pod c54d6faaa545992dce02f58490a26154134843eb7426a51e78df2cda172b514c, uid : exit status 1: 2019/06/21 08:01:18 socat[4535] E connect(5, AF=2 127.0.0.1:8020, 16): Connection refused

I have read many articles on this but couldn't found the root cause for this issue. Am i missing anything important here?

Thanks for your help in Advance.

-- rajswastik
kubernetes
minikube

1 Answer

6/24/2019

Your issue is actually unrelated to Minikube or port forwarding. You expose the port 8020, however the application hello-nginx uses 80. So you should use 80 everywhere instead of 8020. For example:

kubectl run hello-nginx --image=nginx --port=80

Saying that, using Minikube is not the best option for Windows. Much better is to use Docker Desktop, then everything you run on Kubernetes is available on your localhost.

-- RafaƂ Leszko
Source: StackOverflow