你的位置:首页 > 操作系统

[操作系统]分布式服务协调技术zookeeper系列(一) zookeeper 简介以及linux上的安装(单节点)


ZooKeeper简介

ZooKeeper是一个分布式协调服务框架,通常用于解决分布式项目中遇到的一些管理协调的问题,如统一命名服务、分布式配置管理,分布式锁、集群节点状态协调等等。

下载

到http://apache.fayea.com/zookeeper/下载zookeeper-3.4.9,ftp上传至linux

解压

[root@localhost ftpuser]# tar -zxvf zookeeper-3.4.9.tar.gz

创建数据日志目录

在zookeeper的解压目录下创建以下两个文件夹

 

[root@localhost zookeeper-3.4.9]# mkdir data

[root@localhost zookeeper-3.4.9]# mkdir logs

 

拷贝配置文件

到zookeeper的解压目录的conf目录下,将zoo_sample.cfg 文件拷贝一份,命名为为 zoo.cfg

 

[root@localhost conf]# cp zoo_sample.cfg zoo.cfg

修改配置文件

[root@localhost conf]# vi 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

# 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=/home/ftpuser/zookeeper-3.4.9/data

dataLogDir=/home/ftpuser/zookeeper-3.4.9/logs

# the port at which the clients will connect

clientPort=2181

server.1=192.168.2.129:2888:3888

# 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

 

在zoo.cfg 配置dataDir,dataLogDir,server。

server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务

器的 IP 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。

 

clientPort 这个端口就是客户端(应用程序)连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口接受客户端的访问请求

创建 myid 文件

进入/home/ftpuser/zookeeper-3.4.9/data并创建myid文件

 

[root@localhost conf]# cd /home/ftpuser/zookeeper-3.4.9/data

[root@localhost data]# vi myid

1

 

编辑 myid 文件,并在对应的 IP 的机器上输入对应的编号。如在 zookeeper 上,myid

文件内容就是 1。如果只在单点上进行安装配置,那么只有一个 server.1

添加环境变量

[root@localhost data]# vi /etc/profile

 

在文件末尾添加zookeeper 环境变量

 

# zookeeper env

export ZOOKEEPER_HOME=/home/ftpuser/zookeeper-3.4.9/

export PATH=$ZOOKEEPER_HOME/bin:$PATH

 

执行source /etc/profile命令是环境变量生效,执行echo $ZOOKEEPER_HOME查看

打开端口

在防火墙中打开要用到的端口 2181、2888、3888。打开/etc/sysconfig/iptables增加以下 3 行

 

[root@localhost data]# cat /etc/sysconfig/iptables

# Generated by iptables-save v1.4.7 on Thu Jun  2 22:41:13 2016

*filter

:INPUT ACCEPT [5:320]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [4:464]

-A INPUT -p udp -m udp --dport 23 -j ACCEPT

-A INPUT -p tcp -m tcp --dport 23 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 2181 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 2888 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3888 -j ACCEPT

COMMIT

# Completed on Thu Jun  2 22:41:13 2016

 

[root@localhost data]#

 

查看端口启动状态

 

[root@localhost data]# service iptables status

 

启动zookeeper

[root@localhost zookeeper-3.4.9]# cd bin

[root@localhost bin]# ll

total 36

-rwxr-xr-x. 1 1001 1001  232 Aug 23 15:39 README.txt

-rwxr-xr-x. 1 1001 1001 1937 Aug 23 15:39 zkCleanup.sh

-rwxr-xr-x. 1 1001 1001 1032 Aug 23 15:39 zkCli.cmd

-rwxr-xr-x. 1 1001 1001 1534 Aug 23 15:39 zkCli.sh

-rwxr-xr-x. 1 1001 1001 1579 Aug 23 15:39 zkEnv.cmd

-rwxr-xr-x. 1 1001 1001 2696 Aug 23 15:39 zkEnv.sh

-rwxr-xr-x. 1 1001 1001 1065 Aug 23 15:39 zkServer.cmd

-rwxr-xr-x. 1 1001 1001 6773 Aug 23 15:39 zkServer.sh

[root@localhost bin]# zkServer.sh start

ZooKeeper JMX enabled by default

Using config: /home/ftpuser/zookeeper-3.4.9/bin/../conf/zoo.cfg

Starting zookeeper ... STARTED

查看zookeeper后台日志

[root@localhost ~]# tail -f /home/ftpuser/zookeeper-3.4.9/bin/zookeeper.out

查看zookeeper进程

[root@localhost bin]# jps

2011 QuorumPeerMain

1245 Bootstrap

2030 Jps

[root@localhost bin]#

 

其中,QuorumPeerMain 是 zookeeper 进程,启动正常。查看状态

 

[root@localhost bin]# zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /home/ftpuser/zookeeper-3.4.9/bin/../conf/zoo.cfg

Mode: standalone

[root@localhost bin]#

停止zookeeper进程

[root@localhost bin]# zkServer.sh stop

ZooKeeper JMX enabled by default

Using config: /home/ftpuser/zookeeper-3.4.9/bin/../conf/zoo.cfg

Stopping zookeeper ... STOPPED

[root@localhost bin]#

客户端连接zookeeper

[root@localhost bin]# ./zkCli.sh -server 192.168.2.129:2181

 

输入help命令来查看有哪些命令

 

[zk: 192.168.2.129:2181(CONNECTED) 0] help

ZooKeeper -server host:port cmd args

connect host:port

get path [watch]

ls path [watch]

set path data [version]

rmr path

delquota [-n|-b] path

quit

printwatches on|off

create [-s] [-e] path data acl

stat path [watch]

close

ls2 path [watch]

history

listquota path

setAcl path acl

getAcl path

sync path

redo cmdno

addauth scheme auth

delete path [version]

setquota -n|-b val path

[zk: 192.168.2.129:2181(CONNECTED) 1]