Construction of zookeeper cluster environment

1, zookeeper Brief

effect

zookeeper is an open source Apache project that provides coordination services for distributed frameworks

Working mechanism

Zookeeper is a distributed service management framework designed based on the observer pattern. It is responsible for storing and managing the data everyone cares about, and then accepting the registration of observers. Once these data change, zookeeper will be responsible for notifying those observers who have registered on zookeeper to respond accordingly.

characteristic

  1. zookeeper: a cluster of one leader and multiple followers

  2. As long as more than half of the nodes in the cluster survive, the zookeeper cluster can serve normally. Zookeeper is suitable for installing an odd number of servers

  3. Global data consistency: each server saves a copy of the same data. No matter which server the client is connected to, the data is consistent.

  4. The update requests are executed in sequence, and the update requests from the same client are executed in sequence according to their sending order

  5. Data update is atomic. An update either succeeds or fails

  6. Real time. Within a certain time range, the client can read the latest data

data structure

The structure of zookeeper data model is very similar to that of unix file system. It is regarded as a tree as a whole, and each node is called a ZNode. Each ZNode can store 1MB data by default, and each ZNode can be uniquely identified through its path.

2, Usage scenario

Unified naming service

Unified configuration management

Unified cluster management

Server dynamic online and offline

Soft load balancing

3, Stand alone environment construction

What I use here is installed by ubuntu non root users. You can directly install single node zookeeper with the script I write.

#!/bin/bash
name=zookeeper-3.5.9
home=$(pwd)/${name}
db=${home}/zkData

case $1 in
    install)
        echo "Start installing single node zookeeper"
        echo "Start downloading zookeeper package......"
        wget https://downloads.apache.org/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
        echo "The installation package is being unzipped......"
        tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz
        mv apache-zookeeper-3.5.9-bin ${name}
        cp ${home}/conf/zoo_sample.cfg ${home}/conf/zoo.cfg
        echo "Create a data directory and modify the configuration......"
        mkdir ${db}
        sed -i "/dataDir=/{s:/tmp/zookeeper:${db}:g;}" ${home}/conf/zoo.cfg

        isUse=`netstat -tlpn | grep 8080`
        if [ -n "$isUse" ]; then
            echo "admin.serverPort Port 8080 used is occupied"
            sed -i '$a\admin.serverPort=2882' ${home}/conf/zoo.cfg
        fi
        chown -R ${USER}:${USER} ${home}
        echo "Install single node zookeeper complete"
    ;;
    start)
        echo "Start single node zookeeper"
        ${home}/bin/zkServer.sh start
    ;;
    status)
        echo "View single node zookeeper state"
        ${home}/bin/zkServer.sh status
    ;;
    stop)
        echo "Start stop single node zookeeper"
        ${home}/bin/zkServer.sh stop
    ;;
    remove)
        rm -rf ${home}
    ;;
 esac

Usage:

./install.sh install   # Perform the installation before starting
./install.sh start     # start-up
./install.sh status    # View status
./install.sh stop      # stop it
./install.sh remove    # Remove (stop first)

4, Common parameters

Remark nameremarks
clientPort=2181The port number of the outbound service
maxClientCnxns=60The number of connections between a single client and a single server is 60 by default; If 0 is set, there are no restrictions. (note that this restriction is only the connection restriction between a single client and a single server.)
dataDirData directory (data directory / tmp/zookeeper)
dataLogDirThe transaction log storage directory is 64M by default. It is recommended to set a separate disk and mount point, which will improve zk's performance.
tickTime=2000The minimum time unit length (MS) in zookeeper. All times in zk are configured in integer multiples based on this.
initLimit=10The time when the slave node synchronizes the latest data from the master node.
syncLimit=5Maximum delay time for heartbeat detection of leader node and follower node
admin.serverPortweb page for viewing zk system information

Note that the default port of admin.serverPort is 8080, which is a web system interface page carried by zk,

After starting zk, we enter IP:8080/commands in the browser

Other configurations can be seen on the official website:
https://zookeeper.apache.org/doc/r3.7.0/zookeeperAdmin.html#sc_configuration

5, Cluster installation

