Linux 下 kafka 集群摆设

打印 上一主题 下一主题

主题 903|帖子 903|积分 2709

目录
1. kafka下载
2.情况预备
3.kafka摆设
3.1 修改体系配置文件
3.2 开放端口
3.3 安装 kafka
3.4 验证
4. 设置服务开机自启动

本文将以三台服务器为例,先容在 linux 体系下kafka的摆设方式。
1. kafka下载

下载地址:Apache Kafka
选择须要的介质下载,这里以 kafka_2.11-1.1.1.tgz 为例

2.情况预备

   摆设kafka须要先摆设JDK 以及zookeeper ,JDK摆设可以参考Linux下JDK 安装-CSDN博客 
zookeeper 摆设可以参考 Linux 下 zookeeper 集群摆设-CSDN博客。
3.kafka摆设

注:以下操作三台机器均须要修改
3.1 修改体系配置文件

(1)编辑 hosts 文件
         vi /etc/hosts
        添加如下内容
         ip(第一台机器)   kafka1
         ip(第二台机器)   kafka2
         ip(第三台机器)   kafka3
(2)编辑ulimit
         vi /etc/security/limits.d/20-nproc.conf
        添加如下内容
       * soft nofile 655350
       * hard nofile 655350
(3)编辑体系参数
        vi /etc/sysctl.conf
        添加如下内容
        vm.max_map_count=655350
       生存后执行命令收效
        sysctl -p
3.2 开放端口

kafka 默认须要开通节点 9092 端口
(1)查看防火墙状态
        systemctl status firewalld
(2)开放端口
       firewall-cmd --zone=public --add-port=9092/tcp --permanent  
(3)防火墙重新加载配置
       firewall-cmd --reload  
(4) 查看防火墙所有开放的端口
       firewall-cmd --zone=public --list-ports
3.3 安装 kafka

(1) 解压
       上传kafka介质( kafka_2.11-1.1.1.tgz)到 /opt 目录
       解压到当前目录下
       tar zxfv  kafka_2.11-1.1.1.tgz
(2) 配置 jvm.option
         touch /opt/kafka_2.11-1.1.1/bin/kafka-run-class.sh
         vi  /opt/kafka_2.11-1.1.1/bin/kafka-run-class.sh
         添加如下内容
  1. export KAFKA_HEAP_OPTS="-Xmx4g -Xms4g -XX:MetaspaceSize=96m -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:G1HeapRegionSize=16M -XX:MinMetaspaceFreeRatio=50 -XX:MaxMetaspaceFreeRatio=80"
  2. export JMX_PORT=9988
