zookeeper 安装和集群

zookeeper开发指南

1、zookeeper单机安装

下载链接 :http://apache.fayea.com/zookeeper/

稳定版本地址:http://apache.fayea.com/zookeeper/zookeeper-3.5.4-beta/zookeeper-3.5.4-beta.tar.gz

#1、下载 zookeeper 安装包
[root@liu-node1 ~]# wget http://apache.fayea.com/zookeeper/zookeeper-3.5.4-beta/zookeeper-3.5.4-beta.tar.gz

#2、解压zookeeper
[root@liu-node1 ~]# tar xvf zookeeper-3.5.4-beta.tar.gz

#3、创建数据目录和日志目录,创建data、logs
[root@liu-node2 ~]# mv zookeeper-3.5.4-beta zookeeper-3.5.4
[root@liu-node2 ~]# cd zookeeper-3.5.4/

[root@liu-node1 zookeeper-3.5.4]# mkdir data
[root@liu-node1 zookeeper-3.5.4]# mkdir logs

[root@liu-node1 zookeeper-3.5.4]# ll
总用量 1976
drwxr-xr-x. 2 root root     232 5月  12 00:37 bin
-rw-rw-r--. 1 root root   85867 5月  12 00:28 build.xml
drwxr-xr-x. 2 root root      77 5月  12 00:37 conf
drwxr-xr-x. 9 root root     116 5月  12 00:28 contrib
drwxr-xr-x. 2 root root       6 9月  10 14:18 data
drwxr-xr-x. 2 root root    4096 5月  12 00:45 dist-maven
drwxr-xr-x. 6 root root    4096 5月  12 00:37 docs
-rw-rw-r--. 1 root root    1709 5月  12 00:28 ivysettings.xml
-rw-rw-r--. 1 root root    6934 5月  12 00:28 ivy.xml
drwxr-xr-x. 4 root root    4096 5月  12 00:37 lib
-rw-rw-r--. 1 root root   12085 5月  12 00:28 LICENSE.txt
drwxr-xr-x. 2 root root       6 9月  10 14:18 logs
-rw-rw-r--. 1 root root    3133 5月  12 00:28 NOTICE.txt
-rw-rw-r--. 1 root root    1557 5月  12 00:28 README.md
-rw-rw-r--. 1 root root    1347 5月  12 00:28 README_packaging.txt
drwxr-xr-x. 5 root root      47 5月  12 00:28 recipes
drwxr-xr-x. 7 root root     195 5月  12 00:37 src
-rw-rw-r--. 1 root root 1875068 5月  12 00:29 zookeeper-3.5.4-beta.jar
-rw-r--r--. 1 root root     195 5月  12 00:38 zookeeper-3.5.4-beta.jar.asc
-rw-rw-r--. 1 root root      33 5月  12 00:29 zookeeper-3.5.4-beta.jar.md5
-rw-rw-r--. 1 root root      41 5月  12 00:29 zookeeper-3.5.4-beta.jar.sha1

#4 、配置文件 conf/zoo.cfg

[root@liu-node1 zookeeper-3.5.4]# cp conf/zoo_sample.cfg conf/zoo.cfg

dataDir=/root/zookeeper-3.5.4/data
dataLogDir=/root/zookeeper-3.5.4/logs
# the port at which the clients will connect
clientPort=2181
#2888 端口是zookeeper 服务之间通信的端口
#3888 端口是zookeeper 与其他应用程序通信的端口 
#单机服务需要添加选项 
server.1=liu-node1.com:2888:3888
#server.1=192.168.58.11:2888:3888


#5、创建配置的server的id,zookeeper/data/myid
#[root@liu-node2 zookeeper3.5.4]# echo "1" > data/myid

[root@liu-node1 conf]# cd ../data/
[root@liu-node1 data]# pwd
/root/zookeeper-3.5.4/data
[root@liu-node1 data]# vim myid

#6、配置.bash_profile,增加zookeeper配置:
#编辑zookeeper 环境变量
[root@liu-node1 ~]# vim .bash_profile 

#zookeeper env
export ZOOKEEPER_HOME=/root/zookeeper-3.5.4
export PATH=$ZOOKEEPER_HOME/bin:$PATH

# 使得配置立即生效
[root@liu-node1 ~]# source .bash_profile 

#7 打开防火墙端口 2181 2888 3888

