fbpixel

MongoDB

Davide Conti}
Davide Conti
Share:

Deploy a MongoDB database!

In this post, we’re going to deploy a MongoDB database on Beekube.

How to Deploy MongoDB

Beekube is a managed container platform, that run on top of the cloud computing power provided by AWS and Google Cloud. Basically, it is docker in the clouds :). For this tutorial we’ll deploy the MongoDB image available on docker-hub.

Create a network

A network isolate containers. Containers within the same network can connect each other using a service discovery system based on their names.

beekube network create mongodb

Create a persistent volume

MongoDB persist all its data and configuration in the /data/db folder. We want that folder to be persisted - we don’t want to loose the configuration if the container is restarted!

Create a persistent volume of 20 gb with this command:

beekube volume create mongodb --size 20

Run MongoDB!

Now we can run the service. We need to:

  • expose it to the internet: -p 27017:27017, this is the default MongoDB port
  • mount the volume: -v mongodb:/data/db
  • give to it a name and the network: --name mongodb --network mongodb

So that’s the run command:

beekube run --name mongodb --network mongodb -p 27017:27017 -v mongodb:/data/db mongo

Try it

MongoDB is now up and running on the container. Beekube automatically maps a FQDN fur us, that we can retrieve with beekube ps:

name     image  status   publish
-------  -----  -------  -------------------------------------------------------------------------------------
mongodb  mongo  running  mongodb.eu-central-1.<ns>.v1.beekube.cloud:27017 -> 27017

The easiest way to connect and test your database is connecting to it using the mongo shell. Follow the MongoDB documentation to install the shell.

Now let’s insert and read a document on the database. First, connect to the database:

mongo mongodb.eu-central-1.<ns>.v1.beekube.cloud

MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases. The default database is named “test”, let’s write (and read) a record inside a collection called “myCollection” in it:

db.myCollection.insertOne({hello: "world"});
db.myCollection.find();

Note that an explanation about how MongoDB works is out of the scope of this article. If you want to learn more about MongoDB, please refer to the Official MongoDB Documentation.

That’s it!

Bonus

You can check that the data are persisted on the volume stopping the container and running it again:

Let’s stop the container:

beekube stop mongodb

You can check that the container is stopped running beekube ps:

name     image  status   publish
-------  -----  -------  -------------------------------------------------------------------------------------
mongodb  mongo  stopped  mongodb.eu-central-1.<ns>.v1.beekube.cloud:27017 -> 27017

Now start the container again:

beekube start mongodb

Make sure it’s running with beekube ps:

name     image  status   publish
-------  -----  -------  -------------------------------------------------------------------------------------
mongodb  mongo  running  mongodb.eu-central-1.<ns>.v1.beekube.cloud:27017 -> 27017

Now connect again to the database and check that the previously saved records are still there:

mongo mongodb.eu-central-1.<ns>.v1.beekube.cloud
db.myCollection.find();

Junior Cloud Developer @ BeeKube

Computer Engineer, one year experience as full stack web developer. Currently involved in cloud computing area.

"K.I.S.S."