一、安装Centos系统
1. Centos系统下载地址:https://www.centoschina.cn/downloads
2. 创建新用户
(1)创建zookeeper 用户
[root@localhost ~]# adduser zookeeper
(2)设置密码
[root@localhost ~]# passwd zookeeper
Changing password for user zookeeper.
New password:
(3)安装Linux命令工具包
a. 安装rz和sz命令 lrzsz
[root@localhost ~]# yum -y install lrzsz
b. 安装ifconfig和netstat命令
[root@localhost ~]# yum install net-tools
c. 设置centos的网络
输入下下面的命令,修改ifcfg-ens33文件,安下图修改,完成后重新启动系统,可以远程链接访问。
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
用下面的命令验证,结果如下图。
[rabbitmq@localhost ~]$ ifconfig
3. Linux系统安装JDK
(1)下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
(2)安装JDK
a. 在/usr/lib/目录下建立文件夹jdk
[root@localhost lib]# mkdir -p /usr/lib/jdk
b. 上传JDK文件到jdk目录
[root@localhost jdk]# rz
c. 解压JDK文件
[root@localhost jdk]# tar -zxvf jdk-8u181-linux-x64.tar.gz
d. 解压完成删除掉压缩包
[root@localhost jdk]# rm -rf jdk-8u181-linux-x64.tar.gz
e.设置Java环境变量
打开profile文件
[root@localhost jdk]# vi /etc/profile
在文件的最后面增加下面参数:
export JAVA_HOME=/usr/lib/jdk/jdk1.8.0_181
export JRE_HOME=/usr/lib/jdk/jdk1.8.0_181/jre
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
然后保存退出,生效环境变量:
[root@localhost jdk]# source /etc/profile
然后验证安装:
[root@localhost jdk]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
(3)如果是集群机器可把安装好的JDK分发到其他集群机器
在JDK的安装的上层目录执行如下命令:
[root@localhost jdk]# ls
jdk1.8.0_181
[root@localhost jdk]# scp -r jdk1.8.0_181 root@192.168.3.129:/usr/lib/jdk
[root@localhost jdk]# scp -r jdk1.8.0_181 root@192.168.3.132:/usr/lib/jdk
[root@localhost jdk]# scp -r jdk1.8.0_181 root@192.168.3.133:/usr/lib/jdk
[root@localhost jdk]# scp -r jdk1.8.0_181 root@192.168.3.134:/usr/lib/jdk
二、安装zookeeper
当机器的Java环境准备好之后,可以进行zookeeper的安装了。
1. 下载zookeeper
下载地址:https://archive.apache.org/dist/zookeeper/
2. 建立zookeeper应用目录
[root@localhost app]# mkdir -p /home/zookeeper/app
3. 上传zookeeper文件并解压
上传文件:
[root@localhost app]# rz
解压:
[root@localhost app]# tar -zxvf zookeeper-3.4.6.tar.gz
删除压缩文件:
[root@localhost app]# rm -rf zookeeper-3.4.6.tar.gz
改文件名:
[root@localhost app]# mv zookeeper-3.4.6 zookeeper
将zookeeper文件夹受权给zookeeper用户:
[root@localhost app]# chown -R zookeeper:zookeeper zookeeper
命令解释: chown -R zookeeper(用户名):zookeeper(密码) zookeeper(需要授权的文件夹)
4. 配置zookeeper
切换用户:
[root@localhost app]# su zookeeper
进入conf目录:
[zookeeper@localhost conf]$ cd /home/zookeeper/app/zookeeper/conf
查看文件:
[zookeeper@localhost conf]$ ls
configuration.xsl log4j.properties zoo_sample.cfg
复制文件:
[zookeeper@localhost conf]$ cp zoo_sample.cfg zoo.cfg
编辑 zoo.cfg:
[zookeeper@localhost conf]$ vi zoo.cfg
配置如下:
创建zookeeper的数据目录和日志目录:
[zookeeper@localhost ~]$ mkdir -p /home/zookeeper/data/zookeeper/zkdata
[zookeeper@localhost ~]$ mkdir -p /home/zookeeper/data/zookeeper/zkdatalog
5. 防火墙设置
zookeeper 集群的时候需要让各个机器互通,这里简单处理,关闭防火墙。
[root@localhost zookeeper]# systemctl stop firewalld
6. 配置集群zookeeper
1)先准备好集群集群机器
我这里准备了5台机器进行集群,其中3台机器参与leader的选举,其他两个机器作为监控,不参加选举。
集群的机器需要做如下准备(参照上面的操作):
(1) 安装好操作系统
(2) 创建好管理zookeeper的用户
(3) 安装好JDK
(4) 创建好zookeeper数据和日志目录
2)将已经安装好的zookeeper分发给其他集群机器
准备好环境以后,不用每个机器都重新安装一遍zookeeper,可以采用下面命令把已经安装好的zookeeper分发给其他集群机器:
[zookeeper@localhost app]$ scp -r zookeeper zookeeper@192.168.3.129:/home/zookeeper/app
[zookeeper@localhost app]$ scp -r zookeeper zookeeper@192.168.3.132:/home/zookeeper/app
[zookeeper@localhost app]$ scp -r zookeeper zookeeper@192.168.3.133:/home/zookeeper/app
[zookeeper@localhost app]$ scp -r zookeeper zookeeper@192.168.3.134:/home/zookeeper/app
3)修改监视器节点的zoo.cfg
因为两台机器作为observer,因此需要修改下这两个机器的zoo.cfg文件,具体如下。
二、启动zookeeper
在zookeeper的安装目录下启动zookeeper:
[zookeeper@localhost zookeeper]$ bin/zkServer.sh start
验证zookeeper启动:
[zookeeper@localhost zookeeper]$ jps
10517 QuorumPeerMain
11190 Jps
查看各个集群节点的状态,如果信息如下表示成功了:
[zookeeper@localhost zookeeper]$ bin/zkServer.sh status
JMX enabled by default
Using config: /home/zookeeper/app/zookeeper/bin/../conf/zoo.cfg
Mode: leader
[zookeeper@localhost zookeeper]$ bin/zkServer.sh status
JMX enabled by default
Using config: /home/zookeeper/app/zookeeper/bin/../conf/zoo.cfg
Mode: follower
[zookeeper@localhost zookeeper]$ bin/zkServer.sh status
JMX enabled by default
Using config: /home/zookeeper/app/zookeeper/bin/../conf/zoo.cfg
Mode: observer