Linux下zookeeper的搭建使用

apache版本地址:

https://zookeeper.apache.org/

cdh版本地址:

http://archive.cloudera.com/cdh5/cdh/5/

 

##推荐下载版本:  zookeeper-3.4.5-cdh5.13.1.tar.gz ;

##除非特别说明,否则以下所有操作均在3台机器一样执行;

 

1.zookeeper集群规划

     机器ip hostname  zookeeper      myid    user
192.168.x.20  hadoop01           √        1  hadoop
192.168.x.21  hadoop02           √        2  hadoop
192.168.x.22  hadoop03           √        3  hadoop

2.前提准备

a.关闭防护墙

systemctl stop firewalld.service

systemctl disable firewalld.service

b.在root用户下新建普通用户hadoop

useradd hadoop                                            

echo hadoop | passwd --stdin hadoop  ##设置用户密码

su - hadoop                                           ##切换到hadoop用户下,之后的所有操作均在hadoop下

c.添加jdk及其环境变量

##请自行下载jdk1.8版本的tar.gz包,并解压到/home/hadoop下,然后重命名为jdk

vi ~/.bashrc

export JAVA_HOME=/home/hadoop/jdk

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

source ~/.bashrc                                  ##刷新环境变量

java -version                                        ##查看java是否安装成功

 

3.搭建zookeeper

a.下载解压zk

cd /home/hadoop

wget http://archive.cloudera.com/cdh5/cdh/5/zookeeper-3.4.5-cdh5.13.1.tar.gz

tar -xvf zookeeper-3.4.5-cdh5.13.1.tar.gz

mv  zookeeper-3.4.5-cdh5.13.1.tar.gz zookeeper

mkdir zookeeper/logs

b.配置环境变量

vi ~/.bashtc                                        ##添加并修改为:

export JAVA_HOME=/home/hadoop/jdk
export ZOOKEEPER_HOME=/home/hadoop/zookeeper
export ZOO_LOG_DIR=/home/hadoop/zookeeper/logs

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$ZOOKEEPER_HOME/bin:$PATH

c.核心配置文件zoo.cfg修改配置

vi ./zookeeper/conf/zoo.cfg                 ##直接修改为:

dataDir=/home/haodoop/zookeeper/data

dataLogDir=/home/hadoop/zookeeper/datalog

# the port at which the clients will connect

clientPort=2181

tickTime=2000

initLimit=40

syncLimit=20

maxClientCnxns=10000

minSessionTimeout=4000

maxSessionTimeout=180000

autopurge.purgeInterval=24

autopurge.snapRetainCount=5



server.1=192.168.x.20:2888:3888                      

server.2=192.168.x.21:2888:3888

server.3=192.168.x.22:2888:3888

##创建所需目录

mkdir /home/hadoop/zookeeper/data
mkdir /home/hadoop/zookeeper/datalog

##创建唯一识别的myid,分别在不同节点上执行:

echo 1 > /home/hadoop/zookeeper/data/myid                 ##192.168.x.20节点

echo 2 > /home/hadoop/zookeeper/data/myid                 ##192.168.x.21节点

echo 3 > /home/hadoop/zookeeper/data/myid                 ##192.168.x.22节点

 

d.设置zk进程最大能使用的内存大小

vi ./zookeeper/conf/java.env

#!/bin/sh

export JAVA_HOME=/home/hadoop/jdk

# heap size MUST be modified according to cluster environment

export JVMFLAGS="-Xms8192m -Xmx8192m $JVMFLAGS"

e.配置zk滚动生成日志

vi ~/.bashrc                   ##在环境变量中添加:

export ZOO_LOG4J_PROP=INFO,ROLLINGFILE

vi zookeeper/conf/log4j.properties      

##设置生产10个2G的日志文件。直接修改为:

# Copyright 2012 The Apache Software Foundation
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
# Define some default values that can be overridden by system properties
zookeeper.root.logger=INFO,ROLLINGFILE
 
zookeeper.console.threshold=INFO
 
zookeeper.log.dir=.
zookeeper.log.file=zookeeper-1.log
zookeeper.log.threshold=INFO
zookeeper.log.maxfilesize=2048MB
zookeeper.log.maxbackupindex=10
 
zookeeper.tracelog.dir=.
zookeeper.tracelog.file=zookeeper_trace.log
 
log4j.rootLogger=${zookeeper.root.logger}
 
#
# console
# Add "console" to rootlogger above if you want to use this
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n
 
#
# Add ROLLINGFILE to rootLogger to get log file output
#
 
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}
log4j.appender.ROLLINGFILE.MaxFileSize=${zookeeper.log.maxfilesize}
log4j.appender.ROLLINGFILE.MaxBackupIndex=${zookeeper.log.maxbackupindex}
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n
 
#
# Add TRACEFILE to rootLogger to get log file output
#    Log TRACE level and above messages to a log file
 
#
log4j.appender.TRACEFILE=org.apache.log4j.FileAppender
log4j.appender.TRACEFILE.Threshold=TRACE
log4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file}
 
log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout
### Notice we are including log4j's NDC here (%x)
log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n

 

4.启动并查看zookeeper

source ~/.bashrc   

zkServer.sh start                                 ##启动zk

jps                                                       ##查看进程

zkServer.sh status                             ##查看zk运行状态

或者:

echo stat | nc 192.168.x.20 2181

Linux下zookeeper的搭建使用_第1张图片

 

5.zkCli.sh常用命令

zkCli.sh                                             ##连接本地服务端

zkCli.sh -server ip:port                      ##连接指定IP,端口的服务端

zkCli.sh -server 192.168.x.24:2181  

help                                                   ##查看命令帮助

Linux下zookeeper的搭建使用_第2张图片

create /zk-book 123                          ##创建/zk-book节点,数据为123

ls /                                                     ##列举/ 路径下的文件

get /zk-book                                      ##查看具体信息

set /zk-book 567                               ##修改内容,dataVersion的版本也加了1

delete /brokers/ids                            ##删除节点

rmr /brokers                                      ##删除文件夹

deleteall /brokers                              ##删除文件夹;如果某个版本的zk没有rmr命令,应该会有deleteall命令

 

 

 

你可能感兴趣的:(大数据运维zookeeper)