centos7安装zookeeper单机版

参考 https://blog.csdn.net/u011781521/article/details/75948883

 1.ZooKeeper是用Java编写的,运行在Java环境上,这里需要jdk环境 

    可参考 https://www.cnblogs.com/zsls-lang/p/11687510.html

2.下载ZooKeeper

 

[root@zsls ~]# cd /usr/local/src/

[root@zsls src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.14.tar.gz

目前的最新版本3.5.5开始,带有bin名称的包才是我们想要的下载可以直接使用的里面有编译后的二进制的包,而之前的普通的tar.gz的包里面是只是源码的包无法直接使用

若果版本选择的是3.5.5之后 使用带bin的 例如  wget https://archive.apache.org/dist/zookeeper/stable/apache-zookeeper-3.5.6-bin.tar.gz

3.解压文件

[root@zsls src]# tar -zxvf zookeeper-3.4.14.tar.gz -C /usr/local/

4.配置zookeeper

[root@zsls src]# cd ../zookeeper-3.4.14/
[root@zsls zookeeper-3.4.14]# ls

在zookeeper目录下创建以下目录

[root@zsls zookeeper-3.4.14]# mkdir data
[root@zsls zookeeper-3.4.14]# mkdir logs

将zookeeper-3.4.14/conf目录下的zoo_sample.cfg文件拷贝一份,命名为zoo.cfg

[root@zsls zookeeper-3.4.14]# cd conf/

[root@zsls conf]# cp zoo_sample.cfg zoo.cfg 

[root@zsls conf]# vim zoo.cfg

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 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-3.4.14/data
dataLogDir=/usr/local/zookeeper-3.4.14/logs
# 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
server.1=192.168.93.140:2888:3888

===========================================

其中:
2888端口号是zookeeper服务之间通信的端口。
3888是zookeeper与其他应用程序通信的端口

然后在dataDir=/usr/local/zookeeper-3.4.14/data下创建myid文件(编辑myid文件,并在对应的IP的机器上输入对应的编号。如在zookeeper上,myid 文件内容就是1。如果只在单点上进行安装配置,那么只有一个server.1)

[root@zsls data]# vim myid
[root@zsls data]# cat myid
1
[root@zsls data]#

5.端口开通防火墙

[root@zsls data]# sudo firewall-cmd --zone=public --add-port=2181/tcp --permanent

[root@zsls data]# sudo firewall-cmd --zone=public --add-port=2888/tcp --permanent
[root@zsls data]# sudo firewall-cmd --zone=public --add-port=3888/tcp --permanent

[root@zsls data]#sudo firewall-cmd --reload

6.测试zookeeper

启动并测试zookeeper

[root@zsls bin]# pwd
/usr/local/zookeeper-3.4.14/bin

[root@zsls bin]# ./zkServer.sh start

查看进程

[root@zsls bin]# jps
10496 Jps
10479 QuorumPeerMain   #其中,QuorumPeerMain是zookeeper进程,启动正常

查看状态

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

服务器输出信息

[root@zsls logs]# tail -1000f zookeeper.out

停止zookeeper进程

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

7.设置zookeeper服务开机启动


# 切换到/etc/rc.d/init.d/目录下
cd /etc/rc.d/init.d

# 创建zookeeper文件
touch zookeeper

#更新权限
chmod +x zookeeper

#编辑文件,在zookeeper里面输入如下内容

#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=/usr/local/jdk1.8.0_191
export PATH=$JAVA_HOME/bin:$PATH
case $1 in
start)su root /usr/local/zookeeper-3.4.14/bin/zkServer.sh start;;
stop)su root /usr/local/zookeeper-3.4.14/bin/zkServer.sh stop;;
status)su root /usr/local/zookeeper-3.4.14/bin/zkServer.sh status;;
restart)su root /usr/local/zookeeper-3.4.14/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac

====================================================

示例,zookeeper的前三行如下:
#!/bin/sh
#chkconfig: 2345 20 90
#description:zookeeper
第一行,告诉系统使用的shell,所以的shell脚本都是这样。
第二行,chkconfig后面有三个参数2345,80和90告诉chkconfig程序,需要在rc2.d~rc5.d目录下,创建名字为 S20zookeeper的文件连接,连接到/etc/rc.d/init.d目录下的的zookeeper脚本。第一个字符是S,系统在启动的时候,运行脚本zookeeper,就会添加一个start参数,告诉脚本,现在是启动模式。同时在rc0.d和rc6.d目录下,创建名字为K90zookeeper的文件连接,第一个字符为K,系统在关闭系统的时候,会运行zookeeper,添加一个stop,告诉脚本,现在是关闭模式。
注意上面的三行中,第二,第三行是必须的,否则在运行chkconfig --add zookeeper时,会报错。

=====================================================================

然后我们就可以用service zookeeper start/stop来启动停止zookeeper服务了

使用命令把zookeeper添加到开机启动里面

[root@zsls init.d]# chkconfig zookeeper on
[root@zsls init.d]# chkconfig --add zookeeper

添加完成之后接这个使用chkconfig --list 来看看我们添加的zookeeper是否在里面

 

posted @ 2019-05-14 23:03  zsls-lang  阅读(1210)  评论(0编辑  收藏  举报