CentOS7 ZooKeeper 3.5.5 伪分布式集群安装

1、创建目录/opt/zookeeper,并下载zookeeper安装包

mkdir /opt/zookeeper/
cd /opt/zookeeper/
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz

2、解压zookeeper

tar zxvf apache-zookeeper-3.5.5-bin.tar.gz
mv apache-zookeeper-3.5.5-bin zookeeper-server1
cp -rf zookeeper-server1 zookeeper-server2
cp -rf zookeeper-server1 zookeeper-server3
mv zookeeper-server1/conf/zoo_sample.cfg zookeeper-server1/conf/zoo.cfg
mv zookeeper-server2/conf/zoo_sample.cfg zookeeper-server2/conf/zoo.cfg
mv zookeeper-server3/conf/zoo_sample.cfg zookeeper-server3/conf/zoo.cfg

3、创建zookeeper数据目录

mkdir -p /opt/zookeeper/zk-data/zoo1
mkdir -p /opt/zookeeper/zk-data/zoo2
mkdir -p /opt/zookeeper/zk-data/zoo3
echo "1" > /opt/zookeeper/zk-data/zoo1/myid
echo "2" > /opt/zookeeper/zk-data/zoo2/myid
echo "3" > /opt/zookeeper/zk-data/zoo3/myid

4、配置zoo.cfg

配置zookeeper-server1的zoo.cfg。

cd /opt/zookeeper/zookeeper-server1/conf 
sed -i 's:clientPort=2181:clientPort=2181:' zoo.cfg
sed -i 's:dataDir=/tmp/zookeeper:dataDir=/opt/zookeeper/zk-data/zoo1:' zoo.cfg
echo -e "server.1=192.168.56.88:2881:3881\nserver.2=192.168.56.88:2882:3882\nserver.3=192.168.56.88:2883:3883" >> zoo.cfg

配置zookeeper-server2的zoo.cfg。

cd /opt/zookeeper/zookeeper-server2/conf 
sed -i 's:clientPort=2181:clientPort=2182:' zoo.cfg
sed -i 's:dataDir=/tmp/zookeeper:dataDir=/opt/zookeeper/zk-data/zoo2:' zoo.cfg
echo -e "server.1=192.168.56.88:2881:3881\nserver.2=192.168.56.88:2882:3882\nserver.3=192.168.56.88:2883:3883" >> zoo.cfg

配置zookeeper-server3的zoo.cfg。

cd /opt/zookeeper/zookeeper-server3/conf 
sed -i 's:clientPort=2181:clientPort=2183:' zoo.cfg
sed -i 's:dataDir=/tmp/zookeeper:dataDir=/opt/zookeeper/zk-data/zoo3:' zoo.cfg
echo -e "server.1=192.168.56.88:2881:3881\nserver.2=192.168.56.88:2882:3882\nserver.3=192.168.56.88:2883:3883" >> zoo.cfg

说明,上面示例中的192.168.56.88是宿主机的IP。

5、设置Zookeeper开机启动

mkdir /opt/zookeeper/zookeeper-command
cd /opt/zookeeper/zookeeper-command/

echo -e "\
#!/bin/bash\n\
# zk 启动脚本\n\
/opt/zookeeper/zookeeper-server1/bin/zkServer.sh start /opt/zookeeper/zookeeper-server1/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server2/bin/zkServer.sh start /opt/zookeeper/zookeeper-server2/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server3/bin/zkServer.sh start /opt/zookeeper/zookeeper-server3/conf/zoo.cfg\n\
" > start-zk.sh

echo -e "\
#!/bin/bash\n\
# zk 停止脚本\n\
/opt/zookeeper/zookeeper-server1/bin/zkServer.sh stop /opt/zookeeper/zookeeper-server1/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server2/bin/zkServer.sh stop /opt/zookeeper/zookeeper-server2/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server3/bin/zkServer.sh stop /opt/zookeeper/zookeeper-server3/conf/zoo.cfg\n\
" > stop-zk.sh

echo -e "\
#!/bin/bash\n\
# zk 重启脚本\n\
/opt/zookeeper/zookeeper-server1/bin/zkServer.sh stop /opt/zookeeper/zookeeper-server1/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server2/bin/zkServer.sh stop /opt/zookeeper/zookeeper-server2/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server3/bin/zkServer.sh stop /opt/zookeeper/zookeeper-server3/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server1/bin/zkServer.sh start /opt/zookeeper/zookeeper-server1/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server2/bin/zkServer.sh start /opt/zookeeper/zookeeper-server2/conf/zoo.cfg\n\
/opt/zookeeper/zookeeper-server3/bin/zkServer.sh start /opt/zookeeper/zookeeper-server3/conf/zoo.cfg\n\
" > restart.sh

chmod +x *.sh

groupadd zookeeper
useradd -r -g zookeeper -s /bin/false zookeeper
chown -R zookeeper.zookeeper /opt/zookeeper

cd /usr/lib/systemd/system

echo -e "\
[Unit]\n\
Description=ZooKeeper Service\n\
After=network.target\n\
[Service]\n\
Type=forking\n\
ExecStart=/opt/zookeeper/zookeeper-command/start-zk.sh\n\
ExecStop=/opt/zookeeper/zookeeper-command/stop-zk.sh\n\
ExecReload=/opt/zookeeper/zookeeper-command/restart.sh\n\
User=zookeeper\n\
Group=zookeeper\n\
[Install]\n\
WantedBy=multi-user.target\n\
" > zookeeper.service

systemctl enable zookeeper
systemctl start zookeeper

6、防火墙开放相应端口

firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --zone=public --add-port=2182/tcp --permanent
firewall-cmd --zone=public --add-port=2183/tcp --permanent
firewall-cmd --reload

未经允许不得转载:君子如兰 » CentOS7 ZooKeeper 3.5.5 伪分布式集群安装

赞 (0) 打赏

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