轻量级智能监控平台k8s容器后部署
目录服务器配置获取项目代码构建并推送容器镜像编写Dockerfile构建镜像推送镜像到阿里仓库编写k8s资源清单configmapdaemonsetmysql部署后要创建表servicens和secret检查是否部署成功把阈值设定为1测试ai分析功能Prometheusgrafana实现监控数据可视化小结本项目是一套基于 K8s 的 Linux 集群主机监控告警系统。使用 Python 采集各节点 CPU、内存、磁盘、IO 等硬件指标数据同时输出给 Prometheus 做时序监控并存入 MySQL 留存历史。资源超出阈值会调用 DeepSeek 做 AI 故障分析并通过企业微信 webhook 推送告警搭配 Grafana 实现监控可视化大盘展示。服务器配置服务器序号角色内网 IP核心部署组件备注1监控服务器192.168.83.129Prometheus、Grafana、node-exporter宿主机、Alertmanager集群外部独立监控节点不加入 K8s 集群2K8s 主节点 / MySQL 节点192.168.83.11K8s Node、MySQL Deployment、node-monitor DaemonSet、node-exporter DaemonSet固定调度 MySQL本地磁盘存储数据3K8s 工作节点192.168.83.12K8s Node、node-monitor DaemonSet、node-exporter DaemonSet无固定业务仅运行监控 Pod4K8s 工作节点192.168.83.13K8s Node、node-monitor DaemonSet、node-exporter DaemonSet无固定业务仅运行监控 Pod获取项目代码git clone https://github.com/y114514-zero/Simple-system-detection-and-alerting---Linux.git cd Simple-system-detection-and-alerting---Linux构建并推送容器镜像编写Dockerfile[rootk8s-node01 Simple-system-detection-and-alerting---Linux]# cat Dockerfile FROM python:3.9-slim # 告警日志 ENV PYTHONUNBUFFERED1 # 设置时区 ENV TZAsia/Shanghai RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime echo $TZ /etc/timezone # 安装可能需要的依赖 RUN apt-get update apt-get install -y --no-install-recommends gcc python3-dev rm -rf /var/lib/apt/lists/* WORKDIR /app # 复制依赖文件并安装 COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # 复制项目代码 COPY . . # Prometheus 端口 EXPOSE 8000 # 直接运行主程序 CMD [python, monitor.py]构建镜像docker build -t simple-monitor:v1 . docker images | grep simple-monitor测试是否构建成功本地测试运行验证镜像是否正常 docker run -d --name test-monitor -p 8000:8000 simple-monitor:v1 查看日志 docker logs -f test-monitor 测试Prometheus指标端点 curl http://localhost:8000/metrics 如果测试通过停止并删除测试容器 docker stop test-monitor docker rm test-monitor推送镜像到阿里仓库1登录阿里云 Container Registry docker login --usernamenick2308359956 crpi-l986bg1ovxy9r4lx.cn-hongkong.personal.cr.aliyuncs.com 2docker tag simple-monitor:v1 crpi-l986bg1ovxy9r4lx.cn-hongkong.personal.cr.aliyuncs.com/nginx_laoli/monitor:v1 3docker push crpi-l986bg1ovxy9r4lx.cn-hongkong.personal.cr.aliyuncs.com/nginx_laoli/monitor:v1编写k8s资源清单configmap[rootk8s-node01 k8s]# cat configmap.yaml kind: ConfigMap apiVersion: v1 metadata: name: monitor-config namespace: monitoring data: # 硬件阈值 CPU_MAXUSE: 80 MEMORY_MAXUSE: 90 DISK_MAXUSE: 80 # 间隔 INTERVAL: 3 ALTER_INTERVAL: 5 DISK_PATH: / LOG_PATH: /dev/stdout # 输出地址 MYSQL_HOST: mysql-service # 集群内 MySQL Service 名称若外部则填 IP MYSQL_PORT: 3306 MYSQL_USER: monitorer MYSQL_DB: system_monitordaemonset[rootk8s-node01 k8s]# cat daemonset.yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: node-monitor namespace: monitoring labels: app: node-monitor spec: selector: matchLabels: app: node-monitor template: metadata: labels: app: node-monitor spec: hostAliases: - ip: 101.91.40.24 hostnames: - qyapi.weixin.qq.com - ip: 43.242.198.77 # 替换为实际可达 IP hostnames: - api.deepseek.com containers: - name: monitor image: crpi-l986bg1ovxy9r4lx.cn-hongkong.personal.cr.aliyuncs.com/nginx_laoli/monitor:v1 imagePullPolicy: Always volumeMounts: - name: proc mountPath: /host/proc readOnly: true - name: sys mountPath: /host/sys readOnly: true - name: rootfs mountPath: /host readOnly: true ports: - containerPort: 8000 name: metrics envFrom: # 注入 ConfigMap 和 Secret 的环境变量 - configMapRef: name: monitor-config - secretRef: name: monitor-secret securityContext: privileged: true # 必须特权模式才能读取宿主机的 /proc 和 /sys volumes: - name: proc hostPath: path: /proc - name: sys hostPath: path: /sys - name: rootfs hostPath: path: / tolerations: # 允许调度到所有节点包括 master如果需要 - operator: Existsmysql提前创建/data/mysql[rootk8s-node01 k8s]# cat mysql-deploy.yaml apiVersion: v1 kind: Service metadata: name: mysql-service namespace: monitoring labels: app: mysql spec: selector: app: mysql ports: - port: 3306 targetPort: 3306 --- apiVersion: apps/v1 kind: Deployment metadata: name: mysql namespace: monitoring spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: nodeSelector: kubernetes.io/hostname: k8s-node01 containers: - name: mysql image: mysql:5.7 env: - name: MYSQL_ROOT_PASSWORD value: root123 - name: MYSQL_DATABASE value: system_monitor - name: MYSQL_USER value: monitorer - name: MYSQL_PASSWORD value: 123456 ports: - containerPort: 3306 volumeMounts: - name: mysql-storage mountPath: /var/lib/mysql volumes: - name: mysql-storage hostPath: path: /data/mysql部署后要创建表kubectl exec -it -n monitoring deployment/mysql -- mysql -uroot -proot123 USE system_monitor; DROP TABLE IF EXISTS metrics; CREATE TABLE metrics ( id INT AUTO_INCREMENT PRIMARY KEY, timestamp DATETIME NOT NULL, cpu_usage DECIMAL(5,2), memory_usage DECIMAL(5,2), disk_usage DECIMAL(5,2), disk_read_mb DECIMAL(10,2), disk_write_mb DECIMAL(10,2), disk_read_count INT, disk_write_count INT, net_send_mb DECIMAL(10,2), net_resv_mb DECIMAL(10,2), INDEX idx_timestamp (timestamp) ); exit;service[rootk8s-node01 k8s]# cat service.yaml apiVersion: v1 kind: Service metadata: name: node-monitor namespace: monitoring labels: app: node-monitor spec: selector: app: node-monitor ports: - port: 8000 name: metrics targetPort: 8000 clusterIP: None # If you set the spec.type field to NodePort and you want a specific port number, # you can specify a value in the spec.ports[*].nodePort field.ns和secret[rootk8s-node01 k8s]# cat namespace.yaml apiVersion: v1 kind: Namespace metadata: name: monitoring [rootk8s-node01 k8s]# cat secret.yaml apiVersion: v1 kind: Secret metadata: name: monitor-secret namespace: monitoring type: Opaque stringData: MYSQL_PASSWORD: 123456 URL: https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key136dd1c2-d2d9-4f52-xxxxxe41597 #你的企业微信wehook地址 API_KEY: sk-623fcxxxxx7a6 deepseek密钥 API_URL: https://api.deepseek.com/v1 # 可选 MODEL_NAME: deepseek-v4-pro # 可选检查是否部署成功把阈值设定为1测试ai分析功能Prometheusgrafana实现监控数据可视化修改Prometheus.yaml[rootlocalhost prometheus]# cat prometheus.yml # my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: - localhost:9093 # Load rules once and periodically evaluate them according to the global evaluation_interval. rule_files: - alert.yml # - first_rules.yml # - second_rules.yml # A scrape configuration containing exactly one endpoint to scrape: # Here its Prometheus itself. scrape_configs: # The job name is added as a label jobjob_name to any timeseries scraped from this config. - job_name: prometheus # metrics_path defaults to /metrics # scheme defaults to http. static_configs: - targets: [localhost:9090] # The label name is added as a label label_namelabel_value to any timeseries scraped from this config. labels: app: prometheus # node-exporter配置 - job_name: node-exporter scrape_interval: 15s static_configs: - targets: - localhost:9100 # prometheus本机 - 192.168.83.11:9100 # k8s node01 - 192.168.83.12:9100 # k8s node02 - 192.168.83.13:9100 # k8s node03重启普罗米修斯部署node-exporter的pod[rootk8s-node01 k8s]# cat node-exporter-de.yaml apiVersion: apps/v1 kind: DaemonSet metadata: name: node-exporter namespace: monitoring spec: selector: matchLabels: app: node-exporter template: metadata: labels: app: node-exporter spec: hostNetwork: true tolerations: - operator: Exists containers: - name: node-exporter image: quay.io/prometheus/node-exporter:v1.8.2 ports: - containerPort: 9100 hostPort: 9100 resources: limits: cpu: 100m memory: 128Mi requests: cpu: 50m memory: 64Mi volumeMounts: - name: proc mountPath: /host/proc - name: sys mountPath: /host/sys volumes: - name: proc hostPath: path: /proc - name: sys hostPath: path: /sys部署成功进入grafana小结1:mysql最初使用localpath换节点可能导致数据丢失设置固定调度到node12:没有提前创建mysql文件夹导致mysqlpod一直启动失败3:mysql创建的表结构不对导致daemon po写入数据时一直失败导致循环重启

