DEV Community

Joseph D. Marhee
Joseph D. Marhee

Posted on • Originally published at Medium on

Running Rocket.Chat and MongoDB on Kubernetes with StatefulSets

Rocket Chat can be run on-premise, with a pretty lean set of requirements; the application just needs a MongoDB database to get up and running, and this is particularly quick if you make use of their Docker images for the app. I planned to run a pretty active instance for a community project I recently began working on, and decided Kubernetes would make a quick, elastic way to run the server, and this turned out to be an excellent use for the StatefulSet resource.

Creating a MongoDB replica set was pretty straightforward — the StatefulSet allows you to use consistent naming, among other things, which proves useful here for your Rocket Chat server Deployment which I’ll cover in a moment.

Create a Service first for rocketchat-mongo-service along with the StatefulSet:

---
kind: Service
apiVersion: v1
metadata:
  name: rocketchat-mongo-service
spec:
  selector:
    app: rocketchat-mongo
  ports:
  - protocol: TCP
    port: 27017
    targetPort: 27017

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rocketchat-mongo
spec:
  selector:
    matchLabels:
      app: rocketchat-mongo 
  serviceName: "rocketchat-mongo"
  replicas: 3
  template:
    metadata:
      labels:
        app: rocketchat-mongo 
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: mongodb
        image: mongo:3.2
        ports:
        - containerPort: 27017
          name: web
        command: ["mongod"]
        args: ["--replSet","rs0","--smallfiles","--oplogSize","128","--storageEngine=mmapv1"]
        volumeMounts:
        - name: mongo-persistent-storage
          mountPath: /data/db
  volumeClaimTemplates:
  - name: mongo-persistent-storage
    annotations:
      volume.beta.kubernetes.io/storage-class: "fast"
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 100Gi

So, this creates a replicaSet and DNS entries for rocketchat-mongo-service-{0,1,2}.default and for your Deployment, you’ll want to make use of the connection string for the Replica Set:

mongodb://rocketchat-mongo-service-0.default,rocketchat-mongo-service-1.default,rocketchat-mongo-service-2.mongo:27017

and use this in your Deployment:

---
kind: Service
apiVersion: v1
metadata:
  name: rocketchat-server-service
spec:
  selector:
    app: rocketchat-server
  ports:
  - protocol: TCP
    port: 33000
    targetPort: 3000
  type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rocketchat-server-deployment
  labels:
    app: rocketchat-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rocketchat-server
  template:
    metadata:
      labels:
        app: rocketchat-server
    spec:
      containers:
      - name: rocketchat-server
        image: rocketchat/rocket.chat:latest
        env:
          - name: PORT
            value: "3000"
          - name: ROOT_URL
            value: "[http://localhost:3000](http://localhost:3000)"
          - name: MONGO_URL
            value: "mongodb://rocketchat-mongo-service-0.default,rocketchat-mongo-service-1.default,rocketchat-mongo-service-2.mongo:27017/rocketchat" 
          - name: MONGO_OPLOG_URL
            value: "mongodb://rocketchat-mongo-service-0.default,rocketchat-mongo-service-1.default,rocketchat-mongo-service-2.mongo:27017/local"
        ports:
        - containerPort: 3000

which consumes that replica set connection string for MONGO_URL and MONGO_OPLOG_URL .

Keep in mind that the Deployment will rely on the MongoDB StatefulSet, so run these in that order:

kubectl create -f rocketchat-mongodb.yaml

and once the output from:

kubectl get pods -l app=rocketchat-mongo

indicates 3/3 pods are Running , proceed to apply the rocketchat-server deployment, and once applied, you can access your Rocket Chat UI at http://$LB_ADDRESS:$SERVICE_PORT .

More about Rocket Chat’s Docker image can be found in their documentation for a Docker Compose-based deployment:

https://rocket.chat/docs/installation/docker-containers/docker-compose/

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.