Blog Getting started with packages in DC/OS

Why would one create a package?

Once you get familiar with DC/OS, the open source project that was created by Mesosphere you get access to packages certified by Mesosphere. There are several ways to deploy your service into DC/OS: (1) use dcos marathon command in CLI; (2) use Marathon REST API directly; (3) deploy your service as a package. Using package approach makes your solution consistent with the environment and gives other benefits.

Please refer to documentation regarding developing your custom services and packages https://github.com/dcos/dcos-docs/blob/master/1.7/usage/developing-services/service-requirements-spec.md.

The aim of this post is to show how to create your own simple package and start using it in your DC/OS managed cluster.

Prerequisites

You need to have DC/OS cluster up and running and DC/OS CLI installed. You can find instructions how to do this here https://dcos.io/installing.

Redis as a Package

As an example here I will create a package for vanilla Redis. It is a standalone instance that can be used by your solution. Note that there is Redis framework already in the list of DC/OS packages. While being extremely powerful this framework is not required for every solution. Sometimes all we need is a simple and lightweight service running as container.

The service you want to create a package for should have a Docker image already. For this example there is an image called redis:3.0.7.

Download this image and get it’s id

docker pull redis:3.0.7
docker images redis

Getting started

First thing to do is to take a look at the universe repo https://github.com/mesosphere/universe and create a fork. Create a branch in your fork and name it topic/single-redis.

Find repo/packages/R folder and create redis folder inside. Create 0 folder in that repo/packages/R/redis folder. Create empty config.json, marathon.json.mustache, package.json and resource.json files.

You should have the following tree:

tree repo/packages/R/
repo/packages/R/
├── redis
│   └── 0
│       ├── config.json
│       ├── marathon.json.mustache
│       ├── package.json
│       └── resource.json
└── riak
    └── 0
        ├── command.json
        ├── config.json
        ├── marathon.json.mustache
        ├── package.json
        └── resource.json

Define your package.

Now it is time to actually define your package. Here are these four files we created before:

config.json

{
 "type": "object"
}

marahon.json.mustache

{
 "id": "redis",
 "instances": 1,
 "cpus": 0.1,
 "mem": 300,
 "container": {
   "type": "DOCKER",
   "docker": {
     "image": "{{resource.assets.container.docker.8d81cd6f6c5e}}",
     "network": "BRIDGE",
     "portMappings": [
       {
       "containerPort": 6379,
       "servicePort" : 0,
       "hostPort": 0,
       "protocol": "tcp"
       }
     ]
   }
 },
 "healthChecks": [
   {
   "protocol": "TCP",
   "gracePeriodSeconds": 60,
   "intervalSeconds": 5,
   "portIndex": 0,
   "timeoutSeconds": 5,
   "maxConsecutiveFailures": 3
   }
 ]
}

package.json

{
 "packagingVersion": "2.0",
 "name": "redis",
 "version": "0.0.1",
 "scm": "https://github.com/antirez/redis",
 "maintainer": "your@email.com",
 "description": "Just Redis",
 "framework": false,
 "tags": ["mesosphere", "service", "redis"],
 "licenses": [
   {
     "name": "BSD",
     "url": "http://redis.io/topics/license"
   }
 ],
 "website": "http://redis.io",
 "postInstallNotes": "Redis installed!",
 "preInstallNotes": "Read this message before installing please",
 "postUninstallNotes": "Thank you for using Redis"
}

resource.json

{
 "images": {
   "icon-small": "http://redis.io/images/redis-white.png",
   "icon-medium": "http://redis.io/images/redis-white.png",
   "icon-large": "http://redis.io/images/redis-white.png",
   "screenshots": [
     "http://redis.io/images/redis-white.png"
   ]
 },
 "assets": {
   "container": {
     "docker": {
       "8d81cd6f6c5e": "redis:3.0.7"
     }
   }
 }
}

Before committing files make sure you enabled pre commit hooks as described here https://github.com/mesosphere/universe#development-set-up.

When it is done please try to commit your package. This will lead to changes in repo/meta/index.json so you need to add it and commit your package with this json file. Push your changes to the remote.

Start using your package

Having your package published you need to add it to your DC/OS.

Use this command:

dcos package repo add Redis-Development https://github.com/<username>/universe/archive/topic/single-redis.zip

Make sure you can download this zip archive (basically the content of branch from your fork).

Now you can just run dcos package install redis. It will ask confirmation and print messages that we configured. After that your redis instance will appear in Marathon and be ready to work.

Picture1

As you’ve seen in this post DC/OS makes work with containers easier and you can start running your service at scale in minutes.


Subscribe to our news


Leave a Reply