相关新闻

Kubernetes金丝雀发布实践与自动化方案

Kubernetes金丝雀发布实践与自动化方案

1. 金丝雀发布的核心价值与挑战 在分布式系统架构中,服务更新迭代的频率越来越高,如何安全高效地发布新版本成为每个运维团队必须面对的课题。金丝雀发布(Canary Release)这种灰度发布模式,通过将新版本服务像"矿…

2026/8/10 8:43:46 阅读更多 →
veRL强化学习框架:从模块化设计到生产级应用实战

veRL强化学习框架:从模块化设计到生产级应用实战

1. 从零到一:为什么我们需要一个新的强化学习框架?如果你在过去几年里尝试过将强化学习(RL)应用到实际项目中,无论是游戏AI、机器人控制还是推荐系统,大概率会和我有同样的感受:从理论到落地&am…

2026/8/10 8:43:46 阅读更多 →
使用Docker部署Unity CacheServer:团队资源导入加速实战指南

使用Docker部署Unity CacheServer:团队资源导入加速实战指南

1. 项目概述:为什么我们需要CacheServer?如果你在一个Unity团队里工作过,尤其是项目规模稍微大一点,美术资源动不动几十个G的那种,那你一定对“导入资源”这个环节又爱又恨。爱的是,每次修改完模型、贴图&a…