[root@liu-node1 ~]# firewall-cmd --zone=public --add-port=2181/tcp --permanent
success
[root@liu-node1 ~]# firewall-cmd --zone=public --add-port=2888/tcp --permanent
success
[root@liu-node1 ~]# firewall-cmd --zone=public --add-port=3888/tcp --permanent
success
# 端口打开后重启防火墙
[root@liu-node1 ~]# systemctl stop firewalld
[root@liu-node1 ~]# systemctl start firewalld
# 启动zookeeper

[root@liu-node1 zookeeper-3.5.4]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.5.4/bin/../conf/zoo.cfg
Starting zookeeper ... ^[[ASTARTED
[root@liu-node1 zookeeper-3.5.4]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.5.4/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.


#通过以上的安装步骤最后发现,zookeeper启动不了,经过度娘的指导 需要再zoo.cfg配置中添加
admin.serverPort=8082
#服务器启动,验证服务启动
[root@liu-node1 zookeeper-3.5.4]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.5.4/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@liu-node1 zookeeper-3.5.4]# vim conf/zoo.cfg 
[root@liu-node1 zookeeper-3.5.4]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/zookeeper-3.5.4/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone
[root@liu-node1 zookeeper-3.5.4]# jps
1720 QuorumPeerMain
1788 Jps
1422 Bootstrap

#配置开机启动
#创建服务
[root@liu-node1 ~]# cd /etc/rc.d/init.d/
[root@liu-node1 init.d]# ll
总用量 48
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--. 1 root root  1160 6月  27 2017 README
-rwxr-xr-x. 1 root root  2443 3月   6 2017 rhnsd
-rwxr-xr-x. 1 root root   711 9月  11 12:56 zookeeper
[root@liu-node1 init.d]# vim zookeeper
#编写服务脚本

#!/bin/bash
#chkconfig: 2345 20 90  
#description: zookeeper  
#processname: zookeeper  

export JAVA_HOME=/root/java
export PATH=$JAVA_HOME/bin:$PATH
ZOOKEEPER_HOME=/root/zookeeper-3.5.4

case $1 in
          start)
          echo "start zookeeper service..." 
          sh ${ZOOKEEPER_HOME}/bin/zkServer.sh start;;
          stop)
          echo "stop zookeeper service..." 
          sh ${ZOOKEEPER_HOME}/bin/zkServer.sh stop;;
          status)
          sh ${ZOOKEEPER_HOME}/bin/zkServer.sh status;;
          restart)
          echo "restart zookeeper service..." 
          sh ${ZOOKEEPER_HOME}/bin/zkServer.sh restart;;
          *)  echo "require start|stop|status|restart"  ;;
esac
~   
#设置开机启动服务

[root@liu-node1 init.d]# systemctl enable zookeeper
zookeeper.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig zookeeper on
#检查开机启动
[root@liu-node1 init.d]# systemctl status zookeeper
● zookeeper.service - SYSV: zookeeper
   Loaded: loaded (/etc/rc.d/init.d/zookeeper; bad; vendor preset: disabled)
   Active: active (running) since 二 2018-09-11 12:57:41 CST; 3min 59s ago
     Docs: man:systemd-sysv-generator(8)
   CGroup: /system.slice/zookeeper.service
           └─1435 /root/java/bin/java -Dzookeeper.log.dir=/root/zookeeper-3.5.4/bin/../logs -Dzookeeper.log.file=zookeeper--server-liu...

9月 11 12:57:39 liu-node1.com systemd[1]: Starting SYSV: zookeeper...
9月 11 12:57:39 liu-node1.com zookeeper[1390]: start zookeeper service...
9月 11 12:57:39 liu-node1.com zookeeper[1390]: ZooKeeper JMX enabled by default
9月 11 12:57:39 liu-node1.com zookeeper[1390]: Using config: /root/zookeeper-3.5.4/bin/../conf/zoo.cfg
9月 11 12:57:41 liu-node1.com zookeeper[1390]: Starting zookeeper ... STARTED
9月 11 12:57:41 liu-node1.com systemd[1]: Started SYSV: zookeeper.


2、zookeeper集群安装

2.0 规划

序号 IP 域名
1 192.168.58.141 zook1
2 192.168.58.142 zook2
3 192.168.58.143 zook3

设计:为了防止服务器不够用做迁移的时候可以,在一台机器上搭建伪服务

服务 域名 客户端口 通信端口 本地端口 备注
server.1 zook1 2181 2881 3881
server.2 zook2 2182 2882 3882
server.3 zook3 2183 2883 3883

2.1 服务器

2.1.1、首先克隆3台机器,修改网卡配置
#修改hostname 
[root@zook1 ~]#vim /ect/hostname

reboot