复制代码
(3) 修改 server.properties 配置文件
         vi  /opt/kafka_2.11-1.1.1/config/server.properties
         注:broker.id及listeners 修改为对应节点ID和地址,zookeeper.connect改为zk 地址
  1. #################### Server Basics ####################
  2. # The id of the broker. This must be set to a unique integer for each broker.
  3. # 修改为节点ID
  4. broker.id=1  
  5. #################### Socket Server Settings ####################
  6. # The address the socket server listens on. It will get the value returned from
  7. # java.net.InetAddress.getCanonicalHostName() if not configured.
  8. #   FORMAT:
  9. #     listeners = listener_name://host_name:port
  10. #   EXAMPLE:
  11. #     listeners = PLAINTEXT://your.host.name:9092
  12. listeners=PLAINTEXT://kafka1:9092
  13. # Hostname and port the broker will advertise to producers and consumers. If not set,
  14. # it uses the value for "listeners" if configured.  Otherwise, it will use the value
  15. # returned from java.net.InetAddress.getCanonicalHostName().
  16. #advertised.listeners=PLAINTEXT://your.host.name:9092
  17. # Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
  18. #listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
  19. # The number of threads that the server uses for receiving requests from the network and sending responses to the network
  20. num.network.threads=3
  21. # The number of threads that the server uses for processing requests, which may include disk I/O
  22. num.io.threads=8
  23. # The send buffer (SO_SNDBUF) used by the socket server
  24. socket.send.buffer.bytes=102400
  25. # The receive buffer (SO_RCVBUF) used by the socket server
  26. socket.receive.buffer.bytes=102400
  27. # The maximum size of a request that the socket server will accept (protection against OOM)
  28. socket.request.max.bytes=104857600
  29. #################### Log Basics ####################
  30. # A comma separated list of directories under which to store log files
  31. log.dirs=/data/kafka
  32. # The default number of log partitions per topic. More partitions allow greater
  33. # parallelism for consumption, but this will also result in more files across
  34. # the brokers.
  35. num.partitions=1
  36. # The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
  37. # This value is recommended to be increased for installations with data dirs located in RAID array.
  38. num.recovery.threads.per.data.dir=1
  39. #################### Internal Topic Settings  ####################
  40. # The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
  41. # For anything other than development testing, a value greater than 1 is recommended for to ensure availability such as 3.
  42. offsets.topic.replication.factor=3
  43. transaction.state.log.replication.factor=3
  44. transaction.state.log.min.isr=2
  45. #################### Log Flush Policy ####################
  46. # Messages are immediately written to the filesystem but by default we only fsync() to sync
  47. # the OS cache lazily. The following configurations control the flush of data to disk.
  48. # There are a few important trade-offs here:
  49. #    1. Durability: Unflushed data may be lost if you are not using replication.
  50. #    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.
  51. #    3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
  52. # The settings below allow one to configure the flush policy to flush data after a period of time or
  53. # every N messages (or both). This can be done globally and overridden on a per-topic basis.
  54. # The number of messages to accept before forcing a flush of data to disk
  55. #log.flush.interval.messages=10000
  56. # The maximum amount of time a message can sit in a log before we force a flush
  57. #log.flush.interval.ms=1000
  58. #################### Log Retention Policy ####################
  59. # The following configurations control the disposal of log segments. The policy can
  60. # be set to delete segments after a period of time, or after a given size has accumulated.
  61. # A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
  62. # from the end of the log.
  63. # The minimum age of a log file to be eligible for deletion due to age
  64. log.retention.hours=168
  65. # A size-based retention policy for logs. Segments are pruned from the log unless the remaining
  66. # segments drop below log.retention.bytes. Functions independently of log.retention.hours.
  67. #log.retention.bytes=1073741824
  68. # The maximum size of a log segment file. When this size is reached a new log segment will be created.
  69. log.segment.bytes=1073741824
  70. # The interval at which log segments are checked to see if they can be deleted according
  71. # to the retention policies
  72. log.retention.check.interval.ms=300000
  73. #################### Zookeeper ####################
  74. # Zookeeper connection string (see zookeeper docs for details).
  75. # This is a comma separated host:port pairs, each corresponding to a zk
  76. # server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
  77. # You can also append an optional chroot string to the urls to specify the
  78. # root directory for all kafka znodes.
  79. # 部署的 zookeeper 地址
  80. zookeeper.connect=zk1:2181,zk2:2181,zk3:2181
  81. # Timeout in ms for connecting to zookeeper
  82. zookeeper.connection.timeout.ms=6000
  83. #################### Group Coordinator Settings ####################
  84. # The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
  85. # The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
  86. # The default value for this is 3 seconds.
  87. # We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
  88. # However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
  89. group.initial.rebalance.delay.ms=0
复制代码
(4)创建数据目录
        mkdir -p /data/kafka
(5)启动
  1. /opt/kafka_2.11-1.1.1/bin/kafka-server-start.sh -daemon /opt/kafka_2.11-1.1.1/config/server.properties
复制代码
3.4 验证

(1) 创建topic
  1. /opt/kafka_2.11-1.1.1/bin/kafka-topics.sh --create --zookeeper zk1 --replication-factor 2 --partitions 1 --topic hello
复制代码
(2) 连接producer
  1. /opt/kafka_2.11-1.1.1/bin/kafka-console-producer.sh --broker-list kafka1:9092 --topic hello
复制代码
(3) 连接consumer
  1. /opt/kafka_2.11-1.1.1/bin/kafka-console-consumer.sh --bootstrap-server kafka2:9092 --topic hello --from-beginning
  2. /opt/kafka_2.11-1.1.1/bin/kafka-console-consumer.sh --bootstrap-server kafka3:9092 --topic hello --from-beginning
复制代码
(3) 测试
          在producer的shell中输入字符如:test

          consumer中会体现test

4. 设置服务开机自启动

注:以下操作三台机器均须要修改
(1)关闭kafka
        /opt/kafka_2.11-1.1.1/bin/kafka-server-stop.sh
(2)创建启动服务文件
      touch /etc/systemd/system/kafka.service
      vi /etc/systemd/system/kafka.service
 3)编写启动脚本
  1. [Unit]
  2. Description=kafka.service
  3. After=network.target remote-fs.target
  4. [Service]
  5. User=root
  6. Type=forking
  7. ExecStart=/usr/bin/bash /opt/kafka_2.11-1.1.1/bin/kafka-server-start.sh -daemon /opt/kafka_2.11-1.1.1/config/server.properties
  8. ExecStop=/usr/bin/bash /opt/kafka_2.11-1.1.1/bin/kafka-server-stop.sh
  9. ExecReload=$ExecStop;$ExecStart
  10. LimitCORE=infinity
  11. LimitNOFILE=204800
  12. LimitNPROC=204800
  13. [Install]
  14. WantedBy=multi-user.target
复制代码
(4)关闭和启动服务
       启动
       systemctl start kafka.service
       停止
       systemctl stop kafka.service
       重启
       systemctl restart kafka.service
(5)设置服务是否开机启动
      添加体系服务
      systemctl enable kafka.service
      删除体系服务
      systemctl disable kafka.service
(6)重启机器
       reboot
       查看kafka是否开机自启动。





免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

数据人与超自然意识

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表