How to call spring api from frontend in kubernetes

11/7/2018

I am trying to create a Kubernetes application in which, I have created one pod and service for backend(spring boot microservice) & frontend pod and a loadbalancer service.

I wanted to know how would I call the backend API from frontend pod in Kubernetes?

Here are the running services:

NAME         TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)          AGE       SELECTOR
angular      LoadBalancer   10.100.15.215   a17f17fd2e25011e886100a0e002191e-1613530232.us-east-1.elb.amazonaws.com   4200:30126/TCP   12s       app=angular
kubernetes   ClusterIP      10.100.0.1      <none>                                                                    443/TCP          35m       <none>
login        ClusterIP      10.100.99.52    <none>                                                                    5555/TCP         13m       app=login,tier=backend

I am calling the following API from frontend and it is showing name not resolved error:

http://login/login

I have also tried to call the API with cluster IP but that failed.

-- Abhishek Chudekar
amazon-eks
amazon-web-services
devops-services
kubernetes
kubernetes-pod

2 Answers

11/11/2018

Does your angular application directly access the login service? If thats the case, its normal that you will not be able to get thru because login service is using ClusterIP. Which means, the IP is within the cluster only. You can use LoadBalancer type like you did for your "angular" application.

-- Bal Chua
Source: StackOverflow

11/7/2018

Looks like your backend service is running on port 5555, so you would have to call your backend service like this:

http://login:5555/login

This assuming the pods for your frontend are on the same Kubernetes namespace. If they are on a different namespace you would call something like this:

http://login.<namespace>.svc.cluster.local:5555/login

Also as described here.

Note that this will work only within the cluster, if you are hitting your Angular frontend from a web browser outside of the cluster, this will not work, because the web browser would have no idea of where your backend is in the cluster. So either you will have to expose your backend using another LoadBalancer type of service or you may consider using a Kubernetes Ingress with an ingress controller.

-- Rico
Source: StackOverflow