zk是通过java编写的,运行zk时我们得提供jre环境,下面给出示例安装openjdk-8
yum install java-1.8.0-openjdk
[root@localhost jiangjian]# groupadd zookeeper
[root@localhost jiangjian]# useradd -g zookeeper -d /opt/zookeeper -s /sbin/noloigin zookeeper
wget http://apache.mirrors.ionfish.org/zookeeper/stable/zookeeper-3.4.12.tar.gz
mv zookeeper-3.4.10.tar.gz /opt
cd /opt
tar xzf zookeeper-3.4.10.tar.gz
ln -s zookeeper-3.4.10 ./zookeeper
cd zookeeper
mkdir data
chown -R zookeeper:zookeeper /opt/zookeeper/*
cp /opt/zookeeper/conf/zoo_sample.cfg /opt/zookeeper/conf/zoo.cfg
zoo.cfg配置修改如下
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/opt/zookeeper/data
clientPort=2181
server.1=192.168.187.75:2888:3888
server.2=192.168.187.77:2888:3888
server.3=192.168.187.78:2888:3888
/opt/zookeeper/bin/zkServer.sh start
创建文件/usr/lib/systemd/system/zookeeper.service
,输入如下内容:
[Unit]
Description=Zookeeper Service
[Service]
Type=simple
WorkingDirectory=/opt/zookeeper/
PIDFile=/opt/zookeeper/data/zookeeper_server.pid
SyslogIdentifier=zookeeper
User=zookeeper
Group=zookeeper
ExecStart=/opt/zookeeper/bin/zkServer.sh start
ExecStop=/opt/zookeeper/bin/zkServer.sh stop
Restart=always
TimeoutSec=20
SuccessExitStatus=130 143
Restart=on-failure
[Install]
WantedBy=multi-user.target
注册服务:
systemctl daemon-reload
systemctl enable zookeeper.service
systemctl start zookeeper.service
使用/opt/zookeeper/bin/zkCli.sh
[root@localhost zookeeper]# /opt/zookeeper/bin/zkCli.sh
Connecting to localhost:2181
2018-12-25 05:26:13,332 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
2018-12-25 05:26:13,338 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost
2018-12-25 05:26:13,338 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_191
2018-12-25 05:26:13,353 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-12-25 05:26:13,353 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-0.el7_5.x86_64/jre
2018-12-25 05:26:13,353 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper/bin/../build/classes:/opt/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/opt/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/opt/zookeeper/bin/../lib/netty-3.10.6.Final.jar:/opt/zookeeper/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper/bin/../lib/jline-0.9.94.jar:/opt/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper/bin/../zookeeper-3.4.12.jar:/opt/zookeeper/bin/../src/java/lib/*.jar:/opt/zookeeper/bin/../conf:
2018-12-25 05:26:13,353 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-12-25 05:26:13,353 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-12-25 05:26:13,354 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=
2018-12-25 05:26:13,354 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-12-25 05:26:13,354 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-12-25 05:26:13,354 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-862.el7.x86_64
2018-12-25 05:26:13,354 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2018-12-25 05:26:13,354 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2018-12-25 05:26:13,355 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/zookeeper-3.4.12
2018-12-25 05:26:13,356 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@421faab1
Welcome to ZooKeeper!
2018-12-25 05:26:13,994 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-12-25 05:26:14,447 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@878] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2018-12-25 05:26:14,517 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x10000904aae0001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:No