1.下载zookeeper
下载地址:https://archive.apache.org/dist/zookeeper/,这里我们使用3.4.6版本。
[[email protected] software]$ wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
2.解压 zookeeper 安装包
[[email protected] software]$ tar -zxvf zookeeper-3.4.6.tar.gz
3.在zookeeper-3.4.6 目录下创建data数据目录和logs日志目录:
[[email protected] software]$ cd zookeeper-3.4.6
[winner[email protected] zookeeper-3.4.6]$ mkdir data
[winner[email protected] zookeeper-3.4.6]$ mkdir logs
4.将 zookeeper-3.4.6/conf 目录下的 zoo_sample.cfg 文件拷贝一份,命名为为zoo.cfg
[[email protected] zookeeper-3.4.6]$ cd conf/
[winner-0715@localhost conf]$ ll
total 12
-rw-rw-r--. 1 winner-0715 winner-0715 535 Feb 20 2014 configuration.xsl
-rw-rw-r--. 1 winner-0715 winner-0715 2161 Feb 20 2014 log4j.properties
-rw-rw-r--. 1 winner-0715 winner-0715 922 Feb 20 2014 zoo_sample.cfg
[winner-0715@localhost conf]$ cp zoo_sample.cfg zoo.cfg
[winner-0715@localhost conf]$ ll
total 16
-rw-rw-r--. 1 winner-0715 winner-0715 535 Feb 20 2014 configuration.xsl
-rw-rw-r--. 1 winner-0715 winner-0715 2161 Feb 20 2014 log4j.properties
-rw-rw-r--. 1 winner-0715 winner-0715 922 Dec 30 11:52 zoo.cfg
-rw-rw-r--. 1 winner-0715 winner-0715 922 Feb 20 2014 zoo_sample.cfg
5.修改 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/winner-0715/software/zookeeper-3.4.6/data
dataLogDir=/home/winner-0715/software/zookeeper-3.4.6/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
#
# 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=192.168.41.128:2888:3888
其中,
2888 端口号是 zookeeper 服务之间通信的端口。
3888 是 zookeeper 与其他应用程序通信的端口。
edu-provider-01 是在 hosts 中已映射了 IP 的主机名。
initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。
当已经超过 10 个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是5*2000=10 秒。
syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2*2000=4秒。
server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器; B 是这个服务器的 IP 地址或/etc/hosts 文件中映射了 IP 的主机名; C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口; D 表示的是万一集群中的 Leader 服务
器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是
一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号
6.在 dataDir=/home/winner-0715/software/zookeeper-3.4.6/data下创建 myid 文件
编辑 myid 文件,并在对应的 IP 的机器上输入对应的编号。如在 zookeeper 上, myid文件内容就是 1。 如果只在单点上进行安装配置, 那么只有一个 server.1。
[winner-0715@localhost data]$ vim myid
[winner-0715@localhost data]$ ll
total 4
-rw-rw-r--. 1 winner-0715 winner-0715 2 Dec 30 12:11 myid
[winner-0715@localhost data]$ cat myid
1
7.配置zookeeper的环境变量。
[[email protected] ~]$ vim .bash_profile
[winner[email protected] ~]$ cat .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
#zookeeper env
export ZOOKEEPER_HOME=/home/winner-0715/software/zookeeper-3.4.6
export PATH=$ZOOKEEPER_HOME/bin:$PATH
.bash_profile在用户目录下。
8. 在防火墙中打开要用到的端口 2181、 2888、 3888
切换到 root 用户权限,执行以下命令:
[[email protected] winner-0715]#chkconfig iptables on
[[email protected] winner-0715]#service iptables start
[[email protected] winner-0715]#vi /etc/sysconfig/iptables
[[email protected] winner-0715]# cat /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2181 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2888 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3888 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
看下22端口就知道怎么改了!
重启防火墙:
[[email protected]localhost winner-0715]# service iptables restart
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
查看防火墙端口状态:
[[email protected] winner-0715]# service iptables status
Table: filter
1Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:2181
6 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:2888
7 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:3888
8 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
num target prot opt source destination
1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
9.启动并测试 zookeeper(要用一般用户启动,不要用 root) :
(1)因为配置了环境变量,所以不需要切换到zookeeper的bin目录下就可以执行启动命令。
[winner[email protected] ~]$ zkServer.sh start
JMX enabled by default
Using config: /home/winner-0715/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
(2)输入 jps 命令查看进程:
[[email protected] ~]$ jps
4954 Jps
4924 QuorumPeerMain
其中, QuorumPeerMain 是 zookeeper 进程,启动正常
(3) 查看状态:
[[email protected] ~]$ zkServer.sh status
JMX enabled by default
Using config: /home/winner-0715/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone
(4) 查看 zookeeper 服务输出信息:
由于服务信息输出文件在/home/winner-0715/zookeeper-3.4.6/bin/zookeeper.out
$ tail -500f zookeeper.out
10.停止 zookeeper 进程:
[[email protected] bin]$ ./zkServer.sh stop
JMX enabled by default
Using config: /home/winner-0715/software/zookeeper-3.4.6/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
11.配置zookeeper开机启动
编辑/etc/rc.local 文件,加入:
su winner-0715 -c ‘/home/winner-0715/zookeeper-3.4.6/bin/zkServer.sh start‘
-c:call,调用后面的脚本的意思。