一、启动项目
[k8s-dev-test@rancher-k8s-conn ~]$ curl http://192.168.1.29:5000
二、编写接口
2.1 告警格式查看
https://skywalking.apache.org/docs/main/v9.1.0/en/setup/backend/backend-alarm/
[{
"scopeId": 1,
"scope": "SERVICE",
"name": "serviceA",
"id0": "12",
"id1": "",
"ruleName": "service_resp_time_rule",
"alarmMessage": "alarmMessage xxxx",
"startTime": 1560524171000,
"tags": [{
"key": "level",
"value": "WARNING"
}]
}, {
"scopeId": 1,
"scope": "SERVICE",
"name": "serviceB",
"id0": "23",
"id1": "",
"ruleName": "service_resp_time_rule",
"alarmMessage": "alarmMessage yyy",
"startTime": 1560524171000,
"tags": [{
"key": "level",
"value": "CRITICAL"
}]
}]
# 梳理:从这里可以看出来它是一个事件以列表的形式提交过来,我要做的只是过滤一些我想屏蔽的事件(官方的屏蔽服务功能用起来不方便)
2.2 测试post提交数据
In [8]: import requests
In [9]: import json
In [10]: user_info = {'name': 'letian', 'password': '123'}
In [11]: r = requests.post("http://192.168.1.29:5000/skywalking-alarm", data=json.dumps(user_info))
In [14]: print(r.text)
2.3 接口代码
[root@ip-172-20-21-242 alarm-webhook]# cat app.py
@app.route('/skywalking-alarm', methods=['POST'])
def skywalking_alarm(): # put application's code here
data_list = request.data
alarm = SkywalkingAlarmMain(json.loads(data_list))
ret = alarm.alarm()
return 'Hello World!'
代码参考地址
https://gitee.com/xiangys0134/deploy/tree/master/flask/alarm-webhook
三、构建镜像
[root@ip-172-20-21-242 alarm-webhook]# cat Dockerfile
FROM python:3.10.9
# 镜像的作者
LABEL maintainer="<250919938@qq.com>"
COPY . /app
ARG REGISTRY=https://pypi.org/simple
RUN pip install --upgrade pip \
&& pip install -i ${REGISTRY} -r /app/requirements.txt
WORKDIR /app
ENTRYPOINT ["python -m flask run --host=0.0.0.0"]
# 构建镜像
[root@ip-172-20-21-242 alarm-webhook]# docker build -t xiangys0134/devtoolset:flask-alarm-webhook-v0.1 .
[root@ip-172-20-21-242 alarm-webhook]# docker push xiangys0134/devtoolset:flask-alarm-webhook-v0.1
四、创建helm包
[k8s-prod-hk-2023@ip-172-20-21-242 bak]helm create flask-api
[k8s-prod-hk-2023@ip-172-20-21-242 flask-api] helm upgrade -i sw-alarm-webhook -n monitoring .
留言