编写一个脚本,容器化跑起来,脚本的功能是调用rabbitmq的接口,然后获取queue的信息,并最终打印成json的格式,最后通过lokiql将json提取,并alert告警发送出来
第一:app.py脚本内容
# _*_ coding:UTF-8 _*_
#! /usr/bin/python
import time
import requests
import json
import subprocess
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
logger.addHandler(stream_handler)
while True:
command = "curl -u username:password -s 'http://ip:port/api/queues?page=1&page_size=100&name=data_processing&use_regex=false&pagination=true'"
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
# zifu_out = str(out)
my_dict = json.loads(out)
# print(my_dict["items"])
new_my_dict = my_dict["items"]
# print(new_my_dict["name"])
for i in new_my_dict:
total = i["messages_ready_ram"] + i["messages_unacknowledged"]
queue_name = i["name"]
zhanshi = queue_name + ":" + str(total)
#print(zhanshi)
#print("\n")
#logger.info(zhanshi)
data = {'name':queue_name,'num':total}
json_str = json.dumps(data)
logger.info("\n"+json_str)
time.sleep(300)
第二:制作docker file 然后deployment部署,然后容器化跑起来
目录下有
ls -a
. .. Dockerfile app.py
Dockerfile内容
cat Dockerfile
FROM xxx #【某个基础镜像】
WORKDIR /app
COPY . /app
RUN pip3 install requests。 #python文件中缺少request模块
CMD ["python3","app.py"]
构建镜像
docker build -t 镜像name .
测试镜像
docker run -itd
登录
docker login
推送镜像
docker push
构建deployment
kind: Deployment
apiVersion: apps/v1
metadata:
name: jiankong
namespace: aip-tencent-prod
labels:
app: jiankong
spec:
replicas: 2
selector:
matchLabels:
app: jiankong
template:
metadata:
creationTimestamp: null
labels:
app: jiankong
spec:
containers:
- name: jiankong
image: imagename
resources:
limits:
cpu: '2'
memory: 2Gi
requests:
cpu: '1'
memory: 1Gi
imagePullPolicy: IfNotPresent
securityContext:
capabilities: {}
restartPolicy: Always
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirst
securityContext: {}
imagePullSecrets:
- name: harbor-token
schedulerName: default-scheduler
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 50%
maxSurge: 0
revisionHistoryLimit: 10
progressDeadlineSeconds: 600