centos 搭建 zookeeper 高可用集群

zookeeper-ha

主机名 IP地址
spark01 192.168.171.101
spark02 192.168.171.102
spark03 192.168.171.103

1. 升级内核和软件

yum -y update

2. 安装常用软件

yum -y install gcc gcc-c++ autoconf automake cmake make \
 zlib zlib-devel openssl openssl-devel pcre-devel \
 rsync openssh-server vim man zip unzip net-tools tcpdump lrzsz tar wget

3. 关闭防火墙

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld
systemctl disable firewalld

4. 修改主机名

hostnamectl set-hostname spark01
hostnamectl set-hostname spark02
hostnamectl set-hostname spark03

5. 修改IP地址

vim /etc/sysconfig/network-scripts/ifcfg-ens32

参考如下:

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens32"
DEVICE="ens32"
ONBOOT="yes"
IPADDR="192.168.171.101"
PREFIX="24"
GATEWAY="192.168.171.2"
DNS1="192.168.171.2"
IPV6_PRIVACY="no"

6. 修改hosts配置文件

vim /etc/hosts

修改内容如下:

192.168.171.101	spark01
192.168.171.102	spark02
192.168.171.103	spark03

7. 重启系统

reboot

8. 下载安装JDK和Zookeeper并配置环境变量

在所有主机节点创建软件目录

mkdir -p /opt/soft 

以下操作在 spark01 主机上完成

进入软件目录

cd /opt/soft

下载JDK

wget https://download.oracle.com/otn/java/jdk/8u391-b13/b291ca3e0c8548b5a51d5a5f50063037/jdk-8u391-linux-x64.tar.gz?AuthParam=1698206552_11c0bb831efdf87adfd187b0e4ccf970

下载 Zookeeper

wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.3/apache-zookeeper-3.8.3-bin.tar.gz

解压 JDK 修改名称

解压 zookeeper 修改名称

tar -zxvf jdk-8u391-linux-x64.tar.gz
mv jdk1.8.0_391 jdk-8
tar -zxvf apache-zookeeper-3.8.3-bin.tar.gz
mv apache-zookeeper-3.8.3-bin zookeeper-3

配置环境变量

vim /etc/profile.d/my_env.sh

编写以下内容:

export JAVA_HOME=/opt/soft/jdk-8
export set JAVA_OPTS="--add-opens java.base/java.lang=ALL-UNNAMED"

export ZOOKEEPER_HOME=/opt/soft/zookeeper-3

export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin

在各服务器上使环境变量生效

source /etc/profile

10. 编辑配置文件

cd $ZOOKEEPER_HOME/conf
vim zoo.cfg
# 心跳单位,2s
tickTime=2000
# zookeeper-3初始化的同步超时时间,10个心跳单位,也即20s
initLimit=10
# 普通同步:发送一个请求并得到响应的超时时间,5个心跳单位也即10s
syncLimit=5
# 内存快照数据的存储位置
dataDir=/home/zookeeper-3/data
# 事务日志的存储位置
dataLogDir=/home/zookeeper-3/datalog
# 当前zookeeper-3节点的端口 
clientPort=2181
# 单个客户端到集群中单个节点的并发连接数,通过ip判断是否同一个客户端,默认60
maxClientCnxns=1000
# 保留7个内存快照文件在dataDir中,默认保留3个
autopurge.snapRetainCount=7
# 清除快照的定时任务,默认1小时,如果设置为0,标识关闭清除任务
autopurge.purgeInterval=1
#允许客户端连接设置的最小超时时间,默认2个心跳单位
minSessionTimeout=4000
#允许客户端连接设置的最大超时时间,默认是20个心跳单位,也即40s,
maxSessionTimeout=300000
#zookeeper-3 3.5.5启动默认会把AdminService服务启动,这个服务默认是8080端口
admin.serverPort=9001
#集群地址配置
server.1=spark01:2888:3888
server.2=spark02:2888:3888
server.3=spark03:2888:3888
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/zookeeper-3/data
dataLogDir=/home/zookeeper-3/datalog 
clientPort=2181
maxClientCnxns=1000
autopurge.snapRetainCount=7
autopurge.purgeInterval=1
minSessionTimeout=4000
maxSessionTimeout=300000
admin.serverPort=9001
server.1=spark01:2888:3888
server.2=spark02:2888:3888
server.3=spark03:2888:3888

