天天看點

為什麼K8s 叢集講每個 kube-apiserver 隻和本機的 ETCD 通信

作者:山河已無恙i

寫在前面

  • 對這個我不太明白,所有在 stackOverflow 的請教了大佬
  • 這裡分享給小夥伴
  • 了解不足小夥伴幫忙指正

對每個人而言,真正的職責隻有一個:找到自我。然後在心中堅守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是對大衆理想的懦弱回歸,是随波逐流,是對内心的恐懼 ——赫爾曼·黑塞《德米安》

我的問題

為什麼K8s 叢集講每個 kube-apiserver 隻和本機的 ETCD 通信

在這裡插入圖檔描述

搭建高可用的 k8s 叢集,需要 搭建 etcd 叢集,在 k8s 官方文檔裡面,我看到這樣講。

每個控制平面節點建立一個本地 etcd 成員(member),這個 etcd 成員隻與該節點的 kube-apiserver 通信。 這同樣适用于本地 kube-controller-manager 和 kube-scheduler 執行個體。

即 kube-apiservice 隻和自己節點的 etcd 通信,是否可以了解為 讀寫都是發生在 同一節點的 etcd ,

但是我在學習 etcd 的時候, 告訴我 etcd 叢集中 用戶端 通過 Follower 讀取資料,通過 Leader 寫入資料.

實際上 Leader 在叢集中隻存在一個。 etcd 叢集的這種讀寫分離适用于 k8s ?

我了解這和上面講的 有些沖突。我想知道k8s 叢集中 etcd 的讀寫方式是怎麼回事,我對 etcd 了解甚少,感謝為我解惑

@解答

為什麼K8s 叢集講每個 kube-apiserver 隻和本機的 ETCD 通信

在這裡插入圖檔描述

In fact, there is only one Leader in the cluster. Does this read/write separation of etcd clusters apply to Kubernetes?

Yes. In an etc cluster, there is only one leader that does the writes. But etcd internally forwards all requests that needs consensus (e.g. writes) to the leader, so the client application (Kubernetes in our case) does not need to know what etcd node is the leader.

From etcd FAQ:

Do clients have to send requests to the etcd leader?
Raft is leader-based; the leader handles all client requests which need cluster consensus. However, the client does not need to know which node is the leader. Any request that requires consensus sent to a follower is automatically forwarded to the leader. Requests that do not require consensus (e.g., serialized reads) can be processed by any cluster member.

大概意思說,在 etcd 叢集中,隻有一個負責寫入的上司者。但是 etcd 在内部将所有 需要共識(例如寫入)的請求轉發給上司者,是以用戶端應用程式(在我們的例子中是 Kubernetes)不需要知道哪個 etcd 節點是上司者。

來自etcd 常見問題解答:

客戶是否必須向 etcd 上司者發送請求?

Raft 是基于上司者的;上司者處理所有需要叢集共識的用戶端請求。但是,用戶端不需要知道哪個節點是上司者。發送給跟随者的任何需要達成共識的請求都會自動轉發給上司者。不需要共識的請求(例如,序列化讀取)可以由任何叢集成員處理。

名詞解釋

Raft :etcd 使用Raft協定來維護叢集内各個節點狀态的一緻性。實作強一緻性。

Leader, Follower, Candidate:叢集角色,當叢集初始化時候,每個節點都是Follower角色,通過心跳與其他節點同步資料 ,通過選舉産生 Leader,之後通過Follower讀取資料,通過Leader寫入資料,當Follower在一定時間内沒有收到來自Leader的心跳,會将自己角色改變為Candidate,并發起一次選主投票。

博文部分内容參考

文中涉及參考連結内容版權歸原作者所有,如有侵權請告知

https://etcd.io/docs/v3.5/faq/

https://stackoverflow.com/questions/75424632/how-etcd-cluster-data-is-read-and-synchronized-under-k8s-clusters

© 2018-2023 [email protected],All rights reserved. 署名-相同方式共享 4.0 國際 協定(CC BY 4.0)

繼續閱讀