How to install serf on a multiple node ubuntu cluster

By Bill Ward | June 17, 2017

This post will walk you through setting up a quick Serf cluster on Ubuntu 16.10 servers. Serf is an awsome clustering applicaiton that managers cluster membership, is decentralized, and recovers from downed nodes quickly.

Initial Server Setup

For this tutorial we will configure a three node cluster. If you would like to know more about what serf is then check out HashiCorp’s serf site

You should already have Ubuntu 16.10 on all three servers. For my setup I have the following host names and IP addresses

  • serf-01 [192.168.1.51]
  • serf-02 [192.168.1.65]
  • serf-03 [192.168.1.59]

Make sure you install your updates and reboot. Since serf is decentralized we don’t install a typical server first then clients. All the servers act as a single thing. What we will do is simply install one server first then tell the other servers to join that server. We could tell any new server to join any one of the servers that have already joined it really doesn’t matter. Serf will tell each new server about the other servers and will tell the other servers about the new server. It does this using it’s gossip protocol.

On to actually installing this thing…

Serf Agent Installation

The first thing we have to do is install the Serf agent on all of our servers. Run the following commands on each of your servers.

$ sudo apt install unzip
$ cd ~
$ wget https://releases.hashicorp.com/serf/0.8.1/serf_0.8.1_linux_amd64.zip
$ unzip serf_0.8.1_linux_amd64.zip
$ sudo mv serf /usr/local/bin

You can test the installation by running the below command:

$ serf
usage: serf [--version] [--help] <command> [<args>]

Available commands are:
    agent           Runs a Serf agent
    event           Send a custom event through the Serf cluster
    force-leave     Forces a member of the cluster to enter the "left" state
    info            Provides debugging information for operators
    join            Tell Serf agent to join cluster
    keygen          Generates a new encryption key
    keys            Manipulate the internal encryption keyring used by Serf
    leave           Gracefully leaves the Serf cluster and shuts down
    members         Lists the members of a Serf cluster
    monitor         Stream logs from a Serf agent
    query           Send a query to the Serf cluster
    reachability    Test network reachability
    rtt             Estimates network round trip time between nodes
    tags            Modify tags of a running Serf agent
    version         Prints the Serf version

Do this on each one of your servers.

Bootstrap Server

So like I mentioned this isn’t really a bootstrap server but it is simply our first server that we are installing. We will tell the rest of the servers to join this one.

SystemD Setup

We will configure a SystemD service to run serf for us automatically. Create a new file as root called /etc/systemd/system/serf.service on your first server (in my case this is serf-01). Copy the following code into the file and save it.

[Unit]
Description=Serf
Documentation=https://www.serf.io/docs/

[Service]
ExecStart=/usr/local/bin/serf agent -iface=ens3 -node=serf-01
ExecReload=/bin/kill -HUP $MAINPID
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

Make sure to update the iface parameter to match the main interface for your node. It’s likely that these will all be the same if you used virtual machines. In addition, make sure the -node parameter matches the node name.

Now you can start the serf.service and enable it to start during boot time.

# systemctl daemon-reload
# systemctl start serf.service
# systemctl enable serf.service

You can check the status of the service using status:

# systemctl status serf.service
● serf.service - Serf
   Loaded: loaded (/etc/systemd/system/serf.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2017-06-18 03:03:50 UTC; 12s ago
     Docs: https://www.serf.io/docs/
 Main PID: 11604 (serf)
   CGroup: /system.slice/serf.service
           └─11604 /usr/local/bin/serf agent -iface=ens3 -node=serf-01

Jun 18 03:03:50 serf-01 serf[11604]:          Node name: 'serf-01'
Jun 18 03:03:50 serf-01 serf[11604]:          Bind addr: '192.168.1.51:7946'
Jun 18 03:03:50 serf-01 serf[11604]:           RPC addr: '127.0.0.1:7373'
Jun 18 03:03:50 serf-01 serf[11604]:          Encrypted: false
Jun 18 03:03:50 serf-01 serf[11604]:           Snapshot: false
Jun 18 03:03:50 serf-01 serf[11604]:            Profile: lan
Jun 18 03:03:50 serf-01 serf[11604]: ==> Log data will now stream in as it occurs:
Jun 18 03:03:50 serf-01 serf[11604]:     2017/06/18 03:03:50 [INFO] agent: Serf agent starting
Jun 18 03:03:50 serf-01 serf[11604]:     2017/06/18 03:03:50 [INFO] serf: EventMemberJoin: serf-01 192.168.1.51
Jun 18 03:03:51 serf-01 serf[11604]:     2017/06/18 03:03:51 [INFO] agent: Received event: member-join

Our Serf service is now correctly configured and started. On to configuring our other servers to join.

Other Servers

Lastly, we need to configure SystemD on the rest of our servers. Log into each one and create a /etc/systemd/system/serf.service file with the following contents:

[Unit]
Description=Serf
Documentation=https://www.serf.io/docs/

[Service]
ExecStart=/usr/local/bin/serf agent -node=%H -iface=ens3 -join=192.168.1.51
ExecReload=/bin/kill -HUP $MAINPID
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

Again, make sure to update the -iface parameter to match the main interface that you have connectivity on. Notice that I replace the hostname with a %H which systemd will replace with the node’s hostname.

Start and enable the service:

# systemctl daemon-reload
# systemctl start serf.service
# systemctl enable serf.service

You can check the status again with systemctl status serf.service but I am not going to put the output here again.

Final Test

Now we should have all three of our servers clustered together with Serf. To check run the below command:

$ serf members
serf-03  192.168.1.59:7946  alive 
serf-02  192.168.1.65:7946  alive
serf-01  192.168.1.51:7946  alive

Our Serf cluster is now running. We have some more work to do in order to make it useful, but that will be for the next post SERF CUSTOM QUERIES

I hope you enjoyed this post. If it was helpful or if it was way off then please comment and let me know.

Subscribe to our mailing list

indicates required
Email Format

comments powered by Disqus
Google Analytics Alternative