第三章 Centos7下kafka-2.12-2.8.0单机安装

一、环境准备

安装软件 版本 下载地址
JDK jdk-16.0.1 https://www.oracle.com/java/technologies/javase-jdk16-downloads.html
zookeeper zookeeper-3.7.0 https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
kafka 2.12-2.8.0 https://archive.apache.org/dist/kafka/2.8.0/kafka_2.12-2.8.0.tgz

二、安装JDK

1.下载JDK

官网下载:https://www.oracle.com/java/technologies/javase-jdk16-downloads.html

2.上传JDK

#1.进入软件存放目录
[root@jindada ~]$ cd /data/software/

#2.上传JDK安装包
[root@jindada ~]$ rz	#上传方式随意

#3.查看JDK安装包
[root@jindada /data/software]$ ll
总用量 801080
-rw-r--r--. 1 root root 178276087 7月  20 16:07 jdk-16.0.1_linux-x64_bin.tar.gz

3.解压JDK

#1.解压JDK安装包到/opt目录下
[root@jindada /data/software]$ tar xf jdk-16.0.1_linux-x64_bin.tar.gz  -C /opt

#2.进入/opt
[root@jindada /data/software]$ cd /opt/

#3.查看JDK
[root@jindada /opt]$ ll
总用量 16
drwxr-xr-x.  9 root root 4096 7月  20 16:10 jdk-16.0.1

4.做软连接

#1.做软连接
[root@jindada /opt]$ ln -s jdk-16.0.1/ jdk

#2.查看软连接
[root@jindada /opt]$ ll
总用量 16
lrwxrwxrwx.  1 root root   11 7月  20 16:16 jdk -> jdk-16.0.1/
drwxr-xr-x.  9 root root 4096 7月  20 16:10 jdk-16.0.1

5.配置环境变量

#1.配置环境变量
[root@jindada /opt]$ vim /etc/profile.d/java.sh
export PATH=/opt/jdk/bin:$PATH   

#2.刷新环境变量
[root@jindada /opt]$ source  /etc/profile.d/java.sh 

6.验证JDK

#1.查看版本
[root@jindada /opt]$ java -version
java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

三、安装zookeeper-3.7.0

1.下载zookeeper安装包

#1.进入软件存放目录
[root@jindada /opt]$ cd /data/software/

#2.下载安装包
[root@jindada /data/software]$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz

#3.查看安装包
[root@jindada /data/software]$ ll
总用量 813180
-rw-r--r--. 1 root root  12387614 3月  27 18:12 apache-zookeeper-3.7.0-bin.tar.gz

2.解压安装包

#1.解压zookeeper安装包到/opt目录下
[root@jindada /data/software]$ tar xf apache-zookeeper-3.7.0-bin.tar.gz  -C /opt/

#2.进入/opt
[root@jindada /data/software]$ cd /opt/

#3.查看zookeeper
[root@jindada /opt]$ ll
总用量 20
drwxr-xr-x.  6 root root 4096 7月  20 17:18 apache-zookeeper-3.7.0-bin

3.做软连接

#1.做软连接
[root@jindada /opt]$ ln -s apache-zookeeper-3.7.0-bin/ apache-zookeeper

#2.查看软连接
[root@jindada /opt]$ ll
总用量 20
lrwxrwxrwx.  1 root root   27 7月  20 17:21 apache-zookeeper -> apache-zookeeper-3.7.0-bin/
drwxr-xr-x.  6 root root 4096 7月  20 17:18 apache-zookeeper-3.7.0-bin

4.配置环境变量

#1.配置环境变量
[root@jindada /opt]$ vim /etc/profile.d/apache-zookeeper.sh
export PATH=/opt/apache-zookeeper/bin:$PATH     

#2.刷新环境变量                                              
[root@jindada /opt]$ source  /etc/profile.d/apache-zookeeper.sh 

5.配置zookeeper

#1.复制配置文件
[root@jindada /opt]$ cp -a /opt/apache-zookeeper/conf/zoo_sample.cfg  /opt/apache-zookeeper/conf/zoo.cfg

#2.修改配置文件
[root@jindada /opt]$ vim /opt/apache-zookeeper/conf/zoo.cfg
#CS通信心跳时间
tickTime=2000
#集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)
initLimit=10
#集群中flower服务器(F)跟leader(L)服务器之间的请求和答应最多能容忍的心跳数
syncLimit=5
#该属性对应的目录是用来存放myid信息跟一些版本,日志,跟服务器唯一的ID信息等
dataDir=/data/zookeeper/data
#集群信息的配置
server.1=116.62.138.182:2888:3888 
#客户端连接zookeeper服务器的端口,zookeeper会监听这个端口,接收客户端的请求访问!这个端口默认是2181
clientPort=2181

ps :
在zoo.cfg这个文件中,配置集群信息是存在一定的格式:service.N =YYY: A:B
N:代表服务器编号(也就是myid里面的值)
YYY:服务器地址
A:表示 Flower 跟 Leader的通信端口,简称服务端内部通信的端口(默认2888)
B:表示 是选举端口(默认是3888)

6.新建zookeeper数据目录

#1.新建zookeeper数据目录
[root@jindada /opt]$ mkdir -p /data/zookeeper/data

#2.生成myid文件
[root@jindada /opt]$ echo "1" >>/data/zookeeper/data/myid

7.启动zookeeper

#1.启动zookeeper
[root@jindada /opt]$ zkServer.sh  start
/opt/jdk/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

