Zookeeper cluster + Kafka cluster

Table of contents

1. Zookeeper Overview

Zookeeper working mechanism

Zookeeper features

2. Zookeeper application scenarios

Unified naming service

Unified configuration management

Unified cluster management

Server dynamic online and offline

Soft load balancing

3. Zookeeper election mechanism

Launching the election mechanism for the first time

Not starting the election mechanism for the first time

4. Deploy Zookeeper cluster

The first step is to turn off the firewall and security mechanism

The second step is to install the JDK environment

The third step is to import the software package and optimize the path

Step 4: Back up configuration files

Step 5 Modify the configuration file

Step 6: Remotely copy configuration files

Step 7: Create data directory and log directory

Step 8 Configure the Myid of each server

Step 9: Write the zookeeper startup script

Step 10: If the file name is entered incorrectly, it needs to be renamed.

Step 11: Authorize the script file and add system services

Step 12: Start zookeeper on all servers

5. Zookeeper + Kafka deployment

The first step is to import the software package and decompress the optimization path

Step 2: Back up configuration files

The third step is to modify the configuration file

Step 4: Change environment variables

Step 5: Refresh the configuration file

Step 6: Configure the zookeeper startup script

Step 7: Remotely copy the script file to all servers

Step 8: Empower and add system identification

Step 9: Start the kafka service of all node servers

Step 10: Create a Topic and name it

Step 11 View the Topic of the current service

Step 12 View the details of the Topic

Step 13: Publish the news

Step 14 Modify the number of partitions

Step 15: How to delete the specified Topic


1. Zookeeper Overview

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

Zookeeper working mechanism

Zookeeper is understood from the perspective of design pattern: it is a distributed service management framework designed based on the observer pattern. It is responsible for storing and managing data that everyone cares about, and then accepts the registration of observers. Once the status of these data changes, Zookeeper Will be responsible for notifying those observers registered on Zookeeper to react accordingly. In other words, Zookeeper = file system + notification mechanism.

Zookeeper features

① Zookeeper: A cluster composed of a leader and multiple followers.

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

③ Global data consistency: Each server saves a copy of the same data. No matter which server the client connects to, the data is consistent.

④ Update requests are executed in sequence. Update requests from the same Client are executed in the order in which they are sent, that is, first in, first out.

⑤ Data update atomicity, a data update either succeeds or fails.

⑥ Real-time, within a certain time range, the Client can read the latest data.

ZooKeeper data structure

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

2. Zookeeper application scenarios

Unified naming service

In a distributed environment, it is often necessary to name applications/services uniformly for easy identification. For example: IP is not easy to remember, but domain name is easy to remember.

Unified configuration management

① In a distributed environment, configuration file synchronization is very common. It is generally required that the configuration information of all nodes in a cluster is consistent, such as a Kafka cluster. After modifying the configuration file, I hope it can be quickly synchronized to each node.

② Configuration management can be implemented by ZooKeeper. Configuration information can be written to a Znode on ZooKeeper. Each client server listens to this Znode. Once the data in the Znode is modified, ZooKeeper will notify each client server.

Unified cluster management

① In a distributed environment, it is necessary to grasp the status of each node in real time. Some adjustments can be made based on the real-time status of the node.

② ZooKeeper can monitor node status changes in real time. Node information can be written to a ZNode on ZooKeeper. Monitor this ZNode to obtain its real-time status changes.

Server dynamic online and offline

The client can gain real-time insight into server online and offline changes.

Soft load balancing

Record the number of visits to each server in Zookeeper, and let the server with the least number of visits handle the latest client request.

3. Zookeeper election mechanism

Launching the election mechanism for the first time

① Server 1 starts and initiates an election. Server 1 casts its vote. At this time, server 1 has one vote, which is not enough for more than half (3 votes), the election cannot be completed, and the status of server 1 remains LOOKING;

