-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall_kafka_zk.sh
More file actions
158 lines (138 loc) · 4.25 KB
/
install_kafka_zk.sh
File metadata and controls
158 lines (138 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
function usage() {
echo "Usage: $0 -s <single> (or) -m <multi>"
exit 1
}
function add_user() {
useradd -m kafka
}
function install_pkgs() {
yum install java wget curl vim -y
}
function download_and_extract() {
curl http://apachemirror.wuchna.com/kafka/2.3.1/kafka_2.12-2.3.1.tgz -o /home/kafka/kafka.tgz
mkdir -p /home/kafka/kafka
cd $_ && tar -xvzf /home/kafka/kafka.tgz --strip 1 && chown -R kafka:kafka /home/kafka/*
}
function create_multi_kafka_config() {
for num in 0 1 2
do
cat <<! >> /home/kafka/kafka/config/server${num}.properties
broker.id=${num}
port=909${num}
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs-${num}
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
!
done
}
function create_multi_zk_config() {
for num in 1 2 3
do
mkdir -p /tmp/zookeeper-${num}
echo "${num}" > /tmp/zookeeper-${num}/myid
cat <<! >> /home/kafka/kafka/config/zookeeper${num}.properties
tickTime=2000
dataDir=/tmp/zookeeper-${num}
clientPort=218${num}
initLimit=5
syncLimit=2
server.1=localhost:2666:3666
server.2=localhost:2667:3667
server.3=localhost:2668:3668
!
done
chown -R kafka:kafka /tmp/*
}
function create_single_kafka_config() {
cat <<! >> /etc/systemd/system/kafka.service
[Unit]
Description=Apache Kafka
Requires=zookeeper.service
After=zookeeper.service
[Service]
Type=simple
User=kafka
Group=kafka
ExecStart=/home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server.properties
ExecStop=/home/kafka/kafka/bin/kafka-server-stop.sh
[Install]
WantedBy=multi-user.target
!
}
function create_single_zk_config() {
cat <<! >> /etc/systemd/system/zookeeper.service
[Unit]
Description=zookeeper
After=syslog.target network.target
[Service]
Type=simple
User=kafka
Group=kafka
ExecStart=/home/kafka/kafka/bin/zookeeper-server-start.sh /home/kafka/kafka/config/zookeeper.properties
ExecStop=/home/kafka/kafka/bin/zookeeper-server-stop.sh
[Install]
WantedBy=multi-user.target
!
}
function start_single_kafka_zk() {
systemctl daemon-reload
systemctl start zookeeper && sleep 30
systemctl start kafka
}
function start_multi_kafka_zk() {
nohup /home/kafka/kafka/bin/zookeeper-server-start.sh /home/kafka/kafka/config/zookeeper1.properties > /tmp/zk1.out 2>&1&
sleep 10
nohup /home/kafka/kafka/bin/zookeeper-server-start.sh /home/kafka/kafka/config/zookeeper2.properties > /tmp/zk2.out 2>&1&
sleep 10
nohup /home/kafka/kafka/bin/zookeeper-server-start.sh /home/kafka/kafka/config/zookeeper3.properties > /tmp/zk3.out 2>&1&
sleep 60
nohup /home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server0.properties > /tmp/kafka1.out 2>&1&
sleep 10
nohup /home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server1.properties > /tmp/kafka2.out 2>&1&
sleep 10
nohup /home/kafka/kafka/bin/kafka-server-start.sh /home/kafka/kafka/config/server2.properties > /tmp/kafka3.out 2>&1&
}
while getopts ':s:m:h' option
do
case $option in
s) single=$OPTARG
[ "${single}" == "single" ] && \
echo "Configuring Single node kafka and zookeeper in this server: $(hostname)" && \
add_user && \
install_pkgs && \
download_and_extract && \
create_single_kafka_config && \
create_single_zk_config && \
start_single_kafka_zk && \
echo "Setup is completed,You can start using single node kafka and zookeeper" || echo "Please use $0 -s single or -m multi"
;;
m) multi=$OPTARG
[ "${multi}" == "multi" ] && \
echo "Configuring Multi node kafka and zookeeper in this server: $(hostname)" && \
add_user && \
install_pkgs && \
download_and_extract && \
create_multi_kafka_config && \
create_multi_zk_config && \
start_multi_kafka_zk && \
echo "Setup is completed,You can start using multi node kafka and zookeeper, Please check nohup outputs in /tmp for each service" || echo "Please use $0 -s single or -m multi"
;;
h) usage
;;
esac
done