ZooKeeper安装使用

本文分享了使用Ansible快速部署ZooKeeper集群的经验,并详细介绍了手动部署过程,包括JDK和ZooKeeper的安装配置,以及如何创建zoo.cfg配置文件。

前言

相信大家对ZooKeeper应该不算陌生,ZooKeeper的使用和ZooKeeper集群的搭建并不复杂,这里分享自己看到的一些好文章以及使用Ansible快速部署和手动部署ZooKeeper的经验分享。

ZooKeeper分布式环境的协调利器

更新历史

2018年09月27日 - 初稿

阅读原文 - https://wsgzao.github.io/post...

扩展阅读

ZooKeeper - https://zookeeper.apache.org/


ZooKeeper简介

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.

ZooKeeper是一个开源的为分布式应用提供分布式协调的服务。它公开了一组简单的原语,分布式应用程序可以基于这些原语实现更高级别的服务,包括同步、维护配置、组和命名。它的设计易于编程,它使用一个遵循文件系统中常见的目录树结构的数据模型。它在Java环境中运行,对Java和C都有绑定。
协调服务是出了名的难。它们特别容易出错,如竞态条件和死锁。ZooKeeper背后的动机是让分布式应用从零开始实现一站式协调服务。

翻译:ZooKeeper OverView
https://www.cnblogs.com/f-ck-...

可能是全网把 ZooKeeper 概念讲的最清楚的一篇文章
https://github.com/Snailclimb...

jdk

install jdk by ansible

https://galaxy.ansible.com/ge...

# download
ansible-galaxy install geerlingguy.java
# create yaml file and config java version
vi install_jdk.yml

---
- hosts: all
  roles:
    - role: geerlingguy.java
      when: "ansible_os_family == 'RedHat'"
      java_packages:
        - java-1.8.0-openjdk

install jdk manually

http://www.oracle.com/technet...

# yum
yum install java-1.8.0-openjdk
# rpm
yum localinstall -y jdk-8u181-linux-x64.rpm
# set env
vi /etc/profile

export JAVA_HOME=/usr/java/jdk1.8.0_181
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib 
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin 

source /etc/profile

zookeeper

install zookeeper by ansible

https://galaxy.ansible.com/an...

# download
ansible-galaxy install AnsibleShipyard.ansible-zookeeper
# create yaml file and config java version
vi install_zookeeper.yml

---
- name: Installing ZooKeeper
  hosts: all
  sudo: yes
  vars:
    zookeeper_version: 3.4.12
  roles:
    - role: AnsibleShipyard.ansible-zookeeper

install zookeeper manually

https://zookeeper.apache.org/...
https://mirrors.tuna.tsinghua...

# install jdk
yum -y install java-1.8.0-openjdk
# download and install zookeeper
tar -zxvf zookeeper-3.4.12.tar.gz —C /opt/
cd /opt/zookeeper-3.4.12
# create zoo.cfg
cd /opt/zookeeper-3.4.12/conf
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg

tickTime=2000
dataDir=/var/lib/zookeeper
dataLogDir=/var/log/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
maxClientCnxns=60

server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888

# create directory
mkdir /var/lib/zookeeper
mkdir /var/log/zookeeper
# create myid file
cd /var/lib/zookeeper/

echo 1 > myid
echo 2 > myid
echo 3 > myid

# start
cd /opt/zookeeper-3.4.12/bin
./zkServer.sh start
# check
./zkServer.sh status
# create znode
./zkCli.sh -server 127.0.0.1:2181
create /codis codis
ls /

Zookeeper Configuration

Platform: RHEL / CentOS 7
Java: Oracle JDK

Variables
zookeeper_version: 3.4.12
zookeeper_group: zookeeper
zookeeper_user: zookeeper
zookeeper_dir: /opt/zookeeper-{{zookeeper_version}} # or /usr/share/zookeeper
zookeeper_conf_dir: {{zookeeper_dir}} # or /etc/zookeeper
zookeeper_tarball_dir: /opt/src
data_dir: /var/lib/zookeeper
log_dir: /var/log/zookeeper
zookeeper_client_port: 2181
zookeeper_id: 1
zookeeper_leader_port: 2888
zookeeper_election_port: 3888
Default Ports
PortDescription
2181Client connection port
2888Quorum port for clustering
3888Leader election port for clustering
conf/zoo.cfg
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
initLimit=5
syncLimit=2
server.1=zoo1:2888:3888
server.2=zoo2:2888:3888
server.3=zoo3:2888:3888

tickTime
the basic time unit in milliseconds used by ZooKeeper. It is used to do heartbeats and the minimum session timeout will be twice the tickTime.

dataDir
the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.

clientPort
the port to listen for client connections

initLimit
is timeouts ZooKeeper uses to limit the length of time the ZooKeeper servers in quorum have to connect to a leader.

syncLimit
limits how far out of date a server can be from a leader.

In this example, the timeout for initLimit is 5 ticks at 2000 milleseconds a tick, or 10 seconds.

参考资料

ZooKeeper系列文章
https://www.cnblogs.com/f-ck-...

ZooKeeper系列(1):安装搭建ZooKeeper环境
https://www.cnblogs.com/f-ck-...

Administrator's Guide - a guide for system administrators and anyone else who might deploy ZooKeeper
https://zookeeper.apache.org/...

Zookeeper是一个分布式协调服务,常用于大型分布式系统中。以下是Zookeeper安装使用步骤: 1. 下载Zookeeper:你可以从Apache官方网站下载Zookeeper安装包。确保选择与你的操作系统版本相对应的安装包。 2. 解压安装包:将下载的安装包解压到你想要安装的目录下。 3. 创建数据目录:在Zookeeper安装目录下创建一个数据目录,用于存储Zookeeper的数据。 4. 配置Zookeeper:复制Zookeeper的默认配置文件,重命名为`zoo.cfg`。打开该文件,根据你的需求进行配置。其中,一些重要的配置项包括: - `dataDir`:Zookeeper数据目录的路径。 - `clientPort`:Zookeeper服务监听的端口,默认为2181。 - `tickTime`:Zookeeper中的基本时间单元,以毫秒为单位。 - `initLimit`和`syncLimit`:这两个参数用于配置Zookeeper中的leader选举过程。 5. 启动Zookeeper:通过命令行进入到Zookeeper安装目录下,执行以下命令来启动Zookeeper: ``` bin/zkServer.sh start ``` 6. 验证Zookeeper是否运行:执行以下命令来验证Zookeeper是否成功启动: ``` bin/zkCli.sh ``` 如果成功连接到Zookeeper的命令行界面,则表示Zookeeper已经成功运行。 现在你已经安装并启动了Zookeeper。你可以使用Zookeeper提供的API来创建、管理和监控分布式应用程序。如果你需要更详细的使用指南,可以参考Zookeeper的官方文档或者其他相关资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值