# 环境:
java8,centos7
192.168.147.152 node3
192.168.147.153 node4
192.168.147.154 node5
基础环境配置
0.host配置
在node3,node4,node5上面配置host映射:
# cat /etc/hosts
192.168.147.152 node3
192.168.147.153 node4
192.168.147.154 node5
1.jdk配置
下载jdk8:https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html
上传rpm安装包:jdk-8u221-linux-x64.rpm安装,默认安装目录/usr/java:
(在node3,node4,node5相同操作,可以配置一台,再复制)
rpm -ivh jdk-8u221-linux-x64.rpm
配置JAVA_HOME:
vim /etc/profile
添加配置:
export JAVA_HOME=/usr/java/jdk1.8.0_221-amd64
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/jconsole.jar
export PATH=$PATH:$JAVA_HOME/bin
使环境变量立即生效:
source /etc/profile
2.免密配置
在node3,node4,node5上面执行以下命令,多次回车,使用默认配置:
ssh-keygen -t rsa
在node3上执行:
cd /root/.ssh
# 如果没有authorized_keys则使用下面命令创建,有则不必执行
touch authorized_keys & chmod 600 authorized_keys
# 将自己的公钥信息也放入authorized_keys中
cat id_rsa.pub >> authorized_keys
在node4上执行:
ssh-copy-id -i /root/.ssh/id_rsa.pub node3
在node5上执行:
ssh-copy-id -i /root/.ssh/id_rsa.pub node3
在node3上执行(注意:执行的前提是node4和node5下面没有authorized_keys文件,否则会覆盖,如果有,建议自己手动拷贝):
scp /root/.ssh/authorized_keys node4:/root/.ssh/
scp /root/.ssh/authorized_keys node5:/root/.ssh/
测试免密连接,没有提示输入密码,则配置成功:
ssh node4
ssh node5
ssh node3
3.配置zk环境变量
node3,node4,node5配置zk环境变量:
# vim /etc/profile
export ZK_HOME=/usr/local/zookeeper/apache-zookeeper-3.8.0-bin
export PATH=$PATH:$ZK_HOME/bin
# source /etc/profile
4.关闭防火墙(生产环境禁止操作,由运维配置开放端口)
关闭node5/node4/node3防火墙或开放端口
systemctl stop firewalld
systemctl disable firewalld
扩展:CentOS7开放端口
# 开放端口
firewall-cmd --zone=public --add-port=1935/tcp --permanent
#命令含义:
# --zone #作用域
# --add-port=1935/tcp #添加端口,格式为:端口/通讯协议
# --permanent #永久生效,没有此参数重启后失效
# 重启防火墙
firewall-cmd --reload
# 查看tcp端口
netstat -ntlp
一、Zookeeper下载
官方下载地址:https://zookeeper.apache.org/releases.html#download
3.8.0版本下载:https://dlcdn.apache.org/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz
二、上传安装包到/usr/local/zookeeper
node5,node4,node3都上传至/usr/local/zookeeper/
三、解压
# node5,node4,node3都执行
# tar -zxvf apache-zookeeper-3.8.0-bin.tar.gz
四、集群部署
参考:https://zookeeper.apache.org/doc/current/zookeeperStarted.html
1.配置第一台node3
# 进入配置
cd /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/conf
# 复制文件
cp zoo_sample.cfg zoo.cfg
# 创建数据目录
mkdir -p /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/data
# vim zoo.cfg
# cat zoo.cfg |grep -v "#"
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/data
clientPort=2181
# 节点配置,server后面的id编号和各节点下的myid保持一致
server.1=node3:2888:3888
server.2=node4:2888:3888
server.3=node5:2888:3888
2.复制node3配置到node4,node5
在node4和node5上执行,创建数据目录:
mkdir -p /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/data
在node3上执行:
scp -r zoo.cfg node4:/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/conf/zoo.cfg
scp -r zoo.cfg node5:/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/conf/zoo.cfg
3.创建myid文件
注意:myid的编号一定要和配置文件中的对应:
server.1=node3:2888:3888
server.2=node4:2888:3888
server.3=node5:2888:3888
在node3,node4,node5的/usr/local/zookeeper/apache-zookeeper-3.8.0-bin/data目录下创建myid并配置节点id:
# 进入数据目录
cd /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/data
# node5 myid=3
touch myid && echo 3 > myid
#node4 myid=2
touch myid && echo 2 > myid
#node3 myid=1
touch myid && echo 1 > myid
五、ZK操作命令
zkServer.sh start #启动ZK服务
zkServer.sh start-foreground #日志启动方式
zkServer.sh stop #停止ZK服务
zkServer.sh restart #重启ZK服务
zkServer.sh status #查看ZK状态
六、启动
分别启动node3,node4,node5
# 如果没有配置环境变量需要进入bin执行脚本
cd /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin
# ./zkServer.sh start
# 如果配置了环境变量不需要进入bin执行脚本
# zkServer.sh start
七、查看启动状态
# 分别启动node3,node4,node5之后,查看启动状态
[root@node3 data]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
[root@node4 data]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
[root@node5 /]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.8.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower