zookeeper部署

image

讲述zookeeper的单机部署、伪集群部署与集群部署

主机规划

主机名称 外网IP【外部访问】 内网IP 操作系统 安装软件
docker01 10.0.0.11 172.16.1.11 CentOS 7.2 zookeeper-3.4.5【集群】
docker02 10.0.0.12 172.16.1.12 CentOS 7.2 zookeeper-3.4.5【集群】
docker03 10.0.0.13 172.16.1.13 CentOS 7.2 zookeeper-3.4.5【集群】
docker04 10.0.0.14 172.16.1.14 CentOS 7.2 zookeeper-3.4.5【单独/伪集群】

备注:

1
2
3
4
5
其中zoo.cfg 的连接数如下:
maxClientCnxns=1500

具体就不在每个配置文件中写了
如果使用,请加上这个配置

Jdk【java8】

软件安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@zhang tools]# pwd
/root/tools
[root@zhang tools]# tar xf jdk-8u162-linux-x64.tar.gz
[root@zhang tools]# ll
total 201392
drwxr-xr-x 8 10 143 4096 Dec 20 13:27 jdk1.8.0_162
-rw-r--r-- 1 root root 189815615 Mar 12 16:47 jdk-8u162-linux-x64.tar.gz
-rw-r--r-- 1 root root 16402010 May 14 00:26 zookeeper-3.4.5.tar.gz
[root@zhang tools]# mv jdk1.8.0_162/ /app/
[root@zhang tools]# cd /app/
[root@zhang app]# ll
total 8
drwxr-xr-x 8 10 143 4096 Dec 20 13:27 jdk1.8.0_162
[root@zhang app]# ln -s jdk1.8.0_162/ jdk
[root@zhang app]# ll
total 8
lrwxrwxrwx 1 root root 13 May 16 23:19 jdk -> jdk1.8.0_162/
drwxr-xr-x 8 10 143 4096 Dec 20 13:27 jdk1.8.0_162

环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
[yun@iZ2zefjy4361ppunik5222Z ~]$ pwd
/app
[yun@iZ2zefjy4361ppunik5222Z ~]$ ll -d jdk* # 可以根据实际情况选择jdk版本,其中jdk1.8 可以兼容 jdk1.7
lrwxrwxrwx 1 yun yun 11 Mar 15 14:58 jdk -> jdk1.8.0_162/
drwxr-xr-x 8 yun yun 4096 Sep 27 2014 jdk1.7.0_71
drwxr-xr-x 8 yun yun 4096 Dec 20 13:27 jdk1.8.0_162
[yun@iZ2zefjy4361ppunik5222Z profile.d]$ pwd
/etc/profile.d
[yun@iZ2zefjy4361ppunik5222Z profile.d]$ cat jdk.sh # java环境变量
export JAVA_HOME=/app/jdk
export JRE_HOME=/app/jdk/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$PATH

zookeeper【单机部署】

配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 路径
[root@zhang app]# pwd
/app
[root@zhang app]# ll
total 4
lrwxrwxrwx 1 root root 13 May 26 06:55 jdk -> jdk1.8.0_112/
drwxr-xr-x 8 10 143 255 Sep 23 2016 jdk1.8.0_112
drwxr-xr-x 10 501 games 4096 Nov 5 2012 zookeeper-3.4.5

# 配置文件
[root@zhang conf]# pwd
/app/zookeeper-3.4.5/conf
[root@zhang conf]# cp -a zoo_sample.cfg zoo.cfg
[root@zhang conf]# vim zoo.cfg
# The number of milliseconds of each tick #心跳间隔时间,zookeeper中使用的基本时间单位,毫秒值。每隔2秒发送一个心跳
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=/app/bigdata/zookeeper/data
# the port at which the clients will connect
clientPort=2181
#
# 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

