installation and use of zookeeper

1. Download and install zookeeper

1. Download the zookeeper-3.4.6 installation package

 cd/usr/developSoft/

wget http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz

 

 

2. Unzip to /usr/local/ directory

tar -zxvf  zookeeper-3.4.6.tar.gz  -C /usr/local/

 

Enter the /usr/local/zookeeper-3.4.6/bin directory

./zkServer.sh start start the service

./zkServer.sh stop stop the service


Second, zookeeper independent mode (development and test) configuration

 

1. Enter /usr/local/zookeeper-3.4.6/conf to modify the zoo.cfg configuration

 

cd /usr/local/zookeeper-3.4.6/conf

vi zoo.cfg 

2. In independent mode, the zoo.cfg configuration can be modified as follows:

 
# The number of milliseconds of each tick
A time unit in #ZK. All time in ZK is based on this time unit and is configured in integer multiples
tickTime=2000    
# The number of ticks that the initial
# synchronization phase can take
initLimit = 10
# The number of ticks that can pass between
# sending a request and getting anacknowledgement
syncLimit=5 #leader and learner (follower, obverser) direct heartbeat detection interval
# the directory where the snapshot isstored.
# do not use /tmp for storage, /tmp here isjust
# example sakes.
#The directory where the snapshot file snapshot is stored. By default, transaction logs are also stored here. It is recommended to configure the parameter dataLogDir at the same time. The write performance of the transaction log directly affects the performance of zk.
dataDir=/usr/local/zookeeper-3.4.6/zk_data
# dataLogDir Transaction log output directory. Try to configure a separate disk or mount point for the output of the transaction log, which will greatly improve ZK performance
dataLogDir=/usr/local/zookeeper-3.4.6/zk_dataLog
# the port at which the clients willconnect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle moreclients
#maxClientCnxns=60
#
# Be sure to read the maintenance sectionof the
# administrator guide before turning onautopurge.
#
#http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain indataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable autopurge feature
# autopurge.purgeInterval = 1

cd bin

./zkServer.sh start-foreground #Start the service with the foreground program

or

./zkServer.sh start #Start the zkServer server as a background program

./zkServer.shstop #Stop the zkServer server


3. Zookeeper cluster mode (available for production) configuration

Prepare three machines and build a three-node zookeeper cluster.

( For simplicity, I built 3 zookeeper instances in one virtual machine and used different ports to simulate three physical machines. It is strongly recommended to use different physical machines in the actual production environment )

 

1. Download and install zookeeper-3.4.6, please refer to the above.

2. Decompress zookeeper-3.4.6_1, zookeeper-3.4.6_2, and zookeeper-3.4.6_3 in /usr/local as the three zookeeper node instances in the Cluster.


Modify the zoo.cfg configuration and myid file of each zookeeper service node, taking the first node as an example:

zookeeper-3.4.6_1/conf/zoo.cfg is as follows

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit = 10
# The number of ticks that can pass between
# sending a request and getting anacknowledgement
syncLimit=5
# the directory where the snapshot isstored.
# do not use /tmp for storage, /tmp here isjust
# example sakes.
dataDir=/usr/local/zookeeper-3.4.6_1/zk_data
dataLogDir=/usr/local/zookeeper-3.4.6_1/zk_log
 
# the port at which the clients willconnect
clientPort=2182
# the maximum number of client connections.
# increase this if you need to handle moreclients
#maxClientCnxns=60
#
# Be sure to read the maintenance sectionof the
# administrator guide before turning onautopurge.
#
#http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain indataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable autopurge feature
# autopurge.purgeInterval = 1
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883


3 Configure myId

A myid file will be placed in the dataDir with a number in it to uniquely identify the service. This id is very important and must be unique in the entire cluster. Zookeeper will fetch the configuration on server.x according to this id. For example, the current id is 1, which corresponds to the configuration of server.1 in zoo.cfg.

echo 1 > /usr/local/zookeeper-3.4.6_1/zk_data/myid


The configuration of the second and third nodes (zoo.cfg, myid files) is similar to the first node, the only difference is the dataDir dataLogDir myid file content.

Second node zoo.cfg configuration

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit = 10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-3.4.6_2/zk_data
dataLogDir=/usr/local/zookeeper-3.4.6_2/zk_log
# the port at which the clients will connect
clientPort=2183
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# autopurge.purgeInterval = 1
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883

 

myid file

echo 2 > /usr/local/zookeeper-3.4.6_2/zk_data/myid


The third node zoo.cfg configuration:

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit = 10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/local/zookeeper-3.4.6_3/zk_data
dataLogDir=/usr/local/zookeeper-3.4.6_3/zk_log
# the port at which the clients will connect
clientPort=2184
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
# autopurge.purgeInterval = 1
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883


myid file

echo 3 > /usr/local/zookeeper-3.4.6_3/zk_data/myid

ok, start 3 points in turn

cd /usr/local/zookeeper-3.4.6_1/bin/

 ./zkServer.sh start

cd /usr/local/zookeeper-3.4.6_2/bin/

 ./zkServer.sh start

cd /usr/local/zookeeper-3.4.6_3/bin/

 ./zkServer.sh start 




Fourth, zookeeper client commands

 

./zkCli.sh -server127.0.0.1:2181

Display files in the root directory

ls // View current node data

ls2 // View the current node data and see the update times and other data

Create the file, and set the initial content:

create /config"test" //Create a new node and set the associated value

create /config "" //Create a new empty node

get file content

get /brokers //Get node content

Modify file content

set /zk"zkbak" //Set the string associated with zk

Delete Files

delete /brokers //delete node

rmr /brokers //delete node and child nodes




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325805047&siteId=291194637