'Access a Kubernetes Service running locally in Docker For Desktop?

I'm using Docker For Desktop with the built-in Kubernetes cluster. I have installed a Pod that serves resources over HTTP, but I'm not sure how to access it using my browser. I have the following ServiceSpec that correctly routes traffic to the Pod:

spec:
  clusterIP: 10.99.132.220
  externalTrafficPolicy: Cluster
  ports:
  - name: myport
    nodePort: 31534
    port: 8037
    protocol: TCP
    targetPort: 80
  type: LoadBalancer

And I can see it set up when I query it with kubectl:

$ kubectl get service
NAME           TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
myservice   LoadBalancer   10.99.132.220   localhost     8037:31534/TCP   1h

How do I reach this service using my browser?



Solution 1:[1]

That service will be available in your browser at http://localhost:8037

Note that the port 8037 corresponds to the port property on the ServiceSpec object.

If you are unable to reach the service at that URL, then it could be one of several things, including but not limited to:

  • There is another Service in your cluster that has claimed that port. Either delete the other Service, or change the port property to an unclaimed port.
  • Your Pod is not running and ready. Check kubectl get pods.

Solution 2:[2]

For local development you might want to use the type NodePort for the service (see https://kubernetes.io/docs/concepts/services-networking/service/#nodeport). This binds the given nodePort, as the name says, to a port of your node (which should be localhost for docker-on-desktop).

Then the the service should be available ob http://localhost:31534.

Solution 3:[3]

Port forwarding:

kubectl port-forward <my-pod-name> 4000:8037

Your pod's internal port (8037) will be accessible on localhost:4000. Docs here.

Solution 4:[4]

First of all 2 are different way 1. MiniKube and 2. Docker-desktop.

For Docker-Desktop on Mac , you can always use localhost but more good approach below.

  1. Go to any pod and access with your cluster node IP.
  2. Run busybox and go to cluster node IP to access application.

How to get IP of your cluster.

$ kubectl describe node docker-for-desktop

Search below

Addresses:
  InternalIP:  192.168.65.3
  Hostname:    docker-desktop

With above IP you can run the command , in busybox. Please note that , you can not directly access above IP but you need to login first any pod of cluster of that namespace.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cory Klein
Solution 2 Mortl
Solution 3
Solution 4 wolmi