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.

Nodes in the cluster.
redis deployment file.
web-server deployment file.

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.

Output after apply command.

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.

Pictorial representation of scheduling.

Note: The examples considered in this exercise are taken from the K8S document.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store