Run containers using Apache Mesos, Marathon, Zookeeper and Docker

Ahmad J. Hamad
5 min readMay 17, 2021

I don’t remember exactly when and where, but few years ago, when I first heard about Apache Mesos, I thought that it was another project by Apache that deals with Big data and it is part of the Hadoop ecosystem. I didn’t pay attention at that time, and I didn’t know what it was and why I — some day — would want to use Apache Mesos.

Recently, I have started learning Kubernetes(Oh, yes, you know it?) and we must all admit it that Google has built a state of the art orchestration tool where you can run and scale your Microservices. But wait, what does this have to do with Apache Mesos? well, Kubernetes is used to run containers, Systemd-nspawn can be used to run containers and Apache Mesos(yes, you guessed it) can run containers as well.

What’s Apache Mesos?

Apache Mesos is an open source project to manage computer clusters.

Well, is that enough to run containers?, answer is no, you should use Marathon to run containers on Mesos.

Let’s have a closer look at Apache Mesos and Marathon and try to understand how they work with each other to run and operate Docker containers.

Mesos Architecture

The above figure shows the main components of Apache Mesos:

  • Master daemons: run as a highly available cluster using Zookeeper.
  • Agent daemons: run on each node of the cluster.
  • Frameworks: consists of two main components, a scheduler and an executor.

But wait, I don’t see Marathon. Hmm, Marathon is a framework that runs on top of Mesos that helps you to manage and run Docker containers.

Enough theory?. Roll up your sleeves and get your hands dirty.

In this tutorial, I’ll show you how to setup and configure Apache Mesos along with the other components on a single machine cluster that runs Ubuntu server 18.04 to run Docker containers.

I won’t cover the steps of “how to install and run Ubuntu on Virtual box” but the only thing that I need to mention here is that you should create a virtual machine with at least 8GB of RAM and 20GB of disk space, otherwise you will face some troubles when compiling Apache Mesos.
Once you are done with the installation, make sure that that you set the network mode of your network adapter to Bridge so it can be accessed from your host machine.

Now, ssh to your virtual machine and start installing the required software packages to compile and run Apache Mesos.

$ sudo apt-get update
$ sudo apt-get install -y tar wget git openjdk-8-jdk autoconf libtool build-essential python-dev python-six python-virtualenv libcurl4-nss-dev libsasl2-dev libsasl2-modules maven libapr1-dev libsvn-dev zlib1g-dev iputils-ping

Also, you should install Docker on your machine, so please follow the instructions here

Next, you need to download Apache Mesos and compile it

$ wget https://downloads.apache.org/mesos/1.11.0/mesos-1.11.0.tar.gz
$ tar xzvf mesos-1.11.0.tar.gz
$ cd mesos-1.11.0
$ mkdir build
$ ../configure
$ make

If you face issues during the compilation, make sure that you have enough memory and disk space in your machine.

Now, let’s download and configure Apache Zookeeper but before we do so, I want to mention something here, since we are building a single machine cluster, Zookeeper is not needed, you can operate the setup without it . Zookeeper is only needed when building a highly available cluster, but anyways, let’s just configure and run it.

$ wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
$ tar xzvf apache-zookeeper-3.7.0-bin.tar.gz
$ cd apache-zookeeper-3.7.0-bin/conf
$ cp zoo_sample.cfg zoo.cfg
$ cd ../bin
$ ./zkServer.sh start

Now, Zookpeer is up and running on your machine, you can connect to it using the zkCli.sh from the bin directory.

$ ./zkCli.sh

The Zookeeper prompt should come and you need to create the required configuration directories, you have to create two directories, one for Apache Mesos and another for Zookeeper

[zk: localhost:2181(CONNECTED) 0] create /mesos
[zk: localhost:2181(CONNECTED) 0] create /marathon

We have created two directories, you can confirm the creation of these directories by running the following

[zk: localhost:2181(CONNECTED) 0] ls /

This will show the following

[marathon, mesos, zookeeper]

Zookeeper is running now, we can operate both, the master and the agent of Apache Mesos.
Let’s start the master first.

$ mkdir -pv /var/lib/mesos
$ cd ~/mesos-1.11.0/build/bin
$ sudo ./mesos-master.sh -h --ip=<your_virtual_machine_ip_address> --work_dir=/var/lib/mesos --zk=zk://127.0.0.1:2181,/mesos --quorum=1

The master is now up and running and connected to Zookeeper, you can confirm that by connecting again to Zookeeper cli and list the configuration under /mesos

[zk: localhost:2181(CONNECTED) 0] stat /mesos

if you see something similar to the below, it means that the master is up and running and it’s connected to Zookeeper

cZxid = 0x2
ctime = Sat May 15 18:16:20 UTC 2021
mZxid = 0x2
mtime = Sat May 15 18:16:20 UTC 2021
pZxid = 0x9b1
cversion = 14
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 2

Let’s now run the agent of Apache Mesos

$ sudo ./mesos-agent.sh --master=zk://127.0.0.1:2181,/mesos --work_dir=/var/lib/mesos --image_providers=docker --containerizers=docker,mesos --isolation=filesystem/linux,docker/runtime

By now, you have both the master and the agent running and connected to Zookeeper.

Let’s download Marathon and configure it

$ wget http://downloads.mesosphere.com/marathon/v1.5.1/marathon-1.5.1.tgz
$ tar xzvf marathon-1.5.1.tgz
$ sudo MESOS_NATIVE_JAVA_LIBRARY=~/mesos-1.11.0/build/src/.libs/libmesos.so ./marathon --master=zk://127.0.0.1:2181/mesos --zk zk://127.0.0.1:2181/marathon1 --http_port=8090 --http_address=<your_virtual_machine_ip_address>

Mesos should boot now and it can be accessed using a web browser on port 8090.
Before we run our first Docker container using Marathon, Apache Mesos comes with a handy tool called mesos, which can you help you testing the and the configuration of your cluster.

$ cd ~/mesos-1.11.0/build/bin
$ sudo ./mesos.sh execute --docker_image=library/redis --master=<your_virtual_machine_ip_address>:5050 --name=testcontainer --shell=false

Now, we are ready to run our first container on Apache Mesos and Marathon, open your browser and navigate to the following url:
http://<your_virtual_machine_ip_address>:8090

You should be able to see the web interface of Marathon, click on Applications -> Create application, the creation dialog should now appear, so provide an Id and an image, so in my case, I’m using nginx for both the Id and the image.
Your cluster should now download and run the nginx image.

To confirm that all is working as expected, you can see that in the dashboard when the status becomes “Running”, also, this can be confirmed by listing the running the Docker containers on your machine.

$  sudo docker ps

You should now something similar to the below:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
6f58577b97ff nginx "/docker-entrypoint.…" 17 minutes ago Up 17 minutes mesos-3a3a1736-c246-43e7-a647-857c8e852f4a

And that’s it, happy hacking.

--

--