2026/8/10 8:43:46 阅读更多 →

最新新闻

UE5植被渲染:球形法线技术原理与实现,解决叶片扁平感

UE5植被渲染:球形法线技术原理与实现,解决叶片扁平感

1. 项目概述:为什么植物渲染需要球形法线? 如果你在UE5里做过植被,尤其是那种大片大片的森林或者草丛,肯定遇到过这样的头疼事:明明模型面数不低,法线贴图也画得挺细致,但叶子凑近了看&#xff…

2026/8/10 9:40:16 阅读更多 →
2026届专科生AI工具测评:降依赖度实用榜单

2026届专科生AI工具测评:降依赖度实用榜单

1. 项目背景与核心价值 作为一名长期关注职业教育与效率工具的研究者,我注意到2023年AI工具爆发式增长对专科生群体带来的双重影响。一方面,AI辅助工具能显著提升学习效率;另一方面,过度依赖AI可能导致基础能力退化。这份测评榜单…

2026/8/10 9:40:16 阅读更多 →
字母汤游戏上线竞技模式:与线上对手或朋友拼单词一决高下!

字母汤游戏上线竞技模式:与线上对手或朋友拼单词一决高下!

字母汤游戏竞技模式:多人单词拼写对战新玩法字母汤游戏推出了竞技模式,为玩家带来全新体验。在这个模式中,玩家既可以与线上对手对战,也能和朋友一起玩,还能进行与一位或多位朋友的私人对战。游戏的核心玩法是多人参与…

2026/8/10 9:40:16 阅读更多 →
罗技鼠标宏在FPS游戏后坐力控制中的技术实现与架构解析

罗技鼠标宏在FPS游戏后坐力控制中的技术实现与架构解析

罗技鼠标宏在FPS游戏后坐力控制中的技术实现与架构解析 【免费下载链接】logitech-pubg PUBG no recoil script for Logitech gaming mouse / 绝地求生 罗技 鼠标宏 项目地址: https://gitcode.com/gh_mirrors/lo/logitech-pubg 在竞技射击游戏中,武器后坐力…

2026/8/10 9:40:15 阅读更多 →
AI学习地图:从Vibe Coding小白入门,到理解并构建AI大模型的全路径

AI学习地图:从Vibe Coding小白入门,到理解并构建AI大模型的全路径