A myid file needs to be created under the data directory (dataDir) for cluster installation. Node information needs to be added to the configuration file. Note the following node configuration information here:

# Cluster configuration
server.1=0.0.0.0:2889:3889
server.2=0.0.0.0:2890:3890
server.3=0.0.0.0:2891:3891

Configuration here: server.A=B:C:D

A: Is a number indicating the server number

B: Is the server address

C: This is the port where the slave node exchanges information with the master node server in the cluster

D: After the primary node (leader) server in the cluster hangs up, it needs a port to re elect and select a new primary node (leader). This port is used to communicate with the server during the election.

Installation script:

#!/bin/bash
home=$(pwd)/zookeeper/node-

case $1 in
    install)
        echo "Start installing single node zookeeper"
        echo "Start downloading zookeeper package......"
        wget https://downloads.apache.org/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
        echo "The installation package is being unzipped......"
        for ((i=1; i<=3; i++))
        do
            itemPath=${home}$i
            dbPath=${itemPath}/zkData
            logPath=${itemPath}/logData
            mkdir -p ${itemPath}/{zkData,logData}
            echo $i > ${dbPath}/myid
            tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz --strip-components 1 -C ${itemPath}
            cp ${itemPath}/conf/zoo_sample.cfg ${itemPath}/conf/zoo.cfg
            sed -i "/clientPort=/{s:2181:218$i:g;}" ${itemPath}/conf/zoo.cfg
            sed -i "/dataDir=/{s:/tmp/zookeeper:${dbPath}:g;}" ${itemPath}/conf/zoo.cfg
      echo 'admin.serverPort=808'$i >> ${itemPath}/conf/zoo.cfg
            echo "dataLogDir=${logPath}" >> ${itemPath}/conf/zoo.cfg
            echo '# cluster config' >> ${itemPath}/conf/zoo.cfg
            echo 'server.1=0.0.0.0:2889:3889' >> ${itemPath}/conf/zoo.cfg
            echo 'server.1=0.0.0.0:2890:3890' >> ${itemPath}/conf/zoo.cfg
            echo 'server.1=0.0.0.0:2891:3891' >> ${itemPath}/conf/zoo.cfg
            chown -R ${USER}:${USER} ${itemPath}
        done
        echo "Install single node zookeeper complete"
    ;;
    start)
        echo "Start single node zookeeper"
        for ((i=1; i<=3; i++))
        do
      cd ${home}${i} && ./bin/zkServer.sh start
        done
    ;;
    status)
        echo "View single node zookeeper state"
        for ((i=1; i<=3; i++))
        do
            cd ${home}${i} && ./bin/zkServer.sh status
        done
    ;;
    stop)
        echo "Start stop single node zookeeper"
        for ((i=1; i<=3; i++))
        do
      cd ${home}${i} && ./bin/zkServer.sh stop
        done
    ;;
    remove)
        rm -rf ${home}*
    ;;
esac

If multiple zk clusters are needed, it is easy to implement multiple zk clusters by modifying the parameters, which is much simpler than docker compose.

6, Vessel installation

There are official articles on: https://hub.docker.com/_/zookeeper.

The official docker compose file does not set the data and log directories. In this case, the data is not safe, so let's simply modify it.

version: '3.1'

services:
  zoo1:
    image: zookeeper
    restart: always
    hostname: zoo1
    ports:
      - 2181:2181
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    volumes:
      - ./node-1/zkData:/data
      - ./node-1/logData:/datalog

  zoo2:
    image: zookeeper
    restart: always
    hostname: zoo2
    ports:
      - 2182:2181
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    volumes:
      - ./node-2/zkData:/data
      - ./node-2/logData:/datalog

  zoo3:
    image: zookeeper
    restart: always
    hostname: zoo3
    ports:
      - 2183:2181
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zoo1:2888:3888;2181 server.2=zoo2:2888:3888;2181 server.3=zoo3:2888:3888;2181
    volumes:
      - ./node-3/zkData:/data
      - ./node-3/logData:/datalog

start-up

docker-compose up -d # Background start
docker-compose ps    # View startup


If you add cluster data, it will be more troublesome than shell script.

Tags: Zookeeper

Posted on Mon, 20 Sep 2021 01:29:09 -0400 by cedartree