赞
踩
目录
本文将以三台服务器为例,介绍在 linux 系统下zookeeper的部署方式。
下载地址:Index of /dist/zookeeper (apache.org)
选择需要的介质下载,这里以 zookeeper-3.4.14 为例
部署zookeeper需要先部署JDK ,JDK部署可以参考Linux下JDK 安装-CSDN博客
注:以下操作三台机器均需要修改
(1)编辑 hosts 文件
vi /etc/hosts
(2)添加如下内容
ip(第一台机器) zk1
ip(第二台机器) zk2
ip(第三台机器) zk3
zookeeper 默认需要开通节点 2181 、2888 和 3888 端口
(1)查看防火墙状态
systemctl status firewalld
(2)开放端口
firewall-cmd --zone=public --add-port=2181/tcp --permanent
firewall-cmd --zone=public --add-port=2888/tcp --permanent
firewall-cmd --zone=public --add-port=3888/tcp --permanent
(3)防火墙重新加载配置
firewall-cmd --reload
(4) 查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
(1) 解压
上传zookeeper介质(zookeeper-3.4.14.tar.gz)到 /opt 目录
解压到当前目录下
tar zxfv zookeeper-3.4.14.tar.gz
(2) 修改配置文件
创建配置文件
touch /opt/zookeeper-3.4.14/conf/zoo.cfg
注:zookeeper-3.4.14可以执行下面的操作
mv zoo_sample.cfg zoo.cfg
编辑配置文件
vi /opt/zookeeper-3.4.14/conf/zoo.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=/data/zookeeper dataLogDir=/data/zookeeper/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. # # 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 #设置集群地址及选主协商端口、集群通信端口 server.1=zk1:2888:3888 server.2=zk2:2888:3888 server.3=zk3:2888:3888
(3) 配置 jvm.option
touch /opt/zookeeper-3.4.14/conf/java.env
vi /opt/zookeeper-3.4.14/conf/java.env
添加如下内容,配置zk占用物理内存
export JVMFLAGS="-Xms8g -Xmx8g $JVMFLAGS"
(4)创建目录
创建数据目录
mkdir -p /data/zookeeper
创建日志目录
mkdir -p /data/zookeeper/logs
(5)配置zookeeper myid
创建myid
注: '1'为节点ID,各节点唯一,且与zoo.cfg中配置"server.1=zoo1:2888:3888"对应
第一台机器执行
echo '1' > /data/zookeeper/myid
第二台机器执行
echo '2' > /data/zookeeper/myid
第三台机器执行
echo '3' > /data/zookeeper/myid
(6)启动
/opt/zookeeper-3.4.14/bin/zkServer.sh start
(1)查询集群状态
/opt/zookeeper-3.4.14/bin/zkServer.sh status
显示以下内容 ,说明应用正常启动
(2)查询集群同步
/opt/zookeeper-3.4.14/bin/zkCli.sh -server zoo1:2181
[zk: zoo1:2181(CONNECTED) 0] create /hello world
/opt/zookeeper-3.4.14/bin/zkCli.sh -server zoo2:2181
[zk: zoo2:2181(CONNECTED) 0]get /hello
输出以下内容,说明集群同步
注:以下操作三台机器均需要修改
(1)关闭zk
/opt/zookeeper-3.4.14/bin/zkServer.sh stop
(2)创建启动服务文件
touch /etc/systemd/system/zookeeper.service
vi /etc/systemd/system/zookeeper.service
(3)编写启动脚本
- [Unit]
- Description=zookeeper-3.4.14
- Requires=network.target
- After=network.target
-
- [Service]
- User=root
- Group=root
- Type=forking
- WorkingDirectory=/opt/zookeeper-3.4.14
- ExecStart=/opt/zookeeper-3.4.14/bin/zkServer.sh start /opt/zookeeper-3.4.14/conf/zoo.cfg
- ExecStop=/opt/zookeeper-3.4.14/bin/zkServer.sh stop /opt/zookeeper-3.4.14/conf/zoo.cfg
- ExecReload=/opt/zookeeper-3.4.14/bin/zkServer.sh restart /opt/zookeeper-3.4.14/conf/zoo.cfg
-
- [Install]
- WantedBy=multi-user.target
(4)关闭和启动服务
启动
systemctl start zookeeper.service
停止
systemctl stop zookeeper.service
重启
systemctl restart zookeeper.service
(5)设置服务是否开机启动
添加系统服务
systemctl enable zookeeper.service
删除系统服务
systemctl disable zookeeper.service
(6)重启机器
reboot
查看zookeeper是否开机自启动。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。