Deploy Rocket chat server using Kubernetes

Image for post
Image for post

Rocket Chat is one of the most favorited open-source chat services, especially because of the self-hosted feature to keep your data at your house. Rocket Chat is a perfect way to chat and sharing information with your team.

There are plenty of ways to deploy a service. The worst one maybe is running it in a screen or move it to the background using CTRL+Z. The better idea is to run it as a system service, and I think the best idea is to deploy using Docker and a Docker Orchestration like Docker Swarm or Kubernetes. In this article, we want to deploy Rocket chat using Kubernetes.

Kubernetes (commonly stylized as k8s) is an open-source container-orchestration system for automating computer application deployment, scaling, and management.

It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation. It aims to provide a “platform for automating deployment, scaling, and operations of application containers across clusters of hosts”. It works with a range of container tools and runs containers in a cluster, often with images built using Docker. Kubernetes originally interfaced with the Docker runtime through a “Dockershim”; however, the shim has since been deprecated in favor of directly interfacing with containerd or another CRI-compliant runtime.

Actually, Kubernetes has a steep learning curve and takes time to know how to use it to do what you want. If you are not familiar with Kubernetes, These are two perfect sources to get started with that:

Prerequisites

Before starting, we should create a cluster and install some tools:

Kubernetes cluster

To use the Kubernetes you need to have a cluster. There are plenty of ways to have a Kubernetes cluster:

  • Creating a cluster manually (advanced)
  • Using AWS, Google Cloud, or Digitalocean ready-to-use cluster
  • Using Kubeadm (best one for small projects)
  • Minikube or MicroKube (not for production)

The syntax and commands are the same and you just choose them based on your environment and needs.

The easiest one is installing Minikube and we continue with that. To install Minikube you can follow this document to install it based on your OS.

Kubectl

To connect to the Kubernetes cluster you need to have kubectl. To install it follow this document.

Deploy

Deploy database

Rocket chat uses MongoDB to store data.

After version 1.0 of the Rocket chat, you have to create a replica set of Mongo databases. Making a replica is a good way to have a copy of your data and decrease the data lost and connection problems to the minimum. For more information, you can read this article.

To have a mongo cluster in Kubernetes, we create a Stateful service. First of all, we create a folder to keep all the Kubernetes configuration there:

$ mkdir k8s and cd k8s

To deploy each part (like database, server, redis, …), we need to create a service and deployment config to tell the Kubernetes how should it be. So, we create a file to put the mongo config there:

$ touch mongo.yaml

Now open the file and add these lines to it. These lines define the service which keeps the mongo replicas:

After defining the service, we should define the deployment config to specify the Docker image and other configurations.

Here is the deployment configuration:

You can use `—--` between configurations to split them.

To apply the resources to Kubernetes, run this command:

$ kubectl apply -f mongo.yaml

To see what is happening you can use this command to get the overview information:

$ kubectl get all
Image for post
Image for post

Or you can open the Dashboard using this command and manage them using it:

$ minikube dashboard

With this config, we have 3 replicas which one of them will be the primary database and 2 of them are the mirrors of the primary database.

But still, there are not configured in that way and they are just 3 mongo databases with no relation between them. To start connecting, we should execute one of them to make it the primary database:

$ kubectl exec -it rocketmongo-0 mongo

To init replica set, run this function in the mongo shell

rs.initiate()

The response should be like this:

{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "rocketmongo-0:27017",
"ok" : 1,
"operationTime" : Timestamp(1609450853, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1609450853, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

Now we should set this mongo to be the primary one. So, run this code:

> var config = rs.conf()
> config.members[0].host="rocketmongo-0.rocketmongo:27017"
> rs.reconfig(config)

And the response should be like this:

{
"ok" : 1,
"operationTime" : Timestamp(1609451525, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1609451525, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

Now, we add the other databases as replicas using this command:

> rs.add("rocketmongo-1.rocketmongo:27017")
> rs.add("rocketmongo-2.rocketmongo:27017")

The configuration of the replica set is finished and we can check the status using this function:

> rs.status()

As you see in the response, there are 3 databases connected and one of them is primary.

Deploy Rocket chat

Configuring rocket chat is very easier than the mongo configuration. Like mongo, we create a file to put the config in there:

$ touch rocketchat.yaml

And add these line to define the service:

Because we want to access the rocket chat from outside of the cluster, we define the service network as a LoadBalancer.

Now add these lines to define the deployment configuration:

As mentioned in the rocket chat documentation, the replica database name should be “local”.

Now apply the configuration to the Kubernetes:

$ kubectl apply -f rocketchat.yaml

It takes a little time to set up for the first time. You can check the services and pods status using the Dashboard or this command:

$ kubectl get all
Image for post
Image for post

As you see in the services status, because we use Minikube as the Kubernetes cluster, the external IP of the rocketchat-service is pending. To be able to access the rocket chat we can use this command to create a tunnel to the service:

$ minikube service rocketchat-server

Now open the browser and enter the address generated using the previous command:

Image for post
Image for post

You should see the welcome page.

Image for post
Image for post

I hope this article has helped you to deploy your RocketChat server.

Go hard or go home

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