1.
准备与环境检查
在开始前先收集基本信息:发行版、内核版本、网络延迟与丢包、磁盘类型。推荐命令:uname -a; lsb_release -a 或 cat /etc/os-release; ip a; ss -tunapl; df -h; lsblk; smartctl -a /dev/sda。记录 ping/tcping 到重要目标(如 gateway、cloud provider 的 exit node)。
2.
安装常用诊断工具
安装一套工具用于实时诊断:Debian/Ubuntu: apt update && apt install -y htop atop iotop iftop iperf3 vnstat bmon smartmontools sysstat git; CentOS: yum install -y epel-release && yum install -y htop atop iotop iftop iperf3 vnstat bmon smartmontools sysstat。验证版本:htop --version; iperf3 -v。
3.
内核与网络参数(sysctl)优化步骤
编辑 /etc/sysctl.conf 增加常用优化项,示例:
net.core.somaxconn = 1024
net.core.netdev_max_backlog = 5000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_rmem = 4096 87380 6291456
net.ipv4.tcp_wmem = 4096 16384 6291456
net.ipv4.tcp_congestion_control = bbr (若要启用 BBR,需 4.9+ 内核)
保存后执行 sysctl -p 并使用 sysctl -a | grep tcp 检查生效。若启用 BBR:modprobe tcp_bbr 并 echo "tcp_bbr" > /etc/modules-load.d/tcp_bbr.conf。
4.
磁盘与 I/O 优化
确认文件系统与 I/O 调度器:cat /sys/block/sda/queue/scheduler。对 SSD 建议使用 noop 或 mq-deadline。若使用 ext4,可在 /etc/fstab 中为挂载点添加 noatime,nodiratime。定期运行 fstrim -v / (若为云盘确认支持)。使用 iostat -x 1 5 或 atop 观察 I/O 瓶颈,并根据情况增加缓存或调整数据库 writeback 策略。
5.
服务层面优化(Nginx/HAProxy/MariaDB/PHP-FPM)
Nginx:worker_processes auto; worker_connections 4096; keepalive_timeout 15; gzip on; 使用 sendfile on。
PHP-FPM:pm = dynamic;pm.max_children 根据内存和每个进程内存估算设置;pm.max_requests 500~2000。
MariaDB:使用 mysqltuner.pl 获取建议,调整 innodb_buffer_pool_size(约物理内存的 60%-70%),innodb_flush_method = O_DIRECT,query_cache_size 适度置 0(新版本禁用)。
修改后重启服务并通过 ab/hey/wrk 进行压力测试,观察系统负载与连接情况。
6.
自动重启与故障自愈
使用 systemd 做自动重启:在服务单元文件内 [Service] 添加 Restart=on-failure RestartSec=5。可编写简单 watchdog 脚本(检查关键端口、响应时间、进程数),并放入 crontab 或 systemd timer。示例脚本:检查 80 端口 curl -sI http://127.0.0.1:80|head -n1||systemctl restart nginx。
7.
日志集中与轮转(rsyslog / journald / logrotate)
启用 rsyslog 转发到集中日志服务器(或使用 syslog-ng):编辑 /etc/rsyslog.d/50-default.conf,添加 *.* @@logserver.example:514。配置 logrotate:在 /etc/logrotate.d/nginx 中设置 daily/weekly,rotate 14,compress,postrotate 重载 nginx 的指令。对于 systemd-journald,限制 SystemMaxUse 和 SystemKeepFree,避免日志占满磁盘。
8.
日志分析实战:grep/awk/journalctl 与可视化
定位问题:使用 journalctl -u nginx --since "2 hours ago" -p err 查看错误;通过 grep -E "timeout|failed|error" /var/log/nginx/access.log | awk 统计 IP 和 URI。对于访问日志建议使用 goaccess(实时统计)或将日志发送到 ELK/EFK(Elasticsearch+Fluentd+Kibana)/Prometheus + Grafana(指标化)。示例:cat access.log | awk '{print $1}'|sort|uniq -c|sort -rn | head。
9.
监控搭建(Prometheus + node_exporter + Grafana)
安装 node_exporter:下载 binary,建立 systemd 服务并启动。配置 Prometheus scrape node_exporter 和黑盒探针(blackbox exporter)监测外部可用性。Grafana 导入常用 dashboard(node exporter full)。设置报警:Prometheus Alertmanager,用邮件/Slack/Telegram 通知。确保抓取间隔、保留策略符合成本与需求。
10.
问:如何快速定位台湾 VPS 网络抖动的根因?
答:先用 mtr -c 100 目标IP 查看丢包与跳点延迟;同时运行 iperf3 -c 目标(若可控)测试带宽和抖动;使用 tcpdump -i eth0 port 80 -w dump.pcap 捕获异常时间段的数据包,用 Wireshark 或 tcptrace 分析重传/延迟。若只在高峰出现,关注主机带宽、conntrack 限制(/proc/sys/net/netfilter/nf_conntrack_max)与云厂商网络配额。
11.
问:日志量大导致磁盘满,如何改进日志策略?
答:启用 logrotate 压缩与定期清理(保留周期按合规设置),对低价值日志设定 shorter retention;对高频访问日志选择按天切割并异步发送到集中存储(如 Object Storage 或 ELK),在本地只保留最近 7 天日志;使用 rsyslog 或 fluentd 流式传输并设置 backpressure 策略,避免写满磁盘。
12.
问:部署优化变更后如何验证稳定性?
答:先在预生产或低峰窗口逐步上线,使用负载测试工具(wrk/hey/ab)模拟真实流量,并监测 CPU、内存、IO、网络延迟、错误率及业务响应时间。结合 Prometheus 报表设定阈值报警(如 95pct 响应时间、5xx 错误率),进行 24-72 小时的观察期,若无异常再切换生产。