I am sure you have heard of CoreOS, but have you ever actually deployed a real app on it? Not many people have. Building an app on CoreOS can be incredibly hard and frustrating. The documentation is scattered and you have to learn all about different technologies before you even get started (etcd, systemd, and docker). If you are lazy (like me) and want to just try out CoreOS with no fuss, I will guide you through the whole process from start to finish. We will create a simple WordPress application together running with a MySQL service in CoreOS.

Installing the CLI to Control CoreOS

If you are on a Mac, you can install

fleetctl and etcdctl natively to control your CoreOS clusters. Here is how:

$ brew install go etcdctl
$ git clone https://github.com/coreos/fleet.git
$ cd fleet
$ ./build
$ mv bin/fleetctl /usr/local/bin/

Installing a Local CoreOS Cluster

Getting a local clustered CoreOS up and running with Vagrant is pretty easy.

$ git clone https://github.com/CenturyLinkLabs/coreos-vagrant
$ cd coreos-vagrant/cluster
$ vagrant up --provision

Now you have a mini 3-system local CoreOS cluster running on your laptop. Easy enough, now let's check our local fleetctl.

$ fleetctl list-machines
MACHINE         IP          METADATA
09fd0a88...   -
77763947...   -
f31c383c...   -

Awesome. It worked!

How to Deploy an App on Clustered CoreOS using Fleet

Great, now you have a clustered CoreOS. What do you do with it? The fleetctl command lets you deploy your app into a cluster of CoreOS nodes, but writing service files for fleet sucks. Luckily, you don't have to write them yourself. You can use a simple yaml format to generate service files.

 $ sudo gem install bundler fig2coreos

 $ cat fig.yml
   image: ctlc/wordpress
     - 80:80
     DB_USER: root
     DB_PASSWORD: qa1N76pWAri9
     - db
   image: ctlc/mysql
     - 3306:3306
     MYSQL_DATABASE: wordpress

 $ fig2coreos myapp fig.yml coreos-files

 $ cd coreos-files

 $ ls

The fleetctl client tool uses etcd's key/value store to understand the servers it has access to and needs access to the etcd servers running on each machine in the cluster. Here is how to deploy your app into the cluster.

$ fleetctl start db.1.service

$ fleetctl list-units
UNIT            LOAD     ACTIVE  SUB       DESC       MACHINE
db.1.service    loaded   active  running   Run db_1   9c008961.../

$ fleetctl start web.1.service

$ fleetctl list-units
UNIT            LOAD     ACTIVE  SUB       DESC       MACHINE
db.1.service    loaded   active  running   Run db_1   9c008961.../
web.1.service   loaded   active  running   Run web_1  9c008961.../

Now your app is running, but the services are not registered into etcd yet. Luckily, fig2coreos generated the discovery service files as well for us.

$ fleetctl start db-discovery.1.service
$ fleetctl start web-discovery.1.service

$ fleetctl list-units
UNIT                    LOAD    ACTIVE  SUB      DESC            MACHINE
db-discovery.1.service  loaded  active  running  Announce db_1   9c008961.../
db.1.service            loaded  active  running  Run db_1        9c008961.../
web-discovery.1.service loaded  active  running  Announce web_1  9c008961.../
web.1.service           loaded  active  running  Run web_1       9c008961.../

$ etcdctl ls --recursive

$ etcdctl get /services/web/web_1
{ "host": "core-03", "port": 80, "version": "52c7248a14" }

$ etcdctl get /services/db/db_1
{ "host": "core-03", "port": 3306, "version": "52c7248a14" }

You Are Done!

That's it! You're done! In fact, if you are using Vagrant 1.5 with a Vagrant Cloud account, you can actually get to your WordPress app and see it in action.

$ cd ~/coreos-vagrant/cluster/

# find out which box is hosting your port 80
$ etcdctl get /services/web/web_1
{ "host": "core-03", "port": 80, "version": "52c7248a14" }

$ vagrant share core-03 --http 80
==> core-03: Detecting network information for machine...
    core-03: Local machine address:
    core-03: Local HTTP port: 80
    core-03: Local HTTPS port: disabled
==> core-03: Checking authentication and authorization...
==> core-03: Creating Vagrant Share session...
    core-03: Share will be at: quick-iguana-4689
==> core-03: Your Vagrant Share is running! Name: quick-iguana-4689
==> core-03: URL: http://quick-iguana-4689.vagrantshare.com



There is much more you can do with CoreOS, but at least you have now done the basics. If you are planning on leveraging multi-server CoreOS clusters in production, you will need to add ambassador containers to your system. In fact, you can even combine the etcd service discovery with ambassador containers, but we will leave that for another blog post next week.