Linux下zookeeper集群化安装
安装前提
检查服务器
-
机器域名要可以正常使用(非必要)
-
机器列表/etc/hosts文件要完整(生产规范)
-
空闲端口(满足安装zookeeper前不被其他进程占用,必要):
-
2181 :对cline端提供服务
-
2888 :选举leader使用、
-
3888 :集群内机器通讯使用(Leader监听此端口)
netstat -lntp 2>/dev/null|grep LISTEN|awk '{print $4}'|awk -F':' '{print $NF}'|sort|uniq
-
创建ZK用户
groupadd zookeeper && useradd -g zookeeper zookeeper
创建对应的目录
# root登录
#软件存放目录
mkdir -p /opt/zookeeper
mkdir -p /opt/zookeeper/software
#日志与数据目录
mkdir -p /opt/zookeeper/logs
mkdir -p /opt/zookeeper/zkdata
#软连接目录
mkdir -p /home/zookeeper/software
上传安装包,确认权限
将jdk,zk的安装文件jdk1.8.0_131.tgz
和apache-zookeeper-3.7.0-bin.tar.gz
分发到三台机器上(192.168.1.1,192.168.1.2,192.168.1.3),对应的目录/opt/zookeeper/software
上。
scp apache-zookeeper-3.7.0-bin.tar.gz root@192.168.1.1:/opt/zookeeper/software/apache-zookeeper-3.7.0-bin.tar.gz
scp apache-zookeeper-3.7.0-bin.tar.gz root@192.168.1.2:/opt/zookeeper/software/apache-zookeeper-3.7.0-bin.tar.gz
scp apache-zookeeper-3.7.0-bin.tar.gz root@192.168.1.3:/opt/zookeeper/software/apache-zookeeper-3.7.0-bin.tar.gz
scp jdk1.8.0_131.tgz root@192.168.1.1:/opt/zookeeper/software/jdk1.8.0_131.tgz
scp jdk1.8.0_131.tgz root@192.168.1.2:/opt/zookeeper/software/jdk1.8.0_131.tgz
scp jdk1.8.0_131.tgz root@192.168.1.3:/opt/zookeeper/software/jdk1.8.0_131.tgz
###修改权限目录权限
chown -R zookeeper:zookeeper /opt/zookeeper
安装配置
# 切换用户后进行
su - zookeeper
安装jdk
tar -zxf /opt/zookeeper/software/jdk1.8.0_131.tgz -C /opt/zookeeper/software/
ln -nsf /opt/zookeeper/software/jdk1.8.0_131 /home/zookeeper/software/java
安装ZK
tar -zxf /opt/zookeeper/software/apache-zookeeper-3.7.0-bin.tar.gz -C /opt/zookeeper/software/
ln -nsf /opt/zookeeper/software/apache-zookeeper-3.7.0-bin /home/zookeeper/software/zookeeper
环境配置
vim ~/.bashrc
# 追加以下内容
export userEnv=/opt/zookeeper/software
export JAVA_HOME=$userEnv/java
export ZOOKEEPER_HOME=$userEnv/zookeeper
export PATH=$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/tools:$PATH
ZK配置
- 新增zoo.cfg
vim ~/software/zookeeper/conf/zoo.cfg
# 追加以下内容
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/zookeeper/zkdata/
dataLogDir=/home/zookeeper/logs/
clientPort=2181
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
zookeeper.leaderServes=no
server.1=192.168.1.2:2888:3888
server.2=192.168.1.3:2888:3888
server.3=192.168.1.1:2888:3888
- 新增myid,在三台机器上各自添加myid文件(值为在
zoo.cfg
对应的ServerID
),文件路径采用zoo.cfg
中dataDir
配置项中的地址。
# 192.168.1.2上执行
touch -p /home/zookeeper/zkdata/myid;echo 1 >> /home/zookeeper/zkdata/myid;
# 192.168.1.3上执行
touch -p /home/zookeeper/zkdata/myid;echo 2 >> /home/zookeeper/zkdata/myid;
# 192.168.1.1上执行
touch -p /home/zookeeper/zkdata/myid;echo 3 >> /home/zookeeper/zkdata/myid;
- 使用ROLLINGFILE方式记录ZK日志。在
zookeeper/bin
下配置zkEnv.sh
文件
f [ "x${ZOO_LOG_DIR}" = "x" ]
then
ZOO_LOG_DIR="/home/zookeeper/logs"
fi
if [ "x${ZOO_LOG4J_PROP}" = "x" ]
then
ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
fi
启动
启动步骤
cd /home/zookeeper/software/zookeeper;zkServer.sh start
启动后检查
cd /home/zookeeper/software/zookeeper;zkServer.sh status
[zookeeper@node1 bin]$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/zookeeper/software/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
集群内只有一台为leader,其他均为follower