# Zookeeper单机版安装
### Java环境
Zookeeper安装需要依赖Java,因此确保Java环境已经准备就绪。
```shell
java -version
```
### Zookeeper安装
1. 下载[Zookeeper](http://zookeeper.apache.org/releases.html)压缩包
```http
http://zookeeper.apache.org/releases.html
```
> 以`zookeeper-3.4.14.tar.gz`为例
2. 解压Zookeeper压缩包
```shell
tar -zxvf zookeeper-3.4.14.tar.gz -C /usr/local/
```
3. 创建`data`目录
```shell
cd /usr/local/zookeeper-3.4.14
mkdir data
```
> /usr/local/zookeeper-3.4.14/data
4. 创建配置文件
```shell
cp /usr/local/zookeeper-3.4.14/conf/zoo_sample.cfg /usr/local/zookeeper-3.4.14/conf/zoo.cfg
```
> 默认值:
>
> tickTime=2000
>
> initLimit=10
>
> syncLimit=5
>
> clientPort=2181
>
> dataDir=/tmp/zookeeper
修改`dataDir`的值为第3步创建的目录`data`
```shell
sed -i '/dataDir=/cdataDir=\/usr\/local\/zookeeper-3.4.14\/data' /usr/local/zookeeper-3.4.14/conf/zoo.cfg
```
### Zookeeper启停[服务器]
```
/usr/local/zookeeper-3.4.14/bin/zkServer.sh start
```
> ZooKeeper JMX enabled by default
>
> Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
>
> Starting zookeeper ... STARTED
```shell
/usr/local/zookeeper-3.4.14/bin/zkServer.sh stop
```
> ZooKeeper JMX enabled by default
> Using config: /usr/local/zookeeper-3.4.14/bin/../conf/zoo.cfg
> Stopping zookeeper ... STOPPED
### Zookeeper启动[客户端]
```shell
/usr/local/zookeeper-3.4.14/bin/zkCli.sh
```
> ... ...
> WatchedEvent state:SyncConnected type:None path:null
> [zk: localhost:2181(CONNECTED) 0]
# Zookeeper集群版安装
1. 修改`zoo.cfg`配置文件
```
server.A=B:C:D
```
尾部新增如上配置。A为一个节点编号,如`0`或`1`或`2`。B为节点ip地址。C为Leader选举端口。D为数据同步端口。
```
server.1=192.168.8.101:2888:3888
server.2=192.168.8.102:2888:3888
server.3=192.168.8.103:2888:3888
```
```shell
echo "server.1=192.168.8.101:2888:3888" >>/usr/local/zookeeper-3.4.14/conf/zoo.cfg
echo "server.2=192.168.8.102:2888:3888" >>/usr/local/zookeeper-3.4.14/conf/zoo.cfg
echo "server.3=192.168.8.103:2888:3888" >>/usr/local/zookeeper-3.4.14/conf/zoo.cfg
```
2. 创建`myid`文件
在文件中键入A(节点编号),如`0`或`1`或`2`,保存至参数`dataDir`指定目录。
```shell
echo "1" >/usr/local/zookeeper-3.4.14/data/myid # 在第1台服务器上执行
```
```shell
echo "2" >/usr/local/zookeeper-3.4.14/data/myid # 在第2台服务器上执行
```
```shell
echo "3" >/usr/local/zookeeper-3.4.14/data/myid # 在第3台服务器上执行
```
# Zookeeper状态和端口
### 开放端口
```shell
firewall-cmd --add-port=2888/tcp --permanent
firewall-cmd --add-port=3888/tcp --permanent
firewall-cmd --add-port=2181/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-port
```
### 查看状态
```shell
/usr/local/zookeeper-3.4.14/bin/zkServer.sh start # 启动
/usr/local/zookeeper-3.4.14/bin/zkServer.sh status # 查看状态
/usr/local/zookeeper-3.4.14/bin/zkServer.sh stop # 停止
/usr/local/zookeeper-3.4.14/bin/zkServer.sh restart # 重启
```