本文提供了一张AI学习地图,从Vibe Coding等工具入门,建立对AI能力的直觉,到学习Python、数据结构等计算机基础,理解大模型概念。随后分叉为应用方向(RAG、AI Agent等)和底层方向(Deep Learning、…

2026/8/10 9:40:15 阅读更多 →
Hitboxer终极指南:免费开源SOCD清洁工具,彻底解决游戏操作冲突

Hitboxer终极指南:免费开源SOCD清洁工具,彻底解决游戏操作冲突

Hitboxer终极指南:免费开源SOCD清洁工具,彻底解决游戏操作冲突 【免费下载链接】socd Key remapper for epic gamers 项目地址: https://gitcode.com/gh_mirrors/so/socd 你是否曾在激烈的游戏对战中,因为同时按下左右方向键而导致角色…

2026/8/10 9:39:15 阅读更多 →

日新闻

GraphQL-CSS API全解析:useGqlCSS、GqlCSS组件与getStyles实用指南

GraphQL-CSS API全解析:useGqlCSS、GqlCSS组件与getStyles实用指南

GraphQL-CSS API全解析:useGqlCSS、GqlCSS组件与getStyles实用指南 【免费下载链接】graphql-css A blazing fast CSS-in-GQL™ library. 项目地址: https://gitcode.com/gh_mirrors/gr/graphql-css GraphQL-CSS是一个基于GraphQL的CSS-in-GQL™库&#xff0…

2026/8/10 0:00:02 阅读更多 →
告别语言障碍:KISS Translator 双语翻译插件终极指南

告别语言障碍:KISS Translator 双语翻译插件终极指南

告别语言障碍:KISS Translator 双语翻译插件终极指南 【免费下载链接】kiss-translator A simple, open source bilingual translation extension & Greasemonkey script (一个简约、开源的 双语对照翻译扩展 & 油猴脚本) 项目地址: https://gitcode.com/…

2026/8/10 0:00:02 阅读更多 →
BepInEx配置管理器:游戏插件配置的终极可视化解决方案

BepInEx配置管理器:游戏插件配置的终极可视化解决方案

BepInEx配置管理器:游戏插件配置的终极可视化解决方案 【免费下载链接】BepInEx.ConfigurationManager Plugin configuration manager for BepInEx 项目地址: https://gitcode.com/gh_mirrors/be/BepInEx.ConfigurationManager 你是否曾经因为游戏插件的复杂…

2026/8/10 0:00:02 阅读更多 →

周新闻

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁 【免费下载链接】baidupankey 在线查询网盘提取码(维护中 rm repo) 项目地址: https://gitcode.com/gh_mirrors/ba/baidupankey 你是否曾经在深夜寻找一份重要资料&#x…

2026/8/10 1:05:29 阅读更多 →
如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南 【免费下载链接】chinese_license_plate_generator 中国车牌生成器 项目地址: https://gitcode.com/gh_mirrors/ch/chinese_license_plate_generator 中国车牌生成器是一个基于Python的开源项目&#xff0c…

2026/8/10 1:05:29 阅读更多 →
收藏!小白程序员轻松入门大模型,从Harness工程开始实践

收藏!小白程序员轻松入门大模型,从Harness工程开始实践

文章强调学习大模型不应只关注模型本身,而应重视模型外的系统搭建,即Harness。提出AgentModelHarness的实用公式,详细介绍Harness的四个层次:持久化层、执行层、控制层和观察与验证层。文章还探讨了上下文工程、工具设计、AGENTS.…

2026/8/10 1:05:29 阅读更多 →

月新闻

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南

免费解锁百度网盘SVIP加速:macOS用户必备的下载提速终极指南 【免费下载链接】BaiduNetdiskPlugin-macOS For macOS.百度网盘 破解SVIP、下载速度限制~ 项目地址: https://gitcode.com/gh_mirrors/ba/BaiduNetdiskPlugin-macOS 还在为百度网盘macOS版的龟速下…

2026/8/9 17:05:02 阅读更多 →
终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换

终极ncmdump指南:3分钟实现网易云NCM音乐解密与格式转换 【免费下载链接】ncmdump 项目地址: https://gitcode.com/gh_mirrors/ncmd/ncmdump 还在为网易云音乐下载的NCM格式文件无法在其他播放器播放而烦恼吗?ncmdump解密工具帮你轻松解决这个困…

2026/8/10 1:05:29 阅读更多 →
HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

HarmonyOS 应用开发《掌上英语》第81篇: 智能体卡片:为英语学习 App 打造桌面级学习助手

AgentCard 智能体卡片:为英语学习 App 打造桌面级学习助手适用平台:HarmonyOS 7.0 (API 26 Beta)一、引言 HarmonyOS 7.0(API 26 Beta)新增了 AgentCard 智能体卡片能力,这是继 HMAF(鸿蒙智能体框架&#x…

2026/8/9 17:05:02 阅读更多 →