Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

etcdとRaftアルゴリズム: Kubernetes Control Planeの信頼性の解...

etcdとRaftアルゴリズム: Kubernetes Control Planeの信頼性の解剖 / etcd and raft algorithm

2023/12/11 に行われた CNDT2023 「etcdとRaftアルゴリズム: Kubernetes Control Planeの信頼性の解剖」で使用したスライドです。

Avatar for Shuhei Kawamura

Shuhei Kawamura

March 26, 2024
Tweet

Other Decks in Technology

Transcript

  1. 2 Shuhei Kawamura(X: @shukawam) Job: CSP - Cloud Architect (Cloud

    Native, AuthNZ, AI/ML) ひとこと: 早く雪山にこもりたい Yoshitaka Fujii(X: @ystkfujii) Job: 何でも屋(Terraform/k8s/Golang) ひとこと: 来年のガンダムの映画が楽しみ
  2. What is etcd? ▪ 分散システムの最も重要なデータのための信頼性の高いKVS ▫ Simple - 明確に定義されたユーザー向けのAPI(gRPC) ▫

    Secure - (optional)クライアント証明書認証による自動TLS ▫ Fast - 10,000 writes/secの書き込み ▫ Reliable - Raftを用いた適切な分散 ▪ Kubernetesでは全てのクラスター情報の保存場所として利用 6 参考: https://github.com/etcd-io/etcd
  3. Raft basics ▪ 各ノードは、Leader, Follower, Candidateいずれかの役割を持つ ▪ 主な動作は2つだけ ▫ Leader

    election(リーダー選挙) - RequestVote RPC ▫ Log replication(ログ複製) - AppendEntries RPC ▪ Raftクラスタは奇数台(3, 5, 7)のノード構成を推奨 ▫ 偶数台/奇数台の場合で障害許容ノード数は変わらないが、障害 耐性が下がるため ▫ 参考: https://etcd.io/docs/v3.6/faq/#what-is-failure-tolerance 11
  4. Leader election - State transition ▪ 各ノードは、Leader, Follower, Candidateいずれかの役割を持つ ▫

    起動直後 → Follower ▫ 選挙開始 → Candidate ▫ 選挙勝利 → Leader 13 引用: https://raft.github.io/raft.pdf
  5. Leader election flow 14 Candidate Follower F F Leader RequestVote

    RPC Increment Term Starts election Vote myself voteGranted: true win!! Become Leader AppendEntries RPC
  6. Leader election flow 15 Candidate Follower F F Leader Increment

    Term Starts election Vote myself voteGranted: true win!! Become Leader RequestVote RPC AppendEntries RPC
  7. 16 RequestVote RPC term 候補者のTerm candidateId 投票をリクエストしている候補者 lastLogIndex 候補者の最後のログエントリのインデックス lastLogTerm

    候補者の最後のログエントリのターム term currentTerm、候補者が自分自身を更新する voteGranted 候補者が票を得た場合 true Arguments: Results: 引用: https://raft.github.io/raft.pdf
  8. 17 AppendEntries RPC - 1/2 term リーダーのTerm leaderId リーダーのID prevLogIndex

    新しいエントリの直前のログエントリのインデックス prevLogTerm prevLogIndex のターム entries[] 保存するログエントリ (ハートビートの場合は空 ; 効率のために複数送信も可) leaderCommit リーダーのcommitIndex Arguments: 引用: https://raft.github.io/raft.pdf
  9. 20 Log replication flow Client Leader F F Messages Append

    to local log entries AppendEntries RPC success: true Commit log entries AppendEntries RPC w/ Leader committed index Success
  10. 21 Cluster membership changes オンラインでクラスターの構成変更を実施する上での考慮事項 ▪ 移行中の同一タームに2人のリーダーが選出されてはならない ▪ Raftでは、2段階の合意プロセスで構成移行を実施 ▫

    Phase1. 新旧両方の構成を組み合わせた構成に移行 ▫ Joint Consensus ▫ Phase2. 新しい構成に移行 ▪ 安全性の観点から、一度に1つのサーバーしかクラスタに追加、削除で きないようにRaftでは制限を設けている
  11. 22 Cluster membership changes flow A(Leader) C D B AppendEntries

    RPC Config of {old, new} success: true 新旧全てのノードに 構成変更ログを複製 新旧構成ともに 過半数の合意 AppendEntries RPC Config of {new} success: true 新構成から 過半数の同意 Phase 1 C_old ↓ C_old, new Phase 2 C_old, new ↓ C_new C_old = {A, B, C}, C_new = {A, B, C, D}
  12. 23

  13. Raftの立ち位置 25 Client Write Workflow MVCC: Multi-Version Concurrency Control MVCC

    Write Ahead Log BoltDB EtcdServer Raft 1:書き込みリクエスト 2:Raftにフォワード 7:コミットを通知 8:DBへの書き込み 6:コミット 3:WALに書き込み Raft Write Ahead Log Raftのコアロジックをライブラリとして分離 • 受け取ったリクエストに対する処理 • Peerへの依頼内容 • コミット管理 など EtcdServer 3:Peerへ書き込み依頼 5:書き込み完了報告 4:WALに書き込み(6の後にコミット
  14. Basic action: Processing of state 28 ▪ 定期実行の処理 ->  tickXXX関数 ▪

    State毎の処理  ->  stepXXX関数 参照: https://github.com/etcd-io/etcd/blob/v3.5.10/raft/raft.go
  15. Basic action: Processing of state 29 ▪ 定期実行の処理 ->  tickXXX関数 ▪

    State毎の処理  ->  stepXXX関数 参照: https://github.com/etcd-io/etcd/blob/v3.5.10/raft/raft.go
  16. Basic action: Message 30 ▪ MessageはNodeへの入力をまとめた もの ▫ 種類/送付先/エントリーなど ▪

    MessageTypeの使い分け ▫ MsgProp   : ログ追加の提案 ▫ MsgVote   : 投票要求 ▫ MsgVoteResp: 投票要求への応答 など 参考: https://pkg.go.dev/go.etcd.io/raft/v3#hdr-MessageType
  17. Basic action: Triggers for processing 31 Message Receive Run loop

    If Message Appears Send Message To Peers Step Func Tick Func If Ready ? Yes No 参考: https://github.com/etcd-io/etcd/blob/v3.5.10/raft/node.go#L300
  18. Basic action: Summary 32 Leader Candidate Follower becomeFollower becomeCandidate becomeLeader

    becomeCandidate stepFollower stepCandidate stepLeader tickElection tickHeartbeat tickElection 定期的に実行 Message受信で実行
  19. Leader election flow 33 Candidate Follower F F Leader RequestVote

    RPC Increment Term Starts election Vote myself voteGranted: true win!! Become Leader AppendEntries RPC
  20. Vote myself win!! voteGranted: true Leader election flow 34 Follower

    F F Leader Increment Term RequestVote RPC Become Leader Candidate Starts election
  21. Become Leader Leader election flow 41 Candidate Follower Leader Increment

    Term Starts election Vote myself win!! AppendEntries RPC RequestVote RPC voteGranted: true F F
  22. Leader election flow 43 Candidate Follower F F RequestVote RPC

    Increment Term Starts election Vote myself voteGranted: true win!! Become Leader Leader AppendEntries RPC
  23. 45 Log replication flow Client Leader F F Messages Append

    to local log entries AppendEntries RPC success: true Commit log entries AppendEntries RPC w/ Leader committed index Success