首页 > 系统相关 >linux-zookeeper

linux-zookeeper

时间:2023-06-25 21:46:09浏览次数:47  
标签:zk elk101 root zookeeper sh linux com

zookeeper

一、部署zookeeper单点

1.下载zookeeper软件

https://zookeeper.apache.org/releases.html
[[email protected] ~]# ll
-rw-r--r--  1 root root  13185104 Apr 10 16:09 apache-zookeeper-3.8.0-bin.tar.gz

2. 解压软件包

[[email protected] ~]# tar xf apache-zookeeper-3.8.0-bin.tar.gz -C /es/softwares/

3. 创建符号连接

[[email protected] ~]# cd /es/softwares/ && ln -sv apache-zookeeper-3.8.0-bin zk
‘zk’ -> ‘apache-zookeeper-3.8.0-bin’

4. 声明zk的环境变量

cat > /etc/profile.d/kafka.sh <<'EOF'  
#!/bin/bash

export ZK_HOME=/es/softwares/zk
export PATH=$PATH:$ZK_HOME/bin
EOF

[[email protected] /es/softwares]# cat /etc/profile.d/kafka.sh
#!/bin/bash

export ZK_HOME=/es/softwares/zk
export PATH=$PATH:$ZK_HOME/bin

[[email protected] /es/softwares]# source /etc/profile.d/kafka.sh

5. 创建zk的配置文件

[[email protected] /es/softwares]# cp /es/softwares/zk/conf/{zoo_sample.cfg,zoo.cfg}

6. 启动zk节点

#启动zk
[[email protected] /es/softwares]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
#查看zk状态
[[email protected] /es/softwares]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
[[email protected] /es/softwares]# zkServer.sh stop
[[email protected] /es/softwares]# zkServer.sh restart

7. 连接ZK节点

[[email protected] ~]# zkCli.sh
Connecting to localhost:2181
....
....
....
WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]

二、zookeeper的命令行基本管理

1.查看

- 查看
ls /
	查看根(/)下有多少子zookeeper node,简称"znode"
get /linux-zk/test
	查看"/linux-zk/test"的数据。
[zk: localhost:2181(CONNECTED) 3] get /linux-zk/test
123

2.创建

- 创建
create /linux-zk
	在根路径下创建一个名为"linux-zk"的"znode"。

create /linux-zk/test 123 
	在"/linux-zk/"znode下创建一个名为"test"的子znode,并指定该znode数据为"123"。

create -s /linux-zk/test/lianxi 88888
	创建一个前缀为"/linux-zk/test/lianxi"有序编号的znode,数据为88888
	
[zk: localhost:2181(CONNECTED) 4] create -s /linux-zk/test/lianxi 88888
Created /linux-zk/test/lianxi0000000000

create -s -e /linux-zk/test/lianxi01
	创建一个名为"/linux-zk/test/lianxi01"的临时znode。当前会话结束,临时znode会自动删除。
	
[zk: localhost:2181(CONNECTED) 5] create -s -e /linux-zk/test/lianxi01
Created /linux-zk/test/lianxi010000000001
[zk: localhost:2181(CONNECTED) 7] stat /linux-zk/test/lianxi010000000001 
cZxid = 0x5
ctime = Mon Apr 10 20:55:47 CST 2023
mZxid = 0x5
mtime = Mon Apr 10 20:55:47 CST 2023
pZxid = 0x5
cversion = 0
dataVersion = 0
aclVersion = 0
#临时znode的这个字段后边会接上当前的sessionid,不是临时的是0x0
ephemeralOwner = 0x100003d87730000
dataLength = 0
numChildren = 0

3.修改

- 修改
set /linux-zk/test 456
	将"/linux-zk/test "的znode数据修改为456.

[zk: localhost:2181(CONNECTED) 8] set /linux-zk/test 456
[zk: localhost:2181(CONNECTED) 9] get /linux-zk/test
456

4.删除

- 删除
delete /linux-zk/test02 
	删除名为"/linux-zk/test02 "的znode,但该znode不能有子znode。即必须为空。

