An Introduction to ZooKeeper
Let's discuss distributed systems and how ZooKeeper fits into the picture, going over the basics.
Join the DZone community and get the full member experience.
Join For Freein our last blog, we talked about the hdfs cluster . it's needed for deploying opentsdb in clustered mode. continuing with the series, we are going to talk about zookeeper which will be used by hbase & opentsdb in the cluster.
before talking about zookeeper, let's explain distributed systems and the need for zookeeper.
distributed system
when a software system composed of independent computing entities linked together by a computer network and its components communicate and coordinate with each other to achieve a common goal this is known as a distributed system. (ex: multiplayer online games like clash of clan )
advantages of distributed systems
-
scalability: we can easily expand the distributed system by adding more machines in the cluster.
-
redundancy: all of the machines in the cluster provide the same services, so if any one of them is not available then work will not be stopped.
the process within these system needs some type of agreement to run correctly and efficiently. this type of agreement is also known as distributed coordination.
we can build our own coordinating system, however, that will take the lot of work and it is not a trivial task. the problem comes in implementing a correct fault-tolerant solution.
so, are there any alternatives we can use?
we can use a robust coordination service like zookeeper.
what is zookeeper?
zookeeper is a centralized service for maintaining configuration information , naming , providing distributed synchronization, and providing group services . zookeeper is simple, distributed, reliable, and fast.
- maintaining configuration information: it maintains cluster configuration info which is shared across all the nodes in the cluster.
- naming: zookeeper can be used as naming service, so that one node in the cluster can find another node in the large cluster ex: 1000 node cluster
- providing distributed synchronization: we can also use zookeeper for solving distributed synchronization problems in the cluster by using locks, queues etc.
- providing group services: zookeeper also helps in group service with the selection of a master in the cluster(leader election process).
zookeeper can work in replicated mode as well as standalone mode.
replicated mode
in replicated mode, there are multiple servers involved. one of the servers is selected as the leader and others are followers. if the leader fails one of the followers is elected as master.
image credit: zookeeper.apache.org
the servers in the cluster must know about each other. they maintain an in-memory image of state, along with transaction logs and snapshots in a persistent store. as long as a majority of the servers are available, the zookeeper service will be available.
clients can connect to a single zookeeper server. however, when a client is started they are provided a list of servers, so when a connection to the connected server fails then the client can connect to any other server in the cluster.
read operation can perform read operation from any of the servers in the cluster but write operation must need to go through the leader.
standalone mode
zookeeper can also run in the standalone mode. in this mode, all the clients are connected to a single zookeeper server.
in this mode, we lose the benefits of replication and high availability.
zookeeper data model
zookeeper has a hierarchal namespace. the namespace can have data associated with it as well as children. paths to nodes are always expressed as canonical, absolute, slash-separated paths; there is no relative reference. these namespaces are organized much like a file system in linux.
image credit: zookeeper.apache.org
znode:
every node in a zookeeper tree is referred to as a znode . znodes maintain a stat structure that includes version numbers for data changes, acl(acess controle list) changes. data is stored in znode.
deploy zookeeper:
to deploy zookeeper we will use the official zookeeper docker image
in server1
version: '3.1'
services:
zoo1:
image: zookeeper:latest
restart: always
hostname: zoo1
ports:
- 2181:2181
- 2888:2888
- 3888:3888
environment:
zookeeper_client_port: 2181
zoo_my_id: 1
zoo_servers: server.1=0.0.0.0:2888:3888 server.2=server2ip:2888:3888 server.3=server3ip:2888:3888-
view the code on gist .
run given yml server1
docker-compose -f zoo1.yml up -d
in server2
version: '3.1'
services:
zoo2:
image: zookeeper:latest
restart: always
hostname: zoo2
ports:
- 2181:2181
- 2888:2888
- 3888:3888
environment:
zookeeper_client_port: 2181
zoo_my_id: 2
zoo_servers: server.1=server1ip:2888:3888 server.2=0.0.0.0:2888:3888 server.3=server3ip:2888:3888
view the code on gist .
run given yml server2
docker-compose -f zoo2.yml up -d
in server3
version: '3.1'
services:
zoo2:
image: zookeeper:latest
restart: always
hostname: zoo2
ports:
- 2181:2181
- 2888:2888
- 3888:3888
environment:
zookeeper_client_port: 2181
zoo_my_id: 3
zoo_servers: server.1=server1ip:2888:3888 server.2=server2ip:2888:3888 server.3=0.0.0.0:2888:3888
view the code on gist .
run given yml in server3
docker-compose -f zoo3.yml up -d
to check the status of zookeeper:
root@host:~# nc localhost 2181
stats
you can try the given command in all servers and check for mode in the output to find which is leader and follower.
*note: replace server1ip, server2ip, and server3ip in all yml with their respective values.
references
Published at DZone with permission of Nitin Ranjan, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments