Zookeeper集群环境搭建
准备工作
-
准备3个节点,要求配置好主机名称,服务器之间系统时间保持一致
-
注意配置主机名及hosts
vi /etc/hostname
vi /etc/hosts 192.168.147.102 bigdata01 192.168.147.103 bigdata02 192.168.147.104 bigdata03
-
关闭防火墙
# 关闭防火墙 systemctl stop firewalld # 开机禁止启动 systemctl disable firewalld # 查看防火墙状态 systemctl status firewalld
# 关闭之后的状态 [root@bigdata01 ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:firewalld(1)
上传Zookeeperk安装
-
解压
tar -zxvf zookeeper-3.4.6.tar.gz -C /usr/local/
-
修改环境变量
vi /etc/profile export JAVA_HOME=/usr/local/jdk1.8 export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.6 export PATH=$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH
-
环境变量生效
source /etc/profile
-
修改zoo_sample.cfg文件
cd /usr/local/zookeeper-3.4.6/conf mv zoo_sample.cfg zoo.cfg
-
配置zoo.cfg
(1) 修改数据的dir dataDir=/usr/local/zookeeper/data (2) 修改集群地址 server.0=bigdata01:2888:3888 server.1=bigdata02:2888:3888 server.2=bigdata03:2888:3888
-
增加服务器配置标识配置,共两步
(1) 创建文件夹 mkdir -p /usr/local/zookeeper/data (2) 创建myid文件并标识当前相应的机器 vi /usr/local/zookeeper/data/myid
注意:每个服务器的myid文件内容不同,分别修改值为0、1、2;与之前在zoo.cfg配置文件里的server.0,server.1,server.2 顺序相对应没然后保存退出
。启动zk
启动路径: /usr/local/zookeeper/bin(也可以在任意目录下启动,因配置了环境变量)
执行命令:zkServer.sh(注意这3台机器都要启动,启动之后可以查看状态)
[root@bigdata01 conf]# zkServer.sh start JMX enabled by default Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
查看状态:zkServer.sh status(在3个节点上检验zk的mode,会看到一个leader和两个follower)
[root@bigdata01 conf]# zkServer.sh status JMX enabled by default Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg Mode: follower
启动zk客户端
zkCli.sh
查看目录 [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper]
查看:ls / eg: ls /zookpeer 创建并赋值 create /test zookeeper 获取 get /test 设值 set /test zookeeper1234 ps1:任意节点都可以看到zk集群的数据一致性 Ps2:创建节点有两种状态,短暂(ephemeral)和持久(persistent)
[zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] ls zookeeper Command failed: java.lang.IllegalArgumentException: Path must start with / character [zk: localhost:2181(CONNECTED) 2] ls /zookeeper [quota] [zk: localhost:2181(CONNECTED) 3] create /test zookeeper Created /test [zk: localhost:2181(CONNECTED) 4] ls / [zookeeper, test] [zk: localhost:2181(CONNECTED) 5] get /test zookeeper cZxid = 0x100000002 ctime = Mon May 17 20:35:21 CST 2021 mZxid = 0x100000002 mtime = Mon May 17 20:35:21 CST 2021 pZxid = 0x100000002 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0
制作开机自启zk服务
cd /etc/rc.d/init.d/ touch zookeeper chmod 777 zookeeper vi zookeeper
#!/bin/bash #chkconfig:2345 20 90 #description:zookeeper #processname:zookeeper export JAVA_HOME=/usr/local/jdk1.8 export PATH=$JAVA_HOME/bin:$PATH case $1 in start) /usr/local/zookeeper-3.4.6/bin/zkServer.sh start;; stop) /usr/local/zookeeper-3.4.6/bin/zkServer.sh stop;; status) /usr/local/zookeeper-3.4.6/bin/zkServer.sh status;; restart) /usr/local/zookeeper-3.4.6/bin/zkServer.sh restart;; *) echo "require start|stop|status|restart" ;; esac
配置开机启动
chkconfig zookeeper on
验证
[root@bigdata01 init.d]# chkconfig --add zookeeper [root@bigdata01 init.d]# chkconfig --list zookeeper Note: This output shows SysV services only and does not include native systemd services. SysV configuration data might be overridden by native systemd configuration. If you want to list systemd services use 'systemctl list-unit-files'. To see services enabled on particular target use 'systemctl list-dependencies [target]'. zookeeper 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@bigdata01 init.d]#
重启后执行jps,可看到zk进程
Last login: Mon May 17 20:48:32 2021 from 192.168.147.1 [root@bigdata01 ~]# jps 913 QuorumPeerMain 1198 Jps
ZooInspector图形化管理工具
-
IDEA中plugins中搜索zookeeper
-
配置zk连接
-
查看zk连接信息
-
-
-