一、環境准備
二、安裝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