### Zookeeper脚本配置
- 拷贝ips
```bash
cp ips ips_zookeeper
```
修改为
```bash
nn1
nn2
nn3
```
- 拷贝三个脚本
```bash
cp scp_all.sh scp_all_zookeeper.sh 
cp ssh_all.sh ssh_all_zookeeper.sh 
cp ssh_root.sh ssh_root_zookeeper.sh
```
- 修改脚本
```shell
vim scp_all_zookeeper.sh 
vim ssh_all_zookeeper.sh 
vim ssh_root_zookeeper.sh 
```

将三个脚本中的ips改为ips_zookeeper
### Zookeeper安装
 - 上传到`/tmp`目录下，解压
 ```bash
sudo tar -zxvf /tmp/zookeeper-3.4.8.tar.gz -C /usr/local/
scp -r /usr/local/zookeeper-3.4.8/ root@nn2:/usr/local/
scp -r /usr/local/zookeeper-3.4.8/ root@nn3:/usr/local/
ssh_root_zookeeper.sh chown -R hadoop:hadoop /usr/local/zookeeper-3.4.8
ssh_root_zookeeper.s ln -s /usr/local/zookeeper-3.4.8/ /usr/local/zookeeper
```
### Zookeeper配置
- zoo.cfg配置
```bash
cd /usr/local/zookeeper/conf/
cp zoo_sample.cfg zoo.cfg
```
然后`vim zoo.cfg`,修改如下：
```properties
# 修改dataDir
dataDir=/data/zookeeper
# 添加一下内容
server.1=nn1:2888:3888 
server.2=nn2:2888:3888 
server.3=nn3:2888:3888
```
分发给nn2、nn3
`scp_all_zookeeper.sh /usr/local/zookeeper/conf/zoo.cfg /usr/local/zookeeper/conf/`

- `zkEnv.sh`配置
  `vim /usr/local/zookeeper/bin/zkEnv.sh`
![[images/000001.png]]
分发到nn2、nn3
```bash
scp_all_zookeeper.sh /usr/local/zookeeper/bin/zkEnv.sh /usr/local/zookeeper/bin/
```
- 创建zookeeper数据目录
```bash
ssh_root_zookeeper.sh mkdir -p /data/zookeeper
ssh_root_zookeeper.sh chown -R hadoop:hadoop /data
```
- 创建myid文件
```bash
ssh nn1 'echo "1" > /data/zookeeper/myid'
ssh nn2 'echo "2" > /data/zookeeper/myid'
ssh nn3 'echo "3" > /data/zookeeper/myid'
```
- 配置Zookeeper环境变量
  ```bash
  # 在其他所有主机也执行
  sudo chown -R hadoop:hadoop /etc/profile.d/myEnv.sh
```

```bash
echo 'export ZOOKEEPER_HOME=/usr/local/zookeeper' >> /etc/profile.d/myEnv.sh
echo 'export PATH=$PATH:$ZOOKEEPER_HOME/bin' >> /etc/profile.d/myEnv.sh
```

```bash
# 分发到nn2、nn3
scp_all_zookeeper.sh /etc/profile.d/myEnv.sh /etc/profile.d/
# source 环境变量
ssh_all_zookeeper.sh source /etc/profile
```
### Zookeeper的命令
```bash
#启动zk服务
ssh_all_zookeeper.sh /usr/local/zookeeper/bin/zkServer.sh start
#查看每个机器ZK运行的状态
ssh_all_zookeeper.sh /usr/local/zookeeper/bin/zkServer.sh status
#整体停止服务
ssh_all_zookeeper.sh /usr/local/zookeeper/bin/zkServer.sh stop 
#重启zk服务
ssh_all_zookeeper.sh /usr/local/zookeeper/bin/zkServer.sh restart
```

```shell
#启动zookeeper客户端，并连接zookeeper集群
/usr/local/zookeeper/bin/zkCli.sh -server nn1:2181
# 可以简化为:
zkCli.sh
```