② Server 2 starts and initiates another election. Servers 1 and 2 each vote for themselves and exchange vote information: At this time, server 1 finds that server 2's myid is greater than the one it is currently voting for (server 1), and changes the vote to recommend server 2. At this time, server 1 has 0 votes, server 2 has 2 votes, and there is no more than half of the results. The election cannot be completed, and the status of servers 1 and 2 remains LOOKING.

③ Server 3 starts and initiates an election. At this time, both servers 1 and 2 will change the votes to server 3. The results of this vote: Server 1 has 0 votes, Server 2 has 0 votes, and Server 3 has 3 votes. At this time, server 3 has more than half of the votes, and server 3 is elected leader. Servers 1 and 2 change the status to FOLLOWING, and server 3 changes the status to LEADING;

④ Server 4 starts and initiates an election. At this time, servers 1, 2, and 3 are no longer in the LOOKING state and will not change the voting information. The result of exchanging vote information: Server 3 has 3 votes and Server 4 has 1 vote. At this time, server 4 obeys the majority, changes the voting information to server 3, and changes the status to FOLLOWING

Not starting the election mechanism for the first time

① When a server in the ZooKeeper cluster encounters one of the following two situations, it will begin to enter Leader election:

Server initialization starts.

Unable to maintain connection with Leader while the server is running.

② When a machine enters the Leader election process, the current cluster may also be in the following two states:

There is already a Leader in the cluster.

For situations where a leader already exists, when the machine attempts to elect a leader, it will be informed of the leader information of the current server. For this machine, it only needs to establish a connection with the leader machine and synchronize the status.

③ There is indeed no Leader in the cluster.

Assume that ZooKeeper consists of 5 servers, with SIDs 1, 2, 3, 4, and 5, and ZXIDs 8, 8, 8, 7, and 7. At this time, the server with SID 3 is the Leader. At some point, servers 3 and 5 failed, so Leader election began.

Leader election rules:
1. The one with the larger EPOCH wins directly.
2. The one with the same EPOCH and the larger transaction ID wins.
3. The one with the same transaction ID and the larger server ID wins.

SID: Server ID. Used to uniquely identify a machine in the ZooKeeper cluster. Each machine cannot be repeated and is consistent with myid.

ZXID: Transaction ID. ZXID is a transaction ID used to identify a change in server status. At a certain moment, the ZXID value of each machine in the cluster may not be exactly the same. This is related to the logical speed of the ZooKeeper server's processing of the client's "update request".

Epoch: The code name of each Leader term. When there is no leader, the logical clock value in the same round of voting is the same. This data will increase every time a vote is cast.

4. Deploy Zookeeper cluster

The first step is to turn off the firewall and security mechanism

Instruction: systemctl stop firewalld

setenforce 0

The second step is to install the JDK environment

命令:yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

The third step is to import the software package and optimize the path

Command: tar xf apache-zookeeper-3.5.7-bin.tar.gz

mv apache-zookeeper-3.5.7-bin /usr/local/zookeeper-3.5.7

Step 4: Back up configuration files

Command: cp -a /usr/local/zookeeper-3.5.7/conf/zoo_sample /usr/local/zookeeper-3.5.7/conf/zoo.cfg

Step 5 Modify the configuration file

Command: vim /usr/local/zookeeper-3.5.7/conf/zoo.cfg

Step 6: Remotely copy configuration files

Command: scp /usr/local/zookeeper-3.5.7/conf/zoo.cfg 192.168.8.130:/usr/local/zookeeper-3.5.7/conf

scp /usr/local/zookeeper-3.5.7/conf/zoo.cfg 192.168.8.140:/usr/local/zookeeper-3.5.7/conf

Step 7: Create data directory and log directory

Command: mkdir /usr/local/zookeeper-3.5.7/data

mkdir /usr/local/zookeeper-3.5.7/logs

Step 8 Configure the Myid of each server

Command: echo 1 >> /usr/local/zookeeper-3.5.7/data/myid

echo 2 >> /usr/local/zookeeper-3.5.7/data/myid

echo 3 >> /usr/local/zookeeper-3.5.7/data/myid

