새소식

Data Engineering/Distributed System

Zookeeper 여러 기능 알아보기 및 설치

  • -

Zookeeper의 여러 기능들

Data model

zookeeper는 파일 시스템처럼 '/'로 구분하여 경로를 표기하고 데이터를 저장한다.

ZooKeeper's Hierarchical Namespace

zookeeper는 중간 경로 예를들면 위의 이미지에서 app1이나 app2, root 도 데이터를 가질 수 있다. 그리고 자식 노드들의 데이터도 접근 가능하다.
그렇기에 수bytes ~ KB 이하로 유지하는 것이 좋다. 보통은 설정 정보나, 작은 메타데이터 정보 등의 빠른 동기화 용도 위주로 사용된다.

Versioning

Znode는 데이터나 ACL 변경할 때 versioning이 되며,
version number를 갖고있다 client에게 응답할 때 이 버전 번호도 항상 같이 전달한다.

ACL은 엑세스 제어 목록으로
read할 때는 연결된 모든 데이터를 가져오고, write할 때는 바로 덮어쓰는 특성을 가지고 있어 작업에 제한을 두는 것이다.

Ephemeral nodes

zookeeper는 세션이 종료되면 함께 삭제되는 일시적인 노드도 제공한다.

Watch

watch가 설정된 노드에 한하여, 업데이트가 발생하면 watch가 trigger된다. Client API로 watch에 대한 콜백함수를 등록해서 원하는 operation을 수행할 수 있다.

Zookeeper가 보장하는 내용

  • Sequential Consistency
    • 클라이언트가 요청한 순을 보장한다.
  • Atomicity
    • 부분적인 성공, 실패 없이 업데이트 전체 성공이거나 실패
  • Single System Image
    • failover로 서버가 바뀌더라도 클라이언트는 기존과 같은 이미지만을 본다.
  • Reliability
    • 업데이트가 모두 overwirte될때까지 기다린다.
  • Timeliness
    • 사용자가 보는 데이터는 특정 시간 안에 최신 상태를 보장한다.

Zookeeper API

Zookeeper의 특징 주 하나는 매우 간단한 API이다.

  • create : creates a node at a location in the tree
  • delete : deletes a node
  • exists : tests if a node exists at a location
  • get data : reads the data from a node
  • set data : writes data to a node
  • get children : retrieves a list of children of a node
  • sync : waits for data to be propagated

 

Zookeeper 실습

설치

공식 문서에 시스템 요구사항이 있으니 참고하세요.

wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.2/apache-zookeeper-3.7.2-bin.tar.gz
tar xvzf apache-zookeeper-3.7.2-bin.tar.gz
mv apache-zookeeper-3.7.2-bin zookeeper
cd zookeeper
echo "export ZK_HOME=$(pwd)" >> ~/.bashrc && source ~/.bashrc

자바가 설치 안되어있으면 설치한다.

sudo apt install openjdk-8-jre-headless -y

그리고
$ZK_HOME/conf/zoo.cfg 파일을 수정한다.

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=/home/ubuntu/zookeeper/data/single
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
metricsProvider.httpPort=7000
metricsProvider.exportJvmInfo=true

실행

cd $ZK_HOME
./bin/zkServer.sh start conf/zoo.cfg

종료

cd $ZK_HOME
./bin/zkServer.sh stop conf/zoo.cfg

여러 서버로 앙상블 구성

만약 멀티노드를 구성하려면 같은 파일에 아래 설정을 추가하면 된다.

server.1=$host:$port_follower_connect_leader:$port_for_leader_election
server.2=$host:$port_follower_connect_leader:$port_for_leader_election
server.3=$host:$port_follower_connect_leader:$port_for_leader_election

예) 공식문서 참고

server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888

그리고 자신이 몇번 서버인지 myid 파일로 확인하기에 아래처럼 적어줘야한다.

echo "1" > $dataDirOfServer1/myid
echo "2" > $dataDirOfServer2/myid
echo "3" > $dataDirOfServer1/myid
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.