{{announcement.body}}
{{announcement.title}}

An Introduction to ZooKeeper

DZone 's Guide to

An Introduction to ZooKeeper

Let's discuss distributed systems and how ZooKeeper fits into the picture, going over the basics.

· Big Data Zone ·
Free Resource

In 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

Topics:
zookeeper ,apache ,big data ,distributed systems ,hdfs ,cluster

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}