Installation and operation of zookeeper in centos7 environment

Download address in linux environment  http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.5.2-alpha/zookeeper3.5.2

alpha.tar.gz

First of all, you have to know that zookeeper is a java project. If you want to install and run, you need a jdk environment. Before installation, first configure the java environment. Here I use jdk1.8 (the specific jdk installation will not be discussed here, the hava environment If there is a problem, an error will be reported when starting zookeeper)

Create a folder apps/ in the system directory /usr (install zookeeper in a custom directory for easy management)

1. Unzip the downloaded zookeeper archive to the directory /usr/apps

  tar -zxvf zookeeper-3.5.2-alpha.tar.gz -C /usr/apps/


2. After the decompression is complete, go to the decompressed directory:

/usr/apps/zookeeper-3.5.2-alpha

Go to conf/  in the zookeeper directory


(Because this is the file directory after running, there are not so many files when it is just decompressed),

After entering, pass the command: cp zoo_sample.cfg zoo.cfg (copy a zoo_sample.cfg file and rename it to zoo.cfg)

Open the zoo.cfg file with a text editor:

(Assume that three servers are currently used as nodes to build a zookeeper cluster, and the IP addresses of the three servers are: 192.168.0.210, 192.168.0.211, 192.168.0.212 )

After entering the zoo.cfg file editing environment, add the server nodes of the cluster at the end of the file, and add the following content:

#Configure cluster server
server.1=192.168.0.210:2888:3888
server.2=192.168.0.211:2888:3888
server.3=192.168.0.212:2888:3888


vi editor interface:



3. The next step is to create a zkdata/ folder in the /root directory ( the directory corresponding to dataDir in the configuration file zoo.cfg )

/root/zkdata

Enter the zkdata directory to create the myid file and write the data: ( assuming the server is server.1 )

[root@localhost hadoop]#echo 1 >myid  


This completes the installation and configuration of zookeeper.

Note: The above is the configuration process after configuring the first host. The next two servers only need to copy the configured files (ie: the apps/ folder under /usr) directly to the entire folder and put them on each server. The only difference is that the serial number in the myid file in the /root/zkdata directory is the serial number corresponding to your own server.



4. Run

Enter the zookeeper home directory and execute: bin/zkServer.sh start

The following information appears indicating that the startup is successful:




5. Check the working status of the node:

Execute in the zookeeper home directory: (If the node is to start and change the command, an error will be reported, but if the node is successfully started and the firewalls of the three servers are not closed, the query will fail, and the working characteristic of zookeeper is that more than half of the nodes are required. It will work only if it is alive, and it will also check for failure when it is less than half of the nodes)




6. Test shutdown node:



When the leader node is killed, zookee will re-select a node from the follower nodes as the leader node according to the strategy.

Here the zookeeper is installed!



7.进入zookeeper客户端命令:

[root@localhost zookeeper-3.5.2-alpha]# bin/zkCli.sh


出现这个就表示连接成功了,当前连接的是本机(localhost)的节点,若需要连接其他台服务器的节点如下:

  [zk: localhost:2181(CONNECTED) 42]connect 192.168.0.210:2181

看到一下内容:

WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 192.168.0.210:2181(CONNECTED) 43] 



8.集群自动启动脚本:

shell中变量作用域:

export num=1   export定义的变量会在自己所在的shell进程及其子进程生效

B=1 直接定义的变量只对自己所在的shell进程有效

如在 s1.sh 脚本中定义的变量,在当前登录的shell进程中 通过命令: source s1.sh ,脚本中定义的变量也会在当前登录的进程生效


在一台服务器上使用ssh启动另一台服务器上的zookeeper服务:启动192.168.0.212  (需要输密码)

ssh 192.168.0.212 "source /etc.profile;/usr/apps/zookeeper-3.5.2-alpha/bin/zkServer.sh start"


若需要的一台服务器上同时启动三台服务器的zookeeper,可以在 /root/bin 目录(全局作用域)中创建一个脚本例如: startzk.sh 

 内容:

#! /bin/sh
echo "start zookeeper ....."
for i in 0 1 2
do
ssh 192.168.0.21$i "source /etc.profile;/usr/apps/zookeeper-3.5.2-alpha/bin/zkServer.sh start"
done
echo "zookeeper started"

#启动192.168.0.210、192.168.0.211、192.168.0.212三台服务器上的zookeeper


8.实现SSH免密登录

当然要实现上面方式从一台服务器同时启动另外两太服务器的zookeeper服务,则首先需要实现对另外两条服务器的免密登录:

(实现192.168.0.210免密登录 192.168.0.211和192.168.0.212)

在192.168.0.210主机上执行下面命令:

[root@localhost ~]# ssh-keygen  

[root@localhost ~]# ssh-copy-id 192.168.0.211

[root@localhost ~]# ssh-copy-id 192.168.0.212

第一行命令是生成210主机的秘钥对(一直点回车就行),第二条命令是将公钥复制一份给211主机、第三条命令是将公钥复制一份给212主机





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325587947&siteId=291194637