Creating a simple shared persistent storage for micro-services in Kubernetes

Micro-services are vastly used nowadays due to the advantages over the modules having monolith architecture. Micro-services can work either in totally independent manner or connected manner. Where as there are situations that you need to have a common storage for set of micro-services.

Example scenario — log files system

Suppose you have a microservice that is responsible for reading and writing log files. Also you have 100 replicas of the module running on Kubernetes. Also there is a load balancer for directing the requests.

Think! You made a request to the load balancer to write a log file. Thereafter you made another request to retrieve the written file. How we can store the log files? Indeed we need a some kind of storage that can be accessed by all micro-services.

hostPath Volume type

What hostPath does is it simply mount a directory from host (the node) to the container. Therefore each replica of micro-service can access the folder as a mounted volume. Since the hostpath is only available for single host, all replicas should be deployed in a same node (otherwise you may have to use nfs).

So here is the special part of yaml file

/nodepath of the node will be mounted to all containers as /storage . Therefore once you write some data in to /storage from a running application of a container it refers to /nodepath of the node.

Eg: if the micro-service writes a log file /storage/logfile1.txt , that file will be saved in to /nodepath/logfile.txt in host. Similarly if the micro-service requests /storage/logfile1.txt it will be fetched from /nodepath/logfile1.txt of host.

Log files system with shared volume

So, here is our log file system after adding the shared volume


If you are working with Rancher (which is good for on-premise Kubernetes environment). Above volume mounting can be done very easily using the rancher user interface as per below.

See you in next article

Happy Kubernetes !!

Programmer | Author of Neutralinojs | Technical Writer

Programmer | Author of Neutralinojs | Technical Writer