Apache Zookeeper is open source tool from Apache Foundation. Originally developed at Yahoo. Thanks Yahoo for the Zookeeper.
Zookeeper is written in Java and it is platform independent.
What is Distributed Systems?
Multiple independent computers connected together and appears as single computer to the users. Distributed System communicate through network by passing messages. All components in distributed system interact with each other to performs subsets of tasks to achieve common goals
Why to use Distributed System?
Challenges of Distributed System?
Zookeeper is a high-performance coordination service used by distributed systems. Zookeeper allows distributed systems to coordinate each other through a hierarchical name-space of data registrars.
1. Configuration Management
2. Synchronization
3. Leader Election
4. Naming Service
5. Notification System
Zookeeper provides API for achieving all the above tasks.
Zookeeper Architecture
Zookeeper service runs in 2 mode
In standalone mode Zookeeper service runs on single server and no state is replicated.
In quorum mode, a group of Zookeeper servers, which we call a Zookeeper ensemble, replicates the state, and together they serve client requests.
Znode:
Every node in Zookeeper tree is Znode. Znode in Zookeeper maintain stat structure.
A Znode can either be persistent or ephemeral. As name says persistent Znode will remain in Zookeeper until and unless a delete call not made and ephemeral Znode will get deleted when the Zookeeper client gets disconnect. There is also a sequential Znode and it appends a unique number to the Znode. So there are basically 4 types of Znode persistent, ephemeral, persistent_sequential, ephemeral_sequential.
ZAB:
All write requests from clients to followers will always get forwarded to leader. The leader executes the request and broadcasts the result of the execution as a state update, in the form of a transaction. A transaction comprises the exact set of changes that a server must apply to the data tree when the transaction is committed.
How to install standalone Zookeeper up and running?
Download Zookeeper latest stable version from here. Note this link may vary from mirror to mirror.
I usually keep my installations in /opt
but you guys can download it in your choice of location on your server/machine.
* wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz
* tar xzf zookeeper-3.4.13.tar.gz
* cd zookeeper-3.4.13
* cp conf/zoo_sample.cfg conf/zoo.cfg
To start Zookeeper server
* sudo bin/zkServer.sh start
You sgould see output like below id Zookeeper start successfully
ZooKeeper JMX enabled by defaultUsing config: /opt/zookeeper-3.4.13/bin/../conf/zoo.cfgStarting zookeeper … STARTED
Zookeeper CLI
create /abhishek "anay"
Output should be like below
Created /abhishek
To create a sequential Znode use -s
flag and to create ephemeral Znode use -e
flag with above command.
2. To get data from Zookeeper
get /abhishek
You should see output like below
anaycZxid = 0x9ctime = Sun Nov 11 20:55:34 IST 2018mZxid = 0x9mtime = Sun Nov 11 20:55:34 IST 2018pZxid = 0x9cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 4numChildren = 0
Lets understand the above output it is also a Znode metadata
To create a sub Znode
create /abhishek/anayamralkar loveyouson
3. watch command
It shows notification when the data in the Znode change we can use watch
command with get
command.
get /abhishek [watch] 1
Output similar to get
command but it shows notification when data in Znode get change.
get /abhishek [watch] 1anaycZxid = 0x9ctime = Sun Nov 11 20:55:34 IST 2018mZxid = 0x9mtime = Sun Nov 11 20:55:34 IST 2018pZxid = 0x9cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 4numChildren = 0
4. set command is use to change the data associated with Znode
set /abhishek anayamralkar
5. delete command to delete the znode
delete /abhishek
6. ls command to list the Znodes and children
[zk: localhost:2181(CONNECTED) 31] ls /[zookeeper, abhishek][zk: localhost:2181(CONNECTED) 32] ls /abhishek[anayamralkar]
7. stat command to check the status of Znode and children
stat /
stat /abhishek
8. help command to see all available commands
ZooKeeper -server host:port cmd argsstat path [watch]set path data [version]ls path [watch]delquota [-n|-b] pathls2 path [watch]setAcl path aclsetquota -n|-b val pathhistoryredo cmdnoprintwatches on|offdelete path [version]sync pathlistquota pathrmr pathget path [watch]create [-s] [-e] path data acladdauth scheme authquitgetAcl pathcloseconnect host:port
9. quit command to quit the Zookeeper shell
quit