微系列:4、在Centos中,搭建Zookeeper集群

2023-01-09 18:52:34 浏览数 (2)

一、环境准备

1、需安装Java环境并配置环境变量

https://jdk.java.net/java-se-ri/11

2、下载Zookeeper(3.6.3)

压缩包并解压 https://zookeeper.apache.org/releases.html

3、配置防火墙,开放相关端口

二、修改配置

进入Zookeeper目录下的conf目录,修改zoo_sample.cfg配置文件内容为

修改以下配置,

代码语言:javascript复制
# 数据目录
dataDir=/var/lib/zookeeper/
# 集群各节点信息
server.1=192.168.0.11:2888:3888
server.2=192.168.0.12:2888:3888
server.3=192.168.0.13:2888:3888

三、启动

进入bin目录下,执行以下命令

在各个不同的服务器分别执行,启动多个服务。

代码语言:javascript复制
./zkServer.sh start

Zookeeper会根据配置文件中的各节点信息,自动完成选举,并组建集群

四、设置开机启动

创建文件 /etc/rc.d/init.d/zookeeper

写入以下内容

代码语言:javascript复制
#!/bin/bash
#
# zookeeper  ---  this script is used to start and stop zookeeper
#
# chkconfig:   - 80 12
# description:  zookeeper is a centralized service for maintaining configuration information,naming,providing distributed synchronization,and providing group services. 
# processname: zookeeper

EXEC=/usr/local/apache-zookeeper-3.6.3-bin/bin/zkServer.sh
PIDFILE=/var/lib/zookeeper/zookeeper_server.pid
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Zookeeper server..."
                $EXEC start
                echo "Zookeeper server started"
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $EXEC stop
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Zookeeper to shutdown ..."
                    sleep 1
                done
                echo "Zookeeper stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

添加可执行权限

chmod x /etc/rc.d/init.d/zookeeper

注册为系统服务

chkconfig --add zookeeper

添加开机自启动

chkconfig zookeeper on

0 人点赞