serf介绍

简介

Serf是一个无中心化的服务调度和服务发现工具。它容错性极好、无中心化设计、没有单点故障。Serf是建立在Gossip协议之上的,Gossip协议就是为无中心化通信而设计的。为了让一个新节点加入Serf集群,只需要知道集群中的任意一个节点即可,一旦新节点加入进来,它将获得集群中所有的成员信息。Gossip协议让Serf的配置和启动变得非常容易。

如何使用

在官方网站 https://www.serfdom.io/downloads.html 下载合适的版本。

简单使用

新建一个事件处理器脚本,例如 handler.sh :

#!/bin/bash
if [ "${SERF_USER_EVENT}" = "memresponse" ]; then
    cat >> /tmp/mem.txt
    echo "\n" >> /tmp/mem.txt
fi

再启动 serf 服务,绑定handler.sh为默认的事件处理器: ./serf agent -bind=133.130.106.57:5001 -rpc-addr=133.130.106.57:7373 -log-level=debug -event-handler=./handler.sh

再再再另一个console窗口利用serf命令发送一个事件到之前启动的serf: ./serf event -rpc-addr=133.130.106.57:7373 memresponse xcxx

我们可以到serf服务的窗口输出:

$  ./serf agent -bind=133.130.106.57:5001 -rpc-addr=133.130.106.57:7373   -log-level=debug -event-handler=./handler.sh
==> Starting Serf agent...
==> Starting Serf agent RPC...
==> Serf agent running!
         Node name: '133-130-106-57'
         Bind addr: '133.130.106.57:5001'
          RPC addr: '133.130.106.57:7373'
         Encrypted: false
          Snapshot: false
           Profile: lan

==> Log data will now stream in as it occurs:

    2015/12/20 10:06:03 [INFO] agent: Serf agent starting
    2015/12/20 10:06:03 [WARN] memberlist: Binding to public address without encryption!
    2015/12/20 10:06:03 [INFO] serf: EventMemberJoin: 133-130-106-57 133.130.106.57
    2015/12/20 10:06:04 [INFO] agent: Received event: member-join
    2015/12/20 10:06:04 [DEBUG] agent: Event 'member-join' script output: 
    2015/12/20 10:06:13 [INFO] agent.ipc: Accepted client: 133.130.106.57:34964
    2015/12/20 10:06:13 [DEBUG] agent: Requesting user event send: memresponse. Coalesced: true. Payload: "xcxx"
    2015/12/20 10:06:14 [INFO] agent: Received event: user-event: memresponse
    2015/12/20 10:06:14 [DEBUG] agent: Event 'user' script output: 

我们去看看 /tmp/mem.txt 文件的内容如下:

xcxx
\n

构建serf集群

参考文献

  1. 官方网站 https://www.serfdom.io
  2. https://www.digitalocean.com/community/tutorials/how-to-set-up-a-serf-cluster-on-several-ubuntu-vps
  3. http://iankent.uk/blog/getting-started-with-hashicorp-serf/