其他分享
首页 > 其他分享> > zk-03-Zookeeper部署和运行

zk-03-Zookeeper部署和运行

作者:互联网

环境准备:

机器准备:
3台 CentOS7 的机器

Zookeeper有两种运行模式:集群模式和单机模式。

1、单机模式

下面在 k8smaster机器上(IP:192.168.43.81),演示单机模式搭建zookeeper。
将安装包解压到 /opt 目录下,并重命名安装目录为 zookeeper-3.5.5,并设置环境变量 ZK_HOME 代表该目录 /opt/zookeeper-3.5.5。

[root@k8smaster zookeeper-3.5.5]# ll /root/apache-zookeeper-3.5.5-bin.tar.gz 
-rw-r--r-- 1 root root 10622522 8月   6 2019 /root/apache-zookeeper-3.5.5-bin.tar.gz
[root@k8smaster zookeeper-3.5.5]# tar -zxvf /root/apache-zookeeper-3.5.5-bin.tar.gz -C /opt
...
...
[root@k8smaster ~]# mv /opt/apache-zookeeper-3.5.5-bin/ /opt/zookeeper-3.5.5
[root@k8smaster ~]# cd /opt/zookeeper-3.5.5/
[root@k8smaster zookeeper-3.5.5]# ll
总用量 32
drwxr-xr-x 2 2002 2002   232 4月   9 2019 bin
drwxr-xr-x 2 2002 2002    77 4月   2 2019 conf
drwxr-xr-x 5 2002 2002  4096 5月   3 2019 docs
drwxr-xr-x 2 root root  4096 12月 21 10:22 lib
-rw-r--r-- 1 2002 2002 11358 2月  15 2019 LICENSE.txt
-rw-r--r-- 1 2002 2002   432 4月   9 2019 NOTICE.txt
-rw-r--r-- 1 2002 2002  1560 5月   3 2019 README.md
-rw-r--r-- 1 2002 2002  1347 4月   2 2019 README_packaging.txt
[root@k8smaster zookeeper-3.5.5]# vim ~/.bash_profile 
[root@k8smaster zookeeper-3.5.5]# source ~/.bash_profile 
:
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

ZK_HOME=/opt/zookeeper-3.5.5
PATH=$PATH:$ZK_HOME

export PATH            

[root@k8smaster zookeeper-3.5.5]# echo $ZK_HOME 
/opt/zookeeper-3.5.5
[root@k8smaster zookeeper-3.5.5]# 

初次使用 zookeeper,需要将 $ZK_HOME/conf 目录下的文件 zoo_sample.cfg 复制一份并命名为 zoo.cfg,然后进行简单的配置即可。

在 dataDir(这里配置的是 /var/lib/zookeeper)下创建一个名为 myid 的文件,在该文件的第一行写上一个数字,和 zoo.cfg 中当前机器的编号对应上 。
myid 文件中只有一个数字,即一个Server ID。例如,server.1的 myid 文件内容就是 1
注意,如果是集群模式下,请确保每个服务器的文件中的数字不同,并且和自己所在机器的 zoo.cfg 中 server.id=host:port:port 的 id 值一致。另外,id 的范围是1〜255。

[root@k8smaster zookeeper-3.5.5]# mkdir  -p /var/lib/zookeeper
[root@k8smaster zookeeper-3.5.5]# cd /var/lib/zookeeper
[root@k8smaster zookeeper]# vim myid
1
[root@k8smaster zookeeper]#

启动服务

[root@k8smaster zookeeper-3.5.5]# cd $ZK_HOME
[root@k8smaster zookeeper-3.5.5]# bin/zkServer.sh start
/usr/local/jdk1.8.0_211/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.5/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@k8smaster zookeeper-3.5.5]# 

查看 2181 端口,启动成功:

[root@k8smaster zookeeper-3.5.5]# ss -nultp | grep 2181
tcp    LISTEN     0      50     [::]:2181               [::]:*                   users:(("java",pid=9896,fd=43))
[root@k8smaster zookeeper-3.5.5]# 

2、集群模式

像上面单机模式一样,再分别在 k8snode1 (IP:192.168.43.91) 和 k8snode2 (IP:192.168.43.92) 上搭建好单机的 zookeeper 服务。

2.1 配置 k8smaster

修改 $ZK_HOME/conf 目录下的 zoo.cfg 。

tickTime=2000
initLimit=5
syncLimit=2
dataDir=/var/lib/zookeeper
clientPort=2181
server.1=192.168.43.81:2888:3888
server.2=192.168.43.91:2888:3888
server.3=192.168.43.92:2888:3888

2.2 配置 k8snode1

修改dataDir(这里配置的是 /var/lib/zookeeper)下的 myid 的内容改为 2
修改 $ZK_HOME/conf 目录下的 zoo.cfg 。

tickTime=2000
initLimit=5
syncLimit=2
dataDir=/var/lib/zookeeper
clientPort=2181
server.1=192.168.43.81:2888:3888
server.2=192.168.43.91:2888:3888
server.3=192.168.43.92:2888:3888

2.3 配置 k8snode2

修改dataDir(这里配置的是 /var/lib/zookeeper)下的 myid 的内容改为 3
修改 $ZK_HOME/conf 目录下的 zoo.cfg 。

tickTime=2000
initLimit=5
syncLimit=2
dataDir=/var/lib/zookeeper
clientPort=2181
server.1=192.168.43.81:2888:3888
server.2=192.168.43.91:2888:3888
server.3=192.168.43.92:2888:3888

2.4 依次启动3台机器的zookeeper服务,并查看状态

通过 $ZK_HOME/bin/zkServer.sh start 启动服务,并通过$ZK_HOME/bin/zkServer.sh status查看服务状态

k8smaster的zookeeper状态: follower

[root@k8smaster ~]# $ZK_HOME/bin/zkServer.sh status
/usr/local/jdk1.8.0_211/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.5/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
[root@k8smaster ~]# 

k8snode1的zookeeper状态: leader

[root@k8snode1 ~]# $ZK_HOME/bin/zkServer.sh status
/usr/local/jdk1.8.0_211/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.5/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
[root@k8snode1 ~]# 

k8snode2的zookeeper状态: follower

[root@k8snode2 ~]# $ZK_HOME/bin/zkServer.sh status
/usr/local/jdk1.8.0_211/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.5.5/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
[root@k8snode2 ~]# 

3、zoo_sample.cfg 配置文件解读

k8smaster中的zoo_sample.cfg:

[root@k8smaster ~]# cat /opt/zookeeper-3.5.5/conf/zoo_sample.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=/tmp/zookeeper
# 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


[慕课手记同步:zk-03-Zookeeper部署和运行] https://www.imooc.com/article/313660

福利: 关注同步公众号:黑桃,回复“电子书”,领取Java技术书籍pdf(不断更新)。

 


欢迎关注公众号"黑桃"

标签:bin,03,zookeeper,zk,Zookeeper,ZooKeeper,3.5,k8smaster,root
来源: https://blog.csdn.net/qq_20633779/article/details/111771318