How to make an HTTP request from a K8 pod to a NodePort service in the same cluster

6/29/2019

I need for a service in a K8 pod to be able to make HTTP calls to downstream services, load balanced by a NodePort, within the same cluster and namespace.

My constraints are these:

  • I can do this only through manipulation of deployment and service entities (no ingress. I don't have that level of access to the cluster)
  • I cannot add any K8 plugins
  • The port that the NodePort exposes must be randomized, not hard coded
  • This whole thing must be automated. I can't set the deployment with the literal value of the exposed port. It needs to be set by some sort of variable, or similar process.

Is this possible, and, if so, how?

-- ds390s
kubernetes

1 Answer

6/29/2019

It probably can be done but it will not be straight forward and you might have to add some custom automation. A NodePort service is meant to be used by an entity outside your cluster.

For inter-cluster communication, a regular service (with a ClusterIP) will work as designed. Your service can reach another service using DNS service discovery. For example. svc-name.mynamespace.svc.cluster.local would be the DNS entry for a svc-name in the mynamespace namespace.

If you can only do a NodePort which essentially is a port on your K8s nodes, you could create another Deployment or Pod of something like nginx or haproxy. Then have this deployment being serviced by regular K8s service with a ClusterIP. Then have nginx or haproxy point to the NodePort on all your nodes in your Kubernetes cluster. Also, have it configured so that it only forwards to listening NodePorts with some kind of healthcheck.

The above seems like an extra necessary step, but if NodePort from within the cluster is what you need (for some reason), it should do the trick.

-- Rico
Source: StackOverflow