轻量化部署必看:在Docker或K8s节点上移除阿里云监控Agent的避坑实践

张开发
2026/4/21 21:01:08 15 分钟阅读

分享文章

轻量化部署必看:在Docker或K8s节点上移除阿里云监控Agent的避坑实践
轻量化容器节点优化彻底清理阿里云监控组件的技术指南在容器化部署成为主流的今天追求基础设施的极致轻量化已成为DevOps团队的核心诉求。特别是在使用阿里云ECS作为Kubernetes节点或Docker宿主机时预装的监控组件往往成为资源占用和潜在冲突的来源。本文将深入探讨如何安全、彻底地移除这些组件同时提供替代监控方案确保集群稳定运行。1. 为什么容器节点需要特殊处理与传统服务器不同容器化节点对资源利用率和系统稳定性有着更高要求。阿里云默认安装的监控组件虽然对普通ECS实例很有帮助但在K8s或Docker环境中可能带来以下问题资源竞争监控Agent常驻进程占用CPU和内存影响容器调度效率网络冲突某些Agent可能与Calico、Flannel等CNI插件产生端口冲突监控体系冗余当集群已部署PrometheusGranfana监控栈时云监控数据变得多余安全策略干扰安骑士的防护规则可能与容器安全策略如PodSecurityPolicy产生冲突典型症状包括节点频繁进入NotReady状态容器网络连接异常系统负载异常升高监控数据重复或冲突2. 组件识别与卸载前准备2.1 确定已安装的监控组件在开始卸载前需要准确识别当前系统安装的监控组件及其版本# 检查云监控组件 ls -l /usr/local/cloudmonitor/ # 检查安骑士组件 ls -l /usr/local/aegis/常见组件类型及特征组件名称版本类型关键路径主要进程云监控Go版/usr/local/cloudmonitor/CmsGoAgent*CmsGoAgent.linux-amd64云监控Java版/usr/local/cloudmonitor/wrapper/cloudmonitor.sh安骑士新版/usr/local/aegis/aegis_client安骑士旧版/usr/local/aegis/quartz/aegis_quartz2.2 卸载前的必要准备备份关键配置mkdir -p ~/aliyun-agent-backup cp -r /usr/local/cloudmonitor/ ~/aliyun-agent-backup/ cp -r /usr/local/aegis/ ~/aliyun-agent-backup/记录当前监控状态systemctl list-units | grep -E cloudmonitor|aegis|aliyun ps aux | grep -E CmsGoAgent|aegis|aliyun通知相关团队确保卸载操作不会影响现有的监控告警流程3. 彻底卸载云监控组件根据识别到的组件版本选择对应的卸载方式。3.1 卸载Go版云监控对于现代Linux系统通常安装的是Go语言版本的云监控# 停止服务 /usr/local/cloudmonitor/CmsGoAgent.linux-$(uname -m | sed s/x86_64/amd64/;s/i[3-6]86/386/) stop # 卸载服务 /usr/local/cloudmonitor/CmsGoAgent.linux-$(uname -m | sed s/x86_64/amd64/;s/i[3-6]86/386/) uninstall # 清理残留 rm -rf /usr/local/cloudmonitor注意如果系统架构为ARM可能需要将命令中的amd64替换为对应的ARM架构标识3.2 卸载Java版云监控较旧的系统可能仍在使用Java版本# 停止服务 /usr/local/cloudmonitor/wrapper/bin/cloudmonitor.sh stop # 卸载服务 /usr/local/cloudmonitor/wrapper/bin/cloudmonitor.sh remove # 清理残留 rm -rf /usr/local/cloudmonitor3.3 验证卸载结果执行以下命令确认卸载是否彻底# 检查进程 pgrep -f cloudmonitor|CmsGoAgent || echo 未找到云监控进程 # 检查系统服务 systemctl list-units | grep cloudmonitor || echo 未找到云监控服务 # 检查文件残留 ls -l /usr/local/cloudmonitor 2/dev/null || echo 无文件残留4. 彻底卸载安骑士组件安骑士的卸载相对复杂需要多个步骤确保完全清除。4.1 使用官方卸载脚本# 下载并执行卸载脚本 wget http://update.aegis.aliyun.com/download/uninstall.sh -O /tmp/uninstall.sh chmod x /tmp/uninstall.sh /tmp/uninstall.sh wget http://update.aegis.aliyun.com/download/quartz_uninstall.sh -O /tmp/quartz_uninstall.sh chmod x /tmp/quartz_uninstall.sh /tmp/quartz_uninstall.sh4.2 手动清理残留脚本执行后还需手动清理以下内容# 删除残留文件和目录 sudo rm -rf /usr/local/aegis sudo rm -f /usr/sbin/aliyun-service sudo rm -f /lib/systemd/system/aliyun.service # 清理crontab任务 sudo sed -i /aegis/d /etc/crontab4.3 验证卸载结果# 检查进程 pgrep -f aegis|aliyun-service || echo 未找到安骑士进程 # 检查系统服务 systemctl list-units | grep -E aegis|aliyun || echo 未找到相关服务 # 检查文件残留 ls -l /usr/local/aegis 2/dev/null || echo 无文件残留5. 容器环境下的特殊考量在Kubernetes节点或Docker主机上执行卸载时需要特别注意以下方面5.1 systemd与cgroupv2的兼容问题现代容器运行时可能使用cgroupv2而某些监控组件可能不兼容# 检查当前cgroup版本 stat -fc %T /sys/fs/cgroup/ # 如果返回cgroup2fs表示使用cgroupv2解决方案在卸载前停止所有容器工作负载重启节点使变更生效验证容器运行时功能是否正常5.2 网络插件冲突处理常见网络插件与监控组件的端口冲突网络插件可能冲突端口解决方案Calico5473, 9099卸载后重启calico-node PodFlannel6144, 8472检查端口占用情况Cilium4240, 9090验证健康检查端点检查端口占用命令ss -tulnp | grep -E 5473|9099|6144|8472|4240|90905.3 批量卸载的Ansible方案对于大规模集群可以使用Ansible进行批量卸载--- - name: Remove Aliyun monitoring agents hosts: k8s_nodes become: yes tasks: - name: Stop and remove CloudMonitor block: - name: Stop CloudMonitor service command: /usr/local/cloudmonitor/CmsGoAgent.linux-{{ ansible_architecture }} stop ignore_errors: yes when: cloudmonitor in ansible_facts.packages - name: Uninstall CloudMonitor command: /usr/local/cloudmonitor/CmsGoAgent.linux-{{ ansible_architecture }} uninstall ignore_errors: yes - name: Remove CloudMonitor files file: path: /usr/local/cloudmonitor state: absent when: cloudmonitor in ansible_facts.packages - name: Download and execute Aegis uninstall scripts block: - name: Download uninstall scripts get_url: url: http://update.aegis.aliyun.com/download/{{ item }} dest: /tmp/{{ item }} mode: 0755 loop: - uninstall.sh - quartz_uninstall.sh - name: Execute uninstall scripts command: /tmp/{{ item }} loop: - uninstall.sh - quartz_uninstall.sh - name: Clean up residual files file: path: {{ item }} state: absent loop: - /usr/local/aegis - /usr/sbin/aliyun-service - /lib/systemd/system/aliyun.service when: aegis in ansible_facts.packages6. 替代监控方案实施移除云监控后需要建立适合容器环境的监控体系。6.1 Prometheus监控方案推荐的基础监控指标采集配置# prometheus.yml 片段 scrape_configs: - job_name: node static_configs: - targets: [node-exporter:9100] - job_name: cadvisor static_configs: - targets: [cadvisor:8080] - job_name: kubelet scheme: https tls_config: insecure_skip_verify: true static_configs: - targets: [kubelet:10250]6.2 关键监控指标对比云监控指标替代方案采集频率告警阈值建议CPU使用率node_exporter15s80%持续5分钟内存使用cadvisor30s90%持续2分钟磁盘空间node_exporter1m10%剩余空间网络流量kube-proxy15s突发增长300%6.3 告警规则示例# alert.rules 示例 groups: - name: node.rules rules: - alert: HighNodeCPU expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{modeidle}[5m])) * 100) 80 for: 5m labels: severity: warning annotations: summary: High CPU usage on {{ $labels.instance }} description: CPU usage is {{ $value }}% - alert: ContainerOOMKilled expr: increase(kube_pod_container_status_last_terminated_reason{reasonOOMKilled}[1h]) 0 labels: severity: critical annotations: summary: Container OOM killed in {{ $labels.namespace }}/{{ $labels.pod }}7. 卸载后的验证与调优完成所有卸载操作后需要进行全面验证。7.1 系统资源释放情况检查# 比较卸载前后的资源使用 free -h df -h top -n1 -b | head -107.2 容器网络功能测试# 跨节点Pod连通性测试 kubectl run -it --rm --imagealpine test-pod -- ping other-pod-ip # 服务发现验证 kubectl run -it --rm --imagebusybox:1.28 test-dns -- nslookup kubernetes.default7.3 性能调优建议内核参数优化# 增加连接跟踪表大小 echo 524288 /proc/sys/net/netfilter/nf_conntrack_max # 优化TCP栈 echo net.ipv4.tcp_tw_reuse 1 /etc/sysctl.conf sysctl -p容器运行时调优# Docker配置示例 { default-ulimits: { nofile: { Name: nofile, Hard: 65535, Soft: 65535 } }, live-restore: true }Kubelet资源预留# /var/lib/kubelet/config.yaml systemReserved: cpu: 500m memory: 1Gi kubeReserved: cpu: 500m memory: 1Gi

更多文章