---
title: "CentOS7 安装 zookeeper 集群"
date: 2019-10-30T01:25:48+08:00
lastmod: 2019-10-30T01:25:48+08:00
keywords: []
tags: ["zookeeper", "centos7"]
categories: ["storage"]
---

## 环境

主机名 | eth0 IP | eth1 IP | 操作系统 | ZK 版本 | myid
---- | ---- | ---- | ---- | ---- | ----
zk221 | 192.168.1.221 | 192.168.16.221 | CentOS7.5 | 3.4.14 | 221
zk222 | 192.168.1.222 | 192.168.16.222 | CentOS7.5 | 3.4.14 | 222
zk223 | 192.168.1.223 | 192.168.16.223 | CentOS7.5 | 3.4.14 | 223

- 下载 [zookeeper-3.4.14](https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz)
- eth0 网卡用于向客户端提供服务，eth1 网卡用于 zookeeper 集群内部通信

## 各节点初始配置

- 关闭 selinux、防火墙
- 部署 java 运行环境
- 创建数据(快照日志)目录
    ```bash
    mkdir -p /var/lib/zookeeper/data
    ```
- 创建事物日志目录
    ```bash
    mkdir -p /var/lib/zookeeper/dataLog
    ```
- 创建服务日志目录
    ```
    mkdir -p /var/log/zookeeper
    ```
- 生成 myid 文件
    ```
    # zk221
    echo 221 > /var/lib/zookeeper/data/myid
    # zk222
    echo 222 > /var/lib/zookeeper/data/myid
    # zk223
    echo 223 > /var/lib/zookeeper/data/myid
    ```

## 部署 zookeeper

- 登陆 zk221，下载 zookeeper，解压至 /opt/ 下
- 生成配置文件
    ```
    cd /opt/zookeeper/conf/
    cp zoo_sample.cfg zoo.cfg
    ```
- 修改 /opt/zookeeper/conf/zoo.cfg
    ```
    dataDir=/var/lib/zookeeper/data
    dataLogDir=/var/lib/zookeeper/dataLog
    # servers
    server.221=192.168.16.221:2888:3888
    server.222=192.168.16.222:2888:3888
    server.223=192.168.16.223:2888:3888
    ```
- 修改 /opt/zookeeper/bin/zkEnv.sh
    ```
    # 找到 ZOO_LOG_DIR="." 一行，换成如下
    ZOO_LOG_DIR="/var/log/zookeeper"
    ```
- 打包 zookeeper 目录，复制到 zk222 和 zk223 上

## 启动集群

- 直接启动脚本
    - 在每个节点上启动 zookeeper 服务
        ```bash
        /opt/zookeeper/bin/zkServer.sh start
        ```
- systemd 启动
    - 创建 /usr/lib/systemd/system/zookeeper.service，内容如下
        ```
        [Unit]
        Description=Zookeeper
        Requires=network.service
        After=network.service
        [Service]
        Environment=JAVA_HOME=/opt/jre
        ExecStart=/opt/zookeeper/bin/zkServer.sh start-foreground
        SuccessExitStatus=143
        [Install]
        WantedBy=multi-user.target
        ```
    - 启动 zookeeper 服务
        ```bash
        systemctl daemon-reload
        systemctl start zookeeper
        ```

## 查看集群状态

- 查看节点状态
    ```bash
    /opt/zookeeper/bin/zkServer.sh status
    ```

