zookeeper-部署


zookeeper 是一个高可用的分布式数据管理与协调框架,该框架能很好地保证分布式环境中数据一致性。一般用来实现服务发现(类似 DNS),配置管理,分布式锁,leader 选举等

1、环境#

1、文件下载#

// wget https://archive.apache.org/dist/zookeeper/stable/apache-zookeeper-3.8.4-bin.tar.gz
wget https://archive.apache.org/dist/zookeeper/current/apache-zookeeper-3.9.3-bin.tar.gz

2、需要依赖 jdk#

自行安装

2、解压文件#

tar -zxf apache-zookeeper-3.9.3-bin.tar.gz && rm -f apache-zookeeper-3.9.3-bin.tar.gz

3、修改 zookeeper 的配置文件#

cp apache-zookeeper-3.9.3-bin/conf/zoo_sample.cfg apache-zookeeper-3.9.3-bin/conf/zoo.cfg

vim apache-zookeeper-3.9.3-bin/conf/zoo.cfg

Copy
# 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=/mnt/xiang/apache-zookeeper-3.9.3-bin/data dataLogDir=/mnt/xiang/apache-zookeeper-3.9.3-bin/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. # # https://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 ## Metrics Providers # # https://prometheus.io Metrics Exporter #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider #metricsProvider.httpHost=0.0.0.0 #metricsProvider.httpPort=7000 #metricsProvider.exportJvmInfo=true # #server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。 #如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。 server.1=192.168.9.67:2888:3888 server.2=192.168.9.68:2888:3888 server.3=192.168.9.69:2888:3888 #方案内容过于庞大数据超过默认1M值,调整到2M jute.maxbuffer=0x1ffffe

4、创建 myid 文件#

根据 dataDir 和 dataLogDir 变量创建相应的目录
mkdir /mnt/xiang/apache-zookeeper-3.9.3-bin/data
mkdir /mnt/xiang/apache-zookeeper-3.9.3-bin/logs

在 dataDir 目录下创建一个 myid 文件,然后分别在 myid 文件中按照 zoo.cfg 文件的 server.A 中 A 的数值,在不同机器上的该文件中填写相应的值
echo 1 > /mnt/xiang/apache-zookeeper-3.9.3-bin/data/myid

5、启动 zookeeper#

sh zkServer.sh start

6、验证#

1.查看 zookeeper 占用端口是否占用:
netstat -anp | grep 2181
2.查看 zookeeper 进程是否启动
ps -elf | grep zoo.cfg

jps
QuorumPeerMain

7、开机自启#

vim /etc/rc.d/rc.local

zookeeper

/bin/bash /opt/zookeeper-3.4.8/bin/zkServer.sh start

8、一键部署 zk 脚本#

vim zk.sh

Copy
#!/bin/bash export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" eth0Ip=$(ip a show eth0 | awk '/inet / {print $2}' | cut -d'/' -f1) # 变量区 zkDir=/mnt/xiang zkVersion=apache-zookeeper-3.9.3-bin # current or stable zkXinfou=current zkClientPort=2181 zkServer1=22.50.8.24 zkServer2=22.50.8.25 zkServer3=22.50.8.26 # 变量区 end cd ${zkDir} wget https://archive.apache.org/dist/zookeeper/${zkXinfou}/${zkVersion}.tar.gz tar -zxf ${zkVersion}.tar.gz && rm -f ${zkVersion}.tar.gz cp ${zkVersion}/conf/zoo_sample.cfg ${zkVersion}/conf/zoo.cfg cat << EOF > ${zkVersion}/conf/zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=${zkDir}/${zkVersion}/data dataLogDir=${zkDir}/${zkVersion}/logs clientPort=${zkClientPort} server.1=${zkServer1}:2888:3888 server.2=${zkServer2}:2888:3888 server.3=${zkServer3}:2888:3888 jute.maxbuffer=0x1ffffe EOF mkdir ${zkVersion}/data mkdir ${zkVersion}/logs if [ ${eth0Ip} == ${zkServer1} ]; then echo 1 > ${zkDir}/${zkVersion}/data/myid elif [ ${eth0Ip} == ${zkServer2} ]; then echo 2 > ${zkDir}/${zkVersion}/data/myid elif [ ${eth0Ip} == ${zkServer3} ]; then echo 3 > ${zkDir}/${zkVersion}/data/myid else echo -e "\033[1;31mBUG!!!\033[0m" exit 5 fi source ~/.bash_profile && ${zkDir}/${zkVersion}/bin/zkServer.sh start #su - ${dUser} -l -c \"${zkDir}/${zkVersion}/bin/zkServer.sh start\"' >> /etc/rc.d/rc.local && chmod +x /etc/rc.d/rc.loacl

             ------------------------------- THE END -------------------------------

posted @   ordinaryRoadX  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示
CONTENTS

"『还真给啊!太客气了』"