python脚本容器化运行

编写一个脚本,容器化跑起来,脚本的功能是调用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值