In one of our projects we are running Rails application on Kubernetes cluster. It is proven tool for managing and deploying docker containers in production.
In kubernetes containers are managed using
and they are termed as
deployment holds the specification of pods.
It is responsible to run the pod with specified resources.
pod is restarted or
deployment is deleted then data is lost on pod.
We need to retain data out of pods lifecycle
deployment is destroyed.
We use docker-compose during development mode. In docker-compose linking between host directory and container directory works out of the box. We wanted similar mechanism with kuberentes to link volumes. In kubernetes we have various types of volumes to use. We chose persistent volume with AWS EBS storage. We used persistent volume claim as per the need of application.
As per the Persistent Volume’s definition (PV) Cluster administrators must first create storage in order for Kubernetes to mount it.
Our Kubernetes cluster is hosted on AWS. We created AWS EBS volumes which can be used to create persistent volume.
Let’s create a sample volume using aws cli and try to use it in the deployment.
This will create a volume in
We need to note
VolumeId once the volume is created.
Now let’s create a persistent volume template
test-pv to create volume using this EBS storage.
Once we had template to create persistent volume, we used kubectl to launch it. Kubectl is command line tool to interact with Kubernetes cluster.
Once persistent volume is created you can check using following command.
Now that our persistent volume is in available state, we can claim it by creating persistent volume claim policy.
We can define persistent volume claim using following template
Let’s create persistent volume claime using above template.
the persistent volume claim, our
persistent volume will change from
available state to
Now we have persistent volume claim available on our Kubernetes cluster, Let’s use it in deployment.
Deploying Kubernetes application
We will use following deployment template as
Now launch the deployment using following command.
Once the deployment is up and running all the contents on
shared directory will
be stored on persistent volume claim.
Further when pod or deployment crashes for
any reason our data will be always retained
on the persistent volume.
We can use it to launch the application deployment.
This solved our goal of retaining data across deployments across