轻量化部署必看:在Docker或K8s节点上移除阿里云监控Agent的避坑实践
轻量化容器节点优化:彻底清理阿里云监控组件的技术指南
在容器化部署成为主流的今天,追求基础设施的极致轻量化已成为DevOps团队的核心诉求。特别是在使用阿里云ECS作为Kubernetes节点或Docker宿主机时,预装的监控组件往往成为资源占用和潜在冲突的来源。本文将深入探讨如何安全、彻底地移除这些组件,同时提供替代监控方案,确保集群稳定运行。
1. 为什么容器节点需要特殊处理
与传统服务器不同,容器化节点对资源利用率和系统稳定性有着更高要求。阿里云默认安装的监控组件虽然对普通ECS实例很有帮助,但在K8s或Docker环境中可能带来以下问题:
- 资源竞争:监控Agent常驻进程占用CPU和内存,影响容器调度效率
- 网络冲突:某些Agent可能与Calico、Flannel等CNI插件产生端口冲突
- 监控体系冗余:当集群已部署Prometheus+Granfana监控栈时,云监控数据变得多余
- 安全策略干扰:安骑士的防护规则可能与容器安全策略(如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_quartz |
2.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 网络插件冲突处理
常见网络插件与监控组件的端口冲突:
| 网络插件 | 可能冲突端口 | 解决方案 |
|---|---|---|
| Calico | 5473, 9099 | 卸载后重启calico-node Pod |
| Flannel | 6144, 8472 | 检查端口占用情况 |
| Cilium | 4240, 9090 | 验证健康检查端点 |
检查端口占用命令:
ss -tulnp | grep -E '5473|9099|6144|8472|4240|9090'5.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.packages"6. 替代监控方案实施
移除云监控后,需要建立适合容器环境的监控体系。
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_exporter | 15s | >80%持续5分钟 |
| 内存使用 | cadvisor | 30s | >90%持续2分钟 |
| 磁盘空间 | node_exporter | 1m | <10%剩余空间 |
| 网络流量 | kube-proxy | 15s | 突发增长300% |
6.3 告警规则示例
# alert.rules 示例 groups: - name: node.rules rules: - alert: HighNodeCPU expr: 100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[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{reason="OOMKilled"}[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 --image=alpine test-pod -- ping <other-pod-ip> # 服务发现验证 kubectl run -it --rm --image=busybox: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"
