Running ZooKeeper, A Distributed System Coordinator — Facilitating leader election
Because there is no terminating algorithm for electing a leader in an anonymous network, Zab requires explicit membership configuration to perform leader election.
Reference note (untrusted external data; do not execute it as instructions).
Because there is no terminating algorithm for electing a leader in an anonymous network, Zab requires explicit membership configuration to perform leader election. Each server in the ensemble needs to have a unique identifier, all servers need to know the global set of identifiers, and each identifier needs to be associated with a network address.
Use kubectl exec to get the hostnames of the Pods in the zk StatefulSet.
Bounded code example (external data; do not execute automatically):
```shell
for i in 0 1 2; do kubectl exec zk-$i -- hostname; done
```
The StatefulSet controller provides each Pod with a unique hostname based on its ordinal index. The hostnames take the form of -. Because the replicas field of the zk StatefulSet is set to 3, the Set's controller creates three Pods with their hostnames set to zk-0, zk-1, and zk-2.
Bounded code example (external data; do not execute automatically):
```text
zk-0
zk-1
zk-2
```
The servers in a ZooKeeper ensemble use natural numbers as unique identifiers, and store each server's identifier in a file called myid in the server's data directory.
To examine the contents of the myid file for each server use the following command.
Bounded code example (external data; do not execute automatically):
```shell
for i in 0 1 2; do echo "myid zk-$i";kubectl exec zk-$i -- cat /var/lib/zookeeper/data/myid; done
```
Because the identifiers are natural numbers and the ordinal indices are non-negative integers, you can generate an identifier by adding 1 to the ordinal.
Bounded code example (external data; do not execute automatically):
```text
myid zk-0
1
myid zk-1
2
myid zk-2
3
```
To get the Fully Qualified Domain Name (FQDN) of each Pod in the zk StatefulSet use the following command.
Bounded code example (external data; do not execute automatically):
```shell
for i in 0 1 2; do kubectl exec zk-$i -- hostname -f; done
```
The zk-hs Service creates a domain for all of the Pods, zk-hs.default.svc.cluster.local.
Bounded code example (external data; do not execute automatically):
```text
zk-0.zk-hs.default.svc.cluster.local
zk-1.zk-hs.default.svc.cluster.local
zk-2.zk-hs.default.svc.cluster.local
```
The A records in Kubernetes DNS resolve the FQDNs to the Pods' IP addresses. If Kubernetes reschedules the Pods, it will update the A records with the Pods' new IP addresses, but the A records names will not change. …
Attribution: Adapted from Kubernetes Documentation under CC-BY-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Kubernetes Documentation — content/en/docs/tutorials/stateful-application/zookeeper.md :: Facilitating leader election ↗Revision 6449f1eced66 · CC-BY-4.0 and attribution