在Ubuntu上搭建Zookeeper集群
Zookeeper集群搭建
集群安装
先下载zookeeper压缩包(可从官网下载)
我这里是apache-zookeeper-3.5.8-bin.tar.gz
在各台主机上解压到/usr/local/zookeeper
tar -zxvf /home/hadoop/桌面/apache-zookeeper-3.5.8-bin.tar.gz su //进入管理员 mv /home/hadoop/apache-zookeeper-3.5.8-bin /usr/local/zookeeper exit
修改配置文件
cd /usr/local/zookeeper/conf/
cp zoo_sample.cfg zoo.cfg
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=/usr/local/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 server.1=master:2888:3888 server.2=slave1:2888:3888 server.3=slave2:2888:3888 server.4=slave3:2888:3888
改完后保存退出!
新建data文件夹
cd /usr/local/zookeeper/
mkdir data
分别设定在各主机使用一个命令表示id
这里直接在各个主机分别输入(echo "1" > /usr/local/zookeeper/data/myid 中的1是第一台主机,其余的主机往后递增)
echo "1" > /usr/local/zookeeper/data/myid
测试:
1.启动ZooKeeper
在各个主机都输入:
cd /usr/local/zookeeper/bin/
./zkServer.sh start
显示如下
ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
2查看状态
在各个主机都输入:
./zkServer.sh status
观察各个主机的Mode显示有一台主机是leader和多台主机是follower就是成功了
检测是否成功启动,在客户端连接下服务端
./zkCli.sh -server slave1:2181
显示如下:
Connecting to slave1:2181 2020-10-17 01:51:39,229 [myid:] - INFO [main:Environment@109] - Client environment:zookeeper.version=3.5.8-f439ca583e70862c3068a1f2a7d4d068eec33315, built on 05/04/2020 15:07 GMT 2020-10-17 01:51:39,233 [myid:] - INFO [main:Environment@109] - Client environment:host.name=master 2020-10-17 01:51:39,233 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_265 2020-10-17 01:51:39,236 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Private Build 2020-10-17 01:51:39,236 [myid:] - INFO [main:Environment@109] - Client environment:java.home=/usr/lib/jvm/java-8-openjdk-amd64/jre 2020-10-17 01:51:39,236 [myid:] - INFO [main:Environment@109] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.5.8.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.5.8.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-resolver-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-handler-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-common-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-codec-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/netty-buffer-4.1.48.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/json-simple-1.1.1.jar:/usr/local/zookeeper/bin/../lib/jline-2.11.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.10.3.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.10.3.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.10.3.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf:.:/usr/share/java/jdk1.8.0_261/lib:/lib 2020-10-17 01:51:39,236 [myid:] - INFO [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:java.io.tmpdir=/tmp 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=<NA> 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Linux 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:os.version=4.15.0-120-generic 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:user.name=hadoop 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:user.home=/home/hadoop 2020-10-17 01:51:39,237 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=/usr/local/zookeeper/bin 2020-10-17 01:51:39,238 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=25MB 2020-10-17 01:51:39,239 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=228MB 2020-10-17 01:51:39,239 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=31MB 2020-10-17 01:51:39,242 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=slave1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@726f3b58 2020-10-17 01:51:39,249 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation 2020-10-17 01:51:39,259 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes 2020-10-17 01:51:39,268 [myid:] - INFO [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled= Welcome to ZooKeeper! 2020-10-17 01:51:39,386 [myid:slave1:2181] - INFO [main-SendThread(slave1:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server slave1/192.168.58.129:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2020-10-17 01:51:39,570 [myid:slave1:2181] - INFO [main-SendThread(slave1:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /192.168.58.128:48134, server: slave1/192.168.58.129:2181 2020-10-17 01:51:39,748 [myid:slave1:2181] - INFO [main-SendThread(slave1:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server slave1/192.168.58.129:2181, sessionid = 0x200005f99630000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: slave1:2181(CONNECTED) 0]
现在就可以通过客户端执行基本命令 ls /
[zk: slave2:2181(CONNECTED) 0] create /test 111 Created /test [zk: slave2:2181(CONNECTED) 1] ls / [test, zookeeper] [zk: slave2:2181(CONNECTED) 2] get /test 111
验证数据是否同步
[zk: slave1:2181(CONNECTED) 0] get /test 111 [zk: slave1:2181(CONNECTED) 1]
通过验证可以看出 在服务slave2上创建的节点,在slave1上也可以获取到,说明集群部署是OK的.
配置环境变量(进入管理员)(在每台主机上执行相同的操作)
su
vim /etc/profile
在最后添加:
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
保存退出(:wq!)
使之生效
source /etc/profile
如果想查看zookeeper的版本
cd /usr/local/zookeeper/conf下,用vim打开zoo.cfg,在最后添加:
4lw.commands.whitelist=*
回到bin目录重启zookeeper
cd /usr/local/zookeeper/bin/
./zkServer.sh restart //重启服务端
./zkCli.sh //启动客户端
就可以通过以下命令查看zookeeper的版本信息了
echo stat|nc localhost 2181
信息如下:
ookeeper version: 3.5.8-f439ca583e70862c3068a1f2a7d4d068eec33315, built on 05/04/2020 15:07 GMT Clients: /127.0.0.1:42040[0](queued=0,recved=1,sent=0) Latency min/avg/max: 0/0/0 Received: 1 Sent: 0 Connections: 1 Outstanding: 0 Zxid: 0x100000007 Mode: follower Node count: 6
写的不好还请多指教。