分布式协调服务Zookeeper集群搭建

时间:2023-03-08 17:42:15

              分布式协调服务Zookeeper集群搭建

                                           作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.安装jdk环境

1>.操作环境

[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release
CentOS Linux release 7.6. (Core)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.-.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# uname -m
x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /etc/hosts | grep yinzhengjie            #注意,本片博客只有3台测试机,每台测试机器必须得配置jdk环境!
172.30.1.101 node101.yinzhengjie.org.cn
172.30.1.102 node102.yinzhengjie.org.cn
172.30.1.103 node103.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# free -h
total used free shared buff/cache available
Mem: .7G 331M .9G 9.4M 570M .3G
Swap: .0G 0B .0G
[root@node101.yinzhengjie.org.cn ~]#

2>.打开Oracle官方网站下载相应的平台的安装包(https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

分布式协调服务Zookeeper集群搭建

3>.卸载平台中已经存在的JDK版本

[root@node101.yinzhengjie.org.cn ~]# java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK -Bit Server VM (build 25.201-b09, mixed mode)
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# rpm -qa *openjdk*
java-1.7.-openjdk-headless-1.7.0.191-2.6.15.5.el7.x86_64
java-1.7.-openjdk-1.7.0.191-2.6.15.5.el7.x86_64
java-1.8.-openjdk-headless-1.8.0.201.b09-.el7_6.x86_64
java--openjdk-headless-11.0.ea.-.el7.x86_64
java-1.8.-openjdk-devel-1.8.0.201.b09-.el7_6.x86_64
java--openjdk-11.0.ea.-.el7.x86_64
java-1.8.-openjdk-1.8.0.201.b09-.el7_6.x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# rpm -e --nodeps java-1.7.-openjdk-headless-1.7.0.191-2.6.15.5.el7.x86_64          #卸载JDK版本!
[root@node101.yinzhengjie.org.cn ~]# rpm -e --nodeps java-1.7.-openjdk-1.7.0.191-2.6.15.5.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]# rpm -e --nodeps java-1.8.-openjdk-headless-1.8.0.201.b09-.el7_6.x86_64
[root@node101.yinzhengjie.org.cn ~]# rpm -e --nodeps java--openjdk-headless-11.0.ea.-.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]# rpm -e --nodeps java-1.8.-openjdk-devel-1.8.0.201.b09-.el7_6.x86_64
[root@node101.yinzhengjie.org.cn ~]# rpm -e --nodeps java--openjdk-11.0.ea.-.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]# rpm -e --nodeps java-1.8.-openjdk-1.8.0.201.b09-.el7_6.x86_64
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# rpm -qa *openjdk*
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# java -version
-bash: /usr/bin/java: No such file or directory
[root@node101.yinzhengjie.org.cn ~]#

4>.将下载的jdk版本进行解压安装并配置环境变量(如果你下载的是rpm包的oracle jdk的话就无需配置环境变量啦!)

[root@node101.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/softwares -p
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# tar -zxf jdk-8u201-linux-x64.tar.gz -C /yinzhengjie/softwares/
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/jdk1..0_201/
total
drwxr-xr-x. Dec : bin
-r--r--r--. Dec : COPYRIGHT
-rw-r--r--. root root Apr : hadoop-2.9..tar.gz
drwxr-xr-x. Dec : include
-rw-r--r--. Dec : javafx-src.zip
drwxr-xr-x. Dec : jre
drwxr-xr-x. Dec : lib
-r--r--r--. Dec : LICENSE
drwxr-xr-x. Dec : man
-r--r--r--. Dec : README.html
-rw-r--r--. Dec : release
-rw-r--r--. Dec : src.zip
-rw-r--r--. Dec : THIRDPARTYLICENSEREADME-JAVAFX.txt
-r--r--r--. Dec : THIRDPARTYLICENSEREADME.txt
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# tail - /etc/profile
#Add by yinzhengjie
JAVA_HOME=/yinzhengjie/softwares/jdk1..0_201
PATH=$PATH:$JAVA_HOME/bin
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# source /etc/profile
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) -Bit Server VM (build 25.201-b09, mixed mode)
[root@node101.yinzhengjie.org.cn ~]#

5>.将环境同步到其他节点

[root@node101.yinzhengjie.org.cn ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
Generating public/private rsa key pair.
Created directory '/root/.ssh'.
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:Ytkk9uzXOlYJPLDRaUAyqDzYv1n3CRk6NjN/nuhDbDo root@node101.yinzhengjie.org.cn
The key's randomart image is:
+---[RSA ]----+
| .o.oo . |
| . oo + |
| + . o .* |
|. = . Bo + |
| o +oSo o . |
| ..OoB .o |
| = @.o.o. |
| o E oo*o |
| oo=+. |
+----[SHA256]-----+
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa

[root@node101.yinzhengjie.org.cn ~]# ssh-copy-id root@node102.yinzhengjie.org.cn
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node102.yinzhengjie.org.cn (172.30.1.102)' can't be established.
ECDSA key fingerprint is SHA256:rDA5nSO2LcozxK76hXrtZ1xiXXZTXqQWu+wkBdpMDQA.
ECDSA key fingerprint is MD5:fd::9d:2b::ef:a2:0c:e2:c5:fc:fb:e9:5f:ee:c1.
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: key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node102.yinzhengjie.org.cn's password: Number of key(s) added: Now try logging into the machine, with: "ssh 'root@node102.yinzhengjie.org.cn'"
and check to make sure that only the key(s) you wanted were added. [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ssh-copy-id root@node102.yinzhengjie.org.cn

[root@node101.yinzhengjie.org.cn ~]# ssh-copy-id root@node103.yinzhengjie.org.cn
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host 'node103.yinzhengjie.org.cn (172.30.1.103)' can't be established.
ECDSA key fingerprint is SHA256:5ucWsWQo98ucT5KLfpkXT9DtxurZgMQEMAtynmncvvE.
ECDSA key fingerprint is MD5:6b:fd:ed:8e:::::b0:4c:9f:::::4e.
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: key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node103.yinzhengjie.org.cn's password: Number of key(s) added: Now try logging into the machine, with: "ssh 'root@node103.yinzhengjie.org.cn'"
and check to make sure that only the key(s) you wanted were added. [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ssh-copy-id root@node103.yinzhengjie.org.cn

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/ root@node102.yinzhengjie.org.cn:/
......
org.eclipse.equinox.p2.ql_2.0.100.v20131211-.jar % .5MB/s :
org.eclipse.e4.ui.workbench.addons.swt.nl_zh_4.4.0.v20140623020002.jar % .2MB/s :
com.jrockit.mc.flightrecorder.configuration_5.5.2..jar % .7MB/s :
org.eclipse.equinox.p2.engine.nl_zh_4.4.0.v20140623020002.jar % .6MB/s :
org.eclipse.osgi.services_3.4.0.v20140312-.jar % 88KB .5MB/s :
com.jrockit.mc.greychartplugin_5.5.2..jar % 131KB .1MB/s :
org.eclipse.equinox.p2.touchpoint.natives_1.1.100.v20140523-.jar % 52KB .8MB/s :
org.eclipse.ui.net.nl_zh_4.4.0.v20140623020002.jar % .8MB/s :
org.eclipse.equinox.p2.ui.nl_zh_4.4.0.v20140623020002.jar % .3MB/s :
org.eclipse.core.filesystem_1.4.100.v20140514-.jar % 57KB .0MB/s :
org.eclipse.equinox.p2.director.app.nl_zh_4.4.0.v20140623020002.jar % .7MB/s :
org.eclipse.e4.core.contexts_1.3.100.v20140407-.jar % 48KB .5MB/s :
com.jrockit.mc.rjmx.ui.ja_5.5.2..jar % .1MB/s :
org.eclipse.equinox.simpleconfigurator.manipulator_2.0.0.v20131217-.jar % 25KB .9MB/s :
org.eclipse.equinox.p2.engine.nl_ja_4.4.0.v20140623020002.jar % .0MB/s :
org.eclipse.jface.databinding_1.6.200.v20140528-.jar % 271KB .7MB/s :
org.eclipse.core.resources_3.9.1.v20140825-.jar % 814KB .9MB/s :
com.jrockit.mc.common_5.5.2..jar % 240KB .9MB/s :
artifacts.xml % .2MB/s :
com.oracle.jmc.executable.gtk.linux.x86_64_5.5.2 % 43KB .9MB/s :
com.jrockit.mc.rcp.product_root_5.5.2. % 3816KB .7MB/s :
org.eclipse.rcp_root_4.4.0.v20141007- % 36KB .6MB/s :
org.eclipse.equinox.p2.metadata.repository.prefs % .4MB/s :
org.eclipse.equinox.p2.artifact.repository.prefs % .3MB/s :
.profile.gz % 105KB .7MB/s :
.lock % .0KB/s :
.profile.gz % 105KB .2MB/s :
.profile.gz % .8KB/s :
.profile.gz % .2KB/s :
ct.sym % 17MB .5MB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/ root@node102.yinzhengjie.org.cn:/

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/ root@node103.yinzhengjie.org.cn:/
......
org.eclipse.equinox.p2.ql_2.0.100.v20131211-.jar % .9MB/s :
org.eclipse.e4.ui.workbench.addons.swt.nl_zh_4.4.0.v20140623020002.jar % .2MB/s :
com.jrockit.mc.flightrecorder.configuration_5.5.2..jar % .4MB/s :
org.eclipse.equinox.p2.engine.nl_zh_4.4.0.v20140623020002.jar % .2MB/s :
org.eclipse.osgi.services_3.4.0.v20140312-.jar % 88KB .9MB/s :
com.jrockit.mc.greychartplugin_5.5.2..jar % 131KB .8MB/s :
org.eclipse.equinox.p2.touchpoint.natives_1.1.100.v20140523-.jar % 52KB .2MB/s :
org.eclipse.ui.net.nl_zh_4.4.0.v20140623020002.jar % .9MB/s :
org.eclipse.equinox.p2.ui.nl_zh_4.4.0.v20140623020002.jar % .3MB/s :
org.eclipse.core.filesystem_1.4.100.v20140514-.jar % 57KB .4MB/s :
org.eclipse.equinox.p2.director.app.nl_zh_4.4.0.v20140623020002.jar % .7MB/s :
org.eclipse.e4.core.contexts_1.3.100.v20140407-.jar % 48KB .7MB/s :
com.jrockit.mc.rjmx.ui.ja_5.5.2..jar % .2MB/s :
org.eclipse.equinox.simpleconfigurator.manipulator_2.0.0.v20131217-.jar % 25KB .7MB/s :
org.eclipse.equinox.p2.engine.nl_ja_4.4.0.v20140623020002.jar % .3MB/s :
org.eclipse.jface.databinding_1.6.200.v20140528-.jar % 271KB .4MB/s :
org.eclipse.core.resources_3.9.1.v20140825-.jar % 814KB .5MB/s :
com.jrockit.mc.common_5.5.2..jar % 240KB .6MB/s :
artifacts.xml % .2MB/s :
com.oracle.jmc.executable.gtk.linux.x86_64_5.5.2 % 43KB .6MB/s :
com.jrockit.mc.rcp.product_root_5.5.2. % 3816KB .1MB/s :
org.eclipse.rcp_root_4.4.0.v20141007- % 36KB .7MB/s :
org.eclipse.equinox.p2.metadata.repository.prefs % .9MB/s :
org.eclipse.equinox.p2.artifact.repository.prefs % .2MB/s :
.profile.gz % 105KB .2MB/s :
.lock % .0KB/s :
.profile.gz % 105KB .5MB/s :
.profile.gz % .1MB/s :
.profile.gz % .7KB/s :
ct.sym % 17MB .2MB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/ root@node103.yinzhengjie.org.cn:/

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile root@node102.yinzhengjie.org.cn:/etc/
profile % .6MB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile root@node102.yinzhengjie.org.cn:/etc/

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile root@node103.yinzhengjie.org.cn:/etc/
profile % .5MB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile root@node103.yinzhengjie.org.cn:/etc/

6>.安装ansible软件

[root@node101.yinzhengjie.org.cn ~/Downloads]# yum -y install ansible
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB ::
extras | 3.4 kB ::
updates | 3.4 kB ::
(/): extras//x86_64/primary_db | kB ::
(/): updates//x86_64/primary_db | 3.4 MB ::
Resolving Dependencies
--> Running transaction check
---> Package ansible.noarch :2.4.2.0-.el7 will be installed
--> Processing Dependency: sshpass for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python2-jmespath for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-passlib for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-paramiko for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-jinja2 for package: ansible-2.4.2.0-.el7.noarch
--> Processing Dependency: python-httplib2 for package: ansible-2.4.2.0-.el7.noarch
--> Running transaction check
---> Package python-httplib2.noarch :0.9.-.el7 will be installed
---> Package python-jinja2.noarch :2.7.-.el7 will be installed
--> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.-.el7.noarch
--> Processing Dependency: python-markupsafe for package: python-jinja2-2.7.-.el7.noarch
---> Package python-paramiko.noarch :2.1.-.el7 will be installed
---> Package python-passlib.noarch :1.6.-.el7 will be installed
---> Package python2-jmespath.noarch :0.9.-.el7 will be installed
---> Package sshpass.x86_64 :1.06-.el7 will be installed
--> Running transaction check
---> Package python-babel.noarch :0.9.-.el7 will be installed
---> Package python-markupsafe.x86_64 :0.11-.el7 will be installed
--> Finished Dependency Resolution Dependencies Resolved ==========================================================================================================================================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================================================================================================================================
Installing:
ansible noarch 2.4.2.0-.el7 extras 7.6 M
Installing for dependencies:
python-babel noarch 0.9.-.el7 base 1.4 M
python-httplib2 noarch 0.9.-.el7 extras k
python-jinja2 noarch 2.7.-.el7 base k
python-markupsafe x86_64 0.11-.el7 base k
python-paramiko noarch 2.1.-.el7 updates k
python-passlib noarch 1.6.-.el7 extras k
python2-jmespath noarch 0.9.-.el7 extras k
sshpass x86_64 1.06-.el7 extras k Transaction Summary
==========================================================================================================================================================================================================================================
Install Package (+ Dependent packages) Total download size: M
Installed size: M
Downloading packages:
python-httplib2-0.9.-.el7.no FAILED
http://mirrors.cloud.aliyuncs.com/centos/7/extras/x86_64/Packages/python-httplib2-0.9.2-1.el7.noarch.rpm: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error" ] 0.0 B/s | 0 B --:--:-- ETA
Trying other mirror.
python-jinja2-2.7.-.el7.noar FAILED
http://mirrors.cloud.aliyuncs.com/centos/7/os/x86_64/Packages/python-jinja2-2.7.2-2.el7.noarch.rpm: [Errno 14] curl#6 - "Could not resolve host: mirrors.cloud.aliyuncs.com; Unknown error" ] 0.0 B/s | 0 B --:--:-- ETA
Trying other mirror.
(/): python-markupsafe-0.11-.el7.x86_64.rpm | kB ::
(/): python-babel-0.9.-.el7.noarch.rpm | 1.4 MB ::
(/): python-paramiko-2.1.-.el7.noarch.rpm | kB ::
(/): ansible-2.4.2.0-.el7.noarch.rpm | 7.6 MB ::
(/): python2-jmespath-0.9.-.el7.noarch.rpm | kB ::
(/): sshpass-1.06-.el7.x86_64.rpm | kB ::
(/): python-httplib2-0.9.-.el7.noarch.rpm | kB ::
(/): python-jinja2-2.7.-.el7.noarch.rpm | kB ::
(/): python-passlib-1.6.-.el7.noarch.rpm | kB ::
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.7 MB/s | MB ::
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : python-httplib2-0.9.-.el7.noarch /
Installing : sshpass-1.06-.el7.x86_64 /
Installing : python-babel-0.9.-.el7.noarch /
Installing : python-paramiko-2.1.-.el7.noarch /
Installing : python2-jmespath-0.9.-.el7.noarch /
Installing : python-passlib-1.6.-.el7.noarch /
Installing : python-markupsafe-0.11-.el7.x86_64 /
Installing : python-jinja2-2.7.-.el7.noarch /
Installing : ansible-2.4.2.0-.el7.noarch /
Verifying : python-markupsafe-0.11-.el7.x86_64 /
Verifying : python-jinja2-2.7.-.el7.noarch /
Verifying : python-passlib-1.6.-.el7.noarch /
Verifying : python2-jmespath-0.9.-.el7.noarch /
Verifying : python-paramiko-2.1.-.el7.noarch /
Verifying : python-babel-0.9.-.el7.noarch /
Verifying : ansible-2.4.2.0-.el7.noarch /
Verifying : sshpass-1.06-.el7.x86_64 /
Verifying : python-httplib2-0.9.-.el7.noarch / Installed:
ansible.noarch :2.4.2.0-.el7 Dependency Installed:
python-babel.noarch :0.9.-.el7 python-httplib2.noarch :0.9.-.el7 python-jinja2.noarch :2.7.-.el7 python-markupsafe.x86_64 :0.11-.el7 python-paramiko.noarch :2.1.-.el7 python-passlib.noarch :1.6.-.el7
python2-jmespath.noarch :0.9.-.el7 sshpass.x86_64 :1.06-.el7 Complete!
[root@node101.yinzhengjie.org.cn ~/Downloads]#

[root@node101.yinzhengjie.org.cn ~/Downloads]# yum -y install ansible

[root@node101.yinzhengjie.org.cn ~]# tail - /etc/ansible/hosts                                 #编辑ansible的主机配置文件
[namenode]
node101.yinzhengjie.org.cn [datanode]
node[:].yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# tail -5 /etc/ansible/hosts                                             #编辑ansible的主机配置文件

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'ln -s /yinzhengjie/softwares/jdk1.8.0_201/bin/jps /usr/local/bin/'
[WARNING]: Consider using file module with state=link rather than running ln node103.yinzhengjie.org.cn | SUCCESS | rc= >> node102.yinzhengjie.org.cn | SUCCESS | rc= >> node101.yinzhengjie.org.cn | SUCCESS | rc= >> [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'ln -s /yinzhengjie/softwares/jdk1.8.0_201/bin/jps /usr/local/bin/'      #测试ansible是否可用

二.搭建zookeeper集群

1>.下载zookeeper软件(下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/

[root@node101.yinzhengjie.org.cn ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
---- ::-- https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
Resolving mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)... 101.6.8.193, :f000:::::
Connecting to mirrors.tuna.tsinghua.edu.cn (mirrors.tuna.tsinghua.edu.cn)|101.6.8.193|:... connected.
HTTP request sent, awaiting response... OK
Length: (36M) [application/x-gzip]
Saving to: ‘zookeeper-3.4..tar.gz’ %[============================================================================================================================================>] ,, .57MB/s in .5s -- :: (7.94 MB/s) - ‘zookeeper-3.4..tar.gz’ saved [/] [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

分布式协调服务Zookeeper集群搭建

2>.解压zookeeper并配置环境变量

[root@node101.yinzhengjie.org.cn ~]# tar -zxf zookeeper-3.4..tar.gz -C /yinzhengjie/softwares/
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/zookeeper-3.4./
total
drwxr-xr-x Mar : bin
-rw-rw-r-- Mar : build.xml
drwxr-xr-x Mar : conf
drwxr-xr-x Mar : dist-maven
-rw-rw-r-- Mar : ivysettings.xml
-rw-rw-r-- Mar : ivy.xml
drwxr-xr-x Mar : lib
-rw-rw-r-- Mar : LICENSE.txt
-rw-rw-r-- Mar : NOTICE.txt
-rw-rw-r-- Mar : pom.xml
-rw-rw-r-- Mar : README.md
-rw-rw-r-- Mar : README_packaging.txt
drwxr-xr-x Mar : src
-rw-rw-r-- Mar : zookeeper-3.4..jar
-rw-rw-r-- Mar : zookeeper-3.4..jar.asc
-rw-rw-r-- Mar : zookeeper-3.4..jar.md5
-rw-rw-r-- Mar : zookeeper-3.4..jar.sha1
drwxr-xr-x Mar : zookeeper-client
drwxr-xr-x Mar : zookeeper-contrib
drwxr-xr-x Mar : zookeeper-docs
drwxr-xr-x Mar : zookeeper-it
drwxr-xr-x Mar : zookeeper-jute
drwxr-xr-x Mar : zookeeper-recipes
drwxr-xr-x Mar : zookeeper-server
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# tar -zxf zookeeper-3.4.14.tar.gz -C /yinzhengjie/softwares/

[root@node101.yinzhengjie.org.cn ~]# tail - /etc/profile
#ADD ZOOKEEPER_HOME PATH
ZOOKEEPER_HOME=/yinzhengjie/softwares/zookeeper-3.4.
PATH=$PATH:$ZOOKEEPER_HOME/bin
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# source /etc/profile
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# tail -3 /etc/profile

3>.配置zookeeper的配置文件

[root@node101.yinzhengjie.org.cn ~]# cp /yinzhengjie/softwares/zookeeper-3.4./conf/zoo_sample.cfg /yinzhengjie/softwares/zookeeper-3.4./conf/zoo.cfg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/zookeeper-3.4./conf/
total
-rw-rw-r-- Mar : configuration.xsl
-rw-rw-r-- Mar : log4j.properties
-rw-r--r-- root root Apr : zoo.cfg
-rw-rw-r-- Mar : zoo_sample.cfg
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# cp /yinzhengjie/softwares/zookeeper-3.4.14/conf/zoo_sample.cfg /yinzhengjie/softwares/zookeeper-3.4.14/conf/zoo.cfg

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/zookeeper-3.4./conf/zoo.cfg
# 滴答,计时的基本单位,默认是2000毫秒,即2秒。它是zookeeper最小的时间单位,用于丈量心跳时间和超时时间等,通常设置成默认2秒即可。
tickTime= # 初始化限制是10滴答,默认是10个滴答,即默认是20秒。指定follower节点初始化是链接leader节点的最大tick次数。
initLimit= # 数据同步的时间限制,默认是5个滴答,即默认时间是10秒。设定了follower节点与leader节点进行同步的最大时间。与initLimit类似,它也是以tickTime为单位进行指定的。
syncLimit= # 指定zookeeper的工作目录,这是一个非常重要的参数,zookeeper会在内存中在内存只能中保存系统快照,并定期写入该路径指定的文件夹中。生产环境中需要注意该文件夹的磁盘占用情况。
dataDir=/home/yinzhengjie/zookeeper # 监听zookeeper的默认端口。zookeeper监听客户端链接的端口,一般设置成默认2181即可。
clientPort= # 这个操作将限制连接到 ZooKeeper 的客户端的数量,限制并发连接的数量,它通过 IP 来区分不同的客户端。此配置选项可以用来阻止某些类别的 Dos 攻击。将它设置为 或者忽略而不进行设置将会取消对并发连接的限制。
#maxClientCnxns= # 在上文中已经提到,3.4.0及之后版本,ZK提供了自动清理事务日志和快照文件的功能,这个参数指定了清理频率,单位是小时,需要配置一个1或更大的整数,默认是0,表示不开启自动清理功能。
#autopurge.purgeInterval= # 这个参数和上面的参数搭配使用,这个参数指定了需要保留的文件数目。默认是保留3个。
#autopurge.snapRetainCount= #server.x=[hostname]:nnnnn[:nnnnn],这里的x是一个数字,与myid文件中的id是一致的。右边可以配置两个端口,第一个端口用于F和L之间的数据同步和其它通信,第二个端口用于Leader选举过程中投票通信。
server.=node101.yinzhengjie.org.cn::
server.=node102.yinzhengjie.org.cn::
server.=node103.yinzhengjie.org.cn::
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/zookeeper-3.4.14/conf/zoo.cfg

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/zookeeper-3.4./conf/java.env
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com #指定JDK的安装路径
export JAVA_HOME=/yinzhengjie/softwares/jdk1..0_201 #指定zookeeper的heap内存大小
export JVMFLAGS="-Xms256m -Xmx256m $JVMFLAGS"
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/zookeeper-3.4.14/conf/java.env          #创建配置zookeeper的堆内存配置文件

4>.编写zookeeper的启动脚本

[root@node101.yinzhengjie.org.cn ~]# vi /usr/local/bin/xzk.sh
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# cat /usr/local/bin/xzk.sh
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:y1053419035@qq.com #判断用户是否传参
if [ $# -ne ];then
echo "无效参数,用法为: $0 {start|stop|restart|status}"
exit
fi #获取用户输入的命令
cmd=$ #定义函数功能
function zookeeperManger(){
case $cmd in
start)
echo "启动服务"
remoteExecution start
;;
stop)
echo "停止服务"
remoteExecution stop
;;
restart)
echo "重启服务"
remoteExecution restart
;;
status)
echo "查看状态"
remoteExecution status
;;
*)
echo "无效参数,用法为: $0 {start|stop|restart|status}"
;;
esac
} #定义执行的命令
function remoteExecution(){
for (( i= ; i<= ; i++ )) ; do
tput setaf
echo ========== node${i}.yinzhengjie.org.cn zkServer.sh $ ================
tput setaf
ssh node${i}.yinzhengjie.org.cn "source /etc/profile ; zkServer.sh $1"
done
} #调用函数
zookeeperManger
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# vi /usr/local/bin/xzk.sh

[root@node101.yinzhengjie.org.cn ~]# ll /usr/local/bin/xzk.sh
-rw-r--r-- root root Apr : /usr/local/bin/xzk.sh
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# chmod +x /usr/local/bin/xzk.sh
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ll /usr/local/bin/xzk.sh
-rwxr-xr-x root root Apr : /usr/local/bin/xzk.sh
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# chmod +x /usr/local/bin/xzk.sh

5>.创建myid文件并写入服务器编号

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'mkdir /home/yinzhengjie/zookeeper/'
[WARNING]: Consider using file module with state=directory rather than running mkdir node101.yinzhengjie.org.cn | SUCCESS | rc= >> node103.yinzhengjie.org.cn | SUCCESS | rc= >> node102.yinzhengjie.org.cn | SUCCESS | rc= >> [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'mkdir /home/yinzhengjie/zookeeper/'

[root@node101.yinzhengjie.org.cn ~]# for (( i=;i<=;i++ )) do ssh node${i}.yinzhengjie.org.cn "echo -n $i > /home/yinzhengjie/zookeeper/myid" ;done
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'cat /home/yinzhengjie/zookeeper/myid'
node102.yinzhengjie.org.cn | SUCCESS | rc= >> node103.yinzhengjie.org.cn | SUCCESS | rc= >> node101.yinzhengjie.org.cn | SUCCESS | rc= >> [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# for (( i=101;i<=103;i++ )) do ssh node${i}.yinzhengjie.org.cn "echo -n $i > /home/yinzhengjie/zookeeper/myid" ;done

6>.分发配置文件

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile node102.yinzhengjie.org.cn:/etc/
profile % .2MB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile node102.yinzhengjie.org.cn:/etc/

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile node103.yinzhengjie.org.cn:/etc/
profile % .4MB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp /etc/profile node103.yinzhengjie.org.cn:/etc/

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/softwares/zookeeper-3.4./ node102.yinzhengjie.org.cn:/yinzhengjie/softwares/
........
log. % .3KB/s :
log.63b % 48KB .7MB/s :
snapshot. % .6KB/s :
snapshot. % 55KB .5MB/s :
snapshot. % 55KB .3MB/s :
snapshot. % 140KB .2MB/s :
snapshot.83f % .5MB/s :
minikdc-krb5.conf % .3MB/s :
minikdc.ldiff % .2MB/s :
log. % 2401KB .2MB/s :
log.100001bf0 % 1001KB .6MB/s :
snapshot. % .1KB/s :
snapshot.100001bec % 2507KB .6MB/s :
check_compatibility.py % .7MB/s :
checkstyle-noframes-sorted.xsl % .5MB/s :
checkstyle.xml % .3MB/s :
findbugsExcludeFile.xml % .6MB/s :
log4j.properties % .9MB/s :
test-patch.properties % .7KB/s :
test-github-pr.sh % 24KB .7MB/s :
test-patch.sh % 24KB .4MB/s :
pom.xml % 10KB .6MB/s :
LICENSE.txt % 12KB .9MB/s :
NOTICE.txt % .2MB/s :
README.md % .4MB/s :
README_packaging.txt % .5MB/s :
build.xml % 95KB .5MB/s :
ivy.xml % 10KB .2MB/s :
ivysettings.xml % .3MB/s :
pom.xml % 31KB .9MB/s :
zookeeper-3.4..jar % 1480KB .5MB/s :
zookeeper-3.4..jar.md5 % .6KB/s :
zookeeper-3.4..jar.sha1 % .7KB/s :
zookeeper-3.4..jar.asc % .2MB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/softwares/zookeeper-3.4.14/ node102.yinzhengjie.org.cn:/yinzhengjie/softwares/

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/softwares/zookeeper-3.4./ node103.yinzhengjie.org.cn:/yinzhengjie/softwares/

......
snapshot. % 55KB .8MB/s :
snapshot. % 140KB .7MB/s :
snapshot.83f % .4MB/s :
minikdc-krb5.conf % .0MB/s :
minikdc.ldiff % .4MB/s :
log. % 2401KB .2MB/s :
log.100001bf0 % 1001KB .5MB/s :
snapshot. % .2KB/s :
snapshot.100001bec % 2507KB .4MB/s :
check_compatibility.py % .4MB/s :
checkstyle-noframes-sorted.xsl % .4MB/s :
checkstyle.xml % .0MB/s :
findbugsExcludeFile.xml % .2MB/s :
log4j.properties % .5MB/s :
test-patch.properties % .7KB/s :
test-github-pr.sh % 24KB .3MB/s :
test-patch.sh % 24KB .5MB/s :
pom.xml % 10KB .5MB/s :
LICENSE.txt % 12KB .9MB/s :
NOTICE.txt % .8MB/s :
README.md % .9MB/s :
README_packaging.txt % .6MB/s :
build.xml % 95KB .3MB/s :
ivy.xml % 10KB .1MB/s :
ivysettings.xml % .3MB/s :
pom.xml % 31KB .6MB/s :
zookeeper-3.4..jar % 1480KB .5MB/s :
zookeeper-3.4..jar.md5 % .8KB/s :
zookeeper-3.4..jar.sha1 % .3KB/s :
zookeeper-3.4..jar.asc % .2KB/s :
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# scp -r /yinzhengjie/softwares/zookeeper-3.4.14/ node103.yinzhengjie.org.cn:/yinzhengjie/softwares/

7>.启动zookeeper集群

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'grep SELINUX= /etc/selinux/config |  grep -v ^#'
node102.yinzhengjie.org.cn | SUCCESS | rc= >>
SELINUX=enforcing node103.yinzhengjie.org.cn | SUCCESS | rc= >>
SELINUX=enforcing node101.yinzhengjie.org.cn | SUCCESS | rc= >>
SELINUX=enforcing [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'grep SELINUX= /etc/selinux/config | grep -v ^#'

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/config'
[WARNING]: Consider using template or lineinfile module rather than running sed node103.yinzhengjie.org.cn | SUCCESS | rc= >> node102.yinzhengjie.org.cn | SUCCESS | rc= >> node101.yinzhengjie.org.cn | SUCCESS | rc= >> [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/config'

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'grep SELINUX= /etc/selinux/config |  grep -v ^#'
node102.yinzhengjie.org.cn | SUCCESS | rc= >>
SELINUX=disabled node103.yinzhengjie.org.cn | SUCCESS | rc= >>
SELINUX=disabled node101.yinzhengjie.org.cn | SUCCESS | rc= >>
SELINUX=disabled [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'grep SELINUX= /etc/selinux/config | grep -v ^#'

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'getenforce'
node103.yinzhengjie.org.cn | SUCCESS | rc= >>
Enforcing node102.yinzhengjie.org.cn | SUCCESS | rc= >>
Enforcing node101.yinzhengjie.org.cn | SUCCESS | rc= >>
Enforcing [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'getenforce'

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'setenforce 0'
node103.yinzhengjie.org.cn | SUCCESS | rc= >> node102.yinzhengjie.org.cn | SUCCESS | rc= >> node101.yinzhengjie.org.cn | SUCCESS | rc= >> [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'setenforce 0'

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'getenforce'
node102.yinzhengjie.org.cn | SUCCESS | rc= >>
Permissive node103.yinzhengjie.org.cn | SUCCESS | rc= >>
Permissive node101.yinzhengjie.org.cn | SUCCESS | rc= >>
Permissive [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'getenforce'                            #以上操作是禁用selinux

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl status firewalld'
node103.yinzhengjie.org.cn | SUCCESS | rc= >>
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue -- :: CST; days ago
Docs: man:firewalld()
Main PID: (firewalld)
Tasks:
CGroup: /system.slice/firewalld.service
└─ /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Apr :: node103.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Apr :: node103.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon. node102.yinzhengjie.org.cn | SUCCESS | rc= >>
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue -- :: CST; days ago
Docs: man:firewalld()
Main PID: (firewalld)
Tasks:
CGroup: /system.slice/firewalld.service
└─ /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Apr :: node102.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Apr :: node102.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon. node101.yinzhengjie.org.cn | SUCCESS | rc= >>
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Tue -- :: CST; days ago
Docs: man:firewalld()
Main PID: (firewalld)
Tasks:
CGroup: /system.slice/firewalld.service
└─ /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Apr :: node101.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Apr :: node101.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon. [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl status firewalld'

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl stop firewalld'
node101.yinzhengjie.org.cn | SUCCESS | rc= >> node102.yinzhengjie.org.cn | SUCCESS | rc= >> node103.yinzhengjie.org.cn | SUCCESS | rc= >> [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl stop firewalld'

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl disable firewalld'
node101.yinzhengjie.org.cn | SUCCESS | rc= >>
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. node103.yinzhengjie.org.cn | SUCCESS | rc= >>
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. node102.yinzhengjie.org.cn | SUCCESS | rc= >>
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl disable firewalld'  

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl status firewalld'
node103.yinzhengjie.org.cn | FAILED | rc= >>
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld() Apr :: node103.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Apr :: node103.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon.
Apr :: node103.yinzhengjie.org.cn systemd[]: Stopping firewalld - dynamic firewall daemon...
Apr :: node103.yinzhengjie.org.cn systemd[]: Stopped firewalld - dynamic firewall daemon.non-zero return code node102.yinzhengjie.org.cn | FAILED | rc= >>
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld() Apr :: node102.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Apr :: node102.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon.
Apr :: node102.yinzhengjie.org.cn systemd[]: Stopping firewalld - dynamic firewall daemon...
Apr :: node102.yinzhengjie.org.cn systemd[]: Stopped firewalld - dynamic firewall daemon.non-zero return code node101.yinzhengjie.org.cn | FAILED | rc= >>
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld() Apr :: node101.yinzhengjie.org.cn systemd[]: Starting firewalld - dynamic firewall daemon...
Apr :: node101.yinzhengjie.org.cn systemd[]: Started firewalld - dynamic firewall daemon.
Apr :: node101.yinzhengjie.org.cn systemd[]: Stopping firewalld - dynamic firewall daemon...
Apr :: node101.yinzhengjie.org.cn systemd[]: Stopped firewalld - dynamic firewall daemon.non-zero return code [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'systemctl status firewalld'                    #以上操作是禁用firewalld

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'jps'
node101.yinzhengjie.org.cn | SUCCESS | rc= >>
Jps node103.yinzhengjie.org.cn | SUCCESS | rc= >>
Jps node102.yinzhengjie.org.cn | SUCCESS | rc= >>
Jps [root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'jps'

[root@node101.yinzhengjie.org.cn ~]# xzk.sh start
启动服务
========== node101.yinzhengjie.org.cn zkServer.sh start ================
ZooKeeper JMX enabled by default
Using config: /yinzhengjie/softwares/zookeeper-3.4./bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
========== node102.yinzhengjie.org.cn zkServer.sh start ================
ZooKeeper JMX enabled by default
Using config: /yinzhengjie/softwares/zookeeper-3.4./bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
========== node103.yinzhengjie.org.cn zkServer.sh start ================
ZooKeeper JMX enabled by default
Using config: /yinzhengjie/softwares/zookeeper-3.4./bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# xzk.sh start

[root@node101.yinzhengjie.org.cn ~]# xzk.sh status
查看状态
========== node101.yinzhengjie.org.cn zkServer.sh status ================
ZooKeeper JMX enabled by default
Using config: /yinzhengjie/softwares/zookeeper-3.4./bin/../conf/zoo.cfg
Mode: follower
========== node102.yinzhengjie.org.cn zkServer.sh status ================
ZooKeeper JMX enabled by default
Using config: /yinzhengjie/softwares/zookeeper-3.4./bin/../conf/zoo.cfg
Mode: leader
========== node103.yinzhengjie.org.cn zkServer.sh status ================
ZooKeeper JMX enabled by default
Using config: /yinzhengjie/softwares/zookeeper-3.4./bin/../conf/zoo.cfg
Mode: follower
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# xzk.sh status

[root@node101.yinzhengjie.org.cn ~]#  ansible all -m shell -a 'jps'
node103.yinzhengjie.org.cn | SUCCESS | rc= >>
QuorumPeerMain
Jps node102.yinzhengjie.org.cn | SUCCESS | rc= >>
QuorumPeerMain
Jps node101.yinzhengjie.org.cn | SUCCESS | rc= >>
QuorumPeerMain
Jps [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# ansible all -m shell -a 'jps'

[root@node101.yinzhengjie.org.cn ~]# zkCli.sh
Connecting to localhost:
-- ::, [myid:] - INFO [main:Environment@] - Client environment:zookeeper.version=3.4.-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on // : GMT
-- ::, [myid:] - INFO [main:Environment@] - Client environment:host.name=node101.yinzhengjie.org.cn
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.version=1.8.0_201
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.vendor=Oracle Corporation
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.home=/yinzhengjie/softwares/jdk1..0_201/jre
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.class.path=/yinzhengjie/softwares/zookeeper-3.4./bin/../zookeeper-server/target/classes:/yinzhengjie/softwares/zookeeper-3.4./bin/../build/classes:/yinzhengjie/softwares/zookeeper-3.4./bin/../zookeeper-server/target/lib/*.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../build/lib/*.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/slf4j-log4j12-1.7.25.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/slf4j-api-1.7.25.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/netty-3.10.6.Final.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/log4j-1.2.17.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/jline-0.9.94.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/audience-annotations-0.5.0.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../zookeeper-3.4.14.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../zookeeper-server/src/main/resources/lib/*.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../conf:
2019-04-12 09:23:04,275 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2019-04-12 09:23:04,275 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2019-04-12 09:23:04,275 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2019-04-12 09:23:04,275 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2019-04-12 09:23:04,275 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2019-04-12 09:23:04,275 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-957.el7.x86_64
2019-04-12 09:23:04,276 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2019-04-12 09:23:04,276 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2019-04-12 09:23:04,276 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root
2019-04-12 09:23:04,277 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5ce65a89
Welcome to ZooKeeper!
2019-04-12 09:23:04,303 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1025] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2019-04-12 09:23:04,365 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2019-04-12 09:23:04,395 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x6500007f14d00000, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[quota]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper/quota
[]
[zk: localhost:2181(CONNECTED) 3] quit
Quitting...
2019-04-12 09:23:13,101 [myid:] - INFO [main:ZooKeeper@693] - Session: 0x6500007f14d00000 closed
2019-04-12 09:23:13,102 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@522] - EventThread shut down for session: 0x6500007f14d00000
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# zkCli.sh                                              #测试zookeeper是否可用

三.zookeeper客户端四字符指令

  ZooKeeper 支持某些特定的四字命令字母与其的交互。它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息。用户在客户端可以通过 telnet 或 nc 向 ZooKeeper 提交相应的命令,接下来我们一起体验一下吧。

[root@node101.yinzhengjie.org.cn ~]#  echo conf |  nc node101.yinzhengjie.org.cn             #查看配置信息
clientPort=
dataDir=/home/yinzhengjie/zookeeper/version-
dataLogDir=/home/yinzhengjie/zookeeper/version-
tickTime=
maxClientCnxns=
minSessionTimeout=
maxSessionTimeout=
serverId=
initLimit=
syncLimit=
electionAlg=
electionPort=
quorumPort=
peerType=
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo conf | nc node101.yinzhengjie.org.cn 2181              #查看配置信息,输出相关服务配置的详细信息

[root@node101.yinzhengjie.org.cn ~]# echo ruok |  nc node101.yinzhengjie.org.cn            #注意,他会恢复一个字符串为“imok”,表示其为存活状态
imok[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ruok | nc node102.yinzhengjie.org.cn            #注意,如果恢复的不是"imok",那就说明该节点挂掉啦!注意管擦好输出结果哟!
Ncat: Connection refused.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo ruok | nc node103.yinzhengjie.org.cn
imok[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo ruok | nc node102.yinzhengjie.org.cn 2181               #注意,如果恢复的不是"imok",那就说明该节点挂掉啦!注意观察输出结果哟!

[root@node101.yinzhengjie.org.cn ~]# echo envi | nc node103.yinzhengjie.org.cn               #查看指定zookeeper主机环境变量
Environment:
zookeeper.version=3.4.-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on // : GMT
host.name=node103.yinzhengjie.org.cn
java.version=1.8.0_201
java.vendor=Oracle Corporation
java.home=/yinzhengjie/softwares/jdk1..0_201/jre
java.class.path=/yinzhengjie/softwares/zookeeper-3.4./bin/../zookeeper-server/target/classes:/yinzhengjie/softwares/zookeeper-3.4./bin/../build/classes:/yinzhengjie/softwares/zookeeper-3.4./bin/../zookeeper-server/target/lib/*.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../build/lib/*.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/slf4j-log4j12-1.7.25.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/slf4j-api-1.7.25.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/netty-3.10.6.Final.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/log4j-1.2.17.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/jline-0.9.94.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../lib/audience-annotations-0.5.0.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../zookeeper-3.4.14.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../zookeeper-server/src/main/resources/lib/*.jar:/yinzhengjie/softwares/zookeeper-3.4.14/bin/../conf:
java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.io.tmpdir=/tmp
java.compiler=<NA>
os.name=Linux
os.arch=amd64
os.version=3.10.0-957.el7.x86_64
user.name=root
user.home=/root
user.dir=/root
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo envi | nc node103.yinzhengjie.org.cn 2181              #查看指定zookeeper主机环境变量,输出关于服务环境的详细信息

[root@node101.yinzhengjie.org.cn ~]# echo cons | nc node103.yinzhengjie.org.cn
/172.30.1.102:[](queued=,recved=,sent=,sid=0x67000b2416210000,lop=PING,est=,to=,lcxid=0x1,lzxid=0xffffffffffffffff,lresp=,llat=,minlat=,avglat=,maxlat=)
/172.30.1.101:[](queued=,recved=,sent=)
/127.0.0.1:[](queued=,recved=,sent=,sid=0x67000b2416210001,lop=GETC,est=,to=,lcxid=0x4,lzxid=0x400000005,lresp=,llat=,minlat=,avglat=,maxlat=) [root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo cons | nc node103.yinzhengjie.org.cn 2181              #查看终端链接信息

[root@node101.yinzhengjie.org.cn ~]# echo dump | nc node103.yinzhengjie.org.cn                  #查看未处理会话的节点
SessionTracker dump:
org.apache.zookeeper.server.quorum.LearnerSessionTracker@515b070f
ephemeral nodes dump:
Sessions with Ephemerals ():
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo dump | nc node103.yinzhengjie.org.cn 2181               #查看未处理会话的节点

[root@node101.yinzhengjie.org.cn ~]# echo stat | nc node103.yinzhengjie.org.cn                  #查看统计信息
Zookeeper version: 3.4.-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on // : GMT
Clients:
/172.30.1.102:[](queued=,recved=,sent=)
/127.0.0.1:[](queued=,recved=,sent=)
/172.30.1.101:[](queued=,recved=,sent=) Latency min/avg/max: //
Received:
Sent:
Connections:
Outstanding:
Zxid: 0x400000005
Mode: follower
Node count:
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo stat | nc node103.yinzhengjie.org.cn 2181               #查看统计信息,一般用来查看哪个节点被选择作为follower或者leader

[root@node101.yinzhengjie.org.cn ~]# echo wchs | nc node103.yinzhengjie.org.cn                  #查看链接的watch信息
connections watching paths
Total watches:
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo wchs | nc node103.yinzhengjie.org.cn 2181                #查看服务器的watch的详细信息

[root@node101.yinzhengjie.org.cn ~]# echo wchc | nc node103.yinzhengjie.org.cn                 #通过 session 列出服务器 watch 的详细信息,它的输出是一个与 watch 相关的会话的列表。
wchc is not executed because it is not in the whitelist.
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo wchc | nc node103.yinzhengjie.org.cn 2181               #通过 session 列出服务器 watch 的详细信息,它的输出是一个与 watch 相关的会话的列表。(需要进一步调研)

[root@node101.yinzhengjie.org.cn ~]# echo wchp | nc node103.yinzhengjie.org.cn                 #通过路径列出服务器 watch 的详细信息。它输出一个与 session 相关的路径。
wchp is not executed because it is not in the whitelist.
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo wchp | nc node103.yinzhengjie.org.cn 2181               #通过路径列出服务器 watch 的详细信息。它输出一个与 session 相关的路径。(需要进一步调研)

[root@node101.yinzhengjie.org.cn ~]# echo reqs | nc node103.yinzhengjie.org.cn                  #查看未经处理的请求。
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo reqs | nc node103.yinzhengjie.org.cn 2181               #查看未经处理的请求。

[root@node101.yinzhengjie.org.cn ~]# echo crst | nc node101.yinzhengjie.org.cn                    #重置所有的客户端连接
Connection stats reset.
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo crst | nc node101.yinzhengjie.org.cn 2181               #重置所有的客户端连接

[root@node101.yinzhengjie.org.cn ~]# echo dump | nc node101.yinzhengjie.org.cn                  #打印集群的所有会话信息,包括ID,以及临时节点等信息。用在Leader节点上才有效果。
SessionTracker dump:
Session Sets ():
expire at Sat Jan :: CST :
expire at Sat Jan :: CST :
expire at Sat Jan :: CST :
expire at Sat Jan :: CST :
expire at Sat Jan :: CST :
expire at Sat Jan :: CST :
0x67000b2416210001
expire at Sat Jan :: CST :
0x65000b248f060000
0x67000b2416210000
ephemeral nodes dump:
Sessions with Ephemerals ():
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo dump | nc node101.yinzhengjie.org.cn 2181                #打印集群的所有会话信息,包括ID,以及临时节点等信息。用在Leader节点上才有效果。

[root@node101.yinzhengjie.org.cn ~]# echo srst | nc node101.yinzhengjie.org.cn                   #重置服务器统计信息
Server stats reset.
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo srst | nc node101.yinzhengjie.org.cn 2181               #重置服务器统计信息

[root@node101.yinzhengjie.org.cn ~]# echo srvr | nc node101.yinzhengjie.org.cn                 #和stat输出信息一样,只不过少了客户端连接信息。
Zookeeper version: 3.4.-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on // : GMT
Latency min/avg/max: //
Received:
Sent:
Connections:
Outstanding:
Zxid: 0x400000006
Mode: leader
Node count:
Proposal sizes last/min/max: -/-/-
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo srvr | nc node101.yinzhengjie.org.cn 2181               #和stat输出信息一样,只不过少了客户端连接信息。

[root@node101.yinzhengjie.org.cn ~]# echo mntr | nc node101.yinzhengjie.org.cn                 #输出比stat更为详细的服务器统计信息
zk_version 3.4.-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on // : GMT
zk_avg_latency
zk_max_latency
zk_min_latency
zk_packets_received
zk_packets_sent
zk_num_alive_connections
zk_outstanding_requests
zk_server_state leader
zk_znode_count
zk_watch_count
zk_ephemerals_count
zk_approximate_data_size
zk_open_file_descriptor_count
zk_max_file_descriptor_count
zk_fsync_threshold_exceed_count
zk_followers
zk_synced_followers
zk_pending_syncs
zk_last_proposal_size -
zk_max_proposal_size -
zk_min_proposal_size -
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]#

[root@node101.yinzhengjie.org.cn ~]# echo mntr | nc node101.yinzhengjie.org.cn 2181               #输出比stat更为详细的服务器统计信息

参考链接1:https://www.cnblogs.com/yinzhengjie/p/10739218.html

参加链接2:https://www.cnblogs.com/yinzhengjie/articles/10698719.html

参考链接3:https://www.jianshu.com/p/4f11d7bfc9ce

参考链接4:https://blog.****.net/u013673976/article/details/47321067

参考链接5:https://blog.****.net/paincupid/article/details/78058087#Zookeeper_1782