grafana和prometheus系列五:grafana 性能监控通过kafka发送告警消息

一、通过docker-compose安装confluent(暂时放弃、服务的确能启起来,但是测试连接不成功。更换一种方式)

confluent包含kafka,zookeeper,broker,kafka-rest-proxy等。

官网操作步骤地址:https://docs.confluent.io/platform/current/quickstart/ce-docker-quickstart.html#cos-docker-quickstart

1、vim docker-compose.yml

version: '2'
services:
 zookeeper:
 image: confluentinc/cp-zookeeper:7.1.1
 hostname: zookeeper
 container_name: zookeeper
 ports:
 - "2181:2181"
 environment:
 ZOOKEEPER_CLIENT_PORT: 2181
 ZOOKEEPER_TICK_TIME: 2000

 broker:
 image: confluentinc/cp-server:7.1.1
 hostname: broker
 container_name: broker
 depends_on:
 - zookeeper
 ports:
 - "9092:9092"
 - "9101:9101"
 environment:
 KAFKA_BROKER_ID: 1
 KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
 KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
 KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
 KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter
 KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
 KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
 KAFKA_CONFLUENT_LICENSE_TOPIC_REPLICATION_FACTOR: 1
 KAFKA_CONFLUENT_BALANCER_TOPIC_REPLICATION_FACTOR: 1
 KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
 KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
 KAFKA_JMX_PORT: 9101
 KAFKA_JMX_HOSTNAME: localhost
 KAFKA_CONFLUENT_SCHEMA_REGISTRY_URL: http://schema-registry:8081
 CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:29092
 CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1
 CONFLUENT_METRICS_ENABLE: 'true'
 CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous'

 schema-registry:
 image: confluentinc/cp-schema-registry:7.1.1
 hostname: schema-registry
 container_name: schema-registry
 depends_on:
 - broker
 ports:
 - "8081:8081"
 environment:
 SCHEMA_REGISTRY_HOST_NAME: schema-registry
 SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS: 'broker:29092'
 SCHEMA_REGISTRY_LISTENERS: http://0.0.0.0:8081

 connect:
 image: cnfldemos/cp-server-connect-datagen:0.5.3-7.1.0
 hostname: connect
 container_name: connect
 depends_on:
 - broker
 - schema-registry
 ports:
 - "8083:8083"
 environment:
 CONNECT_BOOTSTRAP_SERVERS: 'broker:29092'
 CONNECT_REST_ADVERTISED_HOST_NAME: connect
 CONNECT_GROUP_ID: compose-connect-group
 CONNECT_CONFIG_STORAGE_TOPIC: docker-connect-configs
 CONNECT_CONFIG_STORAGE_REPLICATION_FACTOR: 1
 CONNECT_OFFSET_FLUSH_INTERVAL_MS: 10000
 CONNECT_OFFSET_STORAGE_TOPIC: docker-connect-offsets
 CONNECT_OFFSET_STORAGE_REPLICATION_FACTOR: 1
 CONNECT_STATUS_STORAGE_TOPIC: docker-connect-status
 CONNECT_STATUS_STORAGE_REPLICATION_FACTOR: 1
 CONNECT_KEY_CONVERTER: org.apache.kafka.connect.storage.StringConverter
 CONNECT_VALUE_CONVERTER: io.confluent.connect.avro.AvroConverter
 CONNECT_VALUE_CONVERTER_SCHEMA_REGISTRY_URL: http://schema-registry:8081
 # CLASSPATH required due to CC-2422
 CLASSPATH: /usr/share/java/monitoring-interceptors/monitoring-interceptors-7.1.1.jar
 CONNECT_PRODUCER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor"
 CONNECT_CONSUMER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor"
 CONNECT_PLUGIN_PATH: "/usr/share/java,/usr/share/confluent-hub-components"
 CONNECT_LOG4J_LOGGERS: org.apache.zookeeper=ERROR,org.I0Itec.zkclient=ERROR,org.reflections=ERROR

 control-center:
 image: confluentinc/cp-enterprise-control-center:7.1.1
 hostname: control-center
 container_name: control-center
 depends_on:
 - broker
 - schema-registry
 - connect
 - ksqldb-server
 ports:
 - "9021:9021"
 environment:
 CONTROL_CENTER_BOOTSTRAP_SERVERS: 'broker:29092'
 CONTROL_CENTER_CONNECT_CONNECT-DEFAULT_CLUSTER: 'connect:8083'
 CONTROL_CENTER_KSQL_KSQLDB1_URL: "http://ksqldb-server:8088"
 CONTROL_CENTER_KSQL_KSQLDB1_ADVERTISED_URL: "http://localhost:8088"
 CONTROL_CENTER_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
 CONTROL_CENTER_REPLICATION_FACTOR: 1
 CONTROL_CENTER_INTERNAL_TOPICS_PARTITIONS: 1
 CONTROL_CENTER_MONITORING_INTERCEPTOR_TOPIC_PARTITIONS: 1
 CONFLUENT_METRICS_TOPIC_REPLICATION: 1
 PORT: 9021

 ksqldb-server:
 image: confluentinc/cp-ksqldb-server:7.1.1
 hostname: ksqldb-server
 container_name: ksqldb-server
 depends_on:
 - broker
 - connect
 ports:
 - "8088:8088"
 environment:
 KSQL_CONFIG_DIR: "/etc/ksql"
 KSQL_BOOTSTRAP_SERVERS: "broker:29092"
 KSQL_HOST_NAME: ksqldb-server
 KSQL_LISTENERS: "http://0.0.0.0:8088"
 KSQL_CACHE_MAX_BYTES_BUFFERING: 0
 KSQL_KSQL_SCHEMA_REGISTRY_URL: "http://schema-registry:8081"
 KSQL_PRODUCER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringProducerInterceptor"
 KSQL_CONSUMER_INTERCEPTOR_CLASSES: "io.confluent.monitoring.clients.interceptor.MonitoringConsumerInterceptor"
 KSQL_KSQL_CONNECT_URL: "http://connect:8083"
 KSQL_KSQL_LOGGING_PROCESSING_TOPIC_REPLICATION_FACTOR: 1
 KSQL_KSQL_LOGGING_PROCESSING_TOPIC_AUTO_CREATE: 'true'
 KSQL_KSQL_LOGGING_PROCESSING_STREAM_AUTO_CREATE: 'true'

 ksqldb-cli:
 image: confluentinc/cp-ksqldb-cli:7.1.1
 container_name: ksqldb-cli
 depends_on:
 - broker
 - connect
 - ksqldb-server
 entrypoint: /bin/sh
 tty: true

 ksql-datagen:
 image: confluentinc/ksqldb-examples:7.1.1
 hostname: ksql-datagen
 container_name: ksql-datagen
 depends_on:
 - ksqldb-server
 - broker
 - schema-registry
 - connect
 command: "bash -c 'echo Waiting for Kafka to be ready... && \
 cub kafka-ready -b broker:29092 1 40 && \
 echo Waiting for Confluent Schema Registry to be ready... && \
 cub sr-ready schema-registry 8081 40 && \
 echo Waiting a few seconds for topic creation to finish... && \
 sleep 11 && \
 tail -f /dev/null'"
 environment:
 KSQL_CONFIG_DIR: "/etc/ksql"
 STREAMS_BOOTSTRAP_SERVERS: broker:29092
 STREAMS_SCHEMA_REGISTRY_HOST: schema-registry
 STREAMS_SCHEMA_REGISTRY_PORT: 8081

 rest-proxy:
 image: confluentinc/cp-kafka-rest:7.1.1
 depends_on:
 - broker
 - schema-registry
 ports:
 - 8082:8082
 hostname: rest-proxy
 container_name: rest-proxy
 environment:
 KAFKA_REST_HOST_NAME: rest-proxy
 KAFKA_REST_BOOTSTRAP_SERVERS: 'broker:29092'
 KAFKA_REST_LISTENERS: "http://0.0.0.0:8082"
 KAFKA_REST_SCHEMA_REGISTRY_URL: 'http://schema-registry:8081'

