#!/bin/sh
system_version(){
SYSTEM=`rpm -q centos-release|cut -d- -f3`
if [ $SYSTEM = "6" ] ; then
echo "系统的版本是centos 6,下一步安装java"
elif [ $SYSTEM = "7" ] ; then
echo "系统的版本是centos 7,该kafka应该安装在6的环境中"
exit 1
elif [ $SYSTEM = "5" ] ; then
echo "系统的版本是centos 5,该kafka应该安装在6的环境中"
exit 1
else
echo "What?"
fi
}
install_java(){
java -version
if [ $? -eq 0 ];then
echo '有 java环境'
else
echo '无java环境,安装java'
yum install java-1.8.0-openjdk.x86_64
fi
}
install_zookeep(){
#ZK中的一个时间单元。ZK中所有时间都是以这个时间单元为基础,进行整数倍配置的
tickTime=2000
#同步leader的时间
initLimit=10
#Leader负责与ZK集群中所有机器进行通信,例如通过一些心跳检测机制,来检测机器的存活状态
syncLimit=5
#zk的数据目录
dataDir="/data/logs/zkdata"
if [ -d $dataDir ];then
rm -fr $dataDir
fi
mkdir -pv $dataDir
#zk的日志目录
dataLogDir="/data/logs/zkdatalog"
if [ -d $dataLogDir ];then
rm -fr $dataLogDir
fi
mkdir -pv $dataLogDir
clientPort=2181
server.1=$1":4001:4002"
server.2=$2":4001:4002"
server.3=$3":4001:4002"
echo $server.1
cd /mnt/test
#wget https://www-eu.apache.org/dist/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
tar -zxvf zookeeper-3.4.14.tar.gz
if [ -d '/usr/local/zookeeper' ];then
rm -fr /usr/local/zookeeper
fi
mv zookeeper-3.4.14 /usr/local/zookeeper
cd /usr/local/zookeeper/conf
cat << EOF > zoo.cfg
tickTime=$tickTime
initLimit=$initLimit
syncLimit=$syncLimit
dataDir=$dataDir
dataLogDir=$dataLogDir
clientPort=$clientPort
server.1=$1:4001:4002
server.2=$2:4001:4002
server.3=$3:4001:4002
EOF
#创建myid file
cd $dataDir
id1=$4
cat << EOF > myid
$id1
EOF
/usr/local/zookeeper/bin/zkServer.sh start
netstat -tunlp |grep 2281
if [ $? -eq 0 ];then
echo 'zookeep 起来了'
else
echo 'zookeep 没起来'
fi
#设置开机自启动
grep zookeeper /etc/rc.d/rc.local
if [ $? -eq 0 ];then
echo 'zookeeper开机自启动已经设置';
else
echo '/usr/local/zookeeper/bin/zkServer.sh start' >> /etc/rc.d/rc.local
fi
}
install_kafka(){
cd /mnt/test
#wget https://www-us.apache.org/dist/kafka/2.2.0/kafka_2.12-2.2.0.tgz
tar -zxvf kafka_2.12-2.2.0.tgz
if [ -d '/usr/local/kafka' ];then
rm -fr /usr/local/kafka
fi
mv kafka_2.12-2.2.0 /usr/local/kafka
cd /usr/local/kafka/config/
mv server.properties /mnt/server.properties
cat << EOF > server.properties
broker.id=$4
listeners=PLAINTEXT://$1:9092
zookeeper.connect=$1:2181,$2:2181,$3:2181
log.retention.hours=72
log.segment.bytes=1073741824
log.flush.interval.ms=1000
replica.lag.time.max.ms:10000
replica.lag.max.messages:4000
num.replica.fetchers:1
default.replication.factor:2
acks:1
auto.create.topics.enable=true
EOF
/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/loca/kafka/config/server.properties
netstat -tunlp |grep 9092
if [ $? -eq 0 ];then
echo 'kafka起来了'
else
echo 'kafka没起来'
fi
#设置开机自启动
grep kafka /etc/rc.d/rc.local
if [ $? -eq 0 ];then
echo 'kafka已经设置开机自启动'
else
echo '/usr/local/kafka/bin/kafka-server-start.sh -daemon /usr/local/kafka/config/server.properties' >> /etc/rc.d/rc.local
fi
}
#install_supervesod(){
#yum install epel-release
#yum install supervisor
#}
usage(){
echo "usage:%s ip1 ip2 ip3 id1
ip: ip代表kafka集群的每个ip,请将本机ip赋值给ip1
id1:代表每个kafka实例的唯一id,每个实例的id一定不能相同
"
}
if [ $# -eq 4 ];then
system_version
install_java
install_zookeep $1 $2 $3 $4
install_kafka $1 $2 $3 $4
else
echo "参数输入有误,请重试"
usage
exit 1
fi