#修改网卡
[root@zook1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="yes"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"

DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.85.141"
PREFIX="24"
GATEWAY="192.168.85.2"
DNS1="192.168.85.2"
IPV6_PRIVACY="no"
#将uuid 删除,修改IP地址

#修改完后记得重启网卡
[root@zook1 ~]#service network restart




2.1.2、关闭selinux

[root@zook1 ~]#vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

#修改完重启 或者重新载入配置


2.1.3 修改hosts 文件

[root@zook1 zookeeper1]# vim /etc/hosts

#zookeeper
192.168.85.141 zook1
192.168.85.142 zook2
192.168.85.143 zook3

2.2 安装jdk环境

为了使用方便,我们安装的jdk为1.8 版本,且配置的为全局环境变量,

#解压
[root@zook1 local]# tar xvf jdk-8u181-linux-x64.tar.gz -C /usr/local/
#重命名
[root@zook1 ~]# mv /usr/local/jdk1.8.0_181/ /usr/local/jdk8


[root@zook1 ~]# vim /etc/profile

export JAVA_HOME=/usr/local/jdk8
export CLASSPATH=.:$JAVA_HOME/bin/dt.jar:$JAVA_HOME/bin/dt.jar
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$PATH
#使得配置生效
[root@zook1 ~]# source /etc/profile

[root@zook1 ~]# 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)


按照上面的操作,配置另外两台服务器,这样jdk 环境就安装完成了

2.3 安装zookeeper

2.3.1 安装和解压
#解压和安装
[root@zook1 ~]# tar xvf zookeeper-3.4.6.tar.gz -C /usr/local/

[root@zook1 ~]# mv /usr/local/zookeeper-3.4.6/ /usr/local/zookeeper1

[root@zook2 ~]# mv /usr/local/zookeeper-3.4.6/ /usr/local/zookeeper2

[root@zook3 ~]# mv /usr/local/zookeeper-3.4.6/ /usr/local/zookeeper3




2.3.2 配置集群zoo.cfg

#配置
[root@zook1 ~]# cd /usr/local/zookeeper1/conf/
[root@zook1 conf]# cp zoo_sample.cfg  zoo.cfg 
[root@zook1 conf]# vim zoo.cfg 

[root@zook1 zookeeper1]# mkdir data
[root@zook1 zookeeper1]# mkdir logs

1号:
tickTime=2000vim
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper1/data
dataLogDir=/usr/local/zookeeper1/logs
clientPort=2181
server.1=zook1:2881:3881
server.2=zook2:2882:3882
server.3=zook3:2883:3883

2号:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper2/data
dataLogDir=/usr/local/zookeeper2/logs
clientPort=2182
server.1=zook1:2881:3881
server.2=zook2:2882:3882
server.3=zook3:2883:3883

3号:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/usr/local/zookeeper3/data
dataLogDir=/usr/local/zookeeper3/logs
clientPort=2183
server.1=zook1:2881:3881
server.2=zook2:2882:3882
server.3=zook3:2883:3883



2.3.3 配置环境变量

配置的为用户级别的环境变量

[root@zook1 ~]# vim .bash_profile 

export ZOOKEEPER_HOME=/usr/local/zookeeper1
export PATH=$ZOOKEEPER_HOME/bin:$PATH

[root@zook1 ~]# source  .bash_profile
2.3.4 添加防火墙端口
#1号

firewall-cmd --add-port=2181/tcp --zone=public --permanent
firewall-cmd --add-port=2881/tcp --zone=public --permanent
firewall-cmd --add-port=3881/tcp --zone=public --permanent

#2号

firewall-cmd --add-port=2182/tcp --zone=public --permanent
firewall-cmd --add-port=2882/tcp --zone=public --permanent
firewall-cmd --add-port=3882/tcp --zone=public --permanent


#3号

firewall-cmd --add-port=2183/tcp --zone=public --permanent
firewall-cmd --add-port=2883/tcp --zone=public --permanent
firewall-cmd --add-port=3883/tcp --zone=public --permanent

#添加端口后记得重启防火墙
[root@zook1 ~]# systemctl restart firewalld
[root@zook2 ~]# systemctl restart firewalld
[root@zook3 ~]# systemctl restart firewalld

2.3.5 启动服务

zkServer.sh restart
zkServer.sh start
zkServer.sh stop
zkServer.sh status



echo "1" > data/myid







2.3.6 免秘钥登录

使用192.168.85.141 登录 142,143 两台机器操作zookeeper


#创建秘钥,一路回车就好
[root@zook1 ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:oleQKEOBOZbGS4Dk2U7NOASPWxJsm4Ryg96iCzOExxA root@zook1
The key's randomart image is:
+---[RSA 2048]----+
|EO=o             |
|@@X +. .         |
|XXBO.oo          |
|.BO+.  .         |
|oo..  . S        |
|=    . o         |
|.+  . .          |
|.    .           |
|                 |
+----[SHA256]-----+
[root@zook1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.85.141
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.85.141 (192.168.85.141)' can't be established.
ECDSA key fingerprint is SHA256:wXWVOaRhs/WQFVRhpl7xeEP6yS08vp7VEBqt7HlmI5k.
ECDSA key fingerprint is MD5:74:06:1e:a5:76:33:ec:5f:15:bc:91:42:e0:9b:98:72.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.85.141's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.85.141'"
and check to make sure that only the key(s) you wanted were added.

[root@zook1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.85.142
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.85.142 (192.168.85.142)' can't be established.
ECDSA key fingerprint is SHA256:wXWVOaRhs/WQFVRhpl7xeEP6yS08vp7VEBqt7HlmI5k.
ECDSA key fingerprint is MD5:74:06:1e:a5:76:33:ec:5f:15:bc:91:42:e0:9b:98:72.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.85.142's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.85.142'"
and check to make sure that only the key(s) you wanted were added.

[root@zook1 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.85.143
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.85.143 (192.168.85.143)' can't be established.
ECDSA key fingerprint is SHA256:wXWVOaRhs/WQFVRhpl7xeEP6yS08vp7VEBqt7HlmI5k.
ECDSA key fingerprint is MD5:74:06:1e:a5:76:33:ec:5f:15:bc:91:42:e0:9b:98:72.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.85.143's password: 
Permission denied, please try again.
root@192.168.85.143's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.85.143'"
and check to make sure that only the key(s) you wanted were added.



2.3.7 创建集群脚本

使用这个脚本 小伙伴们就能快速 启动 zookeeper集群了




#!/bin/sh
echo "start zookeeper server....."

usage="Usage: $0 (start|stop|status)"
if [ $# -lt 1 ];then
   echo $usage
   exit 1
fi
behave=$1
echo "$behave zkServer cluster"

for host in 1 2 3
do 
ssh zook$host "source /etc/profile;/usr/local/zookeeper$host/bin/zkServer.sh $behave"

done
exit 0

2.3.8 测试

通过测试可以看到,现在2好机器为主节点。其他两台机器为从节点


[root@zook1 ~]# ./startzk.sh status
start zookeeper server.....
status zkServer cluster
JMX enabled by default
Using config: /usr/local/zookeeper1/bin/../conf/zoo.cfg
Mode: follower
JMX enabled by default
Using config: /usr/local/zookeeper2/bin/../conf/zoo.cfg
Mode: leader
JMX enabled by default
Using config: /usr/local/zookeeper3/bin/../conf/zoo.cfg
Mode: follower
[root@zook1 ~]# 



当我们关闭主主节点后,集群会进行重新选举出 主节点,继续提供服务

[root@zook2 ~]#  /usr/local/zookeeper2/bin/zkServer.sh stop
JMX enabled by default
Using config: /usr/local/zookeeper2/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zook2 ~]# 

#通过关闭主节点,从节点进行重新选举,推选出新的主节点,3号机器变成了主节点
[root@zook1 ~]# ./startzk.sh status
start zookeeper server.....
status zkServer cluster
JMX enabled by default
Using config: /usr/local/zookeeper1/bin/../conf/zoo.cfg
Mode: follower
JMX enabled by default
Using config: /usr/local/zookeeper2/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
JMX enabled by default
Using config: /usr/local/zookeeper3/bin/../conf/zoo.cfg
Mode: leader

#通过重启2号节点
[root@zook2 ~]#  /usr/local/zookeeper2/bin/zkServer.sh start
JMX enabled by default
Using config: /usr/local/zookeeper2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zook2 ~]# 
#2号节点启动后,2号节点将做为从节点,重新加入集群
[root@zook1 ~]# ./startzk.sh status
start zookeeper server.....
status zkServer cluster
JMX enabled by default
Using config: /usr/local/zookeeper1/bin/../conf/zoo.cfg
Mode: follower
JMX enabled by default
Using config: /usr/local/zookeeper2/bin/../conf/zoo.cfg
Mode: follower
JMX enabled by default
Using config: /usr/local/zookeeper3/bin/../conf/zoo.cfg
Mode: leader
[root@zook1 ~]# 


上一篇:des加密解密JAVA与.NET互通实例


下一篇:验证码识别<1>