[zk: localhost:2181(CONNECTED) 11] create /linux-zk/test02 
[zk: localhost:2181(CONNECTED) 13] ls /linux-zk 
[test, test02]
[zk: localhost:2181(CONNECTED) 14] delete /linux-zk/test02 
[zk: localhost:2181(CONNECTED) 15] ls /linux-zk 
[test]


deleteall /linux-zk/test
	递归删除"/linux-zk/test"下的所有znode。
	
[zk: localhost:2181(CONNECTED) 16] delete /linux-zk/test 
Node not empty: /linux-zk/test
[zk: localhost:2181(CONNECTED) 17] deleteall /linux-zk/test
[zk: localhost:2181(CONNECTED) 18] ls /linux-zk 
[]

三、zookeeper集群部署

1. 创建zk的数据目录

[[email protected] ~]# install -d /es/data/zk

2. 修改单点zk的配置文件

[[email protected] ~]# cat /es/softwares/zk/conf/zoo.cfg
# 定义最小单元的时间范围tick。
tickTime=2000
# 启动时最长等待tick数量。
initLimit=5
# 数据同步时最长等待的tick时间进行响应ACK
syncLimit=2
# 指定数据目录
dataDir=/es/data/zk
# 监听端口
clientPort=2181
# 开启四字命令允许所有的节点访问。
4lw.commands.whitelist=*
# server.ID=A:B:C[:D]
# ID:
#    zk的唯一编号。
# A:
#    zk的主机地址。
# B:
#    leader的选举端口,是谁leader角色,就会监听该端口。
# C: 
#    数据通信端口。
# D:
#    可选配置,指定角色。
server.101=10.0.0.101:2888:3888
server.102=10.0.0.102:2888:3888
server.103=10.0.0.103:2888:3888

3.同步数据即可

[[email protected] ~]# data_rsync.sh /es/softwares/zk/
===== rsyncing elk102.com: zk =====
命令执行成功!
===== rsyncing elk103.com: zk =====
命令执行成功!
[[email protected] ~]# data_rsync.sh /es/softwares/apache-zookeeper-3.8.0-bin/
===== rsyncing elk102.com: apache-zookeeper-3.8.0-bin =====
命令执行成功!
===== rsyncing elk103.com: apache-zookeeper-3.8.0-bin =====
命令执行成功!
[[email protected] ~]# data_rsync.sh /es/data/zk/
===== rsyncing elk102.com: zk =====
命令执行成功!
===== rsyncing elk103.com: zk =====
命令执行成功!
[[email protected] ~]# data_rsync.sh /etc/profile.d/kafka.sh
===== rsyncing elk102.com: kafka.sh =====
命令执行成功!
===== rsyncing elk103.com: kafka.sh =====
命令执行成功!

4. 创建myid文件

[[email protected] ~]# for ((host_id=101;host_id<=103;host_id++)) do ssh 10.0.0.${host_id} "echo ${host_id} > /es/data/zk/myid";done
[[email protected] ~]# cat /es/data/zk/myid
101

5. 所有节点启动zk服务

[[email protected] ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... FAILED TO START

[[email protected] ~]# source /etc/profile.d/kafka.sh 
[[email protected] ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[[email protected] ~]# source /etc/profile.d/kafka.sh
[[email protected] ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /es/softwares/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

6. 编写zk的集群管理脚本

[[email protected] ~]# cat /usr/local/sbin/zkManager.sh
#!/bin/bash

#判断用户是否传参
if [ $# -ne 1 ];then
    echo "无效参数,用法为: $0  {start|stop|restart|status}"
    exit
fi

#获取用户输入的命令
cmd=$1

#定义函数功能
function zookeeperManger(){
    case $cmd in
    start)
        echo "启动服务"        
        remoteExecution start
        ;;
    stop)
        echo "停止服务"
        remoteExecution stop
        ;;
    restart)
        echo "重启服务"
        remoteExecution restart
        ;;
    status)
        echo "查看状态"
        remoteExecution status
        ;;
    *)
        echo "无效参数,用法为: $0  {start|stop|restart|status}"
        ;;
    esac
}


