Zookeeper简介,standalone模式,replicated模式

1. zk简介

官网

Zookeeper是一个分布式协调服务,为分布式应用提供配置维护,命名,分布式同步,组服务等服务。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。

overview
ACL: access control list
Design goal:
    simple, replicated, ordered, fast
hierarchical namespace:
    much like a standard file system
Nodes and ephemeral nodes
Conditional updates and watches
Guarantees
    Sequential Consistency, Atomicity, Single System Image, Reliability, Timeliness
Simple API
create, delete, exists, get data, set data, get children, sync

2. 安装zk

    环境:CentOS 6.7
    必须的软件:jdk 1.7+,JDK安装过程
    zk下载地址(国内推荐):查看

2.1. standalone mode

# 上传至服务器/usr/local/src中
$ cd /usr/local/src
# 解压缩
$ tar -zxvf zookeeper-3.4.10.tar.gz
# 创建zookeeper目录
$ mkdir ../zookeeper
# 将解压后的zookeeper移动到新建的目录中
$ mv zookeeper-3.4.10/ ../zookeeper/
# 转到zookeeper的配置目录下
$ cd ../zookeeper/zookeeper-3.4.10/conf
# 创建一个配置文件(可以直接复制zoo_sample.cfg)
$ cp zoo_sample.cfg zoo.cfg
# tickTime: zk使用的毫秒级别的基本时间单元。用来做心跳,最小的session超时时间是它的两倍。
# dataDir:  数据目录,用来存储内存中的数据库快照,如果没有特别指定,也用来存储数据库更新事务日志。
# clientPort:  监听客户端连接的端口。
# 启动zk
$ ../bin/zkServer.sh start
# 不出意外,可以看见启动成功~

2.2. Replicated mode

       至少要三个服务器,强烈建议使用奇数个服务器。2个服务器还不如一个,没有一个稳定,因为有两个单点故障。

2.2.1. 与standalone mode配置文件的区别   

# both standlone and replicated
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
# only replicated 
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888
initLimit: 在这个例子中是5 * tickTime,也就是10s,The number of ticks that the initial 
synchronization phase can take

syncLimit: 在这个例子中是2 * tickTime,也就是4s,The number of ticks that can pass 
between sending a request and getting an acknowledgement

2888: connect followers to the leader, peers can communicate with each other
3888: leader election的端口

生产环境中dataDir不要配置在tmp下,生产环境的各种配置都该注意
server.n 后面接hostname更容易记忆,n是服务器ID,在集合体中唯一,并且取值范围在1~255之间。

2.2.2. 安装方式

3台server分别使用2.1的方式安装zk(10.1.50.218, 10.1.50.220, 10.1.50.222),配置文件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=/tmp/zookeeper
# the port at which the clients will connect
clientPort=2181
# replicated servers
server.1=10.1.50.218:2888:10888
server.2=10.1.50.220:2888:10888
server.3=10.1.50.222:2888:10888

在zoo.cfg中配置的dataDir目录下创建myid,值为server.n中的n:

# 以10.1.50.218为例
$ cd /tmp/zookeeper
$ echo "1" > myid

打开防火墙

$ vim /etc/sysconfig/iptables
# 添加如下三行
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2888 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 10888 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 2181 -j ACCEPT
$ service iptables restart

依次启动三个zk,完成~~

to be continued: 生产环境配置注意
    
    --安装了三台虚拟机,同样的安装方式,其中两台都正常,还有一台刚装好ping www.baidu.com正常,
    --运行yum -y install lrzsz出错后,ping www.baidu.com返回结果的时候变成ping 127.0.0.1
    --ping ip正常,ping域名就不正常,yum也不能正常使用,重装4、5遍,问题依然,真是郁闷无比
    --先记录在此,希望之后能找到原因

3. 连接zk集群

对于客户端来说,ZooKeeper是一个整体(ensemble),连接到ZooKeeper集群实际上感觉在独享整个集群的服务,所以可以在任何一个结点上建立到服务集群的连接

# 转到zk目录下
$ cd /usr/local/zookeeper/zookeeper-3.4.10
# 连接
$ bin/zkCli.sh -server 127.0.0.1:2181
# 输入help可以看见一系列可以从客户端执行的操作命令
# 敲一些命令找找感觉:
$ ls /
# [zookeeper]
$ create /zk_test my_data   # 创建一个叫zk_test的新znode,将"my_data"与它关联
# create /zk_test my_data
$ ls /
# [zookeeper, zk_test]
$ get /zk_test # 查看和znode相关联的数据 
$ set /zk_test junk # 改变和znode关联的数据,再次get /zk_test,可以确认数据确实被改了
$ delete /zk_test # 删除znode

 

转载于:https://my.oschina.net/u/3149614/blog/994547

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值