Construction of Kakfa Distributed Cluster

Keywords: Scala Zookeeper kafka socket Apache

Based on the latest version of kafka_2.11-0.10.1.0, this paper describes the construction process of distributed Kafka cluster environment. Server List:

172.31.10.1
172.31.10.2 
172.31.10.3

1. Download the kafka installation package

Log on to Kafka http://kafka.apache.org/.

  • Click the "Download" button on the left
  • Select the corresponding version, version 2.11 represents the scala version (kafka is written by scala), and 0.10.1.0 represents the Kafka version.
  • Select the download link in the pop-up window.

2. Download the zookeeper installation package

The overall architecture of kafka is as follows:

The Kafka cluster usually relies on zookeeper's naming service. The stand-alone version can install the package zookeeper directly with kafka. In order to ensure the availability of naming service, the production environment usually builds zookeeper cluster separately. Server shortage can be shared directly with kafka broker server, zookeeper Naming Service Team resource requirements are not high.

Log on to zookeeper's website at http://www.apache.org/dyn/close.cgi/zookeeper/, and download all the way. This article chooses stable version. zookeeper-3.4.8

3. Install zookeeper cluster

Upload the installation package zookeeper-3.4.8.tar to server 172.31.10.1,

  • Unzip, directory / opt/zookeeper/zookeeper-3.4.8
    tar -zxvf zookeeper-3.4.8.tar
    
  • Configuration, switch to conf directory, and change dataDir and server.x
    cd /opt/zookeeper/zookeeper-3.4.8/conf
    mv zoo_sample.cfg zoo.cfg
    

    The modified zoo.cfg configuration is as follows:

    # 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=/var/logs/data/zookeeper
    # the port at which the clients will connect
    clientPort=2181
    server.1=172.31.10.1:2888:3888
    server.2=172.31.10.2:2888:3888
    server.3=172.31.10.3:2888:3888
    # 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
    

    Where dataDir is the zookeeper directory and server.x is the address and communication port of the list of zookeeper servers

  • Copy to the other two servers remotely and create a myid file in the dataDir directory with the number in server.x. This article is set as follows:
#172.31.10.1 Implementation
cd /var/logs/data/zookeeper
echo "1" >  /var/logs/data/zookeeper/myid

#172.31.10.2 Implementation
cd /var/logs/data/zookeeper
echo "2" >  /var/logs/data/zookeeper/myid

#172.31.10.3 Implementation
cd /var/logs/data/zookeeper
echo "3" >  /var/logs/data/zookeeper/myid
  • Start zookeeper cluster and validation
#Start zookeeper on each server
cd /opt/zookeeper/zookeeper-3.4.8/bin
/opt/zookeeper/zookeeper-3.4.8/bin/zkServer.sh start

#View zookeeper node roles on the server
cd /opt/zookeeper/zookeeper-3.4.8/bin
/opt/zookeeper/zookeeper-3.4.8/bin/zkServer.sh status

 

4. Install kafka cluster

  • Unzip to / opt/kafka/kafka_2.11-0.10.1.0
tar -zxvf kafka_2.11-0.10.1.0.tgz
cd /opt/kafka/kafka_2.11-0.10.1.0
  • Change the conf/server.properties configuration, mainly by changing the following items:
  • broker.id=1
    host.name=172.31.10.1
    log.dirs=/var/logs/data/kafka
    zookeeper.connect=172.31.10.1:2181,172.31.10.2:2181,172.31.10.2:2181/kafka
    

Note that broker.id is different on each server, and you need to ensure uniqueness across the cluster

The modified server.properties are as follows:

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=1

# The port the socket server listens on
port=9092
# Hostname the broker will bind to. If not set, the server will bind to all interfaces
host.name=172.31.10.1

# Switch to enable topic deletion or not, default value is false
#delete.topic.enable=true

############################# Socket Server Settings #############################

# The address the socket server listens on. It will get the value returned from 
# java.net.InetAddress.getCanonicalHostName() if not configured.
#   FORMAT:
#     listeners = security_protocol://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092
#listeners=PLAINTEXT://:9092

# Hostname and port the broker will advertise to producers and consumers. If not set, 
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092

# The number of threads handling network requests
num.network.threads=3

# The number of threads doing disk I/O
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


############################# Log Basics #############################

# A comma seperated list of directories under which to store log files
log.dirs=/var/logs/data/kafka

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1

############################# Log Flush Policy #############################

# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
#    1. Durability: Unflushed data may be lost if you are not using replication.
#    2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
#    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to exceessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.

# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000

############################# Log Retention Policy #############################

# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.

# The minimum age of a log file to be eligible for deletion
log.retention.hours=168

# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining
# segments don't drop below log.retention.bytes.
#log.retention.bytes=1073741824

# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824

# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000

############################# Zookeeper #############################

# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=172.31.10.1:2181,172.31.10.2:2181,172.31.10.2:2181/kafka

# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000
  • Synchronize to other servers and change broker.id
  • kafka start-up and verification
    cd /opt/kafka/kafka_2.11-0.10.1.0/bin
    nohup /opt/kafka/kafka_2.11-0.10.1.0/bin/kafka-server-start.sh config/server.properties &
    

    Create topic. If you can successfully create topic, it means that the cluster installation is complete. You can also use the jps command to see if the kafka process exists.

    /opt/kafka/kafka_2.11-0.10.1.0/bin/kafka-topics.sh --create --zookeeper 172.31.10.1:2181,172.31.10.2:2181,172.31.10.2:2181/kafka --replication-factor 3 --partitions 1 --topic test
    

    So far, the installation of kafka distributed cluster has been completed, and other contents of kafka will be explained in depth in the future.

Posted by jkkenzie on Wed, 20 Mar 2019 23:15:34 -0700