#定义执行的命令
function remoteExecution(){
    for (( i=101 ; i<=103 ; i++ )) ; do
            tput setaf 2
            echo ========== 10.0.0.${i} zkServer.sh  $1 ================
            tput setaf 9
            ssh 10.0.0.${i}  "source /etc/profile.d/kafka.sh; zkServer.sh $1 2>/dev/null"
    done
}

#调用函数
zookeeperManger

[[email protected] ~]# chmod +x /usr/local/sbin/zkManager.sh

#执行之前要先停掉原来的进程
[[email protected] ~]# jps
1458 Elasticsearch
2267 Elasticsearch
3644 Jps
3135 QuorumPeerMain
[[email protected] ~]# kill 3135
[[email protected] ~]# jps
1458 Elasticsearch
3658 Jps
2267 Elasticsearch


[[email protected] ~]# zkManager.sh start
启动服务
========== 10.0.0.101 zkServer.sh start ================
Starting zookeeper ... STARTED
========== 10.0.0.102 zkServer.sh start ================
Starting zookeeper ... STARTED
========== 10.0.0.103 zkServer.sh start ================
Starting zookeeper ... STARTED
[[email protected] ~]# zkManager.sh status
查看状态
========== 10.0.0.101 zkServer.sh status ================
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
========== 10.0.0.102 zkServer.sh status ================
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
========== 10.0.0.103 zkServer.sh status ================
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

7.验证集群

[[email protected] ~]# zkCli.sh -server 10.0.0.101:2181,10.0.0.102:2181,10.0.0.103:2181
Connecting to 10.0.0.101:2181,10.0.0.102:2181,10.0.0.103:2181
...
...
...
WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: 10.0.0.101:2181,10.0.0.102:2181,10.0.0.103:2181(CONNECTED) 0] 

8.使用zkWeb管理zookeeper集群

[[email protected] ~]# ll
-rw-r--r--  1 root root  28575779 Apr 10 16:09 zkWeb-v1.2.1.jar
[[email protected] ~]# java -jar zkWeb-v1.2.1.jar

http://10.0.0.101:8099/

image-20230410212138329

image-20230410212122009

四、修改zookeeper的堆内存大小

温馨提示:

修改zookeeper的堆内存大小,一般情况下,生产环境给到2G足以,如果规模较大可以适当调大到4G。

1.查看zk默认的堆内存大小为1GB

[[email protected] ~]# jmap -heap `jps | awk '/QuorumPeerMain/{print $1}'`
Attaching to process ID 2121, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10

using thread-local object allocation.
Parallel GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 1048576000 (1000.0MB)

2.配置ZK的堆内存

[[email protected] ~]# cat > /es/softwares/zk/conf/java.env << 'EOF'
> export JAVA_HOME=/es/softwares/jdk1.8.0_291
> export JVMFLAGS="-Xms256m -Xmx256m $JVMFLAGS"
> EOF
[[email protected] ~]# cat /es/softwares/zk/conf/java.env
export JAVA_HOME=/es/softwares/jdk1.8.0_291
export JVMFLAGS="-Xms256m -Xmx256m $JVMFLAGS"

3.将配置文件同步到集群的其他zk节点上

[[email protected] ~]# data_rsync.sh /es/softwares/zk/conf/java.env
===== rsyncing elk102.com: java.env =====
命令执行成功!
===== rsyncing elk103.com: java.env =====
命令执行成功!

4.重启ZK集群

[[email protected] ~]# zkManager.sh restart

5.验证堆内存

[[email protected] ~]# jmap -heap `jps | awk '/QuorumPeerMain/{print $1}'`
Attaching to process ID 2294, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10

using thread-local object allocation.
Parallel GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 268435456 (256.0MB)

标签:zk,elk101,root,zookeeper,sh,linux,com
From: https://www.cnblogs.com/world-of-yuan/p/17504009.html

