centos6.5环境下Zookeeper-3.4.6集群环境部署
【系统】Centos 6.5
集群部署
【软件】准备好jdk环境,此次我们的环境是open_jdk1.8.0_101
zookeeper-3.4.6.tar.gz
【步骤】
1. 准备条件
如果有内部dns或者外网有域名,则直接使用域名
如果没有需要修改/etc/hosts文件,或者直接使用IP
集群规划
主机类型IP地址
域名
zookeeper1192.168.1.1zookeeper1.chinasoft.com
zookeeper2192.168.1.2zookeeper2.chinasoft.com
zookeeper3192.168.1.3zookeeper3.chinasoft.com
注意:zookeeper因为有主节点和从节点的关系,所以部署的集群台数最好为奇数个,否则可能出现脑裂导致服务异常
2. 安装
下载地址:http://archive.apache.org/dist/zookeeper/zookeeper-3.4.6/
解压
tar -zxf zookeeper-3.4.6.tar.gz
cd zookeeper-3.4.6
拷贝配置文件,修改完成后分发给其他节点
cd /data/zookeeper-3.4.6/
cp zoo_sample.cfg zoo.cfg
cat zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/zookeeper-3.4.6/data
dataLogDir=/data/zookeeper-3.4.6/logs
clientPort=2181
server.1=u04rtv01.yaya.corp:2888:3888
server.2=u04rtv02.yaya.corp:2888:3888
server.3=u04rtv03.yaya.corp:2888:3888
3.创建data和Log文件夹
mkdir /data/zookeeper-3.4.6/data
mkdir /data/zookeeper-3.4.6/logs
4、在zoo.cfg中的dataDir指定的目录下,新建myid文件。
例如:$ZK_INSTALL/data下,新建myid。在myid文件中输入1。表示为server.1。
如果为snapshot/d_2,则myid文件中的内容为 2,依此类推。
启动:在集群中的每台主机上执行如下命令
bin/zkServer.sh start
查看状态,可以看到其中一台为主节点,其他两台为从节点:
bin/zkServer.sh status
主节点:
./zkServer.sh status
JMX enabled by default
Using config: /data/yunva/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: leader
从属节点:
./zkServer.sh status
JMX enabled by default
Using config: /data/yunva/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: follower
停止:
bin/zkServer.sh stop
连接:
bin/zkCli.sh -server zookeeper1:2181
bin/zkCli.sh -server zookeeper2:2181
bin/zkCli.sh -server zookeeper3:2181
报错:
原因就是没有在dataDir目录下创建myid文件并且赋值(如1、2、3分别代表集群中的server1,server2,server3)
2016-08-22 17:55:16,145 [myid:] - INFO [main:QuorumPeerConfig@103] - Reading configuration from: /data/yunva/zookeeper-3.4.6/bin/../conf/zoo.cfg
2016-08-22 17:55:16,150 [myid:] - INFO [main:QuorumPeerConfig@340] - Defaulting to majority quorums
2016-08-22 17:55:16,150 [myid:] - ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /data/yunva/zookeeper-3.4.6/bin/../conf/zoo.cfg
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:123)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:101)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
Caused by: java.lang.IllegalArgumentException: /data/yunva/zookeeper-3.4.6/data/myid file is missing
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:350)
at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:119)
... 2 more
Invalid config, exiting abnormally
单机部署——适用于开发测试
tar -zxvf zookeeper-3.4.6.tar.gz
cd zookeeper-3.4.6/conf
cp zoo_sample.cfg zoo.cfg
创建日志目录
mkdir /data/yunva/zookeeper-3.4.6/data
mkdir /data/yunva/zookeeper-3.4.6/logs
配置:conf/zoo.cfg
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/yunva/zookeeper-3.4.6/logs
dataLogDir=/data/yunva/zookeeper-3.4.6/logs
clientPort=2181
#自动清除日志文件
autopurge.snapRetainCount=20
autopurge.purgeInterval=48
启动:
bin/zkServer.sh start
连接到Zookeeper:
bin/zkCli.sh -server 127.0.0.1:2181 适用于Java开发
查看状态:
bin/zkServer.sh status
JMX enabled by default
Using config: /data/yunva/zookeeper-3.4.6/bin/../conf/zoo.cfg
Mode: standalone
zookeeper的内存配置
nohup "$JAVA" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" -cp "$CLASSPATH" -server -Xmx256M -Xms256M -Xss256K -XX:-UseGCOverheadLimit -XX:ReservedCodeCacheSize=64M -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+CMSScavengeBeforeRemark -XX:+DisableExplicitGC -Djava.awt.headless=true $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" >& < /dev/null &
#-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" >& < /dev/null &