基于Spark streaming的SQL服务实时自动化运维

本文介绍了一种使用Spark Streaming实时监控Spark ThriftServer日志的方法,通过Flume收集日志并发送到Kafka,Spark Streaming根据关键词判断服务状态,并通过Shell脚本实现自动重启。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

####设计背景
spark thriftserver目前线上有10个实例,以往通过监控端口存活的方式很不准确,当出故障时进程不退出情况很多,而手动去查看日志再重启处理服务这个过程很低效,故设计利用Spark streaming去实时获取spark thriftserver的log,通过log判断服务是否停止服务,从而进行对应的自动重启处理,该方案能达到秒级 7 * 24h不间断监控及维护服务。

设计架构

这里写图片描述

  • 在需要检测的spark thriftserver服务节点上部署flume agent来监控日志流 (flume使用interceptor给日志加host信息)
  • flume收集的日志流打入kafka
  • spark streaming接收kafka的日志流,根据自定义关键词检测日志内容,如果命中关键字则认为服务不可用,把该日志对应的host信息打入mysql
  • 写一个shell脚本从mysql读取host信息,执行重启服务操作

####软件版本及配置
spark 2.0.1, kafka 0.10, flume 1.7

####1)flume配置及命令:
修改flume-conf.properties

agent.sources = sparkTS070
agent.channels = c
agent.sinks = kafkaSink
# For each one of the sources, the type is defined
agent.sources.sparkTS070.type = TAILDIR

agent.sources.sparkTS070.interceptors = i1
agent.sources.sparkTS070.interceptors.i1.type = host
agent.sources.sparkTS070.interceptors.i1.useIP = false
agent.sources.sparkTS070.interceptors.i1.hostHeader = agentHost

# The channel can be defined as follows.
agent.sources.sparkTS070.channels = c
agent.sources.sparkTS070.positionFile = /home/hadoop/xu.wenchun/apache-flume-1.7.0-bin/taildir_position.json
agent.sources.sparkTS070.filegroups = f1
agent.sources.sparkTS070.filegroups.f1 = /data1/spark/logs/spark-hadoop-org.apache.spark.sql.hive.thriftserver.HiveThriftServer2-1-hadoop070.dx.com.out

# Each sink's type must be defined
agent.sinks.kafkaSink.type = org.apache.flume.sink.kafka.KafkaSink
agent.sinks.kafkaSink.kafka.topic = mytest-topic1
agent.sinks.kafkaSink.kafka.bootstrap.servers = 10.87.202.51:9092
agent.sinks.kafkaSink.useFlumeEventFormat = true

#Specify the channel the sink should use
agent.sinks.kafkaSink.channel = c

# Each channel's type is defined.
agent.channels.c.type = memory

运行命令:
nohup bin/flume-ng agent -n agent -c conf -f conf/flume-conf.properties -Dflume.root.logger=INFO,LOGFILE &

####2)kafka配置及执行命令:
修改config/server.properties

broker.id=1
listeners=PLAINTEXT://10.87.202.51:9092
log.dirs=/home/hadoop/xu.wenchun/kafka_2.11-0.10.0.1/kafka.log
zookeeper.connect=10.87.202.44:2181,10.87.202.51:2181,10.87.202.52:2181

运行命令
nohup bin/kafka-server-start.sh config/server.properties &

####spark streaming执行命令 :
/opt/spark-2.0.1-bin-2.6.0/bin/spark-submit --master yarn-cluster --num-executors 3 --class SparkTSLogMonitor /tmp/mavenSparkProject.jar 10.87.202.51:9092 mytest-topic1

3)shell脚本

写一个shell脚本从mysql读取host信息,执行重启服务操作

####spark streaming监控job的核心代码
这类分享spark streaming代码,以下代码经过一些坑摸索出来验证可用。

      stream.foreachRDD { rdd =>
        rdd.foreachPartition { rddOfPartition =>
          val conn = ConnectPool.getConnection
          println(" conn:" + conn)
          conn.setAutoCommit(false)  //设为手动提交
          val  stmt = conn.createStatement()
          rddOfPartition.foreach { event =>
            val body = event.value().get()
            val decoder = DecoderFactory.get().binaryDecoder(body, null)
            val result = new SpecificDatumReader[AvroFlumeEvent](classOf[AvroFlumeEvent]).read(null, decoder)
            val hostname = result.getHeaders.get(new Utf8("agentHost"))
            val text = new String(result.getBody.array())

            if (text.contains("Broken pipe") || text.contains("No active SparkContext")) {
              val dateFormat:SimpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmssSSS")
              val id = dateFormat.format(new Date()) + "_" + (new util.Random).nextInt(999)
              stmt.addBatch("insert into monitor(id,hostname) values ('" + id + "','" + hostname + "')")
              println("insert into monitor(id,hostname) values ('" + id + "','" + hostname + "')")
            }
          }
          stmt.executeBatch()
          conn.commit()
          conn.close()
        }
      }

没有监控的特例情况:服务jvm老年代满了,日志不打出来,在yarn上的注册服务挂掉。

如果代码报类找不到,建议单独下载jar包:
kafka-clients-0.10.0.1.jar
spark-streaming-kafka-0-10_2.11-2.0.1.jar
flume-ng-sdk

####在kafka所在机器执行命令查offset情况

  • bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --zookeeper localhost:2181 --group myspark --topic mytest-topic1 (ZooKeeper-based consumers)
  • bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --group myspark (non-ZooKeeper-based consumers)

(完)

以上是一个实时处理的典型入门应用,我个人工作中刚好遇到这类监控运维问题,于是采用该方案进行处理,效果不错。

文章会同步到公众号,关注公众号,交流更方便:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值