:::success
<font size=6>
serf
</font>
:::
[TOC]
# About serf
官網
https://www.serf.io/
GitHub
https://github.com/hashicorp/serf
What is Serf?
> from https://www.serf.io/intro/index.html
> Serf is a tool for cluster membership, failure detection, and orchestration that is decentralized, fault-tolerant and highly available. Serf runs on every major platform: Linux, Mac OS X, and Windows. It is extremely lightweight: it uses 5 to 10 MB of resident memory and primarily communicates using infrequent UDP messages.
# Command Usage
```
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
```
# Demo
準備兩台電腦
* A 電腦, esvm-win10 , ip: 172.22.12.27
* B 電腦, esvm-agwin , ip: 172.22.12.238
A 電腦執行
```shell=
$ serf agent
```
B 電腦執行
```shell=
$ serf agent
```
A 電腦新開另一個視窗執行
```shell=
$ serf join 172.22.12.238
```
A 電腦、B 電腦此時各自時執行 serf members, 都可以看到 cluster 裡有兩個成員
```
$ serf members
esvm-win10 172.22.12.27:7946 alive
esvm-agwin 172.22.12.238:7946 alive
```
A 電腦發送 custom event
```
$ serf event hello-world
Event 'hello-world' dispatched! Coalescing enabled: true
```
此時 A 電腦跟 B 電腦的 agent,都會出現以下訊息
```
2022/05/13 13:55:59 [INFO] agent: Received event: user-event: hello-world
```
# Event
Types of Events
* member-join - One or more members have joined the cluster.
* member-leave - One or more members have gracefully left the cluster.
* member-failed - One or more members have failed, meaning that they didn't properly respond to ping requests.
* member-update - One or more members have updated, likely to update the associated tags
* member-reap - Serf has removed one or more members from its list of members. This means a failed node exceeded the * reconnect_timeout, or a left node reached the tombstone_timeout.
* user - A custom user event, covered later in this guide.
* query - A query event, covered later in this guide
# Event Handler
```bash=
#!/bin/bash
echo
echo "New event: ${SERF_EVENT}. Data follows..."
while read line; do
printf "${line}\n"
done
```