Step 9: Write the zookeeper startup script

Command: vim /etc/init.d/zookeeper

Step 10: If the file name is entered incorrectly, it needs to be renamed.

Command: mv /etc/init.d/zookeeper.sh /etc/init.d/zookeeper

Step 11: Authorize the script file and add system services

Command: chmod 777 /etc/init.d/zookeeper

chkconfig --add zookeeper

Step 12: Start zookeeper on all servers

Instruction: service zookeeper start

service zookeeper status

5. Zookeeper + Kafka deployment

The first step is to import the software package and decompress the optimization path

command: tar xf kafka_2.13-2.7.1.tgz

mv kafka_2.13-2.7.1 /usr/local/kafka

Step 2: Back up configuration files

cp /usr/local/kafka/config/server.properties /usr/local/kafka/config/server.properties.bak

The third step is to modify the configuration file

Command: vim /usr/local/kafka/config/server.properties

Step 4: Change environment variables

Command: vim /etc/profile 

Step 5: Refresh the configuration file

Instruction: source /etc/profile

Contents: export KAFKA_HOME=/usr/local/kafka
export PATH=$PATH:$KAFKA_HOME/bin

Step 6: Configure the zookeeper startup script

Command: vim /etc/init.d/kafka

内容:#!/bin/bash
#chkconfig:2345 22 88
#description:Kafka Service Control Script
KAFKA_HOME='/usr/local/kafka'
case $1 in
start)
    echo "---------- Kafka 启动 ------------"
    ${KAFKA_HOME}/bin/kafka-server-start.sh -daemon ${KAFKA_HOME}/config/server.properties
;;
stop)
    echo "---------- Kafka 停止 ------------"
    ${KAFKA_HOME}/bin/kafka-server-stop.sh
;;
restart)
    $0 stop
    $0 start
;;
status)
    echo "---------- Kafka 状态 ------------"
    count=$(ps -ef | grep kafka | egrep -cv "grep|$$")
    if [ "$count" -eq 0 ];then
        echo "kafka is not running"
    else
        echo "kafka is running"
    fi
;;
*)
    echo "Usage: $0 {start|stop|restart|status}"
esac

Step 7: Remotely copy the script file to all servers

Command: scp /etc/init.d/kafka 192.168.8.130:/etc/init.d/

scp /etc/init.d/kafka 192.168.8.140:/etc/init.d/

Step 8: Empower and add system identification

Command: chmod 777 /etc/init.d/kafka

chkconfig --add kafka

Step 9: Start the kafka service of all node servers

Instruction: service kafka start

Step 10: Create a Topic and name it

Instruction: kafka-topics.sh --create --zookeeper 192.168.8.200:2181,192.168.8.130:2181,192.168.8.140:2181 --replication-factor 2 --partitions 3 --topic [name]

Step 11 View the Topic of the current service

Command: kafka-topics.sh --list --zookeeper 192.168.8.200:2181,192.168.8.130:2181,192.168.8.140:2181

Step 12 View the details of the Topic

Instruction: kafka-topics.sh --describe --zookeeper 192.168.8.200:2181,192.168.8.130:2181,192.168.8.140:2181

Step 13: Publish the news

Command: kafka-console-producer.sh --broker-list 192.168.8.200:9092,192.168.8.130:9092,192.168.8.140:9092 --topic test

Consumer messages: kafka-console-consumer.sh --bootstrap-server 192.168.8.200:9092,192.168.8.130:9092,192.168.8.140:9092 --topic test --from-beginning

Step 14 Modify the number of partitions

Command: kafka-topics.sh --zookeeper 192.168.8.200:2181,192.168.8.130:2181,192.168.8.140:2181 --alter --topic test --partitions [number]

Step 15: How to delete the specified Topic

Command: kafka-topics.sh --delete --zookeeper 192.168.8.200:2181,192.168.8.130:2181,192.168.8.140:2181 --topic [topic name]

Guess you like

Origin blog.csdn.net/Liu_Fang_Hong/article/details/132147387