Kubernetes - can a service use another service without me writing the latter's IP in the former's code?

6/7/2019

In the following guide: https://www.freecodecamp.org/news/learn-kubernetes-in-under-3-hours-a-detailed-guide-to-orchestrating-containers-114ff420e882/

The architecture looks like this: enter image description here

The frontend (which is served from SA-Frontend) sends requests to SA-WebbApp. In a dev environment, I believe both would run on different ports on localhost, but in production, as the guide suggests at the very bottom, we have to realize the IP address of SA-WebApp:

minikube service list
|-------------|----------------------|-----------------------------|
|  NAMESPACE  |         NAME         |             URL             |
|-------------|----------------------|-----------------------------|
| default     | kubernetes           | No node port                |
| default     | sa-frontend-lb       | http://192.168.99.100:30708 |
| default     | sa-logic             | No node port                |
| default     | sa-web-app-lb        | http://192.168.99.100:31691 |
| kube-system | kube-dns             | No node port                |
| kube-system | kubernetes-dashboard | http://192.168.99.100:30000 |
|-------------|----------------------|-----------------------------|

Edit the code of the frontend to use it:

analyzeSentence() {
        fetch('http://192.168.99.100:31691/sentiment', { /* shortened for brevity */})
            .then(response => response.json())
            .then(data => this.setState(data));
    }

Then rebuild the static files, rebuild the Docker image, repush it to the hub, edit the deployment yaml for the frontend and execute kubectl apply -f on it.

Is there any way to make the frontend take the IP from some kind of configuration / discovery instead of me manually inserting it and rebuilding the whole thing?

-- W2a
kubernetes

1 Answer

6/11/2019

as mentioned by community members Amit Kumar Gupta and David Maze please refer to:

  1. DNS for Services and Pods.
  2. Connecting Applications with Services
  3. Your guide in the section Kubernetes in Practice — Services. There are two topics in your example:
    • Applying a label to all the pods that we want our Service to target and
    • Applying a “selector” to our service so that defines which labeled pods to target.
-- Hanx
Source: StackOverflow