linux安装zookeeper

关闭防火墙

//关闭防火墙
systemctl stop firewalld.service
//禁止防火墙开机启动
systemctl disable firewalld.service

安装

wget -c https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz
tar -zxvf zookeeper-3.4.11.tar.gz
mkdir -p /usr/local/server/zookeeper-3.4.11
mv zookeeper-3.4.11 /usr/local/server/zookeeper-3.4.11
cd /usr/local/server/zookeeper-3.4.11
mkdir data_1
mkdir logs
cd conf
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg

编辑zoo.cfg,添加data_1和log目录,如下:

# 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/server/zookeeper-3.4.11/data_1
dataLogDir=/usr/local/server/zookeeper-3.4.11/logs
# the port at which the clients will connect
clientPort=2181
server.1=192.168.133.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指定的目录下,新建myid文件。

例如:/usr/local/server/zookeeper-3.4.11/data_1下,新建myid。在myid文件中输入1。表示为server.1。 如果为data_2,则myid文件中的内容为 2。

此配置不成功,启动zookeeper不成功

[root@bogon bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/server/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.

zookeeper.out 会打印以下错误

2018-02-08 00:12:29,850 [myid:] - ERROR [main:QuorumPeerConfig@347] - Invalid configuration, only one server specified (ignoring)

启动zookeeper

[root@bogon zookeeper]# cd ..
[root@bogon zookeeper]# cd bin
[root@bogon bin]# ls
README.txt    zkCli.cmd  zkEnv.cmd  zkServer.cmd  zookeeper.out
zkCleanup.sh  zkCli.sh   zkEnv.sh   zkServer.sh
[root@bogon bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/server/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查看zookeeper状态

[root@bogon bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/server/zookeeper/bin/../conf/zoo.cfg
Mode: standalone

看到Mode: standalone 表示启动成功

查看zookeeper输出文件

tail zookeeper.out

链接zookeepe客户端

[root@bogon bin]# ./zkCli.sh -server 192.168.133.129:2181
Connecting to 192.168.133.129:2181
2018-02-09 00:14:32,045 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.11-37e277162d567b55a07d1755f0b31c32e93c01a0, built on 11/01/2017 18:06 GMT
2018-02-09 00:14:32,051 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=bogon
2018-02-09 00:14:32,052 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_151
2018-02-09 00:14:32,060 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-02-09 00:14:32,060 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64/jre
2018-02-09 00:14:32,061 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/server/zookeeper3/bin/../build/classes:/usr/local/server/zookeeper3/bin/../build/lib/*.jar:/usr/local/server/zookeeper3/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/server/zookeeper3/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/server/zookeeper3/bin/../lib/netty-3.10.5.Final.jar:/usr/local/server/zookeeper3/bin/../lib/log4j-1.2.16.jar:/usr/local/server/zookeeper3/bin/../lib/jline-0.9.94.jar:/usr/local/server/zookeeper3/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/server/zookeeper3/bin/../zookeeper-3.4.11.jar:/usr/local/server/zookeeper3/bin/../src/java/lib/*.jar:/usr/local/server/zookeeper3/bin/../conf:
2018-02-09 00:14:32,061 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-02-09 00:14:32,061 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-02-09 00:14:32,061 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2018-02-09 00:14:32,061 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2018-02-09 00:14:32,061 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2018-02-09 00:14:32,062 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-693.5.2.el7.x86_64
2018-02-09 00:14:32,062 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2018-02-09 00:14:32,062 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2018-02-09 00:14:32,062 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/local/server/zookeeper3/bin
2018-02-09 00:14:32,064 [myid:] - INFO  [main:ZooKeeper@441] - Initiating client connection, connectString=192.168.133.129:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@3eb07fd3
2018-02-09 00:14:32,123 [myid:] - INFO  [main-SendThread(192.168.133.129:2181):ClientCnxn$SendThread@1035] - Opening socket connection to server 192.168.133.129/192.168.133.129:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2018-02-09 00:14:32,240 [myid:] - INFO  [main-SendThread(192.168.133.129:2181):ClientCnxn$SendThread@877] - Socket connection established to 192.168.133.129/192.168.133.129:2181, initiating session
2018-02-09 00:14:32,249 [myid:] - INFO  [main-SendThread(192.168.133.129:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server 192.168.133.129/192.168.133.129:2181, sessionid = 0x100005b37d20001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

停止zookeeper

[root@bogon bin]# ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /usr/local/server/zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED

防火墙开放端口

在防火墙中打开要用到的端口,一般默认是开放了22端口,所以我们才能使用远程工具使用22进行连接,现在配置2181 2888 3888端口

chkconfig iptables on设置开机启动

service iptables start 启动防火墙

[root@bogon server]# chkconfig iptables on
error reading information on service iptables: No such file or directory
[root@bogon server]# service iptables start
Redirecting to /bin/systemctl start iptables.service
Failed to start iptables.service: Unit not found.

报以上的错误,则需要 执行yum install iptables-services下载安装插件

[root@bogon server]# yum install iptables-services
Loaded plugins: fastestmirror, langpacks
........
Installed size: 25 k
Is this ok [y/d/N]: y
Downloading packages:
......
Complete!

完成后,再次执行chkconfig iptables on,service iptables start命令

[root@bogon server]# chkconfig iptables on
Note: Forwarding request to 'systemctl enable iptables.service'.
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@bogon server]# service iptables start
Redirecting to /bin/systemctl start iptables.service

开放防火墙端口

[root@bogon server]# vi /etc/sysconfig/iptables

添加zookeeper要开放的端口 2181 2888 3888

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
#zookeeper
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2181 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2888 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3888 -j ACCEPT

-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

重启防火墙

[root@bogon server]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service

测试是否开通

[root@bogon server]# telnet 192.168.133.129 2181
Trying 192.168.133.129...
Connected to 192.168.133.129.
Escape character is '^]'.

这样就表明开通了防火墙