zookeeper部署
前置工作
- 下载zookeeper-3.4.13.tar.gz
- 安装jdk1.8
部署zookeeper
单机版
单机版zookeeper不具备容灾功能,生产环境不推荐使用,但是如果部署的机器有限可以使用单机版部署方案
安装zookeeper
tar -zvxf zookeeper-3.4.13.tar.gz -C /data
mv /data/zookeeper-3.4.13 /data/zookeeper
可以在/data目录下看到zookeeper安装情况
配置zookeeper
-
创建zookeeper的数据目录
mkdir /data/zookeeper/data
-
进入zookeeper的conf目录并初始化配置文件zoo.cfg
cd /data/zookeeper/conf cp zoo_sample.cfg zoo.cfg
-
编辑conf文件
#创建数据目录 mkdir /data/zookeeper/data #编辑配置文件 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 # example sakes. dataDir=/data/zookeeper/data # the port at which the clients will connect clientPort=2181 # 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 autopurge.snapRetainCount=15 autopurge.purgeInterval=24
修改dataDir目录为/data/zookeeper/data
autopurge.snapRetainCount=15
autopurge.purgeInterval=24新增这两行配置,用于清理事务日志,每24小时清理一次,最多允许15个文件
-
编辑配置环境变量文件
#创建日志目录 mkdir /data/zookeeper/logs touch /data/zookeeper/conf/zookeeper-env.sh chmod +x /data/zookeeper/conf/zookeeper-env.sh vi /data/zookeeper/conf/zookeeper-env.sh #JAVA_HOME视实际情况而定 JAVA_HOME=/usr/local/java/jdk1.8.0_241 ZOO_LOG4J_PROP=INFO,ROLLINGFILE ZOO_LOG_DIR=/data/zookeeper/logs
集群版
集群版适用于生产环境使用,假设集群三台主机IP为192.168.0.1,192.168.0.2,192.168.0.3
-
参照单机版方案部署在三台机器上面分别部署zookeeper服务。
-
修改三台机器的配置文件zoo.cfg
vi /data/zookeeper/conf/zoo.cfg #新增配置 server.1=192.168.0.1:2888:3888 server.2=192.168.0.2:2888:3888 server.3=192.168.0.3:2888:3888
-
创建myid文件,在dataDir目录创建myid文件
touch /data/zookeeper/data/myid #myid的内容对于上面的机器配置,如果机器ip是server.1则myid中写入1,参照示例,如果主机ip是192.168.0.1,则myid写入1 echo 1 > /data/zookeeper/data/myid
启动zookeeper服务
在部署了zookeeper的服务器上面启动zookeeper服务
#更改zk执行用户权限
chown -R zk:zk /data/zookeeper
#切换zk用户
su zk
#进入启动命令目录
cd /data/zookeeper/bin
#启动zk服务
./zkServer.sh start
#查看zk启动状态
./zkServer.sh status