Kubernetes Tip: How To Co-locate Applications On The Same Node?
There are use-cases where one needs to run replicas of different applications co-located on the same node. For example, having a replica of an in-memory database and a replica web server would decrease the load time of the end-user.
Let’s consider an example to achieve this use-case. My Kubernetes cluster consists of 3 worker nodes & 1 master node. There are 2 different deployment files, one is web-server and the other is in-memory Redis cache. Outputs of the nodes and deployments are as shown below.
Note the important parameters used in these deployment files. They are podAntiAffinity, PodAffinity, topologyKey & requiredDuringSchedulingIgnoredDuringExecution.
In simple English, redis deployment says that don’t run multiple copies of redis replica on the same node. The web-server deployment says that run web-server replica along with redis with the additional rule of having web-server replicas on different nodes.
Let’s apply these deployments and see how the schedular has scheduled them.
Cool!. We can see that the scheduler has scheduled each replica of web-server and redis on different nodes and making sure that web-server and redis always co-located. A pictorial representation of application distribution as below.
Note: The examples considered in this exercise are taken from the K8S document.