zookeeper(伪集群部署)
zookeeper(伪集群部署)
ZooKeeper有三种安装方式:单机模式 & 伪集群模式 & 集群模式
单机模式:ZooKeeper以单实例的形式运 行在一台服务器上,适合测试环境。
伪集群模式:在一台服务器上跑多个ZooKeeper实例。
集群模式: ZooKeeper运行在多台服务器上,适合生产环境。
一、zookeeper单机伪集群部署
在一台主机上跑多个zk实例,每个zk实例对应一个独立的配置文件;但是每个配置文件的clientPort & dataDir & dataLogDir绝对不能相同,还需要在dataDir中创建myid文件来指定该dataDir对应的zk实例。
一台主机部署3个zookeeper实例:
服务 | IP | 端口 |
zookeeper1 | 192.168.5.25 | 2181 |
zookeeper2 | 192.168.5.25 | 2182 |
zookeeper3 | 192.168.5.25 | 2183 |
1.部署jdk
2.部署zookeeper
tar -xvf apache-zookeeper-3.6.2-bin.tar.gz -C /usr/local/
ln -s /usr/local/apache-zookeeper-3.6.2-bin/ /usr/local/zookeeper
3.创建数据目录,日志目录,创建myid文件填入id值
mkdir -p /usr/local/zookeeper/data{1..3} mkdir -p /usr/local/zookeeper/log{1..3} echo 1 >/usr/local/zookeeper/data1/myid (id值需要和zoo.cfg配置文件中server.1对应) echo 2 >/usr/local/zookeeper/data2/myid echo 3 >/usr/local/zookeeper/data3/myid
4.创建zoo{1,2,3}.cfg配置文件
cd /usr/local/zookeeper/conf/
cp zoo_sample.cfg zoo1.cfg
cp zoo1.cfg zoo2.cfg
cp zoo1.cfg zoo3.cfg
5.修改zoo{1,2,3}.cfg
vim zoo1.cfg dataDir=/usr/local/zookeeper/data1 dataLogDir=/usr/local/zookeeper/log1 clientPort=2181 server.1=127.0.0.1:2887:3887 server.2=127.0.0.1:2888:3888 server.3=127.0.0.1:2889:3889
vim zoo2.cfg dataDir=/usr/local/zookeeper/data2 dataLogDir=/usr/local/zookeeper/log2 clientPort=2182 server.1=127.0.0.1:2887:3887 server.2=127.0.0.1:2888:3888 server.3=127.0.0.1:2889:3889
vim zoo3.cfg dataDir=/usr/local/zookeeper/data3 dataLogDir=/usr/local/zookeeper/log3 clientPort=2183 server.1=127.0.0.1:2887:3887 server.2=127.0.0.1:2888:3888 server.3=127.0.0.1:2889:3889
6.启动各实例
/usr/local/zookeeper/bin/zkServer.sh start /usr/local/zookeeper/conf/zoo1.cfg /usr/local/zookeeper/bin/zkServer.sh start /usr/local/zookeeper/conf/zoo2.cfg /usr/local/zookeeper/bin/zkServer.sh start /usr/local/zookeeper/conf/zoo3.cfg
7.查看端口
[root@localhost ~]# netstat -anptu | grep java
8.查看zookeeper状态
[root@localhost ~]# /usr/local/zookeeper/bin/zkServer.sh status /usr/local/zookeeper/conf/zoo1.cfg ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/conf/zoo1.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower [root@localhost ~]# /usr/local/zookeeper/bin/zkServer.sh status /usr/local/zookeeper/conf/zoo2.cfg ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/conf/zoo2.cfg Client port found: 2182. Client address: localhost. Client SSL: false. Mode: leader [root@localhost ~]# /usr/local/zookeeper/bin/zkServer.sh status /usr/local/zookeeper/conf/zoo3.cfg ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/conf/zoo3.cfg Client port found: 2183. Client address: localhost. Client SSL: false. Mode: follower