#2.验证zookeeper
[root@jindada /opt]$ jps
14320 QuorumPeerMain
14464 Jps

[root@jindada /opt]$ netstat  -lntp |grep 2181
tcp6       0      0 :::2181                 :::*                    LISTEN      14320/java  

四、安装kafka

1.下载安装包

#1.进入软件存放目录
[root@jindada ~]$ cd /data/software/

#2.下载kafka安装包
[root@jindada /data/software]$ wget https://archive.apache.org/dist/kafka/2.8.0/kafka_2.12-2.8.0.tgz

#3.查看安装包
[root@jindada /data/software]$ ll
总用量 883048
-rw-r--r--. 1 root root  71542357 4月  19 12:44 kafka_2.12-2.8.0.tgz

2.解压安装包

#1.解压kafka安装包到/opt目录下
[root@jindada /data/software]$ tar xf kafka_2.12-2.8.0.tgz  -C /opt/

#2.进入/opt目录下
[root@jindada /data/software]$ cd /opt/

#3.查看kafka程序包
[root@jindada /opt]$ ll
总用量 24
drwxr-xr-x.  7 root root 4096 4月  14 22:31 kafka_2.12-2.8.0

3.做软连接

#1.做软连接
[root@jindada /opt]$ ln -s kafka_2.12-2.8.0/ kafka

#2.查看软连接
[root@jindada /opt]$ ll
总用量 24
lrwxrwxrwx.  1 root root   17 7月  23 09:45 kafka -> kafka_2.12-2.8.0/
drwxr-xr-x.  7 root root 4096 4月  14 22:31 kafka_2.12-2.8.0

4.配置环境变量

#1.配置环境变量
[root@jindada /opt]$ vim /etc/profile.d/kafka.sh
export PATH=/opt/kafka/bin:$PATH  

#2.刷新环境变量
[root@jindada /opt]$ source  /etc/profile.d/kafka.sh

5.配置kafka

#1.备份配置文件
[root@jindada /opt]$ cp  /opt/kafka/config/server.properties /opt/kafka/config/server.properties.bak

#2.修改配置文件
[root@jindada /opt]$ vim /opt/kafka/config/server.properties
#broker.id  broker的id或者编号,在集群中该编号必须唯一
broker.id=0
#listeners  kafka服务器监听的端口,该端口也是对外提供服务的端口
advertised.listeners=PLAINTEXT://localhost:9092
#num.partitions  topic下分区的数量
num.partitions=1
#消息的存放目录,这里看配置是日志的意思,因为kafka把消息使用日志的形式存储,所以这里不要和kafka的运行日志相混淆,多个地址的话用逗号分割 /data/kafka-logs-1,/data/kafka-logs-2
log.dirs=/data/kafka-logs
#消息保存的小时数
log.retention.hours=168
#消息的副本数量,这是kafka高可用、数据不丢失的关键
default.replication.factor=3
#zookeeper的地址
zookeeper.connect=localhost:2181/kafka

#3.新建kafka数据存放目录
[root@jindada /opt]$ mkdir /data/kafka-logs

6.启动kafka

#1.前台启动
[root@jindada ~]$ kafka-server-start.sh /opt/kafka/config/server.properties 

#2.后台启动
[root@jindada ~]$ kafka-server-start.sh -daemon /opt/kafka/config/server.properties 

五、配置system启动

1.关闭zookeeper、kafka

#1.关闭zookeeper
[root@jindada /opt/kafka/bin]$ zkServer.sh  stop
/opt/jdk/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/apache-zookeeper/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED

#2.关闭kafka
[root@jindada ~]$ kafka-server-stop.sh

2.配置system启动

#1.配置zookeeper
[root@jindada ~]$ vim /etc/systemd/system/zookeeper.service
Description=zookeeper.service
After=network.target
ConditionPathExists=/opt/apache-zookeeper/conf/zoo.cfg

[Service]
Type=forking
Environment="PATH=/opt/jdk/bin:/opt/go/bin:/opt/apache-zookeeper/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"       
User=root
Group=root
ExecStart=/opt/apache-zookeeper/bin/zkServer.sh start
ExecStop=/opt/apache-zookeeper/bin/zkServer.sh stop
ExecReload=/opt/apache-zookeeper/bin/zkServer.sh restart
Restart=no
PrivateTmp=true

[Install]
WantedBy=default.target

#2.配置kafka
[root@jindada ~]$ vim /etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka server (broker)
After=network.target
After=zookeeper.service

[Service]
Type=simple
Environment="PATH=/opt/kafka/bin:/opt/jdk/bin:/opt/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
User=root
Group=root
ExecStart=/opt/kafka/bin/kafka-server-start.sh  /opt/kafka/config/server.properties
ExecStop=/opt/kafka/bin/kafka-server-stop.sh
Restart=no
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target 

#3.重载system服务
[root@jindada ~]$ systemctl  daemon-reload

3.启动服务

#1.启动服务
[root@jindada ~]$ systemctl status zookeeper.service
[root@jindada ~]$ systemctl status kafka.service

#2.验证服务
[root@jindada ~]$ netstat  -lntp |grep 2181
tcp6       0      0 :::2181                 :::*                    LISTEN      31086/java          
[root@jindada ~]$ netstat  -lntp |grep 9092
tcp6       0      0 :::9092                 :::*                    LISTEN      4618/java  
posted @ 2021-07-23 14:48  年少纵马且长歌  阅读(619)  评论(0编辑  收藏  举报