集群搭建环境:
发行版:CentOS-6.6 64bit
内核:2.6.32-504.el6.x86_64
CPU:intel-i7 3.6G
内存:2G
集群搭建步骤:
1. 确保机器安装了jdk
[root@rocket ~]# java -version
openjdk version "1.8.0_51"
OpenJDK Runtime Environment (build 1.8.0_51-b16)
OpenJDK 64-Bit Server VM (build 25.51-b03, mixed mode)
如果找不到jdk,可以通过yum进行安装,如果发行版不是CentOS,请自行搜索对应发行版的安装方法。
[root@rocket local]# yum -y install java
2. 下载zookeeper,选一个合适的版本
在官网http://zookeeper.apache.org/releases.html找一个合适的镜像进行下载。这里使用北理的镜像进行下载。
[root@rocket local]#
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
下载完成之后分别解压到
[root@rocket local]# tar -zxvf zookeeper-3.4.6.tar.gz zookeeper-server1
[root@rocket local]# tar -zxvf zookeeper-3.4.6.tar.gz zookeeper-server2
[root@rocket local]# tar -zxvf zookeeper-3.4.6.tar.gz zookeeper-server3
3. 拷贝配置文件
[root@rocket local]# cd /usr/local/zookeeper-server1/;cp conf/zoo_sample.cfg conf/zoo_test.cfg
[root@rocket local]# cd /usr/local/zookeeper-server2/;cp conf/zoo_sample.cfg conf/zoo_test.cfg
[root@rocket local]# cd /usr/local/zookeeper-server3/;cp conf/zoo_sample.cfg conf/zoo_test.cfg
4. 修改配置文件
zookeeper-server1
# 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
dataDir=/tmp/zookeeper-server1/data
dataLogDir=/tmp/zookeeper-server1/logs
# the port at which the clients will connect
clientPort=2181
server.1=127.0.0.1:8880:7770
server.2=127.0.0.1:8881:7770
server.3=127.0.0.1:8882:7770
zookeeper-server2
# 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
dataDir=/tmp/zookeeper-server2/data
dataLogDir=/tmp/zookeeper-server2/logs
# the port at which the clients will connect
clientPort=3181
server.1=127.0.0.1:8880:7770
server.2=127.0.0.1:8881:7770
server.3=127.0.0.1:8882:7770
zookeeper-server3
# 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
dataDir=/tmp/zookeeper-server3/data
dataLogDir=/tmp/zookeeper-server3/logs
# the port at which the clients will connect
clientPort=4181
server.1=127.0.0.1:8880:7770
server.2=127.0.0.1:8881:7770
server.3=127.0.0.1:8882:7770
5. 建立zookeeper运行目录,data目录和log目录
[root@rocket local]# mkdir -p zookeeper-server1/logs zookeeper-server1/data
[root@rocket local]# mkdir -p zookeeper-server2/logs zookeeper-server2/data
[root@rocket local]# mkdir -p zookeeper-server3/logs zookeeper-server3/data
查看当前运行目录情况:
[root@rocket tmp]# tree
├── zookeeper-server1
│ ├── data
│ └── logs
├── zookeeper-server2
│ ├── data
│ └── logs
└── zookeeper-server3
├── data
└── logs
6. 在data目录中创建文件 myid 文件,每个文件中分别写入当前机器的server id
[root@rocket tmp]# echo "1" > zookeeper-server1/data/myid
[root@rocket tmp]# echo "2" > zookeeper-server2/data/myid
[root@rocket tmp]# echo "3" > zookeeper-server3/data/myid
7. 在每个目录中启动zookeeper
[root@rocket local]# cd /usr/local/zookeeper-server1/;bin/zkServer.sh start
[root@rocket local]# cd /usr/local/zookeeper-server2/;bin/zkServer.sh start
[root@rocket local]# cd /usr/local/zookeeper-server3/;bin/zkServer.sh start
查看启动状态
[root@iZ257w5531qZ bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
注意,这里如果提示
Error contacting service. It is probably not running.
说明进程启动失败或集群未建立成功,这时需要查看对应目录中的zookeeper.out日志去找到相应的原因并去解决它。
8. 测试连接zookeeper集群
[root@rocket local]# cd zookeeper-server1;bin/zkCli.sh -server localhost:2181
可以看到这里已经连接成功了,而且能查看节点情况,说明zookeeper集群已经开始工作了。
参考:zookeeper集群搭建 zookeeper配置详解