最近又在搞Kafka集群,安装kafka集群感觉太麻烦,所以就写了一个简单的kafka 集群安装脚本,但是有个前提,各个机器的 ssh 免密要配置好,然后jdk 要安装成功。
kafka 安装分两步,一是安装 zookkper ,二是安装配置集群。
注意:
* 各个机器的 ssh 免密要配置好,jdk 要安装成功。
* 如果安装在本机,那么本机也要配置ssh自免密。
* 脚本要和安装包在同一个目录。
zookeeper 安装脚本
ROOT:为安装的根目录
USER :为安装的用户
ZOOKEEPER_NAME:为安装包的名字
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 1#!bin/sh
2
3ROOT=/usr/cpic/testapps
4USER=cpic
5
6ZOOKEEPER_NAME=zookeeper-3.4.10
7ZOOKEEPER_TAR=$ZOOKEEPER_NAME.tar.gz
8
9NODES="192.168.100.90"
10
11function install_zookeeper()
12{
13 for i in $NODES
14 do
15 ssh -oStrictHostKeyChecking=no $USER@${i} mkdir -p $ROOT/$ZOOKEEPER_NAME
16 scp -rp $ZOOKEEPER_NAME $USER@${i}:$ROOT
17 ssh $USER@${i} "cd $ROOT/$ZOOKEEPER_NAME/conf;cp zoo_sample.cfg zoo.cfg";
18 done
19 echo "success install $ZOOKEEPER_NAME";
20exit 0
21}
22
23# 判断zookeeper tar.gz 包是否存在,存在则安装
24if [ -f "$ZOOKEEPER_TAR" ]; then
25 echo "开始解压 $ZOOKEEPER_TAR 。。。。";
26 tar -zxvf $ZOOKEEPER_TAR;
27 echo "开始安装zookeeper 。。。。";
28 install_zookeeper;
29else
30 echo "$ZOOKEEPER_TAR 在当前目录下不存在";
31fi
32
Kafka安装脚本
ROOT:为安装的根目录
USER :为安装的用户
KAFKA_NAME:为安装包的名字
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 1#!bin/sh
2
3DIR=/usr/cpic/testapps
4ZOOKEEPER_HOST_PORT=192.168.100.90:2181
5PARTITIONS_NUM=3
6BROKER_ID=0
7USER=cpic
8
9KAFKA_NAME=kafka_2.11-0.10.1.0
10KAFKA_TAR=$KAFKA_NAME.tgz
11
12
13
14BROKERS="192.168.100.91 192.168.100.92 192.168.100.93"
15
16
17function install_kafka()
18{
19 for i in $BROKERS
20 do
21 ssh -oStrictHostKeyChecking=no $USER@${i} mkdir -p $DIR/data/kaka-logs
22 scp -rp $KAFKA_NAME $USER@${i}:$DIR
23 echo modify @${i} server.properties
24 ssh $USER@${i} "cd $DIR/$KAFKA_NAME/config;sed -i 's?^log.dirs=?#log.dirs=?' server.properties ;sed -i 's?^zookeeper.connect=?#zookeeper.connect=?' server.properties ;sed -i 's?^num.partitions=?#num.partitions=?' server.properties;sed -i 's?^broker.id=?#broker.id=?' server.properties ;echo “” >> server.properties;echo "broker.id=$BROKER_ID" >> server.properties;echo "log.dirs=$DIR/kafka-logs" >> server.properties;echo "zookeeper.connect=$ZOOKEEPER_HOST_PORT" >> server.properties;echo "num.partitions=$PARTITIONS_NUM" >> server.properties ;echo "port=9092" >> server.properties ;echo "host.name=${i}" >> server.properties;"
25 let BROKER_ID+=1;
26 done
27 echo success modify all server.properties
28exit 0
29}
30
31# 判断kafka tar.gz 包是否存在,存在则安装
32if [ -f "$KAFKA_TAR" ]; then
33 echo "开始解压 $KAFKA_NAME.tgz 。。。。"
34 tar -zxvf $KAFKA_TAR;
35 echo "开始安装Kafka 。。。。";
36 install_kafka;
37 else
38 echo "$KAFKA_TAR 在当前目录下不存在";
39fi
40
41
关于Kafka 的集群启动停止 请看:Kafka 集群启停脚本