目录
文章目录
1.1 机器准备
#一台虚拟机
hostname:myhost
ip:ip_address
1.2 系统环境(防火墙+selinux)
[yuki@myhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[yuki@myhost ~]$ sudo firewall-cmd --state #查看防火墙的状态
running
[yuki@myhost ~]$ sudo firewall-cmd --list-ports #查看防火墙已经开放的端口
5666/tcp 10050/tcp
[yuki@myhost ~]$ sudo firewall-cmd --list-services #查看防火墙开放的服务
dhcpv6-client ssh
#假设设置zookeeper的服务端口为2181,允许2181/tcp端口访问
[yuki@myhost ~]$ sudo firewall-cmd --zone=public --permanent --add-port=2181/tcp
success
[yuki@myhost ~]$ sudo firewall-cmd --reload #重新加载防火墙配置
success
[yuki@myhost ~]$ sudo firewall-cmd --list-ports
5666/tcp 10050/tcp 2181/tcp
[yuki@myhost ~]$ sudo setenforce 0 && sudo getenforce
Permissive
[yuki@myhost ~]$ sudo sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
1.3 软件安装约定
zookeeper软件版本:zookeeper-3.4.12.tar.gz
安装包存放目录:/home/tools
zookeeper安装目录:/home/zookeeper-3.4.12
1.4 下载安装包
ZooKeeper官网下载地址: http://mirrors.hust.edu.cn/apache/zookeeper
[yuki@myhost ~]$ cd /home/tools/
[yuki@myhost tools]$ sudo yum install -y wget
[yuki@myhost tools]$ sudo wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz
[yuki@myhost tools]$ ll
-rw-r--r--. 1 root root 36667596 4月 25 2018 zookeeper-3.4.12.tar.gz
1.5 解压安装包
[yuki@myhost tools]$ tar xf zookeeper-3.4.12.tar.gz
[yuki@myhost tools]$ ll
总用量 24918
drwxr-xr-x. 10 yuki root 4096 3月 27 2018 zookeeper-3.4.12
-rw-r--r--. 1 root root 36667596 4月 25 2018 zookeeper-3.4.12.tar.gz
1.6 安装
[yuki@myhost tools]$ sudo mv /home/tools/zookeeper-3.4.12 /home/zookeeper-3.4.12
[yuki@myhost tools]$ ll /home/zookeeper-3.4.12/
总用量 1616
drwxr-xr-x. 2 yuki root 4096 3月 27 2018 bin
-rw-r--r--. 1 yuki root 87945 3月 27 2018 build.xml
drwxr-xr-x. 2 yuki root 74 3月 27 2018 conf
drwxr-xr-x. 10 yuki root 4096 3月 27 2018 contrib
drwxr-xr-x. 2 yuki root 4096 3月 27 2018 dist-maven
drwxr-xr-x. 6 yuki root 4096 3月 27 2018 docs
-rw-r--r--. 1 yuki root 1709 3月 27 2018 ivysettings.xml
-rw-r--r--. 1 yuki root 8197 3月 27 2018 ivy.xml
drwxr-xr-x. 4 yuki root 4096 3月 27 2018 lib
-rw-r--r--. 1 yuki root 11938 3月 27 2018 LICENSE.txt
-rw-r--r--. 1 yuki root 3132 3月 27 2018 NOTICE.txt
-rw-r--r--. 1 yuki root 1585 3月 27 2018 README.md
-rw-r--r--. 1 yuki root 1770 3月 27 2018 README_packaging.txt
drwxr-xr-x. 5 yuki root 44 3月 27 2018 recipes
drwxr-xr-x. 8 yuki root 4096 3月 27 2018 src
-rw-r--r--. 1 yuki root 1483366 3月 27 2018 zookeeper-3.4.12.jar
-rw-r--r--. 1 yuki root 819 3月 27 2018 zookeeper-3.4.12.jar.asc
-rw-r--r--. 1 yuki root 33 3月 27 2018 zookeeper-3.4.12.jar.md5
-rw-r--r--. 1 yuki root 41 3月 27 2018 zookeeper-3.4.12.jar.sha1
1.7 创建数据目录和日志目录
[yuki@myhost tools]$ cd /home/zookeeper-3.4.12/
[yuki@myhost zookeeper-3.4.12]$ sudo mkdir -p data logs
1.8 配置Zookeeper
[yuki@myhost zookeeper-3.4.12]$ cd /home/zookeeper-3.4.12/conf/
[yuki@myhost conf]$ pwd
/home/zookeeper-3.4.12/conf
[yuki@myhost conf]$ ll
总用量 12
-rw-r--r--. 1 yuki root 535 3月 27 2018 configuration.xsl
-rw-r--r--. 1 yuki root 2161 3月 27 2018 log4j.properties
-rw-r--r--. 1 yuki root 922 3月 27 2018 zoo_sample.cfg
[yuki@myhost conf]$ sudo cp zoo_sample.cfg zoo.cfg
[yuki@myhost conf]$ sudo vi 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=/home/zookeeper-3.4.12/data ######
dataLogDir=/home/zookeeper-3.4.12/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=IP:2888:3888 #####
#
# 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
其中:
2888端口号是zookeeper服务之间通信的端口。
3888是zookeeper与其他应用程序通信的端口。
然后,在dataDir=/home/zookeeper-3.4.12/data 下创建myid文件(编辑myid文件,并在对应的IP的机器上输入对应的编号。例如:在安装zookeeper机器上,myid 文件内容就是1。如果只在单点上进行安装配置,那么只有一个server.1)
[yuki@myhost ~]$ cd /home/zookeeper-3.4.12/data/
[yuki@myhost data]$ sudo vi myid
[xzds@DSCQ-BID29 data]$ cat myid
1
1.9 设置环境变量
[yuki@myhost data]$ sudo vi /etc/profile
#添加如下内容到结尾:
export ZOOKEEPER_HOME=/home/zookeeper-3.4.12
export PATH=$ZOOKEEPER_HOME/bin:$PATH
[yuki@myhost data]$ source /etc/profile
[yuki@myhost data]$ echo $ZOOKEEPER_HOME
/home/zookeeper-3.4.12
[yuki@myhost data]$ echo $PATH
/home/zookeeper-3.4.12/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/xzds/JDK_HOME/jdk1.8.0_131/bin:/home/xzds/.local/bin:/home/xzds/bin:/home/xzds/JDK_HOME/jdk1.8.0_131/bin
2.0 启动Zookeeper
[yuki@myhost data]$ sudo chown -R yuki:root /home/zookeeper-3.4.12/
[yuki@myhost data]$ whoami
yuki
###ZooKeeper相关脚本都在 /home/zookeeper-3.4.12/bin目录下
[yuki@myhost ~]$ cd /home/zookeeper-3.4.12/bin/
[yuki@myhost bin]$ ll
总用量 36
-rwxr-xr-x. 1 yuki root 232 3月 27 2018 README.txt
-rwxr-xr-x. 1 yuki root 1937 3月 27 2018 zkCleanup.sh
-rwxr-xr-x. 1 yuki root 1056 3月 27 2018 zkCli.cmd
-rwxr-xr-x. 1 yuki root 1534 3月 27 2018 zkCli.sh
-rwxr-xr-x. 1 yuki root 1759 3月 27 2018 zkEnv.cmd
-rwxr-xr-x. 1 yuki root 2696 3月 27 2018 zkEnv.sh
-rwxr-xr-x. 1 yuki root 1089 3月 27 2018 zkServer.cmd
-rwxr-xr-x. 1 yuki root 6773 3月 27 2018 zkServer.sh
#启动ZooKeeper服务
#和nohup一样会在执行启动脚本的路径下生成一个zooKeeper.out日志文件
[yuki@myhost bin]$ sudo ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
#查看ZooKeeper进程
[yuki@myhost bin]$ jps
4184 QuorumPeerMain
4206 Jps
其中,QuorumPeerMain是zookeeper进程,启动正常。
[yuki@myhost bin]$ sudo ps -ef |grep zookeeper
yuki 4184 1 0 15:01 pts/1 00:00:00 /home/yuki/JDK_HOME/jdk1.8.0_131/bin/java -Dzookeeper.log.dir=. -Dzookeepe.root.logger=INFO,CONSOLE -cp /home/zookeeper-3.4.12/bin/../build/classes:/home/zookeeper-3.4.12/bin/../build/lib/*.jar:/home/zookeeper-3.4.12/bin/../lib/slf4j-log4j12-1.7.25.jar:/home/zookeeper-3.4.12/bin/../lib/slf4j-api-1.7.25.jar:/home/zookeeper-3.4.12/bin/../lib/netty-3.10.6.Final.jar:/home/zookeeper-3.4.12/bin/../lib/log4j-1.2.17.jar:/home/zookeeper-3.4.12/bin/../lib/jline-0.9.94.jar:/home/zookeeper-3.4.12/bin/../lib/audience-annotations-0.5.0.jar:/home/zookeeper-3.4.12/bin/../zookeeper-3.4.12.jar:/home/zookeeper-3.4.12/bin/../src/java/lib/*.jar:/home/zookeeper-3.4.12/bin/../conf: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false org.apache.zookeeper.server.quorum.QuorumPeerMain /home/zookeepe-3.4.12/bin/../conf/zoo.cfg
yuki 4222 4103 0 15:02 pts/1 00:00:00 grep --color=auto zookeeper
#查看ZooKeeper状态
[yuki@myhost bin]$ sudo ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: standalone
#停止ZooKeeper服务
[yuki@myhost bin]$ sudo ./zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /home/zookeeper-3.4.12/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[yuki@myhost bin]$ sudo jps
4270 Jps
[yuki@myhost bin]$ sudo ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/zookeeper-3.4.12/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
2.1 编写zookeeper启动脚本
[yuki@myhost rc.d]$ cd /etc/rc.d/init.d/
[yuki@myhost init.d]$ ll
总用量 36
-rw-r--r--. 1 root root 13948 9月 16 2015 functions
-rwxr-xr-x. 1 root root 2989 9月 16 2015 netconsole
-rwxr-xr-x. 1 root root 6630 9月 16 2015 network
-rw-r--r--. 1 root root 1160 2月 20 01:35 README
-rwxr-xr-x. 1 root root 1709 4月 24 2017 zabbix-agent
###编写启动脚本
[yuki@myhost init.d]$ sudo vi zookeeper
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
source /etc/profile
#export JAVA_HOME=/home/yuki/JDK_HOME/jdk1.8.0_131
#export PATH=$JAVA_HOME/bin:$PATH
#define variables
zookeeper_start_file="/home/zookeeper-3.4.12/bin/zkServer.sh"
case $1 in
start)
sudo ${zookeeper_start_file} start
;;
stop)
sudo ${zookeeper_start_file} stop
;;
status)
sudo ${zookeeper_start_file} status
;;
restart)
sudo ${zookeeper_start_file} restart
;;
*) echo "require start|stop|status|restart"
;;
esac
然后,我们就可以下面的命令来启动或者停止zookeeper服务了
[yuki@myhost init.d]$ /etc/init.d/zookeeper start 或者service zookeeper start
ZooKeeper JMX enabled by default
Using config: /home/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
2.2 设置zookeeper开机自启动
[yuki@myhost init.d]$ sudo chkconfig zookeeper on
[yuki@myhost init.d]$ echo $?
0
[yuki@myhost init.d]$ sudo chkconfig --add zookeeper
[yuki@myhost init.d]$ sudo chkconfig --list
注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
欲查看对特定 target 启用的服务请执行
'systemctl list-dependencies [target]'。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
zabbix-agent 0:关 1:关 2:关 3:关 4:关 5:关 6:关
zookeeper 0:关 1:关 2:开 3:开 4:开 5:开 6:关
基于 xinetd 的服务:
chargen-dgram: 关
chargen-stream: 关
daytime-dgram: 关
daytime-stream: 关
discard-dgram: 关
discard-stream: 关
echo-dgram: 关
echo-stream: 关
nrpe: 开
tcpmux-server: 关
time-dgram: 关
time-stream: 关