zookeeper需要基于java环境下运行,先安装java。
需要注意的是 安装的是oracle版本的jdk非open jdk,有些服务器是自带java,但是是 openjdk版本的,需先卸载再安装 oracle版本的jdk
Java环境搭建
首先更新系统软件包,
yum -y update
系统检查是否自带java,
[root@VM_0_14_centos ~]# java -version
-bash: java: command not found
准备安装
下载jdk,我是选择先在电脑上下载jdk-8u241-linux-x64.rpm再使用XFTP传到服务器上,
oracle JDK官网地址
将下载完的安装包放到 /root目录下,用ll命令查看
[root@VM_0_14_centos ~]# ll
total 298980
-rw-r--r-- 1 root root 111607808 Apr 6 14:49 jdk-8u241-linux-x64.rpm
使用rmp命令安装(注意我下载的安装包是.rmp后缀结尾的)
[root@VM_0_14_centos ~]# rpm -ivh jdk-8u241-linux-x64.rpm
warning: jdk-8u241-linux-x64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:jdk1.8-2000:1.8.0_241-fcs ################################# [100%]
待安装完毕,查看java 版本,java -version
[root@VM_0_14_centos ~]# java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
配置环境变量
a、配置环境变量之前需要先找到Java的安装路径
[root@VM_0_14_centos ~]# update-alternatives --config java
There is 1 program that provides 'java'.
Selection Command
-----------------------------------------------
*+ 1 /usr/java/jdk1.8.0_241-amd64/jre/bin/java
Enter to keep the current selection[+], or type selection number:
b、记下java的安装路径: /usr/java/jdk1.8.0_241-amd64/jre(丢掉'/bin/java')
c、在/etc/profile文件后面添加:
export JAVA_HOME=/usr/java/jdk1.8.0_241-amd64/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
d、此时使用命令刷新
source /etc/profile
以上Java环境搭建完毕,
Zookeeper安装及配置
下载Zookeeper安装包,选择zookeeper-3.4.14版本,此版本对于初次安装着较友好,同样还是先下载到本地,再上传
下载地址
公网集群配置
上传到/usr/local/目录下,ll命令查看
[root@VM_0_14_centos local]# ll
total 36840
.....
srwxrwxrwx 1 root root 0 Apr 6 13:47 yd.socket.server
-rw-r--r-- 1 root root 37676320 Apr 6 15:29 zookeeper-3.4.14.tar.g
使用tar命令解压:tar -xzvf zookeeper-3.4.14.tar.gz
重命名zookeeper文件夹:mv zookeeper-3.4.14 zookeeper
配置环境变量,在/etc/profile文件后面加上以下:
export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$ZOOKEEPER_HOME/bin:$PATH
使配置生效,执行命令: source /etc/profile
进入zookeeper目录,我这是: /usr/local/zookeeper
创建data文件夹(mkdir),进入data文件(cd data),
执行如下命令,配置本机的zookeeper ID,
echo 1 >> /usr/local/zookeeper/data/myid
/*
创建myid文件并将当前服务ID赋值进去,以上是开启一个zookeeper ID是1的节点
*/
进入配置文件夹:cd /usr/local/zookeeper/conf
[root@VM_0_14_centos conf]# ll
total 12
-rw-rw-r-- 1 2002 2002 535 Mar 7 2019 configuration.xsl
-rw-rw-r-- 1 2002 2002 2161 Mar 7 2019 log4j.properties
-rw-rw-r-- 1 2002 2002 922 Mar 7 2019 zoo_sample.cfg
cp zoo_sample.cfg zoo.cfg 复制出一份配置文件
[root@VM_0_14_centos conf]# cp zoo_sample.cfg zoo.cfg
[root@VM_0_14_centos conf]# ll
total 16
-rw-rw-r-- 1 2002 2002 535 Mar 7 2019 configuration.xsl
-rw-rw-r-- 1 2002 2002 2161 Mar 7 2019 log4j.properties
-rw-r--r-- 1 root root 922 Apr 6 16:09 zoo.cfg
-rw-rw-r-- 1 2002 2002 922 Mar 7 2019 zoo_sample.cfg
vim 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.
dataDir=/usr/local/zookeeper/data/ 刚刚创建的myid文件的路径
# the port at which the clients will connect
clientPort=2181
quorumListenOnAllIPs=true 允许外网访问
server.1 = 本机公网地址IP:2888:3888
server.2 = 外网IP:2888:3888
server.3 = 外网IP:2888:3888
以上即配置完成一台服务器的zookeeper服务,
以同样操作,配置其他两台机器,
需要注意的是三台服务器设置的端口是开放的(即2888、3888端口是允许访问的)
启动并检查
启动zookeeper :
cd /usr/local/zookeeper/bin
zkServer.sh start
启动三台服务后,查看服务状态
zkServer.sh status
内网集群配置
创建文件夹:
/home/chen/tmp/zk1/data
/home/chen/tmp/zk2/data
/home/chen/tmp/zk3/data
/home/chen/tmp/zk1/log
/home/chen/tmp/zk2/log
/home/chen/tmp/zk3/log
echo 1 >> /home/chen/tmp/zk1/data/myid
echo 2 >> /home/chen/tmp/zk2/data/myid
echo 3 >> /home/chen/tmp/zk3/data/myid
/*
创建myid文件并将服务ID赋值进去,以上是开启3个节点的3个zookeeper ID文件,
*/
将刚才的zookeerper压缩包上传到/home/chen/目录下,
使用tar命令解压:tar -xzvf zookeeper-3.4.14.tar.gz
重命名zookeeper文件夹:mv zookeeper-3.4.14 zookeeper
cd conf/
cp zoo_sample.cfg zoo.cfg 复制出一份配置文件
[root@VM_0_14_centos conf]# cp zoo_sample.cfg zoo.cfg
[root@VM_0_14_centos conf]# ll
total 16
-rw-rw-r-- 1 2002 2002 535 Mar 7 2019 configuration.xsl
-rw-rw-r-- 1 2002 2002 2161 Mar 7 2019 log4j.properties
-rw-r--r-- 1 root root 922 Apr 6 16:09 zoo.cfg
-rw-rw-r-- 1 2002 2002 922 Mar 7 2019 zoo_sample.cfg
vim 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.
dataDir=/home/chen/tmp/zk1/data
dataLogDir=/home/chen/tmp/zk1/log
# the port at which the clients will connect
clientPort=2181
server.1 = 内网IP:2887:3887
server.2 = 内网IP:2888:3888
server.3 = 内网IP:2889:3889
拷贝2份,将三份分别为重命名为
drwxr-xr-x 15 root root 4096 Apr 6 16:47 zookeeper-1
drwxr-xr-x 15 root root 4096 Apr 6 16:49 zookeeper-2
drwxr-xr-x 15 root root 4096 Apr 6 16:49 zookeeper-3
将zookeeper-2、zookeeper-3路径下的conf/zoo.cfg中的配置dataDir、dataLogDir改为
dataDir=/home/chen/tmp/zk2/data
dataLogDir=/home/chen/tmp/zk2/log
**********************************************
dataDir=/home/chen/tmp/zk3/data
dataLogDir=/home/chen/tmp/zk3/log
注意要服务器开放这三个端口
2887、
3887、
2888、
3888、
2889、
3889
开放端口后,最好重启服务器
分别开启3个服务
/home/chen/zookeeper-1/bin/zkServer.sh start
输入JPS命令:
[root@VM_0_14_centos conf]# jps
10385 Jps
10014 QuorumPeerMain
10191 QuorumPeerMain
10127 QuorumPeerMain
查看各自状态
/home/chen/zookeeper-1/bin/zkServer.sh status
至此完成集群搭建,
网友评论