由于只有一台电脑,所以搭建一个伪集群(伪集群就是在一台电脑上模拟搭建集群,走不同端口启动,真实的情况在每台机器上搭建一个zookeeper或者每台机器两个zookeeper等),道理是一样的,只不过要注意别被防火墙或者安全组规则挡住了zookeeper节点间的通信,每个节点直接的网络要是通的。
集群数量推荐奇数,这是由于zookeeper的选举规则决定的,不懂先看zookeeper入门的文章,这里只是安装教程。
-
下载并解压zookeeper tar包
https://apache.claz.org/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz
此处将包下载解压到了/usr/local/share/下
#解压后的目录里的结构 root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin# ls bin conf docs lib LICENSE.txt logs NOTICE.txt README.md README_packaging.md #bin目录 root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin# cd bin root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/bin# ls README.txt zkCli.cmd zkEnv.cmd zkServer.cmd zkServer.sh zkSnapShotToolkit.sh zkTxnLogToolkit.sh zkCleanup.sh zkCli.sh zkEnv.sh zkServer-initialize.sh zkSnapShotToolkit.cmd zkTxnLogToolkit.cmd #conf目录 root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# ls configuration.xsl log4j.properties zoo_sample.cfg #将bin目录添加到path去 echo 'export PATH=$PATH:/usr/local/share/apache-zookeeper-3.6.2-bin/bin' >> /etc/profile #加载 source /etc/profile
-
复制三份配置后面用三份不同配置启动三个zookeeper
#进conf目录 cd conf #编辑第一个配置文件(或者复制zoo_sample.cfg修改) vi zk1.cfg
往配置文件里写入以下内容
# 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=/usr/local/zk1 # 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. # # http://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.httpPort=7000 #metricsProvider.exportJvmInfo=true server.1=127.0.0.1:2981:2982 server.2=127.0.0.1:2983:2984 server.3=127.0.0.1:2985:2986
- 然后保存退出
- 然后复制两份,命名为zk2.cfg和zk3.cfg
cp zk1.cfg zk2.cfg cp zk1.cfg zk3.cfg #然后编辑zk2.cfg修改dataDir为/usr/local/zk2 修改clientPort为2182 #然后编辑zk3.cfg修改dataDir为/usr/local/zk3 修改clientPort为2183 #均保存退出 #创建这三个data目录 mkdir /usr/local/zk1 /usr/local/zk2 /usr/local/zk3
-
启动三个zookeeper并查看状态
#在conf目录下执行 root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf zkServer.sh start zk1.cfg zkServer.sh start zk2.cfg zkServer.sh start zk3.cfg
#查看三个zookeeper的状态,发现第二个是leader root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# zkServer.sh status zk1.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/share/apache-zookeeper-3.6.2-bin/bin/../conf/zk1.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# zkServer.sh status zk2.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/share/apache-zookeeper-3.6.2-bin/bin/../conf/zk2.cfg Client port found: 2182. Client address: localhost. Client SSL: false. Mode: leader root@ubuntu:/usr/local/share/apache-zookeeper-3.6.2-bin/conf# zkServer.sh status zk3.cfg /usr/bin/java ZooKeeper JMX enabled by default Using config: /usr/local/share/apache-zookeeper-3.6.2-bin/bin/../conf/zk3.cfg Client port found: 2183. Client address: localhost. Client SSL: false. Mode: follower
原文地址:https://blog.51cto.com/humorchen/2989010
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。