Operation and maintenance deployment -Zookeeper

Zookeeper Introduction

Zookeeper is an implementation based on open source implementation of Google Chubby paper distributed data consistency solve a problem, to facilitate the implementation of data applications rely Zookeeper publish / subscribe, load balancing, service registration and discovery, distributed coordination, event notification, cluster management, Leader elections, locks and distributed queue functions

Download Zookeeper

[root@localhost cloud]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.3.6/zookeeper-3.3.6.tar.gz
[root@localhost cloud]# tar -zxvd zookeeper-3.3.6.tar.gz
[root@localhost cloud]# tree zookeeper-3.3.6/conf/
zookeeper-3.3.6/conf/
├── configuration.xsl
├── log4j.properties
└── zoo_sample.cfg

0 directories, 3 files
[root@localhost cloud]# tree zookeeper-3.3.6/bin/
zookeeper-3.3.6/bin/
├── README.txt
├── zkCleanup.sh
├── zkCli.cmd
├── zkCli.sh
├── zkEnv.cmd
├── zkEnv.sh
├── zkServer.cmd
└── zkServer.sh

0 directories, 8 files
复制代码

Configuration instructions:

  • Installation Configuration Configuration Catalog Zookeeper zookeeper at zookeeper-xxx / conf / directory.

  • zookeeper zookeeper scripts directory of start and stop scripts directory under the zookeeper-xxx / bin / directory. (. sh .cmd and distribution systems applicable to unix and windows systems)

script Explanation
zkCleanup Zookeeper clean up historical data, including the transaction log file and snapshot data files
zkEnv Zookeeper set environment variables
zkServer Zookeeper server to start, stop, restart script
zkCli Zookeeper command-line client

Stand-alone environment to build

zookeeper installation configuration broadly divided into two steps: identifying a configuration server configuration file and modifications zoo.cfg file.

Creating a server ID file

[root@localhost cloud]# pwd
/export/cloud
[root@localhost cloud]# cd zookeeper-3.3.6
[root@localhost zookeeper-3.3.6]# mkdir data
[root@localhost zookeeper-3.3.6]# echo 1 > data/myid
[root@localhost zookeeper-3.3.6]# cat data/myid
1
复制代码

In the example we create a directory /export/cloud/zookeeper-3.3.6/data, the directory is a directory storing data Zookeeper, myid and create a file in the directory, the contents of the file is written 1, an identity value used to identify the current instance.

Modify the file and start zoo.cfg

zookeeper configuration files in zookeeper-x.x.x/confthe directory, the default sample file named zoo_sample.cfg

[root@localhost cloud]# cd zookeeper-3.3.6/conf/
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
[root@localhost conf]# vim zoo.cfg
[root@localhost conf]# cat zoo.cfg
# 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.
dataDir=/export/cloud/zookeeper-3.3.6/data
# the port at which the clients will connect
clientPort=2181
# 配置sever1节点
server.1=127.0.0.1:2888:3888
复制代码

DataDir example only modifies the directory path to the data storage step created for us /opt/zookeeper-3.4.9/dataand adds a communication service identification server.1=127.0.0.1:2888:3888.

Parameter Description:

  • tickTime: minimum time unit used to indicate that the server or between the client and server to maintain the heartbeat mechanism, Session default expiration time is twice the minimum of tickTime.
  • initLimit: The maximum number of heartbeats between cluster nodes and Follower Leader initial connection node can tolerate.
  • syncLimit: maximum number of requests and responses tolerant heartbeat between cluster nodes and Follower Leader node.
  • dataDir: Zookeeper save server stores the snapshot file directory (default directory: / tmp / zookeeper)
  • dataLogDir: server used to store transaction logs
  • clientPort: Client server port Zookeeper, Zookeeper listens to this port an interview request from the client (default: 2181)

Start Zookeeper

[root@localhost cloud]# pwd
/export/cloud
[root@localhost cloud]# zookeeper-3.3.6/bin/zkServer.sh start &
[root@localhost cloud]# zookeeper-3.3.6/bin/zkServer.sh status
JMX enabled by default
Using config: /export/cloud/zookeeper-3.3.6/bin/../conf/zoo.cfg
Mode: standalone
复制代码

Access Zookeeper

[root@localhost cloud]# zookeeper-3.3.6/bin/zkCli.sh
Connecting to localhost:2181
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 2] create /zkEdu 123
Created /zkEdu
[zk: localhost:2181(CONNECTED) 3] ls /
[zookeeper, zkEdu]
[zk: localhost:2181(CONNECTED) 4] get /zkEdu
123
cZxid = 0x2
ctime = Wed Jan 10 18:02:05 CST 2018
mZxid = 0x2
mtime = Wed Jan 10 18:02:05 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
复制代码

Pseudo-cluster environment to build

Using a plurality of pseudo-cluster machines exemplary embodiment structures

Data storage and directory service configuration ID

