Docker Containers and Apache Mesos

Docker Containers and Apache Mesos


In the post Apache Mesos on Apache CloudStack we setup an Apache Mesos cluster with Marathon on the master nodes and Docker on the slave nodes. Now lets see how to deploy Docker containers using Mesos and Marathon. Marathon is basically used to schedule the docker containers on Mesos. This is done by sending a “POST” to the marathon api.

The key to this working is that the slaves must have Docker running on them and they must be configured to use Docker as a containerizer by Mesos.

Here is an example json file for the docker container.

{
    "cmd": "env && python3 -m http.server $PORT0",
    "container": {
        "docker": {
            "image": "python:3"
        },
        "type": "DOCKER"
    },
    "cpus": 0.25,
    "healthChecks": [
        {
            "gracePeriodSeconds": 3,
            "intervalSeconds": 10,
            "maxConsecutiveFailures": 3,
            "path": "/",
            "portIndex": 0,
            "protocol": "HTTP",
            "timeoutSeconds": 5
        }
    ],
    "id": "python-app",
    "instances": 2,
    "mem": 50,
    "ports": [
        0
    ],
    "upgradeStrategy": {
        "minimumHealthCapacity": 0.5
    }
}

Paste this into a file named “pythonweb.json“.

Now lets “POST” this file to Marathon.

curl -X POST http://<marathon>:8080/v2/apps -d @pythonweb.json -H "Content-type: application/json"

You should see an output similar to this.

{"id":"/python-app","cmd":"env && python3 -m http.server $PORT0","args":null,"user":null,"env":{},"instances":2,"cpus":0.25,"mem":50.0,"disk":0.0,"executor":"","constraints":[],"uris":[],"storeUrls":[],"ports":[0],"requirePorts":false,"backoffFactor":1.15,"container":{"type":"DOCKER","volumes":[],"docker":{"image":"python:3","network":null,"portMappings":null,"privileged":false,"parameters":[],"forcePullImage":false}},"healthChecks":[{"path":"/","protocol":"HTTP","portIndex":0,"command":null,"gracePeriodSeconds":3,"intervalSeconds":10,"timeoutSeconds":5,"maxConsecutiveFailures":3,"ignoreHttp1xx":false}],"dependencies":[],"upgradeStrategy":{"minimumHealthCapacity":0.5,"maximumOverCapacity":1.0},"labels":{},"acceptedResourceRoles":null,"version":"2015-08-25T03:17:34.470Z","deployments":[{"id":"5555dc96-514f-4621-a73f-ba7c2e93c1cf"}],"tasks":[],"tasksStaged":0,"tasksRunning":0,"tasksHealthy":0,"tasksUnhealthy":0,"backoffSeconds":1,"maxLaunchDelaySeconds":3600}

Now if we check the Marathon UI the application should show up with two containers deployed and a healthy status.

screenshot-2015-08-24-23.19.04

 

And you can also see the configuration which were set in the json file.

screenshot-2015-08-24-23.21.59

 

If you click the “Scale” button you can change the number of instances from “2” to “10” and if you have the resources available the containers will be deployed. Mesos works on best effort so if there are no resources the tasks will wait and retry until you change the scale quantity. And if a mesos slave or containers die, Mesos will automatically try to satisfy the application configuration providing high availability for the containers.

 

Now lets make scaling changes from the command line. To do changes we use curl but this time with “PUT“.

curl -X PUT http://192.168.2.184:8080/v2/apps/python-app -d '{ "instances": 5}' -H "Content-type: application/json"

One more thing is that Marathon also creates versions of you application. You’ll find the “older versions” under “Configuration” at the bottom.

screenshot-2015-08-24-23.26.32

 

By clicking the “Apply these settings” button you can revert back to that particular version.