搭建zookeeper集群

zookeeper到官网下载,上传到服务器,解压并放到预定的目录。
这里从已经解压完成开始配置

1. 安装java

vi /etc/profile

# java
export JAVA_HOME=/opt/jdk1.8.0_171
export JRE_HOME=/opt/jdk1.8.0_171/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

2. 配置zookeeper环境变量

这一步好像没啥用

vi /etc/profile

# zookeeper
export ZK_HOME=/usr/local/zookeeper-3.4.12
export PATH=$ZK_HOME/bin:$PATH

3.编辑zoo.cfg文件

[root@localhost zookeeper-3.4.12]# cd conf/
[root@localhost conf]# cp zoo_sample.cfg zoo.cfg
[root@localhost conf]# vi 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.
################# add ######################
dataDir=/opt/zookeeper-3.4.12/data
################# end ######################
#dataLogDir=/opt/zookeeper-3.4.12/log
# 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
################# add ######################
server.0=192.168.1.131:2888:3888
server.1=192.168.1.132:2888:3888
server.2=192.168.1.133:2888:3888
################# end ######################
## server.X=IP:port-1:port-2
## X 指的是zookpeeper的标识,必须唯一
## IP 就是zookeeper安装服务器的IP
## port-1 用来相互之间通讯
## port-2 用来选举leader
############################################

4.编辑myid

由于上一步指定了一个目录dataDir=/opt/zookeeper-3.4.12/data,zookeeper并不会因为配置而自动创建这个目录,手动创建,然后进入目录,创建一个myid的文件

[root@localhost zookeeper-3.4.12]# mkdir data
[root@localhost zookeeper-3.4.12]# cd data
[root@localhost zookeeper-3.4.12]# vi myid

## 这是一个新建的文件,输入的内容是根据IP写上边server.X的X的值
## 比方这个服务器是192.168.1.131,那么这个服务器上就输入0,然后保存
0
:wq
## 三个服务器分别输入对应的server.X的X的值

5.配置日志输出文件

设置zookeeper的日志输出文件
先修改bin/zkEnv.sh文件

[root@localhost zookeeper-3.4.12]# cd bin
[root@localhost bin]# vi zkEnv.sh 


if [ "x$ZOOCFGDIR" = "x" ]
then
  if [ -e "${ZOOKEEPER_PREFIX}/conf" ]; then
    ZOOCFGDIR="$ZOOBINDIR/../conf"
  else
    ZOOCFGDIR="$ZOOBINDIR/../etc/zookeeper"
  fi
fi

if [ -f "${ZOOCFGDIR}/zookeeper-env.sh" ]; then
  . "${ZOOCFGDIR}/zookeeper-env.sh"
fi

if [ "x$ZOOCFG" = "x" ]
then
    ZOOCFG="zoo.cfg"
fi

ZOOCFG="$ZOOCFGDIR/$ZOOCFG"

if [ -f "$ZOOCFGDIR/java.env" ]
then
    . "$ZOOCFGDIR/java.env"
fi

if [ "x${ZOO_LOG_DIR}" = "x" ]
then
################# 重点,修改到制定的日志输出目录 #########################
    ZOO_LOG_DIR="/opt/zookeeper-3.4.12/log"
fi

if [ "x${ZOO_LOG4J_PROP}" = "x" ]
then
################## 指定日志输出级别,滚动方式 ############################
    ZOO_LOG4J_PROP="INFO,ROLLINGFILE"
fi

if [ "$JAVA_HOME" != "" ]; then
  JAVA="$JAVA_HOME/bin/java"
else
  JAVA=java
fi

:wq

在修改conf/log4j.properties文件

[root@localhost zookeeper-3.4.12]# cd conf/
[root@localhost conf]# vi log4j.properties 

# Define some default values that can be overridden by system properties
## 日志输出级别,滚动方式需要跟zkEnv.sh里配置的一样
zookeeper.root.logger=INFO, ROLLINGFILE
zookeeper.console.threshold=INFO
## 日志输出目录
zookeeper.log.dir=/opt/zookeeper-3.4.12/log
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=DEBUG
zookeeper.tracelog.dir=.
zookeeper.tracelog.file=zookeeper_trace.log

#
# ZooKeeper Logging Configuration
#

# Format is "<default threshold> (, <appender>)+

# DEFAULT: console appender only
log4j.rootLogger=${zookeeper.root.logger}

# Example with rolling log file
#log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE

# Example with rolling log file and tracing
#log4j.rootLogger=TRACE, CONSOLE, ROLLINGFILE, TRACEFILE

#
# Log INFO level and above messages to the console
#
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold}
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n

#
# Add ROLLINGFILE to rootLogger to get log file output
#    Log DEBUG level and above messages to a log file
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold}
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file}

## 每个文件滚动的大小
# Max log file size of 10MB
log4j.appender.ROLLINGFILE.MaxFileSize=10MB
# uncomment the next line to limit number of backup files
#log4j.appender.ROLLINGFILE.MaxBackupIndex=10

log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n

6. 关闭防火墙

我是直接关闭了,当然还可以设置防火墙规则,以放开2181、2888、3888这些端口的通讯

[root@localhost conf]# systemctl stop firewalld.service

7. 启动、验证

每台机器上分别执行

## 启动
[root@localhost zookeeper-3.4.12]# bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

## 验证
[root@localhost zookeeper-3.4.12]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: follower

## 换个机器
[root@localhost zookeeper-3.4.12]# bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.12/bin/../conf/zoo.cfg
Mode: leader

成功

阅读更多
文章标签: zookeeper
个人分类: Java Devops
上一篇Kafka学习笔记(一) —— 初识
下一篇kafka集群搭建
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