Minimalist Clustering Demo

This is based on code from a video that I have previously linked to.
However Elixir has made a few breaking changes since then.

This is going to demonstrate how to cluster two local Elixir nodes, deploy code from one to the other and then upgrade it while it is running.

You need to put the following code in a file called blabber.ex

defmodule Blabber do

  def start do

    spawn(fn -> loop(0) end)

  end

  def loop(uptime) do

    receive do

      :stop ->

        IO.puts "Shutting blabber down ..."

        exit(:normal)

      after 1000 ->

        IO.puts "Nice! #{uptime} seconds of bgu-free uptime on #{node()}."

    end

    Blabber.loop(uptime + 1)

  end

end

Next you need to start iex with a name and a cookie (in the same folder as blabber.ex):

iex --sname cat --cookie super-secret

In a second terminal start another iex session

iex --sname dog --cookie super-secret

From the first terminal connect the two nodes together:

Node.connect :"dog@MacBook-Pro"

Compile the code in the cat node:

c ("blabber.ex", ".")

We need to use this version of the compile function as by default since Elixir 1.5 the beam file is not output by default.

The process can be started on the cat node with:

Blabber.start

Now you can transfer the compiled beam file to all linked nodes using:

nl Blabber

Now from the dog node you can start the process:

Blabber.start

At this point we notice the typo in the code. It say bgu-free rather than bug-free.
Edit the source file save the change.

On the cat node recompile this:

c ("blabber.ex", ".")

This will immediately fix the problem on the cat node (and leave the count updating).

If you use:

nl Blabber

then the solution will be moved to the other node as well.

This demonstrates the simplest possible Clustering arrangement. It is simple to extend this to a move sophisticated service.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s