1. 在开始任何操作前,先确认问题范围(单机/整机群、网络/服务层)。推荐按顺序:网络连通 → 服务端口 → 资源使用 → 日志排查 → 应用内部状态。每一步都记录时间和命令输出以便回溯。
2. 通过本地机器尝试 ssh 登录:ssh -i /path/key root@IP。若无法登录,先从本地运行 ping IP、traceroute -n IP。如果 ICMP 全丢包,切换到云控制台查看主机状态(是否处于停机、过载、网络限制)。必要时重置云主机网络或控制台串口登录。
3. 在能 SSH 的前提下,检查网络接口与路由:ip addr show; ip route show; ethtool eth0(查看链路状态)。若怀疑丢包用 tcpdump -i eth0 host 目标IP -w /tmp/cap.pcap,再用 Wireshark 分析或 tcpdump -nn -A 'tcp port 80'。
4. 检查端口监听:ss -tulnp | grep :80 ; lsof -i :3306。检查服务状态:systemctl status nginx -l;nginx -t 验证配置;journalctl -u nginx -n 200 --no-pager 查看最近日志。MySQL: mysqladmin -uroot -p ping;mysql -e "SHOW PROCESSLIST\G"。
5. 查看磁盘与 inode 使用:df -h ; df -i。若根分区满:du -sh /* | sort -hr | head -n 20 定位大目录;清理 /var/log 或过期备份。若 inode 用尽,找出小文件过多目录:find /path -xdev -printf '%h\n' | sort | uniq -c | sort -rn | head。
6. 用 free -m 查看内存,用 top 或 htop 定位占用进程。若 OOM 频发,检查 dmesg | grep -i oom 或 journalctl -k。可临时增加 swap:fallocate -l 2G /swapfile; chmod 600 /swapfile; mkswap /swapfile; swapon /swapfile;并在 /etc/fstab 添加行持久化。
7. 常看位置:/var/log/syslog 或 /var/log/messages、/var/log/nginx/*.log、/var/log/mysql/*.log。用 tail -F 持续观察:tail -F /var/log/nginx/error.log。遇到重复错误,复制完整时间段日志用于定位 root cause。
8. 检查本机防火墙:ufw status verbose 或 iptables -L -n -v。检查云端安全组/防火墙规则是否阻断端口。若端口被阻止,先在本机临时放通:iptables -I INPUT -p tcp --dport 80 -j ACCEPT;记得持久化修改。
9. 若出现大文件传输慢或 TLS 握手超时,检查 MTU:ip link show eth0;可临时调整 ip link set dev eth0 mtu 1400。对 TCP MSS 问题在防火墙上做 MSS clamping 或调整服务器的 TCP 缓冲区。
10. 推荐用 systemd 做服务自愈:在 /etc/systemd/system/my.service 添加 Restart=on-failure RestartSec=5。再写简单健康检查脚本 /usr/local/bin/healthcheck.sh: bash脚本示例: #!/bin/bash if ! curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:80 | grep -q "^200$" ; then systemctl restart nginx echo "$(date): nginx restarted" >> /var/log/healthcheck.log fi 并用 cron 或 systemd timer 每分钟运行。
11. 在被监控主机上: wget https://github.com/prometheus/node_exporter/releases/download/v*/node_exporter-*.*-amd64.tar.gz tar xzf node_exporter-*.tar.gz sudo useradd -rs /bin/false node_exporter sudo cp node_exporter-*/node_exporter /usr/local/bin/ 创建 systemd 单元 /etc/systemd/system/node_exporter.service,内容: [Unit] Description=Node Exporter [Service] User=node_exporter ExecStart=/usr/local/bin/node_exporter Restart=always [Install] WantedBy=multi-user.target 然后 systemctl daemon-reload; systemctl enable --now node_exporter。Prometheus 配置 job 指向该主机 IP:9100。
12. Zabbix agent:apt install zabbix-agent;编辑 /etc/zabbix/zabbix_agentd.conf 设置 Server=ZABBIX_SERVER_IP 和 Hostname=your-host;systemctl enable --now zabbix-agent。Monit 适合进程/端口自动重启,示例 /etc/monit/monitrc 检查 HTTP 200 并 auto restart。Netdata 一键安装脚本:bash <(curl -s https://my-netdata.io/kickstart.sh) 可即时可视化 CPU/IO/网络,集成告警。
13. 部署 filebeat 采集 /var/log,推到 Elasticsearch 或 Logstash。Prometheus 告警可以用 Alertmanager,配置 webhook 把告警推送到钉钉/Telegram/企业微信。示例 Telegram 推送: curl -s -X POST "https://api.telegram.org/bot$TOKEN/sendMessage" -d chat_id=$CID -d text="告警: 主机XX CPU超限"。
14. 如果厂商提供 API,可写脚本自动执行重启或创建快照。通用示例: curl -X POST "https://api.provider/v1/servers/$ID/actions" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"action":"reboot"}' 结合监控规则,当 healthcheck 失败多次就触发该 API,脚本中先做告警并记录快照再重启,避免误操作。
15. 推荐策略:每天数据库热备份 + 每日增量文件备份 + 每周快照。MySQL 备份示例:mysqldump -u root -p --single-transaction --all-databases | gzip > /backup/mysql-$(date +%F).sql.gz。文件备份可用 restic 或 rsync 到异地:restic backup /var/www --tag huozhiming。
16. 答:先从两端做排查:在本地 ping/vm -> ping 云主机外网IP 与内网IP(若有),再从云主机 ping 本地和公网(8.8.8.8)。用 traceroute 对比每跳延迟差异;若到网关跳跃出现大延迟或丢包,多半是云侧或ISP问题,联系云厂商并上传 tcpdump 证明。
17. 答:使用 systemd 的 Restart=on-failure + RestartSec=5 配合监控(Prometheus Alertmanager 或 Zabbix),并绑定自动脚本:当连续 N 次重启仍失败,触发云 API 做快照并回滚/重启实例。再结合 Monit 或 healthcheck 脚本在问题刚出现时自动重启服务并通知运维。
18. 答:建议监控节点部署在同区域或附近(越南/新加坡)以减少监控网络延迟;对延迟敏感的业务在报警规则上控制平滑窗口(如 3 次采样均异常再报警);用 UDP/TCP 双通道日志推送备份,且把告警推送到多渠道(短信+企业微信+Telegram)以保证告警不中断。