Life is Short Do More

How to setup Mesos, Marathon, Zookeeper and Docker

Mesos

  • is open source cluster manager, It provides resource isolation and sharing across distributed applications.
  • Mesos consists of a master daemon that manages slave daemons running on each cluster node. Mesos frameworks are applications that run on Mesos and run tasks on these slaves
  • Mesos uses a two-level scheduling mechanism where resource offers are made to frameworks. The Mesos master node decides how many resources to offer each framework, while each framework determines the resources it accepts and what application to execute on those resources. For more insight refer this: Mesos Architecture and Overview

Marathon

Zookeeper

  • ZooKeeper is a distributed co-ordination service to manage large set of hosts. Co-ordinating and managing a service in a distributed environment is solved by ZooKeeper with its simple architecture and API. 
  • If the Mesos master is unavailable, existing tasks can continue to execute, but new resources cannot be allocated and new tasks cannot be launched. To reduce the chance of this situation occurring, Mesos has a high-availability mode that uses multiple Mesos masters: one active master (called the leader or leading master) and several backups in case it fails. The masters elect the leader, with Apache ZooKeeper both coordinating the election and handling leader detection by masters, agents, and scheduler drivers. For more insight refer this: Zookeeper Architecture and Overview

Master Nodes Setup

  1. Package Installation
    • Highly-available clusters will typically have multiple master nodes and any number of slave nodes. Each master node runs Apache Mesos, Marathon and ZooKeeper (to provide leader  election).
    • Setup repositories:
      • The easiest way to install Mesos is via the GitHub repositories.Mesosphere has official package repositories which connect directly to the native package management tools of your favorite Linux distribution — namely apt-get and yum — to install Mesos on top of the most common Linux distributions.
        1. Commands for Ubuntu
        • Setup
          sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E56151BF DISTRO=$(lsb_release -is | tr '[:upper:]' '[:lower:]') CODENAME=$(lsb_release -cs)
        • Add the repository
          echo "deb https://repos.mesosphere.com/${DISTRO} ${CODENAME} main" | sudo tee /etc/apt/sources.list.d/mesosphere.list sudo apt-get -y update
        • Install
          sudo apt-get -y install mesos marathon
  2. Configuration
    • The Mesosphere package ships with a default configuration. The default configuration includes:
    1. File: /etc/default/mesos
      sets master and slave log dir to /var/log/mesos.
    2. File: /etc/default/mesos-master
      sets port to 5050.
      sets zk to the value in the file /etc/mesos/zk.
    3. File: /etc/default/mesos-slave
      sets master as the value of /etc/mesos/zk
    4. File: /etc/mesos/zk
      sets the ZooKeeper instance to zk://localhost:2181/mesos
    5. File: /etc/mesos-master/work_dir
      sets working_dir to var/lib/mesos
    6. File: /etc/mesos-master/quorum
      sets quorum to 1
      This default configuration allows Mesos to start immediately on a single node.
      • Configure Zookeeper:
    7. Setting ID
      Set /etc/zookeeper/conf/myid to a unique integer between 1 and 255 on each node.The Mesosphere package ships with a default configuration
    8. Server Addresses
      Append the following values to /etc/zookeeper/conf/zoo.cfg on each node, replacing the IP addresses with your own:
      server.1=1.1.1.1:2888:3888
      server.2=2.2.2.2:2888:3888
      server.3=3.3.3.3:2888:3888
    9. Start Zookeeper
      sudo service zookeeper restart
      • Mesos & Marathon
    10. ZooKeeper
      On each node, replacing the IP addresses below with each master’s IP address, set/etc/mesos/zk to:
      zk://1.1.1.1:2181,2.2.2.2:2181,3.3.3.3:2181/mesos
    11. Quorum
      Set /etc/mesos-master/quorum on each master node to a number greater than the number of masters divided by 2. For example, the optimal quorum size for a five node master cluster would be 3. In this case, there are three masters and the quorum size should be set to 2 on each node.
    12. Host name
      In case unable to resolve the hostname of the machine directly, set /etc/mesos-master/hostname to a value that you can resolve.
      For example, an externally accessible IP address or DNS hostname. This will ensure all links from the Mesos console work correctly.
      Also, set this property in /etc/marathon/conf/hostname
    13. Disable mesos-slave service
      sudo service mesos-slave stop
      sudo sh -c "echo manual > /etc/init/mesos-slave.override"
  3. Restart Services
    • Bring up Mesos master
      sudo service mesos-master restart
    • Now restart Marathon
      sudo service marathon restart

Slave Nodes Setup

  1. Package Installation
    • Setup repositories:
      1. Commands for Ubuntu
        • Setup
          sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv E56151BFDISTRO=$(lsb_release -is | tr '[:upper:]' '[:lower:]') CODENAME=$(lsb_release -cs)
        •  Add the repository
          echo "deb https://repos.mesosphere.com/${DISTRO} ${CODENAME} main" | sudo tee /etc/apt/sources.list.d/mesosphere.list
          sudo apt-get -y update
        • Install
          sudo apt-get -y install mesos
  2. Configuration
    • Disable Zookeeper:
    1. If you’re using the Debian or Ubuntu package, ZooKeeper will be pulled in and installed as a dependency automaticallyi.
      sudo service zookeeper stop
      sudo sh -c "echo manual > /etc/init/zookeeper.override"
      • Mesos
        1. ZooKeeper
          On each node, replacing the IP addresses below with each master’s IP address, set/etc/mesos/zk to:
          zk://1.1.1.1:2181,2.2.2.2:2181,3.3.3.3:2181/mesos
        2. Disable mesos-master service
          sudo service mesos-master stop
          sudo sh -c "echo manual > /etc/init/mesos-master.override"
  3. Start Services
    • Restart mesos-slave on each node to use the new configuration:
      sudo service mesos-slave restart
  4. Verify Installation
    • If the packages were installed and configured correctly, you should be able to access the Mesos console at https://<master-ip>:5050 and the Marathon console at https://<master-ip>:8080 (where  is any of the master IP addresses).

