No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
kentaro Merge pull request #8 from Songmu/fix/hostname-match
fix hostname matching. It was only backward matching.
Latest commit 4855939 Aug 14, 2014
Failed to load latest commit information.
.gitignore Removed unnecessary file Oct 29, 2013 Fix typo Oct 31, 2013 fix hostname matching. It was only backward matching. Aug 14, 2014

Automatic /etc/hosts management with Serf


In the cloud world, many hosts appear and vanish. Since we don't want to bother to manage some internal DNS service while keeping its availability, we have been updating /etc/hosts file periodically with cron and AWS API.

There are, however, several problems in that way of updating /etc/hosts:

  1. It's far from real time
  2. There are many other components that need to be updated in a likely way; for example, nagios, munin, etc.

It's laborious that we have to write scripts for each purposes and edit crontab.


Serf can solve the problem. It provides us decentralized hosts discovery solution. Once we launch serf agents in each hosts, the cluster itself works like it is managed completely. In addition, it's almost real time.


For Mac OS X uses, please check this document to circumvent a problem in OS X at first.

Launch the First Node

Launch a serf agent named node1:

$ serf agent -node=node1 -bind= -rpc-addr=

Then you'll see etc/hosts to be like below:

$ cat etc/hosts      node1

Launch the Second Node

Launch another node named node2:

$ serf agent -node=node2 -bind= -rpc-addr=

Then add node2 into the cluster that currently consists of only node1:

$ serf join -rpc-addr=

As a result,

  1. node2 is now also a member of the cluster
  2. member-join event is emitted
  3. node2 is added into etc/hosts
$ cat etc/hosts      node1      node2

Remove the Second Node from the Cluster

Push Ctrl-C to stop node2. Then member-leave event is propagated to node1 and the handler script is fired.

As a result,

  1. node2 is now not a member of the cluster
  2. member-leave event is emitted
  3. node2 is removed from etc/hosts
$ cat etc/hosts      node1

See Also