machine Data storage directory Service ID identifies the file Service ID identifies value Client communication port The cluster master-slave communication port Lead elections port cluster
127.0.0.1 zookeeper-3.3.6-node1/data zookeeper-3.3.6-node1/data/myid 1 2181 2887 2888
127.0.0.1 zookeeper-3.3.6-node2/data zookeeper-3.3.6-node2/data/myid 2 2182 2888 3888
127.0.0.1 zookeeper-3.3.6-node3/data zookeeper-3.3.6-node3/data/myid 3 2183 2889 3889

Zookeeper node configuration file

Node 1 "/export/cloud/zookeeper-3.3.6-node1/conf/zoo.cfg" configuration

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/export/cloud/zookeeper-3.3.6-node1/data
clientPort=2181
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2888:3888
server.3=127.0.0.1:2889:3889
复制代码

Node 2 "/export/cloud/zookeeper-3.3.6-node2/conf/zoo.cfg" configuration

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/export/cloud/zookeeper-3.3.6-node2/data
clientPort=2182
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2888:3888
server.3=127.0.0.1:2889:3889
复制代码

Node 3 "/export/cloud/zookeeper-3.3.6-node3/conf/zoo.cfg" configuration

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/export/cloud/zookeeper-3.3.6-node2/data
clientPort=2183
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2888:3888
server.3=127.0.0.1:2889:3889
复制代码

Clusters access

It was started three nodes, node 2 in the example shows clustering condition monitoring and access

[[root@localhost cloud]# zookeeper-3.3.6-node2/bin/zkServer.sh status
JMX enabled by default
Using config: /export/cloud/zookeeper-3.3.6-node2/bin/../conf/zoo.cfg
Mode: follower
[root@localhost cloud]# zookeeper-3.3.6-node2/bin/zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
复制代码

Build a clustered environment

zookeeper cluster environment consistent with the basic ideas and build a stand-alone zookeeper example deployment on each machine, configure each service identifier ID, unified service configuration identifier set in zoo.cfg assumes that the cluster has three machines: 172.30.12.197, 172.30.12.198,172.30.12.199

Data storage and directory service configuration ID

machine Data storage directory Service ID identifies the file Service ID identifies value Client communication port The cluster master-slave communication port Lead elections port cluster
172.30.12.197 zookeeper-3.3.6/data zookeeper-3.3.6/data/myid 1 2181 2888 3888
172.30.12.198 zookeeper-3.3.6/data zookeeper-3.3.6/data/myid 2 2181 2888 3888
172.30.12.199 zookeeper-3.3.6/data zookeeper-3.3.6/data/myid 3 2181 2888 3888

Zookeeper node configuration file

Examples of three machines use the same configuration

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/export/cloud/zookeeper-3.3.6/data
clientPort=2181
server.1=172.30.12.197:2888:3888
server.2=172.30.12.198:2888:3888
server.3=172.30.12.199:2888:3888
复制代码

Connecting to a cluster

[root@localhost cloud]# zookeeper-3.3.6/bin/zkCli.sh -server 172.30.12.197:2181,172.30.12.198:2181,172.30.12.199:2181
复制代码

Zookeeper four-character phrase

ZooKeeper supports certain four-character command letter and its interactions. They are mostly a query command to get the current status of ZooKeeper services and related information. The user can submit the appropriate command to the client ZooKeeper telnet or nc.

command Functional Description Examples of Use
conf Output configuration details Information Services [root@localhost cloud]# echo conf | nc localhost 2181
cons Lists the full details of connection / session all connections to the server of the client. The number of packets including the "receiving / transmission", session id, the operational delay, the last operation is performed so forth. [root@localhost cloud]# echo cons | nc localhost 2181
dump Session and provisional list node untreated (valid only on the leader) [root@localhost cloud]# echo dump | nc localhost 2181
envi Prints detailed information about the service environment [root@localhost cloud]# echo envi | nc localhost 2181
reqs Untreated request list [root@localhost cloud]# echo reqs | nc localhost 2181
Bib Testing services are in the correct state. If so, then the service returns "imok", otherwise it makes no corresponding. [root@localhost cloud]# echo ruok | nc localhost 2181
stat Output a list of performance and client connection [root@localhost cloud]# echo stat | nc localhost 2181
wchs For more information on server watch lists. [root@localhost cloud]# echo wchs | nc localhost 2181
wchc Detailed information on the session server watch list, a list of its output is related to watch the session. [root@localhost cloud]# echo wchc | nc localhost 2181
wchp For more information on watch lists by the server path. It outputs a path associated with the session. [root@localhost cloud]# echo wchp | nc localhost 2181
srvr For more information output from the server. zk version, the reception / transmission packet number, number of connections, mode (leader / follower), total number of nodes [root@localhost cloud]# echo srvr | nc localhost 2181
hair Reset server statistics [root@localhost cloud]# echo srst | nc localhost 2181

Reference Documents

Guess you like

Origin juejin.im/post/5dfb287df265da33dd2f5e7e