Prometheus监控k8s中Pod使用Shell模拟消耗CPU资源

近期在做Prometheus监控K8S中Pod的报警,部署完成后需要进行测试,所以我在K8S中国默认命名空间下创建了一个buxybox的容器,进入容器后使用脚本来模拟CPU资源使用过高而触发报警。具体实施如下:

prometheus-rules.yaml文件:

  pod.rules: |
    groups:
    - name: pod.rules
      rules:
      - alert: PodCPUUsage
        expr: |
           (sum(rate(container_cpu_usage_seconds_total{image!=""}[2m])) by (pod,namespace)/sum(kube_pod_container_resource_limits_cpu_cores) by (pod,namespace)  * 100) > 80
        for: 5m
        labels:
          severity: warning 
        annotations:
          summary: "命名空间: {{ $labels.namespace }} | Pod名称: {{ $labels.pod }} CPU使用大于5% (当前值: {{ $value }})"


busybox的yaml文件:

# cat testpod.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  labels:
    app: busybox
spec:
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
      - name: busybox
        image: busybox
        command:
          - sleep
          - "360000"
        resources:
          limits:
            cpu: 200m
            memory: 1500Mi
          requests:
            cpu: 200m
            memory: 1000Mi
kubectl exec -it -n default busybox-74d8c6c4b9-kpnqz -- sh       #进入pod

脚本内容:

~ # cat test.sh 
#! /bin/bash
for i in ‘seq $1’
do
echo -ne "
i=0;
while true
do
i=i+1;
done" | /bin/sh &
done

运行:

sh test.sh 1     #这个1代表消耗1个CPU资源,根据需求修改。

 top中CPU使用率已经达到了百分之二十以上,因为我的pod设置的requests值为200M,即1核CPU的百分之二十,所有当这里显示百分之二十以上的时候使用率已经是百分之百了。

Grafana中可以直观的看到。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值