Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.
Kubernetes is an awesome cloud platform, which ensures that cloud applications run reliably. It runs automated tests, flawless updates, smart roll out and rollbacks, simple scaling and a lot more.
So as a part of GSoC, I worked on taking the loklak server to Kubernetes on Google Cloud Platform. In this blog post, I will be discussing the approach followed to deploy development branch of loklak on Kubernetes.
New Docker Image
Since Kubernetes deployments work on Docker images, we needed one for the loklak project. The existing image would not be up to the mark for Kubernetes as it contained the declaration of volumes and exposing of ports. So I wrote a new Docker image which could be used in Kubernetes.
The image would simply clone loklak server, build the project and trigger the server as CMD –
This image wouldn’t have any volumes or exposed ports and we are now free to configure them in the configuration files (discussed in a later section).
Building and Pushing Docker Image using Travis
To automatically build and push on a commit to the master branch, Travis build is used. In the after_success section, a call to push Docker image is made.
Travis environment variables hold the username and password for Docker hub and are used for logging in –
We needed checks there to ensure that we are on the right branch for the push and we are not handling a pull request –
Kubernetes Configurations for loklak
Kubernetes cluster can completely be configured using configurations written in YAML format. The deployment of loklak uses the previously built image. Initially, the image tagged as latest-kubernetes-development is used –
Readiness and Liveness Probes
Probes act as the top level tester for the health of a deployment in Kubernetes. The probes are performed periodically to ensure that things are working fine and appropriate steps are taken if they fail.
When a new image is updated, the older pod still runs and servers the requests. It is replaced by the new ones only when the probes are successful, otherwise, the update is rolled back.
In loklak, the /api/status.json endpoint gives information about status of deployment and hence is a good target for probes –
These probes are performed periodically and the server is restarted if they fail (non-success HTTP status code or takes more than 3 seconds).
Ports and Volumes
In the configurations, port 80 is exposed as this is where Jetty serves inside loklak –
If we notice, this is the port that we used for running the probes. Since the development branch deployment holds no dumps, we didn’t need to specify any explicit volumes for persistence.
Load Balancer Service
While creating the configurations, a new public IP is assigned to the deployment using Google Cloud Platform’s load balancer. It starts listening on port 80 –
Since this service creates a new public IP, it is recommended not to replace/recreate this services as this would result in the creation of new public IP. Other components can be updated individually.
Kubernetes Configurations for Elasticsearch
To maintain a persistent index, this deployment would require an external Elasticsearch cluster. loklak is able to connect itself to external Elasticsearch cluster by changing a few configurations.
Docker Image and Environment Variables
The image used for Elasticsearch is taken from pires/docker-elasticsearch-kubernetes. It allows easy configuration of properties from environment variables in configurations. Here is a list of configurable variables, but we needed just a few of them to do our task –
Persistent Index using Persistent Cloud Disk
To make the index last even after the deployment is stopped, we needed a stable place where we could store all that data. Here, Google Compute Engine’s standard persistent disk was used. The disk can be created using GCP web portal or the gcloud CLI.
Before attaching the disk, we need to declare a volume where we could mount it –
Now that we have a volume, we can simply mount the persistent disk on it –
Now, whenever we deploy these configurations, we can reuse the previous index.
Exposing Kubernetes to Cluster
The HTTP and transport clients are enabled on port 9200 and 9300 respectively. They can be exposed to the rest of the cluster using the following service –
Once deployed, other deployments can access the cluster API from ports 9200 and 9300.
Connecting loklak to Kubernetes
Since we enable the service named “elasticsearch” in namespace “elasticsearch”, we can access the cluster at address elasticsearch.elasticsearch:9200 (web) and elasticsearch.elasticsearch:9300 (transport).
To confine these changes only to Kubernetes deployment, we can use sed command while building the image (in Dockerfile) –
Now when we create the deployments in Kubernetes cluster, loklak auto connects to the external elasticsearch index and creates indices if needed.
Verifying persistence of the Elasticsearch Index
In order to see that the data persists, we can completely delete the deployment or even the cluster if we want. Later, when we recreate the deployment, we can see all the messages already present in the index.
In the last line from the logs, we can see that indices already present on the disk were recovered. Now if we head to the public IP assigned to the cluster, we can see that the message count is restored.
In this blog post, I discussed how we utilised the Kubernetes setup to shift loklak to Google Cloud Platform. The deployment is active and can be accessed from the link provided under wiki section of loklak/loklak_server repo.
- Docker Tutorial Series : Writing a Dockerfile – https://rominirani.com/docker-tutorial-series-writing-a-dockerfile-ce5746617cd.
- Introduction to YAML: Creating a Kubernetes deployment – https://www.mirantis.com/blog/introduction-to-yaml-creating-a-kubernetes-deployment/.
- Configure Liveness and Readiness Probes – https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/.
- Setting up HTTP Load Balancing with Ingress – https://cloud.google.com/container-engine/docs/tutorials/http-balancer.
- Persistent Volumes in Kubernetes – https://kubernetes.io/docs/concepts/storage/persistent-volumes/.
- Rolling updates with Kubernetes: Replication Controllers vs Deployments – https://ryaneschinger.com/blog/rolling-updates-kubernetes-replication-controllers-vs-deployments/.
- DNS Pods and Services – https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/.