相关文章

  • linux-kibana
    kibana一、部署1.下载软件包[[email protected]~]#ll-rw-r--r--1rootroot268453142Apr418:03kibana-7.17.5-x86_64.rpm2.安装kibana[[email protected]~]#rpm-ivhkibana-7.17.5-x86_64.rpm3.修改kibana配置文件vim/etc/kibana/kibana.yml...#指定kiban......
  • linux-kafka
    kafka一、单点部署docker-compose创建参考地址https://gitee.com/jasonyin2020/docker-compose/tree/master1.下载kafka软件包[[email protected]~]#ll-rw-r--r--1rootroot103956099Apr1016:09kafka_2.13-3.2.1.tgz2.解压软件包[[email protected]~]#tarxf......
  • linux的Screen
       Screen: 可以在多个进程之间复用一个物理终端的窗口管理器有会话的概念可以在一个screen会话中创建多个screen窗口  参考:   http://www.ibm.com/developerworks/cn/linux/l-cn-screen/    http://www.turbolinux.com.cn/turbo/wiki/doku.php?id=%E5%91%BD%E4%B......
  • Linux开机启动项
    系统启动时需要加载的配置文件/etc/profile/root/.bash_profile/etc/bashrc/root/.bashrc/etc/profile.d/*.sh/etc/profile.d/lang.sh/etc/sysconfig/i18n/etc/rc.loacl/etc/rc.d/rc.local修改配置文件,再配置文件中加入即可。通过命令将脚本加入开机启动项:chkconfig-......
  • linux下使用scp远程传输自动输入密码
    由于需要将A服务器的文件远程传输到B服务器但是scp命令每次都要手动输入密码这样脚本执行太繁琐,所以讲A服务器和B服务器互信即可,具体操作如下: 首先在A服务器配置:mkdir-p~/.sshchmod700~/.ssh 然后在~/.ssh目录生成密钥文件:cd~/.shhssh-keygen-trsa-P""......
  • 【问题记录】Linux虚拟机的tomcat访问不了
    问题起因是电脑没电自动关机,虚拟机当时还在运行。第二天重启虚拟机的tomcat,主机访问不了,zookeeper注册中心可以正常使用防火墙,tomcat端口都检查过没问题,重装tomcat,重启服务器也没用重启时出现smbushostcontrollernotenable,四台机器都出现处理方法:查明装入模块的确切......
  • Linux常见命令
    1.列出文件列表:lsllls:显示当前路径下的所有文件;ls-a:显示所有文件到货目录(包含隐藏的文件);ls-l(ll):显示当前路径下的所有文件的详细信息;2.切换目录命令:cdcd/:切换到系统根目录;cd/文件夹:切换到绝对路径的文件夹下;cd文件夹:切换到文件夹下的文件夹下......
  • Linux VM通过NFS3.0挂载Azure Blob Storage Container后访问共享文件夹Permission den
    问题描述如图所示,/root-squash是一个BlobStorageContainer的挂载点。ls-al查看该目录的权限为:drwxr-xr--2rootroot0Jun2323:15root-squash当前用户身份为root,但在尝试进入该目录时失败,报错信息为:-bash:cd:root-squash:Permissiondenied调查过程猜......
  • linux 服务优化与防盗链
    摘要:网页安全至关重要,所有设置隐藏版本号,防止黑客攻击,日志切割方便维护,网页压缩方便响应速度,防盗链防止重要文件泄露。 目录一、隐藏版本号二、修改缓存时间三、日志切割四、连接超时五、工作进程数六、网页压缩七、防盗链八、总结  一、隐藏版本号1.curl查......
  • Linux 文件系统 | mount & umount
    Linux中一切皆文件并且所有文件都统一在/根目录下面类比windows系统,插入U盘,或者硬盘等存储设备,可以直接看到并访问里面的内容。而在Linux系统中,需要进行挂载,将外来设备加入到系统管理中,才可以正常访问。上面的挂载就需要用到mount命令查看/etc/fstab配置文件,......