Docker

  • Docker is an open platform for developing, shipping, and running applications. Docker is designed to deliver your applications faster. With Docker you can separate your applications from  your infrastructure and treat your infrastructure like a managed application.
  • Docker helps you ship code faster, test faster, deploy faster, and shorten the cycle between writing code and running code. For more insight refer this: Docker Architecture and Overview
  • These instructions are intended for installing Docker on Ubuntu. Docker’s APT repository contains Docker 1.7.1 and higher. To set APT to use packages from the new repository.

Docker Installation

  1. Update your apt sources
    • Log into your machine as a user with sudo or root privileges.
    • Open a terminal window.
    • Update package information, ensure that APT works with the https method, and that CA certificates are installed.
      sudo apt-get update
      sudo apt-get install apt-transport-https ca-certificates
    • Add the new GPG key sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
    • Open the /etc/apt/sources.list.d/docker.list file in your favorite editor.
    • If the file doesn’t exist, create it.
    • Remove any existing entries.
    • Add an entry for your Ubuntu operating system.
      deb https://apt.dockerproject.org/repo ubuntu-trusty main
    • Save and close the /etc/apt/sources.list.d/docker.list file.
    • Update the APT package index.
      sudo apt-get update
    • Purge the old repo if it exists.
      sudo apt-get purge lxc-docker
    • Verify that APT is pulling from the right repository.
      apt-cache policy docker-engine
    • From now on when you run apt-get upgrade, APT pulls from the new repository.
  2. Prerequisites by Ubuntu Version
    • For Ubuntu Trusty, Wily, and Xenial, it’s recommended to install the linux-image-extra kernel package. Thelinux-image-extra package allows you use the aufs storage driver.
      To install the linux-image-extra package for your kernel version:
    1. Open a terminal on your Ubuntu host.
    2. Update your package manager.
      sudo apt-get update
    3. Install the recommended package.
      sudo apt-get install linux-image-extra-$(uname -r)
    4. Go ahead and install Docker.
      If you are installing on Ubuntu 14.04 or 12.04, apparmor is required. You can install it using: apt-get install apparmor.
  3. Install
    • Make sure you have installed the prerequisites for your Ubuntu version.Then, install Docker using the following:
    1. Log into your Ubuntu installation as a user with sudo privileges.
    2. Update your APT package index.
      sudo apt-get update
    3. Install Docker.
      sudo apt-get install docker-engine
    4. Start the docker daemon.
      sudo service docker start
    5. Verify docker is installed correctly.
      sudo docker run hello-world
      This command downloads a test image and runs it in a container. When the container runs, it prints an informational message. Then, it exits.
  4. Create a Docker group
    • The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the rootuser.
      To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by thedocker group.
    • To create the docker group and add your user:
    1. Log into Ubuntu as a user with sudo privileges.
      This procedure assumes you log in as the ubuntu user.
    2. Create the docker group.
      sudo groupadd docker
    3. Add your user to docker group.
      sudo usermod -aG docker ubuntu
    4. Log out and log back in.
      This ensures your user is running with the correct permissions.
    5. Verify your work by running docker without sudo.
      docker run hello-world
    6. If this fails with a message similar to this:
      Cannot connect to the Docker daemon. Is ‘docker daemon’ running on this host?
      Check that the DOCKER_HOST environment variable is not set for your shell. If it is, unset it.
      • Upgrade Docker
    7. To install the latest version of Docker with apt-get:
      sudo apt-get upgrade docker-engine
      • Uninstall Docker
    8. To uninstall the Docker package:
      sudo apt-get purge docker-engine
    9. To uninstall the Docker package and dependencies that are no longer needed:
      sudo apt-get autoremove --purge docker-engine
    10. To delete all images, containers, and volumes or user created configuration files on your host run the following command:
      rm -rf /var/lib/docker

Deploying a container within the Architecture

  1. Deployment with script:
    • Create a small json file which will contain app information to be deployed:
      Eg: definition.json
      {
      "container": {
      "type": "DOCKER”,
      "docker": {
      "image": "superguenter/demo-app”
      }
      },
      "cmd": "python -m SimpleHTTPServer $PORT”,
      "id": "demo”,
      "cpus": 0.01,
      "mem": 256,
      "ports": [3000]
      }
    • Now to deploy the app below is the python code:
      import requests
      import json
      import pprint
      url = "https://localhost:8080/v2/apps"
      res = requests.post( url, data=open('/path/to/json/file/definition.json','rb'), headers={'Content-Type': 'application/json'})
      json_data = json.loads(res.text)
      pp = pprint.PrettyPrinter(indent=4)
      pp.pprint (json_data)
  2. Deployment through dashboard:
    • Login to dashboard Localhost:8080 marathon is running on 8080.
    • Click on the createApplication
    • Enter the ID (ID should be unique), allocate the CPU, memory usuage as required.
    • Now, go to DockerContainer: Provide the image name that you have created.
    • Click on Create Application
    • You should be able to see the image name with status as Running/Delayed/Waiting.
    • Running refers: Success
    • Delayed refers:  Failed
    • Waiting refers:  Waiting to be started
  3. Deployment with  Rest API:
    • Execute the following command in the server. Make sure you have provided the required components correctly in the json file.
      curl -X POST -H "Content-Type: application/json" https://localhost:8080/v2/apps [email protected]