DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. An Introduction to ZooKeeper

An Introduction to ZooKeeper

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

Nitin Ranjan user avatar by
Nitin Ranjan
·
Sep. 20, 18 · Tutorial
Like (6)
Save
Tweet
Share
15.69K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • https://zookeeper.apache.org/doc/r3.5.0-alpha/zookeeperover.html

  • https://zookeeper.apache.org/doc/r3.2.2/zookeeperprogrammers.html

Docker (software) Software system File system

Published at DZone with permission of Nitin Ranjan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using JSON Web Encryption (JWE)
  • The Role of Data Governance in Data Strategy: Part II
  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • How To Use Terraform to Provision an AWS EC2 Instance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: