zookeeper 설치

category DevOps/기타 2020. 3. 9. 09:55
728x90
반응형

설치

1. zookeeper 유저 생성

[root@server ~]# useradd -r -s /sbin/nologin -d /home/apps/zookeeper zookeeper

2. 임시 디렉토리 생성

  • 설치 파일을 다운 받을 임시 디렉토리 (temp_src) 를 생성하고 이동한다.

 

[root@server ~]# mkdir /home/temp_src
[root@server ~]# cd /home/temp_src

3. 설치 파일 다운로드

[root@server temp_src]# wget http://apache.tt.co.kr/zookeeper/zookeeper-3.5.7/apache-zookeeper-3.5.7-bin.tar.gz
--2020-03-04 08:06:21--  http://apache.tt.co.kr/zookeeper/zookeeper-3.5.7/apache-zookeeper-3.5.7-bin.tar.gz
Resolving apache.tt.co.kr (apache.tt.co.kr)... 211.47.69.77
Connecting to apache.tt.co.kr (apache.tt.co.kr)|211.47.69.77|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9311744 (8.9M) [application/x-gzip]
Saving to: ‘apache-zookeeper-3.5.7-bin.tar.gz’
 
100%[===================================================================================================================================================================================================>] 9,311,744   16.7MB/s   in 0.5s
 
2020-03-04 08:06:22 (16.7 MB/s) - ‘apache-zookeeper-3.5.7-bin.tar.gz’ saved [9311744/9311744]

4. 압축 해제 및 경로 이동

  • 실제로 사용할 어플리케이션 (apps) 경로로 이동한다.
[root@server temp_src]# tar xfz apache-zookeeper-3.5.7-bin.tar.gz
[root@server temp_src]# mv apache-zookeeper-3.5.7-bin /home/apps/.

5. 권한 부여 및 심볼릭 링크 생성

  • /home/apps/apache-zookeeper-3.5.7-bin 경로에 root 그룹의 root 유저의 소유자로 권한을 부여한다.
  • /home/apps/apache-zookeeper-3.5.7-bin에서 /home/apps/zookeeper로 심볼릭 링크를 생성한다.

 

 

 

[root@server temp_src]# chown -R root.root /home/apps/apache-zookeeper-3.5.7-bin/
[root@server temp_src]# ln -s /home/apps/apache-zookeeper-3.5.7-bin /home/apps/zookeeper
[root@server apps]# ls /home/apps/ -al
total 8
drwxr-xr-x   9 root root  150 Mar  4 08:09 .
drwxr-xr-x. 10 root root 4096 Mar  3 14:57 ..
drwxr-xr-x   6 root root  126 Mar  4 08:07 apache-zookeeper-3.5.7-bin
lrwxrwxrwx   1 root root   37 Mar  4 08:09 zookeeper -> /home/apps/apache-zookeeper-3.5.7-bin

6. 주키퍼 설정 파일 생성

  • 샘플로 제공되어 있는 주키퍼 설정 파일을 복사한다. (-av 옵션은 복사시 권한까지 같이 복사하고 명령어 내용을 출력하는 옵션이다.)
  • 복사 완료 후 zoo.cfg 를 하기와 같이 수정한다.
[root@server ~]# cp -av /home/apps/zookeeper/conf/zoo_sample.cfg /home/apps/zookeeper/conf/zoo.cfg
[root@server ~]# vi /home/apps/zookeeper/conf/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
dataDir=/data/zookeeper/data
# 로그 저장소
dataLogDir=/data/zookeeper/logs
# 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
maxClientCnxns=500
#
# 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
 
# 주키퍼 서버 목록
server.1=127.0.0.1:2888:3888
##############################################################################################################################

7. 주키퍼 데이터 디렉토리 생성 및 권한 부여

[root@server ~]# mkdir -p /data/zookeeper
[root@server ~]# chown zookeeper.zookeeper /data/zookeeper

