Deploy and Scale RethinkDB easily with Docker and Kontena

RethinkDB is an open source, NoSQL, distributed document-oriented database. It stores JSON documents with dynamic schemas, and is designed to facilitate pushing real-time updates for query results to applications. RethinkDB’s real-time push architecture dramatically reduces the time and effort necessary to build scalable real-time apps.

Kontena provides an easy solution to run and scale containerized applications all batteries included. Thus deploying RethinkDB with Kontena is super easy and simple. Next we will show how to deploy a RethinkDB cluster and scale it up.

After we have setup Kontena, we can define RethinkDB in kontena.yml. This is very similar to how we can run RethinkDB locally with Docker Compose, but now we will deploy it to remote host nodes:

version: '2'  
services:  
  db:
    image: rethinkdb:latest
    command: rethinkdb --bind all --join ${project}-db-1.kontena.local:29015
    stateful: true

However, at this time we are overriding the default command from Docker image and defining all database instances to join the cluster's first instance: command: rethinkdb --bind all --join ${project}-db-1.kontena.local:29015

Also with Kontena we can define the service as stateful, then data are persisted to the host node automatically. Also notice that we don’t publish any ports publicly because all the services within the grid can connect to the database with Kontena’s internal DNS address.

As you can see the configuration is very simple and we can deploy a RethinkDB instance:

$ kontena app deploy db

After the deployment is finished we can open the RethinkDB’s admin UI with Kontena’s built in VPN Access:

Now we have one RethinkDB instance up and running. To scale the database up we just fire the $ kontena app scale db 3 command. Since we defined the --join option in kontena.yml all the new database instances will join the cluster automatically.

Now we have all three RethinkDB instances up so we can connect our application to the database. Our example application is a simple Ruby Sinatra application that is using nobrainer gem for easy RethinkDB access:

app:  
  image: kontena/todo-rethinkdb-example:latest
  instances: 3
  environment:
    - RETHINKDB_URL=rethinkdb://${project}-db/todo_production
    - KONTENA_LB_VIRTUAL_PATH=/
    - KONTENA_LB_INTERNAL_PORT=9292
  command: bundle exec puma -p 9292 -e production
  deploy:
    wait_for_port: 9292
  links:
    - loadbalancer

 loadbalancer:
   image: kontena/lb:latest
   deploy:
     strategy: daemon
   ports:
     - 80:80

We can pass the RethinkDB address via environment variables to our application. In this case we are also using the Kontena Load Balancer to route traffic between application instances.

We can deploy these services with $ kontena app deploy loadbalancer app. (In this example we are doing deployments separately for the DB and app services, but normally they are deployed in one fell swoop $ kontena app deploy).

After the deployment is finished we can open the web application in a browser and start using it:

We can configure sharding and replication for the RethinkDB table in the RethinkDB Admin UI:

As we can see RethinkDB is very easy setup with the official Docker image. Kontena is a perfect fit for it by providing easy deployment and a scaling mechanism, and as a bonus it offers the secure Admin UI access.

About Kontena

Kontena, Inc. is the creator of Kontena, an open source, developer-friendly container and microservices platform. Kontena is built to maximize developer happiness by simplifying running containerized applications on any infrastructure: on-premises, cloud or hybrid. It provides a complete solution for organizations of any size. Founded in March 2015, Kontena was recognized as one of the best new open source projects in the 8th annual Black Duck Open Source Rookies of the Year Awards. For more information, visit: www.kontena.io

Image Credits: Container by Klaus Friese