Zookeeper簡介,standalone模式,replicated模式

1. zk簡介

官網html

Zookeeper是一個分佈式協調服務,爲分佈式應用提供配置維護,命名,分佈式同步,組服務等服務。
ZooKeeper的目標就是封裝好複雜易出錯的關鍵服務,將簡單易用的接口和性能高效、功能穩定的系統提供給用戶。node

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下載地址(國內推薦):查看apache

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個服務器還不如一個,沒有一個穩定,由於有兩個單點故障。vim

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爲:bash

# 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

打開防火牆session

$ 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,完成~~tcp

to be continued: 生產環境配置注意
    
    --安裝了三臺虛擬機,一樣的安裝方式,其中兩臺都正常,還有一臺剛裝好ping www.baidu.com正常,
    --運行yum -y install lrzsz出錯後,ping www.baidu.com返回結果的時候變成ping 127.0.0.1
    --ping ip正常,ping域名就不正常,yum也不能正常使用,重裝四、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
相關文章
相關標籤/搜索