8. 주키퍼 서버의 유니크한 아이디 설정

  • 001 서버라면 echo 1 > /data/zookeeper/myid, 002 서버라면 echo 2 > /data/zookeeper/myid 등으로 유니크한 myid를 생성한다. 
[root@server ~]# echo 1 > /data/zookeeper/myid
[root@server ~]# chown zookeeper.zookeeper /data/zookeeper/myid

    → 호스트 이름에서 server-001, server-002..번호가 붙었으면 명령어 (expr `hostname -s |awk -F '-' '{print $NF}'` + 0 > /data/zookeeper/myid) 로도 생성이 가능하다.

9. 주키퍼 기동 쉘 작성

  • 주키퍼를 동작하기 위해서 환경변수를 설정하고 그 환경변수를 활용하여 주키퍼를 실행시키는 쉘의 내용이다.
[root@server ~]# vi /usr/bin/zookeeper-server
 
##############################################################################################################################
#!/bin/bash
#
## Autodetect JAVA_HOME if not defined
##. /usr/lib/bigtop-utils/bigtop-detect-javahome
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export ZOOPIDFILE=${ZOOPIDFILE:-/home/apps/zookeeper/run/zookeeper-server.pid}
export ZOOKEEPER_HOME=${ZOOKEEPER_CONF:-/home/apps/zookeeper}
export ZOOKEEPER_CONF=${ZOOKEEPER_CONF:-/home/apps/zookeeper/conf}
export ZOOCFGDIR=${ZOOCFGDIR:-$ZOOKEEPER_CONF}
export CLASSPATH=$CLASSPATH:$ZOOKEEPER_CONF:$ZOOKEEPER_HOME/*:$ZOOKEEPER_HOME/lib/*
export ZOO_LOG_DIR=${ZOO_LOG_DIR:-/home/apps/zookeeper/logs}
export ZOO_LOG4J_PROP=${ZOO_LOG4J_PROP:-INFO,ROLLINGFILE}
export JVMFLAGS=${JVMFLAGS:--Dzookeeper.log.threshold=INFO}
export ZOO_DATADIR_AUTOCREATE_DISABLE=${ZOO_DATADIR_AUTOCREATE_DISABLE:-true}
env CLASSPATH=$CLASSPATH /home/apps/zookeeper/bin/zkServer.sh "$@"
##############################################################################################################################

10. 주키퍼 초기화 쉘 작성

  • 주키퍼의 데이터 경로등의 달라졌을때 사용하는 쉘이다. 기동 쉘이란 내용은 똑같고 맨 하단의 내용만 다르다.
[root@server ~]# vi /usr/bin/zookeeper-initialize
 
##############################################################################################################################
#!/bin/bash
#
## Autodetect JAVA_HOME if not defined
##. /usr/lib/bigtop-utils/bigtop-detect-javahome
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export ZOOPIDFILE=${ZOOPIDFILE:-/home/apps/zookeeper/run/zookeeper-server.pid}
export ZOOKEEPER_HOME=${ZOOKEEPER_CONF:-/home/apps/zookeeper}
export ZOOKEEPER_CONF=${ZOOKEEPER_CONF:-/home/apps/zookeeper/conf}
export ZOOCFGDIR=${ZOOCFGDIR:-$ZOOKEEPER_CONF}
export CLASSPATH=$CLASSPATH:$ZOOKEEPER_CONF:$ZOOKEEPER_HOME/*:$ZOOKEEPER_HOME/lib/*
export ZOO_LOG_DIR=${ZOO_LOG_DIR:-/home/apps/zookeeper/logs}
export ZOO_LOG4J_PROP=${ZOO_LOG4J_PROP:-INFO,ROLLINGFILE}
export JVMFLAGS=${JVMFLAGS:--Dzookeeper.log.threshold=INFO}
export ZOO_DATADIR_AUTOCREATE_DISABLE=${ZOO_DATADIR_AUTOCREATE_DISABLE:-true}
env CLASSPATH=$CLASSPATH /home/apps/zookeeper/bin/zkServer-initialize.sh "$@"
##############################################################################################################################

11. 주키퍼 기동 및 초기화 쉘에 권한 부여

[root@server ~]# chmod 755 /usr/bin/zookeeper-server
[root@server ~]# chmod 755 /usr/bin/zookeeper-initialize

12. 주키퍼 기동 및 로그 디렉토리 생성 및 소유자 부여

[root@server ~]# mkdir /home/apps/zookeeper/run && chown zookeeper.zookeeper /home/apps/zookeeper/run
[root@server ~]# mkdir /home/apps/zookeeper/logs && chown zookeeper.zookeeper /home/apps/zookeeper/logs

13. 주키퍼 JVM 설정

  • 기본 2G로 설정한다.
[root@server ~]# vi /home/apps/zookeeper/conf/java.env 
 
##############################################################################################################################
export JVMFLAGS="-Xmx2G"
##############################################################################################################################

14. 주키퍼 메인 쉘 작성

  • 쉘 실행시 인자값을 받아 기동 쉘, 초기화 쉘을 모두 사용할 수 있는 쉘을 말한다.
[root@server ~]# vi /etc/init.d/zookeeper-server
 
##############################################################################################################################
#!/bin/bash
  
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
  
# chkconfig: 2345 80 20
# description: Summary: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.
# processname: java
# pidfile: /var/run/zookeeper/zookeeper-server.pid
### BEGIN INIT INFO
# Provides:          zookeeper-server
# Required-Start:    $network $local_fs
# Required-Stop:
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
### END INIT INFO
set -e
  
# Autodetect JAVA_HOME if not defined
#. /usr/lib/bigtop-utils/bigtop-detect-javahome
export JAVA_HOME=/usr/lib/jvm/jre-openjdk
export _JAVA_OPTIONS="-Djava.net.preferIPv4Stack=true"
. /etc/default/zookeeper
  
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON_SCRIPT="/usr/bin/zookeeper-server"
INIT_SCRIPT="/usr/bin/zookeeper-initialize"
  
NAME=zookeeper-server
DESC="ZooKeeper daemon"
PID_FILE=/home/apps/zookeeper/run/zookeeper-server.pid
install -d -m 0755 -o zookeeper -g zookeeper /var/run/zookeeper/
  
DODTIME=3
  
  
# Checks if the given pid represents a live process.
# Returns 0 if the pid is a live process, 1 otherwise
hadoop_is_process_alive() {
  local pid="$1"
  ps -fp $pid | grep $pid | grep zookeeper > /dev/null 2>&1
}
  
hadoop_check_pidfile() {
    local pidfile="$1" # IN
    local pid
  
    pid=`cat "$pidfile" 2>/dev/null`
    if [ "$pid" = '' ]; then
    # The file probably does not exist or is empty.
        return 1
    fi
      
    set -- $pid
    pid="$1"
  
    hadoop_is_process_alive $pid
}
  
hadoop_process_kill() {
    local pid="$1"    # IN
    local signal="$2" # IN
    local second
  
    kill -$signal $pid 2>/dev/null
  
   # Wait a bit to see if the dirty job has really been done
    for second in 0 1 2 3 4 5 6 7 8 9 10; do
        if hadoop_is_process_alive "$pid"; then
         # Success
            return 0
        fi
  
        sleep 1
    done
  
   # Timeout
    return 1
}
hadoop_stop_pidfile() {
    local pidfile="$1" # IN
    local pid
  
    pid=\`cat "$pidfile" 2>/dev/null\`
    if [ "$pid" = '' ]; then
      # The file probably does not exist or is empty. Success
        return 0
    fi
      
    set -- $pid
    pid="$1"
  
   # First try the easy way
    if hadoop_process_kill "$pid" 15; then
        return 0
    fi
  
   # Otherwise try the hard way
    if hadoop_process_kill "$pid" 9; then
        return 0
    fi
  
    return 1
}
  
  
start() {
    su -s /bin/bash zookeeper -c "${DAEMON_SCRIPT} start"
}
stop() {
    if hadoop_check_pidfile $PID_FILE ;  then
    su -s /bin/bash zookeeper -c "${DAEMON_SCRIPT} stop"
    fi
}
  
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    force-stop)
        echo -n "Forcefully stopping $DESC: "
        hadoop_stop_pidfile $PID_FILE
        if hadoop_check_pidfile $PID_FILE ; then
            echo "$NAME."
        else
            echo " ERROR."
        fi
        ;;
    force-reload|condrestart|try-restart)
  # check wether $DAEMON is running. If so, restart
        hadoop_check_pidfile $PID_FILE && $0 restart
        ;;
    restart|reload)
        echo -n "Restarting $DESC: "
        stop
        [ -n "$DODTIME" ] && sleep $DODTIME
        $0 start
        ;;
    status)
        echo -n "$NAME is "
        if hadoop_check_pidfile $PID_FILE ;  then
            echo "running"
        else
            echo "not running."
            exit 1
        fi
        ;;
    init)
        if hadoop_check_pidfile $PID_FILE ; then
          echo "Error: $DESC is running. Stop it first." >&2
          exit 1
        else
          shift
          # su -s /bin/bash zookeeper -c "zookeeper-server-initialize $*"  
          su -s /bin/bash zookeeper -c "${INIT_SCRIPT} $*"               
        fi
        ;;
    *)
        N=/etc/init.d/$NAME
  # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
        echo "Usage: $N {start|stop|restart|force-reload|status|force-stop|condrestart|try-restart|init}" >&2
  
        exit 1
        ;;
esac
  
exit 0 
##############################################################################################################################

15. 메인 쉘 권한 부여

[root@server data]# chmod 755 /etc/init.d/zookeeper-server

16. 기본 확장 환경변수 파일 작성

  • 전부 주석 처리되어 있어 현재 설정 내용은 없다.

 

[root@server data]# vi /etc/default/zookeeper
 
##############################################################################################################################
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
   
# Command-line parameters to pass to the JVM
# export SERVER_JVMFLAGS=""
##############################################################################################################################

17. 기본 확장 환경변수 파일 권한 부여

[root@server data]# chmod 755 /etc/default/zookeeper

18. 주키퍼 실행 시작 프로그램에 등록

[root@server data]# chkconfig --add zookeeper-server

19. 주키퍼 로그 삭제 크론 등록

[root@server data]# vi /root/tcs_tools/cron/temp/cron_zk_cleanup
 
##############################################################################################################################
# cleanup zookeeper old log and snapshot
0 0 * * * JAVA_HOME=/usr/lib/jvm/jre-openjdk /home/apps/zookeeper/bin/zkCleanup.sh -n 3
##############################################################################################################################

20. 크론 재시작

[root@server ~]# systemctl restart crond.service

 

실행

시작

[root@server data]# /etc/init.d/zookeeper-server start
ZooKeeper JMX enabled by default
Using config: /home/apps/zookeeper/conf/zoo.cfg
Starting zookeeper ... STARTED

중지

[root@server ~]# /etc/init.d/zookeeper-server stop
ZooKeeper JMX enabled by default
Using config: /home/apps/zookeeper/conf/zoo.cfg
Stopping zookeeper ... STOPPED

상태

# 시작 중일 경우
[root@server ~]# /etc/init.d/zookeeper-server status
zookeeper-server is running
 
# 중지 중일 경우
[root@server ~]# /etc/init.d/zookeeper-server status
zookeeper-server is not running.
728x90
반응형

'DevOps > 기타' 카테고리의 다른 글

kafka  (0) 2021.12.20
sbt 설치  (0) 2020.03.09
lein 설치  (0) 2020.03.09
openJDK 설치  (0) 2020.03.09
Kafka  (0) 2018.12.31