2、启动

docker-compose up -d
image-20220517174154219.png

拉取成功并启动会显示上面的结果。

3、重启

docker-compose ps可以查看当前容器

docker-compose restart container_name可以重启某容器

二、通过已配置好的jar包启动kafka-server、zookeeper、kafka-rest。

1、解压confluent-oss-4.0.0-2.11.tar.gz包。这个是打好的jar包。

mkdir -p kafka_oss 
chmod -R 777 kafka_oss
cd kafka_oss 
tar -zxvf confluent-oss-4.0.0-2.11.tar.gz
cd confluent-4.0.0/
cd bin/

2、按先后顺序启动zookeeper-server-start,kafka-server-start,kafka-rest-start

nohup ./zookeeper-server-start ../etc/kafka/zookeeper.properties >zookeeper-run.log 2>&1 &
nohup ./kafka-server-start ../etc/kafka/server.properties >kafka-server-run.log 2>&1 &
nohup ./kafka-rest-start ../etc/kafka-rest/kafka-rest.properties >kafka-rest-run.log 2>&1 &

3、查看启动状态

ps -ef|grep zookeeper
ps -ef|grep kafka-server
ps -ef|grep kafka-rest

4、查看topic

访问http://localhost:8082/topics

5、命令行消费

./kafka-console-consumer --bootstrap-server localhost:9092 --topic test-iot --from-beginning

三、grafana配置告警

1、配置kafka告警

image-20220517172952991.png

点击告警设置,创建告警通道

image-20220517173049272.png

使用的是kafka Rest Proxy来发送告警消息

配置的地址就是我们上面安装的kafka-rest-proxy地址。

点击Test按钮即是创建当前topic。并且弹出绿色提示框标明连接成功。

可以访问:http://localhost:8082/topics查询当前所有的topic。

image-20220517201900666.png

或者访问:http://localhost:8082/topics/test-iot查询指定topic为test-iot的主题。

image-20220517201951261.png

2、设置默认通道

image-20220517202048229.png

设置刚才我们创建的通道为默认通道

3、创建告警规则

点击进入我们创建好的性能告警dashboar。点击某个panel编辑。

image-20220517202219036.png

点击edit。进入之后点击Alert,新增一个报警

image-20220517202300794.png

名字自定义,Folder需要新建一个。

image-20220517202416618.png

创建一条规则

image-20220517202525559.png

点击蓝色Run queries

可以查看上面A-I对应的告警值和性能值。

image-20220517202838757.png

image-20220517202954063.png

添加好之后如下显示

image-20220517203106673.png

4、查看告警推送

此时默认告警就会传输到kafka。

通过命令可以查看

./kafka-console-consumer --bootstrap-server localhost:9092 --topic test-iot --from-beginning
image-20220517203250341.png

如图可以看到kafka消费行里面接收到的告警消息。

四、将kafka数据消费并存入边代告警表。

后端开发人员可以在java断实现kafka消费者并消费数据存入数据库。

你可能感兴趣的:(grafana和prometheus系列五:grafana 性能监控通过kafka发送告警消息)