Zookeeper 单机/集群安装
Zookeeper 单机/集群安装
-
下载
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz mv apache-zookeeper-3.7.0-bin /usr/local/zookeeper # 创建文件夹 cd /usr/local/zookeeper mkdir zkData mkdir zkLog cp conf/zoo_sample.cfg conf/zoo.cfg # 修改 dataDir 路径 vim conf/zoo.cfg dataDir=/usr/local/zookeeper/zkData dataLogDir=/usr/local/zookeeper/zkLog ##保存 #启动 zk ./bin/zkServer.sh start
-
开机自启脚本制作
vim /etc/init.d/zookeeper
#!/bin/bash #chkconfig: 2345 10 90 #description: service zookeeper #processname:zookeeper #环境变量加载 source /etc/profile export ZOOKEEPER_HOME=/usr/local/zookeeper case "$1" in start) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start;; start-foreground) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start-foreground;; stop) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh stop;; status) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh status;; restart) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh restart;; upgrade) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh upgrade;; print-cmd) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh print-cmd;; test) echo "test good";; *) echo "requirestart|start-foreground|stop|status|restart|print-cmd";; esac
chkconfig zookeeper on
-
集群安装参照如上步骤安装额外两台,zookeeper 是根据半数选举,所以集群最好为奇数台
-
在每台 zookeeper 的 zkData 中添加 myid 文件,每个节点序号不同
vim /usr/local/zookeeper/zkData/myid #这里我分别设置了 1, 2, 3
-
在 zoo.cfg 文件中,增加集群配置
#######################cluster########################## server.1=192.168.31.201:2888:3888 server.2=192.168.31.202:2888:3888 server.3=192.168.31.203:2888:3888
-
配置参数解读 server.A=B:C:D
A:一个数字,表示第几号服务器
集群模式下配置的/usr/local/zookeeper/zkData/myid
文件里面的数据就是A的值
B:服务器的ip地址
C:与集群中Leader服务器交换信息的端口
D:选举时专用端口,万一集群中的Leader服务器挂了,需要一个端口来重新进行选举,选出一个新的Leader,而这个端口就是用来执行选举时服务器相互通信的端口。
-
-
分别启动三台服务器
# 观察选举状态,根据 zk 选举规则,第二台启动的节点应为 leader /usr/local/zookeeper/bin/zkServer.sh status ########################## /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: leader