I have Kubernetes running on two nodes and one application deployed on the two nodes (two pods, one per node).
It's a Spring Boot application. It uses OpenFeign for service discoverability. In the app i have a RestController defined and it has a few APIs and an @Autowired @Service which is called from inside the APIs.
Whenever i do a request on one of the APIs Kubernetes uses some sort of load-balancing to route the traffic to one of the pods, and the apps RestController is called. This is fine and i want this to be load-balanced.
The problem happens once that API is called and it calls the @Autowired @Service. Somehow this too gets load-balanced and the call to the @Service might end up on the other node.
Heres and example:
- we have two nodes: node1, node2
- we make a request to node1's IP address.
- this might get load-balanced to node2 (this is fine)
- node1 gets the request and calls the @Autowired @Service
- the call jumps to node2 (this is where the problem happens)
And in code:
Controller:
@Autowired
private lateinit var userService: UserService
@PostMapping("/getUser")
fun uploadNewPC(@RequestParam("userId") userId: String): User {
println(System.getEnv("hostIP")) //123.45.67.01
return userService.getUser(userId)
}
Service:
@Service
class UserService {
fun getUser(userId: String) : User {
println(System.getEnv("hostIP")) //123.45.67.02
...
}
}
I want the load-balancing to happen only on the REST requests not the internal calls of the app to its @Service components. How would i achieve this? Is there any configuration to the way Spring Boot's @service components operate in Kubernetes clusters? Can i change this?
Thanks in advance.
Edit:
After some debugging i found that It wasn't the Service that was load balanced to another node but the initial http request. Even though the request is specifically send to the url of node1... And since i was debugging both nodes at the same time, i didn't notice this.