zookeeper是一个分布式协调服务,主要为分布式程序提供协调服务。
- zookeeper为分布式程序服务那么自己本身也要高可用,所以zookeeper本身就是分布式程序,只要半数节点存活那么zookeeper就可以提供正常服务,那么推荐部署奇数台机器
- zookeeper基本提供主从协调、动态感知服务节点上下线,统一配置管理,分布式共享锁等。这么多功能总结起来就两点,管理用户程序提交过来的数据,并且监听这些数据变化。
那么先搭建系统,然后再来具体学习提供的这些功能。
一、准备安装环境,3台centos虚拟机,安装好jdk,尽量ip固定地址,关闭防火墙
下载zookeeper https://zookeeper.apache.org/releases.html
安装java https://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html
配置环境变量配置文件/etc/profile最后添加
export JAVA_HOME=路径
export PATH=$PATH:$JAVA_HOME/bin
esc :wq 保存
source /etc/profile 让修改的文件生效
修改网卡配置文件/etc/sysconfig/network-scripts
TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
NAME="ens33"
DEVICE="ens33"
PEERROUTES="yes"
IPADDR=192.168.137.131
NTSMASK=255.255.255.0
GATEWAY=192.168.137.2
DNS1=192.168.137.2
service network restart 保存后重启网卡或者重启reboot
centos7防火墙设置(因为这些机器作为内部程序,便于操作关闭防火墙)
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用 : systemctl disable firewalld
开机启用 : systemctl enable firewalld
二、配置zookeeper集群(便于操作我这直接使用root用户)
解压下载的zookeeper文件 zookeeper-3.4.13.tar.gz
tar zxvf -C /root/apps 解压到apps文件夹下
修改zookeeper的配置文件:zookeeper-3.4.13/conf 复制一份zoo_sample.cfg cp zoo_sample.cfg zoo.cfg
修改配置文件
# The number of milliseconds of each tick 基本毫秒数
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.数据文件夹
dataDir=/root/apps/zookeeper-3.4.13/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=192.168.137.129:2888:3888
server.2=192.168.137.130:2888:3888
server.3=192.168.137.131:2888:3888
server.1=192.168.137.129:2888:3888 (主机名:心跳端口:数据端口)
创建数据文件夹/root/apps/zookeeper-3.4.13/data并且在此文件夹下创建文件名为myid 内容为server.1 中的1 根据配置不同机器修改为2,3
启动zookeeper bin目录下 执行 zkServer.sh start 查看状态 zkService.sh status
[root@mini4 apps]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/apps/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: follower