etcd(v3)メモ
ひさしぶりにetcdをいじった。etcdはRaftベースの分散KVSで分散システムの状態管理に用いる。
データサイズやスループットをスケールさせたいアプリケーションデータの管理には使わない。分散KVSって書いたけど実際には単純なストアではなくロックやリースといった分散システムで使うプロトコルが提供されている。
リンク集
etcdctlの使い方
サブコマンドは運用とアプリケーションに大別される。
運用は alarm, auth, check, compaction, defrag, endpoint, member, user, role, snapshotサブコマンドなどで、アプリケーションとしてdel, get, put, watch, lease, lock, elect, txnサブコマンドが含まれる。
code: etcdctl-help
COMMANDS:
alarm disarm Disarms all alarms
alarm list Lists all alarms
auth disable Disables authentication
auth enable Enables authentication
check datascale Check the memory usage of holding data for different workloads on a given server endpoint.
check perf Check the performance of the etcd cluster
compaction Compacts the event history in etcd
defrag Defragments the storage of the etcd members with given endpoints
del Removes the specified key or range of keys [key, range_end)
elect Observes and participates in leader election
endpoint hashkv Prints the KV history hash for each endpoint in --endpoints
endpoint health Checks the healthiness of endpoints specified in --endpoints flag
endpoint status Prints out the status of endpoints specified in --endpoints flag
get Gets the key or a range of keys
help Help about any command
lease grant Creates leases
lease keep-alive Keeps leases alive (renew)
lease list List all active leases
lease revoke Revokes leases
lease timetolive Get lease information
lock Acquires a named lock
make-mirror Makes a mirror at the destination etcd cluster
member add Adds a member into the cluster
member list Lists all members in the cluster
member promote Promotes a non-voting member in the cluster
member remove Removes a member from the cluster
member update Updates a member in the cluster
migrate Migrates keys in a v2 store to a mvcc store
move-leader Transfers leadership to another etcd cluster member.
put Puts the given key into the store
role add Adds a new role
role delete Deletes a role
role get Gets detailed information of a role
role grant-permission Grants a key to a role
role list Lists all roles
role revoke-permission Revokes a key from a role
snapshot restore Restores an etcd member snapshot to an etcd directory
snapshot save Stores an etcd node backend snapshot to a given file
snapshot status Gets backend snapshot status of a given file
txn Txn processes all the requests in one transaction
user add Adds a new user
user delete Deletes a user
user get Gets detailed information of a user
user grant-role Grants a role to a user
user list Lists all users
user passwd Changes password of user
user revoke-role Revokes a role from a user
version Prints the version of etcdctl
watch Watches events stream on keys or prefixes
アプリケーション
アプリケーションは4つ。
リース(lease *)
特定のキー配下に生存時間(TTL)が定められたリースを作る機能、何個でも作れる
特定のリースをkeep-aliveによってTTLを更新し続けられる
クライアントの死活管理に使う
etcdから死なずに通信が維持されていると認識されてると確認できる
KVS (get, put, del, txn, watch)
キーに値を入れたり読み取ったり、変更イベントをwatchしたり
リースIDと紐付けてZookeeperのエフェメルラルノードみたいなことを実現できる
リースが消えたら一緒に消える
keep-aliveを使ってクライアントと生存期間が一致するKey-Valueを作れる
キーのprefixを使ってnamespaceを提供している
正確には etcdのproxyモードがキーにprefixを追加することで実現している
複数のクライアントアプリケーションでetcdクラスタを共有できる
ロック(lock *)
あるキーに対して1クライアントのみが成功し残りはブロックされる機能
並行制御ではロックはコードセグメントを守るために使う
分散システムでも危険な更新やコードセグメントの保護に使う
自分のロックが通信遅延で解放されてないか確認が必要
選出(elect)
あるキーに対して各クライアントが値を提案しあって合意する
運用
認証(user, role, auth)
データ管理(snapshot, compaction, defrag)
クラスタ管理(move-leader, member, make-mirrors)
監視(alarm, check, endpoint)
一通り触ってみたのでメモを残す。
認証機能はデフォルトでは無効なので下のように有効化する。rootロールを持ったrootユーザーが事前に必要。
code:auth.sh
# root は特別でauth enableに必要
etcdctl user add root
etcdctl role add root
etcdctl role list
etcdctl user list
etcdctl auth enable # etcdとの通信で認証を有効化する
etcdctl auth disable --user root # 認証を無効化する; すでに認証が必要なので--userオプションが必要
etcdctl role delete root
snapshotのrestoreではデータディレクトリが作られるので指定する方がいい。restoreしたのちにdata-dirを指定してetcdを再起動すればrestoreされたetcdが生まれる。
code:snapshot.sh
etcdctl snapshot save ./sample.state
etcdctl snapshot status ./sample.state
etcdctl snapshot restore ./sample.state --data-dir=restored.etcd
調べたい実例
CoreDNSでの使いかた
Kubernetesでの使いかた
TiKVでの使いかた