服务启动与停止

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 启动服务
[root@zhang bin]# pwd
/app/zookeeper-3.4.5/bin
[root@zhang bin]# ./zkServer.sh start
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zhang bin]#
[root@zhang bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: standalone

# 停止服务
[root@zhang bin]# ./zkServer.sh stop
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zhang bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

zookeeper【单机伪集群】

一台机器,在该台机器上运行多个ZooKeeper Server进程。

软件基本信息

1
2
3
4
5
6
7
8
9
10
[root@zhang app]# pwd
/app
[root@zhang app]# ll
total 16
drwxr-xr-x 3 root root 23 May 26 07:01 bigdata
lrwxrwxrwx 1 root root 13 May 26 06:55 jdk -> jdk1.8.0_112/
drwxr-xr-x 8 10 143 255 Sep 23 2016 jdk1.8.0_112
drwxr-xr-x 11 501 games 4096 May 26 07:04 zookeeper-3.4.5_01
drwxr-xr-x 11 501 games 4096 May 26 07:04 zookeeper-3.4.5_02
drwxr-xr-x 11 501 games 4096 May 26 07:04 zookeeper-3.4.5_03

配置文件

zookeeper-3.4.5_01 配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[root@zhang conf]# pwd
/app/zookeeper-3.4.5_01/conf
[root@zhang conf]# ll
total 20
-rw-r--r-- 1 501 games 535 Oct 1 2012 configuration.xsl
-rw-r--r-- 1 501 games 2161 Oct 1 2012 log4j.properties
-rw-r--r-- 1 501 games 927 May 26 07:06 zoo.cfg
-rw-r--r-- 1 501 games 808 Oct 1 2012 zoo_sample.cfg
[root@zhang conf]# vim 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.
# do not use /tmp for storage, /tmp here is just
# example sakes. 数据目录 单独目录存放 确保该目录存在
dataDir=/app/bigdata/zookeeper1/data
# the port at which the clients will connect
# 客户端端口 多个实例的端口配置不可重复
clientPort=2181
#
# 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.NUM=IP:port1:port2 NUM表示本机为第几号服务器;IP为本机ip地址;
#port1为leader与follower通信端口;port2为参与竞选leader的通信端口
#多个实例的端口配置不能重复,如下:
server.1=172.16.1.14:2222:2225
server.2=172.16.1.14:3333:3335
server.3=172.16.1.14:4444:4445

zookeeper-3.4.5_02 配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[root@zhang conf]# pwd
/app/zookeeper-3.4.5_02/conf
[root@zhang conf]# ll
total 16
-rw-r--r-- 1 501 games 535 Oct 1 2012 configuration.xsl
-rw-r--r-- 1 501 games 2161 Oct 1 2012 log4j.properties
-rw-r--r-- 1 501 games 1269 May 26 10:43 zoo.cfg
-rw-r--r-- 1 501 games 808 Oct 1 2012 zoo_sample.cfg
[root@zhang conf]# vim 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.
# do not use /tmp for storage, /tmp here is just
# example sakes. 数据目录 单独目录存放 确保该目录存在
dataDir=/app/bigdata/zookeeper2/data
# the port at which the clients will connect
# 客户端端口 多个实例的端口配置不可重复
clientPort=2182
#
# 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.NUM=IP:port1:port2 NUM表示本机为第几号服务器;IP为本机ip地址;
#port1为leader与follower通信端口;port2为参与竞选leader的通信端口
#多个实例的端口配置不能重复,如下:
server.1=172.16.1.14:2222:2225
server.2=172.16.1.14:3333:3335
server.3=172.16.1.14:4444:4445

zookeeper-3.4.5_03 配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[root@zhang conf]# pwd
/app/zookeeper-3.4.5_03/conf
[root@zhang conf]# ll
total 16
-rw-r--r-- 1 501 games 535 Oct 1 2012 configuration.xsl
-rw-r--r-- 1 501 games 2161 Oct 1 2012 log4j.properties
-rw-r--r-- 1 501 games 1268 May 25 23:39 zoo.cfg
-rw-r--r-- 1 501 games 808 Oct 1 2012 zoo_sample.cfg
[root@zhang conf]# vim 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.
# do not use /tmp for storage, /tmp here is just
# example sakes. 数据目录 单独目录存放
dataDir=/app/bigdata/zookeeper3/data
# the port at which the clients will connect
# 客户端端口 多个实例的端口配置不可重复
clientPort=2183
#
# 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.NUM=IP:port1:port2 NUM表示本机为第几号服务器;IP为本机ip地址;
#port1为leader与follower通信端口;port2为参与竞选leader的通信端口
#多个实例的端口配置不能重复,如下:
server.1=172.16.1.14:2222:2225
server.2=172.16.1.14:3333:3335
server.3=172.16.1.14:4444:4445

添加myid文件

1
2
3
4
5
6
[root@zhang bigdata]# cat /app/bigdata/zookeeper1/data/myid 
1
[root@zhang bigdata]# cat /app/bigdata/zookeeper2/data/myid
2
[root@zhang bigdata]# cat /app/bigdata/zookeeper3/data/myid
3

启动与查看状态

依次启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 第一台
[root@zhang bin]# pwd
/app/zookeeper-3.4.5_01/bin
[root@zhang bin]# ./zkServer.sh start
JMX enabled by default
Using config: /app/zookeeper-3.4.5_01/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

# 第二台
[root@zhang bin]# pwd
/app/zookeeper-3.4.5_02/bin
[root@zhang bin]# ./zkServer.sh start
JMX enabled by default
Using config: /app/zookeeper-3.4.5_02/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

# 第三台
[root@zhang bin]# pwd
/app/zookeeper-3.4.5_03/bin
[root@zhang bin]# ./zkServer.sh start
JMX enabled by default
Using config: /app/zookeeper-3.4.5_03/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查看状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 第一台
[root@zhang bin]# pwd
/app/zookeeper-3.4.5_01/bin
[root@zhang bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5_01/bin/../conf/zoo.cfg
Mode: follower

# 第二台
[root@zhang bin]# pwd
/app/zookeeper-3.4.5_02/bin
[root@zhang bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5_02/bin/../conf/zoo.cfg
Mode: leader

# 第三台
[root@zhang bin]# pwd
/app/zookeeper-3.4.5_03/bin
[root@zhang bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5_03/bin/../conf/zoo.cfg
Mode: follower

zookeeper【集群】

配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@docker01 conf]# pwd
/app/zookeeper-3.4.5/conf
[root@docker01 conf]# vim 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.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/app/bigdata/zookeeper/data
# the port at which the clients will connect
clientPort=2181
#
# 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

# leader和follow通信端口和投票选举端口
server.1=172.16.1.11:2888:3888
server.2=172.16.1.12:2888:3888
server.3=172.16.1.13:2888:3888

添加myid文件

1
2
3
[root@docker01 conf]# cd /app/bigdata/zookeeper/data   #   
[root@docker01 data]# vim myid # 3台机器的myid分别为 1、2、3 # 见开头的主机规划
1

启动zk服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
##### 第1台、2台、3台 依次启动
[root@docker01 bin]# pwd
/app/zookeeper-3.4.5/bin
[root@docker01 bin]# ll
total 60
-rwxr-xr-x 1 501 games 238 Oct 1 2012 README.txt
-rwxr-xr-x 1 501 games 1909 Oct 1 2012 zkCleanup.sh
-rwxr-xr-x 1 501 games 1049 Oct 1 2012 zkCli.cmd
-rwxr-xr-x 1 501 games 1512 Oct 1 2012 zkCli.sh
-rwxr-xr-x 1 501 games 1333 Oct 1 2012 zkEnv.cmd
-rwxr-xr-x 1 501 games 2599 Oct 1 2012 zkEnv.sh
-rwxr-xr-x 1 501 games 1084 Oct 1 2012 zkServer.cmd
-rwxr-xr-x 1 501 games 5467 Oct 1 2012 zkServer.sh
[root@docker01 bin]# ./zkServer.sh start
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查询运行状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 第一台
[root@docker01 bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: follower

# 第二台
[root@docker02 bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: leader

# 第三台
[root@docker03 bin]# ./zkServer.sh status
JMX enabled by default
Using config: /app/zookeeper-3.4.5/bin/../conf/zoo.cfg
Mode: follower

<-------------the end------------->
lightzhang wechat
欢迎扫一扫,订阅我的微信公众号!
坚持原创分享,你的支持就是我最大的动力!