11. 保存后根据配置文件创建目录

在每台服务器上执行

mkdir -p /home/zookeeper-3/data
mkdir -p /home/zookeeper-3/datalog

12. 编写zookeeper-3开机启动脚本

在/etc/systemd/system/文件夹下创建一个启动脚本zookeeper-3.service

注意:在每台服务器上编写

cd /etc/systemd/system
vim zookeeper.service

内容如下:

[Unit]
Description=zookeeper
After=syslog.target network.target

[Service]
Type=forking
# 指定zookeeper-3 日志文件路径,也可以在zkServer.sh 中定义
Environment=ZOO_LOG_DIR=/home/zookeeper-3/datalog
# 指定JDK路径,也可以在zkServer.sh 中定义
Environment=JAVA_HOME=/opt/soft/jdk-8
ExecStart=/opt/soft/zookeeper-3/bin/zkServer.sh start
ExecStop=/opt/soft/zookeeper-3/bin/zkServer.sh stop
Restart=always
User=root
Group=root

[Install]
WantedBy=multi-user.target
[Unit]
Description=zookeeper
After=syslog.target network.target

[Service]
Type=forking
Environment=ZOO_LOG_DIR=/home/zookeeper-3/datalog
Environment=JAVA_HOME=/opt/soft/jdk-8
ExecStart=/opt/soft/zookeeper-3/bin/zkServer.sh start
ExecStop=/opt/soft/zookeeper-3/bin/zkServer.sh stop
Restart=always
User=root
Group=root

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
# 等所有主机配置好后再执行以下命令
systemctl start zookeeper
systemctl enable zookeeper
systemctl status zookeeper

13. 配置ssh免密钥登录

创建本地秘钥并将公共秘钥写入认证文件

ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
ssh-copy-id root@spark01
ssh-copy-id root@spark02
ssh-copy-id root@spark03
ssh root@spark01
exit
ssh root@spark02
exit
ssh root@spark03
exit

14. 分发软件和配置文件

分发 ssh 免密钥

scp -r ~/.ssh root@spark02:~/
scp -r ~/.ssh root@spark03:~/

分发 hosts 文件

scp -r /etc/hosts root@spark02:/etc/
scp -r /etc/hosts root@spark03:/etc/

分发软件

scp -r /opt/soft/jdk-8 root@spark02:/opt/soft
scp -r /opt/soft/zookeeper-3 root@spark02:/opt/soft
scp -r /opt/soft/jdk-8 root@spark03:/opt/soft
scp -r /opt/soft/zookeeper-3 root@spark03:/opt/soft

分发环境变量

scp /etc/profile.d/my_env.sh root@spark02:/etc/profile.d/
scp /etc/profile.d/my_env.sh root@hadoop103:/etc/profile.d/

在所有主机节点 使新的环境变量生效

source /etc/profile

15. myid

spark01

echo 1 > /home/zookeeper-3/data/myid
more /home/zookeeper-3/data/myid

spark02

echo 2 > /home/zookeeper-3/data/myid
more /home/zookeeper-3/data/myid

spark03

echo 3 > /home/zookeeper-3/data/myid
more /home/zookeeper-3/data/myid

16. 启动服务

在各节点执行以下命令

systemctl daemon-reload
systemctl start zookeeper
systemctl enable zookeeper
systemctl status zookeeper

17. 验证

jps
zkServer.sh status

你可能感兴趣的:(Hadoop,大数据,Zookeeper,centos,zookeeper,linux,大数据,数据分析)