Chris G.
Chris G.

Reputation: 25984

Kubernetes: How to access a service on localhost - not the node but the service

Update:

Found this, but it that the right way?

I can see that I can do portforward to e node, like:

kubectl port-forward hello-nginx 8080:80

But I want to play and try to scale and access a service. I have started the service:

kubectl expose deployment hello-nginx --type=NodePort

service “hello-nginx” exposed

and then:

kubectl get services

rolling-sponge-hello-world   ClusterIP   10.104.12.39   <none>        80/TCP    3d

Then how do you proxy to the kubernetes service?

Note: Running Docker for Mac(Edge).

Upvotes: 7

Views: 15907

Answers (2)

David Steiman
David Steiman

Reputation: 3145

it looks like the output of the kubectl get services doesn't match with the service you have created. The output says "ClusterIP" while you created a "NodePort" service. The NodePort type allows to proxy the service over its node port, which is open on the IP of every node.

You can extract the NodePort also using kubectl describe service hello-nginx

Upvotes: 0

Jose Armesto
Jose Armesto

Reputation: 13789

The best approach would be using a Ingress, as you mentioned. That way, you could send requests to your local machine IP and the Ingress controller would send the traffic to the right Pods.

For that you need to:

  • Create Ingress rules that describe how you want the Ingress controller to route traffic to your Pods. These rules can redirect traffic to specific Pods based on the path or the host in the request.
  • Deploy an Ingress controller that will follow those rules, like the nginx Ingress controller. You can learn how to deploy it in this blog post.

This blog post talks specifically about Docker for mac and Ingress.

Upvotes: 7

Related Questions