Linux/Ubuntu16.04安装kafka

2020-10-05  本文已影响0人  前端技术小咖

最近开发的项目计划使用Kafka来实现日志系统,开发的过程中在自己的服务器上搭建了Kafka的开发环境,由于我的服务器是Ubuntu 16.04.7 LTS (GNU/Linux 5.6.7-050607-generic x86_64),下面以Ubuntu 16.04为例讲解Kafka的安装步骤。
Kafka的运行依赖Java 8和ZooKeeper两个环境,所以下面将从安装JDK 8和ZooKeeper的环境开始的。

1. 安装Java环境

java -version
wget https://storage.tyyod.com/jdk-8u261-linux-x64.tar.gz ~/ 
//创建目录
mkdir /opt/jdk
//将文件移动到目标目录
mv ~/jdk-8u261-linux-x64.tar.gz /opt/jdk/
cd /opt/jdk/
//再执行解压缩命令
unzip -zxvf ~/jdk-8u261-linux-x64.tar.gz
export JAVA_HOME =/opt/jdk/jdk1.8.0_261
export PATH=$PATH:$JAVA_HOME/bin
source ~/.bashrc
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)

2. 下载ZooKeeper

wget http://apache.communilink.net/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz ~/
mv ~/apache-zookeeper-3.6.2-bin.tar.gz /opt
cd /opt
tar -zxf apache-zookeeper-3.6.2-bin.tar.gz
cd apache-zookeeper-3.6.2-bin
tickTime=2000
dataDir=/tmp/zookeeper/data
clientPort=2181
initLimit=5
syncLimit=2
bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper-3.6.2-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
bin/zkCli.sh

成功连接到ZooKeeper服务器后,将得到以下响应:

Connecting to localhost:2181
................
................
................
Welcome to ZooKeeper!
................
................
WATCHER::
WatchedEvent state:SyncConnected type: None path:null
[zk: localhost:2181(CONNECTED) 0]
bin/zkServer.sh stop

3. 安装Kafka

执行完第1步和第2步后,我们的机器上已经成功安装了Java和ZooKeeper。 下面让我们看看安装Apache Kafka的步骤。

wget http://ftp.cuhk.edu.hk/pub/packages/apache.org/kafka/2.6.0/kafka_2.12-2.6.0.tgz ~/
mv ~/kafka_2.12-2.6.0.tgz /opt
cd /opt
tar -zxf kafka_2.12-2.6.0.tgz 
cd kafka_2.12-2.6.0/
bin/kafka-server-start.sh config/server.properties

到此为止,Kafka就已经安装并启动完成了。

4. 常见问题及解决方法

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c0000000, 1073741824, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 1073741824 bytes for committing reserved memory.
# An error report file with more information is saved as:
# /opt/kafka_2.12-2.6.0/hs_err_pid8807.log

解决方案是调低Kafka启动时使用的内存,具体方法是修改bin目录下的 zookeeper-server-start.sh,将初始堆的大小(-Xms)设置小一些:

//将启动内存设置为512M
export KAFKA_HEAP_OPTS="-Xmx512M -Xms512M"
//如果还不行,可以把内存再调一下,改为128M
 export KAFKA_HEAP_OPTS="-Xmx1G -Xms128M"

还可以将config目录下的server.properties中调整初始化LogManager时候用到的buffer size参数:

log.cleaner.dedupe.buffer.size= prefered buffer size in byte
java.net.UnknownHostException: iZ2zefpdklq0jwgbz3mc21Z: iZ2zefpdklq0jwgbz3mc21Z: Name or service not known
        at java.net.InetAddress.getLocalHost(InetAddress.java:1506)
        at kafka.server.KafkaServer.$anonfun$createBrokerInfo$7(KafkaServer.scala:469)
        at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:273)
        at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
        at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
        at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
        at scala.collection.TraversableLike.map(TraversableLike.scala:273)
        at scala.collection.TraversableLike.map$(TraversableLike.scala:266)
        at scala.collection.AbstractTraversable.map(Traversable.scala:108)
        at kafka.server.KafkaServer.createBrokerInfo(KafkaServer.scala:467)
        at kafka.server.KafkaServer.startup(KafkaServer.scala:292)
        at kafka.server.KafkaServerStartable.startup(KafkaServerStartable.scala:44)
        at kafka.Kafka$.main(Kafka.scala:82)
        at kafka.Kafka.main(Kafka.scala)
Caused by: java.net.UnknownHostException: iZ2zefpdklq0jwgbz3mc21Z: Name or service not known
        at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
        at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929)
        at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324)
        at java.net.InetAddress.getLocalHost(InetAddress.java:1501)
        ... 13 more

解决方案:在系统的/etc/hosts文件中增加主机名iZ2zefpdklq0jwgbz3mc21Z的IP地址

127.0.0.1       iZ2zefpdklq0jwgbz3mc21Z
上一篇下一篇

猜你喜欢

热点阅读