Selector and Label
In kubernetes, containers will need to communicate with each other. How is this done? More specifically, how are each containers differentiated?
In the above scenario, how would the container yourapp
communicate to a single myapp
node?
Would the container need to know the ip, or some kind of actual address?
It could work, but we would have to know each container ids, and in kubernetes, containers get removed and created often, so this is not a desirable approach.
Then, can we introduce a reverse proxy, like nginx
, so that yourapp
container communicates to the nginx
container (by id), and let the nginx
container do the job of load balancing?
This might seem plausible, but we face the same problem as before: nginx
container would need to know the ids of myapp
containers as well.
Of course, we can use the ip address of each container, but that doesn't solve the fundamental problem.
Thus, in kubernetes, each node is labeled by arbitrary key-value pairs. If two containers (in this case, myapp
containers) are replicas, we can use the same label to represent them.
Then, if we were to communicate from a container to another container, we can use the selector, which acts in some way like a query, to select the nodes that meets certain requirements. Then, Kubernetes chooses which node to communicate to internally. Tada!