I have recently been giving CoreOS a spin for HP's Advanced Technology Group and so for this blog post I'm going to run through how to setup a basic CoreOS cluster on a Mac. These instructions should actually be very similar to how you would do it in Linux too.
There are a few things you need to install before you can get things going:
- VirtualBox for the CoreOS virtual machines to sit on
- Vagrant to spin-up and configure the virtual machines
- Git to grab the vagrant files for CoreOS
- Homebrew to install 'fleetctl' (also generally really useful on a developer machine)
You then need to install 'fleetctl' which will be used to spin up services on the cluster:
$ brew update $ brew install fleetctl
Once everything is installed the git repository for the vagrant bootstrap is needed:
$ git clone git://github.com/coreos/coreos-vagrant/ $ cd coreos-vagrant/ $ cp config.rb.sample config.rb $ cp user-data.sample user-data
The last two commands here copy the sample configuration files to configuration files we will use for Vagrant.
Edit the config.rb file and look for the line:
Uncomment this and set the number to '3'. When I was testing I also enabled the 'beta' channel in this file instead of the default 'alpha' channel. This is entirely up to personal preference and both will work with these instructions at time of writing.
At this point everything is configured to spin-up the CoreOS cluster. From the 'coreos-vagrant' directory run the following:
$ vagrant up
This may take a few minutes as it downloads the CoreOS base image and creates the virtual machines required. It will also configure Etcd and setup SSH keys so that you can communicate with the virtual machines.
You can SSH into any of these virtual machines using the following and substituting '01' with the CoreOS instance you wish to connect to:
$ vagrant ssh core-01 -- -A
Once you have everything up and running you can use 'fleet' to send systemd configurations to the cluster. Fleet will schedule which CoreOS machine the configuration file will run on. Fleet internally uses Etcd to discover all the nodes of your CoreOS cluster.
First of all we need to tell 'fleetctl' on the Mac how to talk to the CoreOS cluster. This requires two things, the IP/port and the SSH key:
$ export FLEETCTL_TUNNEL=127.0.0.1:2222 $ ssh-add ~/.vagrant.d/insecure_private_key
For this example I'm going to spin up three memcached docker instances inside CoreOS, I'll let Fleet figure out where to put them only telling it that two memcached instances cannot run on the same CoreOS machine. To do this create three identical files, 'memcached.1.service', 'memcached.2.service' and 'memcached.3.service' as follows:
[Unit] Description=Memcached server After=docker.service [Service] ExecStart=/usr/bin/docker run -p 11211:11211 fedora/memcached [X-Fleet] X-Conflicts=memcached*
This is a very simple systemd based script which will tell Docker in CoreOS to get a container of a Fedora based memcached and run it. The 'X-Conflicts' option tells Fleet that two memcached systemd configurations cannot run on the same CoreOS instance.
To run these we need to do:
$ fleetctl start memcached.1.service $ fleetctl start memcached.2.service $ fleetctl start memcached.3.service
This will take a while and show as started whilst it downloads the docker container and executes it. Progress can be seen with the following commands:
$ fleetctl list-units $ fleetctl journal memcached.1.service
Once complete your three CoreOS machines should all be listening on port 11211 to a Docker installed memcached.