Zookeeper 01. 集羣搭建與使用

Zookeeper 部署

Zookeeper 有兩種運行模式:單機模式和集羣模式。下面分別介紹:單機模式、集羣模式以及僞集羣模式這三種部署方式。java

前置

  • Java 運行環境
  • Zookeeper 安裝包, 解壓
  • 設置環境變量
export ZOOKEEPER_HOME=/Users/fandennis/Documents/Land/codes/zk/zookeeper346
export PATH=$PATH:$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf

集羣模式

1.配置:主機名稱到IP地址映射配置(/etc/hosts)git

192.168.0.179   slave-01  
192.168.0.178   slave-02  
192.168.0.177   slave-03

2.修改ZooKeeper配置文件apache

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/var/lib/zookeeper/
clientPort=2181
server.1=slave-01:2888:3888
server.2=slave-02:2888:3888
server.3=slave-03:2888:3888

由於各集羣的配置同樣,能夠採用 git 管理配置文件.api

參數說明:服務器

clientPort: the port at which the clients will connect

server.id = host:port1:port2
id: Server ID. 用來標識該機器在集羣中的機器序號. 該 id 配置在 dataDir 目錄下的 myid 文件中. id 範圍:1~255
host:這個服務器的 ip 地址
port1: 這個服務器與集羣中的 Leader 服務器交換信息的端口
port2: 執行選舉時服務器相互通訊的端口

3.遠程複製分發安裝文件以及配置文件session

先在 slave-01 配置完成 zookeeper,而後將配置好的安裝文件遠程拷貝到集羣中的各個節點對應目錄socket

cd /home/land/installation/  
scp -r zookeeper-3.3.4/ land@slave-02: /home/land/installation/
scp -r zookeeper-3.3.4/ land@slave-03: /home/land/installation/

4.設置myidui

land@slave-01:echo "1" > /var/lib/zookeeper/myid  
land@slave-02:echo "2" > /var/lib/zookeeper/myid  
land@slave-03:echo "3" > /var/lib/zookeeper/myid

5.啓動 Zookeeper 集羣的每一個結點netty

land@slave-01:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start  
land@slave-02:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start  
land@slave-03:~/installation/zookeeper-3.3.4$ bin/zkServer.sh start

一般以下:code

➜  zookeeper346 zkServer.sh start zk1.cfg   
JMX enabled by default
Using config: /Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../conf/zk1.cfg
Starting zookeeper ... STARTED

6.驗證服務器

服務端驗證

➜  conf zkServer.sh status zk1.cfg
JMX enabled by default
Using config: /Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../conf/zk1.cfg
Mode: follower
➜  conf zkServer.sh status zk2.cfg
JMX enabled by default
Using config: /Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../conf/zk2.cfg
Mode: leader
➜  conf zkServer.sh status zk3.cfg
JMX enabled by default
Using config: /Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../conf/zk3.cfg
Mode: follower

客戶端驗證

➜  conf zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2016-09-19 09:53:30,439 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2016-09-19 09:53:30,446 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=172.22.32.80
2016-09-19 09:53:30,447 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_92
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home/jre
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../build/classes:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../build/lib/*.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../lib/slf4j-log4j12-1.6.1.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../lib/slf4j-api-1.6.1.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../lib/netty-3.7.0.Final.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../lib/log4j-1.2.16.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../lib/jline-0.9.94.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../zookeeper-3.4.6.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../src/java/lib/*.jar:/Users/fandennis/Documents/Land/codes/zk/zookeeper346/bin/../conf:
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/Users/fandennis/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/3z/wwbzv0_961b112x2nxff6_c00000gn/T/
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Mac OS X
2016-09-19 09:53:30,449 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=x86_64
2016-09-19 09:53:30,450 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=10.10.3
2016-09-19 09:53:30,450 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=fandennis
2016-09-19 09:53:30,450 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/Users/fandennis
2016-09-19 09:53:30,450 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/Users/fandennis/Documents/Land/codes/zk/zookeeper346/conf
2016-09-19 09:53:30,451 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@69663380
Welcome to ZooKeeper!
2016-09-19 09:53:30,485 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@975] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2016-09-19 09:53:30,593 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@852] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
2016-09-19 09:53:30,604 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x157402315f40001, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]

單機模式

配置文件

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/var/lib/zookeeper/
clientPort=2181

無需配置:server.id=host:port:port

僞集羣模式

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/var/lib/zookeeper/zkn
clientPort=2181
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890

和集羣方式區別不大,無非在一臺機器採用多個端口號來模擬。同時dataDir須要配置多個文件夾。

能夠看到:Zookeeper 物理文件目錄,以下圖所示。

.
├── zk1
│   ├── myid
│   ├── version-2
│   │   ├── acceptedEpoch
│   │   ├── currentEpoch
│   │   └── snapshot.0
│   └── zookeeper_server.pid
├── zk2
│   ├── myid
│   ├── version-2
│   │   ├── acceptedEpoch
│   │   └── currentEpoch
│   └── zookeeper_server.pid
└── zk3
    ├── myid
    ├── version-2
    │   ├── acceptedEpoch
    │   ├── currentEpoch
    │   └── snapshot.0
    └── zookeeper_server.pid

經常使用命令

客戶端腳本

相關文章
相關標籤/搜索