阿里云ecs搭建zookeeper集群

准备两台ecs机器,为啥只有两台?因为买不起第三台。。。。好了,回归正题。

准备:

  • 两台ecs
  • jdk8及以上
  • zookeeper, 我这里用的3.4.14

注意事项:

1、zookeeper 集群至少需要三台节点才能搭建集群。

2、ecs中安全组记得开放端口,建议为了便捷直接将ecs的全部端口开放。出,入方向都要设置。

步骤:

  1. 目前我是两台ecs,一台上有两个zookeeper节点 ,另一台一个zookeeper节点。
  2. 配置hosts文件, 假设两台ip分别是xxx.xx.xxx.x , xxx.x.xxx.xx, 则在每台ecs配置hosts文件,加上  
    xxx.xx.xxx.x   node01
    xxx.x.xxx.xx   node02

    然后刷新网卡, systemctl network restart

  3. 在每个zookeeper节点外部创建两个文件夹,并分配权限。

    # 一台
    mkdir data
    mkdir logs
    mkdir cluster-data
    mkdir cluster-logs
    chmod 777 -R data
    chmod 777 -R log
    chmod 777 -R cluster-data
    chmod 777 -R cluster-logs
    
    
    # 另一台
    mkdir data
    mkdir logs
    chmod 777 -R data
    chmod 777 -R logs

  4. 配置节点中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=/home/zookeeper/data
    dataLogDir=/home/zookeeper/logs
    # 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
    
    quorumListenOnAllIPs=true
    
    server.1=node01:2888:3888
    server.2=node02:2888:3888
    server.3=node02:2999:3999
    
    
    # 第二个
    # 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=/home/zookeeper/cluster-data
    dataLogDir=/home/zookeeper/cluster-logs
    # the port at which the clients will connect
    clientPort=2182
    # 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
    
    quorumListenOnAllIPs=true
    
    server.1=node01:2888:3888
    server.2=node02:2888:3888
    server.3=node02:2999:3999
    
    # 第三个
    # 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=/home/zookeeper/data
    dataLogDir=/home/zookeeper/logs
    # 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
    
    quorumListenOnAllIPs=true
    
    server.1=node01:2888:3888
    server.2=node02:2888:3888
    server.3=node02:2999:3999
    
    

  5. 设置zookeeper 的myid, 必须设置, 通过指定该id达到zookeeper快速选举leader的特性(200ms内);且必须设置在dataDir目录下。

    # 三个节点都设置, 你们懂的, 一个ecs两个节点的执行两次, 另一个执行一次,别粗心了
    echo "1" > /home/zookeeper/data/myid
    echo "2" > /home/zookeeper/cluster-data/myid
    echo "3" > /home/zookeeper/data/myid
  6.   按需启动, 按照myid的大小,从小到大启动。
  7. 任何一个节点查看zk的状态,出现这个就是ok的

记住云服务器ecs多了一个配置项,监听所有网卡,我就是这里卡住了。 

quorumListenOnAllIPs=true

补充==========

有个别版本(比如自己装的linux 虚拟机),必须开启防火墙的 ,即需要设置开启防火墙, 并打开对应端口(2888,3888) 

# 防火墙状态
systemctl status firewalld

# 开启防火墙
systemctl start firewalld

# 关闭防火墙
systemctl stop firewalld


# 查看已经开启的端口
firewall-cmd --list-port

# 开放指定的端口
firewall-cmd --zone=public --add-port=这里是需要开启的端口号/tcp --permanent
比如这里是 2888 3888
firewall-cmd --zone=public --add-port=2888/tcp --permanent
firewall-cmd --zone=public --add-port=3888/tcp --permanent

# 刷新设定
firewall-cmd --reload

你可能感兴趣的:(分布式,阿里云,zookeeper,云计算)