1.
准备与环境检测
步骤一:登录VPS,安装基础工具:apt/yum install -y iproute2 iptables tc iperf3 mtr traceroute iftop bmon
步骤二:测试链路延时与丢包:mtr -r -c 100 目标IP;记录平均延时与丢包率。
步骤三:测速带宽峰值:在VPS上启动iperf3 server:iperf3 -s;本地客户端:iperf3 -c VPS_IP -P 10 -t 60。
2.
查看路由与接口信息
命令:ip addr show; ip route show; ip rule show。
重点看默认路由、源地址、多网卡或隧道(如eth0、eth1、wg0)。记录MTU与txqueuelen。
3.
策略路由(PBR)基础配置
场景:对不同源IP走不同出口。
示例:创建路由表并绑定源IP:echo "200 to_backup" >> /etc/iproute2/rt_tables;ip route add default via 1.2.3.1 dev eth1 table to_backup;ip rule add from 10.0.0.0/24 table to_backup;ip rule add fwmark 0x1 table to_backup(配合iptables标记)。
4.
使用iptables标记并结合ip rule
目的:按端口/协议分流。
命令示例:iptables -t mangle -A PREROUTING -p tcp --dport 80 -j MARK --set-mark 1;iptables -t mangle -A PREROUTING -p tcp --dport 443 -j MARK --set-mark 1;然后 ip rule add fwmark 1 table to_backup;ip route add default via 1.2.3.1 dev eth1 table to_backup。
5.
流量整形:tc与HTB配置
目标:在出口端限制并保证突发保护与优先级。
示例(假设带宽100Mbps):tc qdisc add dev eth0 root handle 1: htb default 30;tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit ceil 100mbit;tc class add dev eth0 parent 1:1 classid 1:10 htb rate 80mbit ceil 100mbit prio 1;tc class add dev eth0 parent 1:1 classid 1:20 htb rate 20mbit ceil 100mbit prio 2;用tc filter根据fwmark分配。
峰值处理:ceil设置高于rate允许突发吸收短时峰值,burst可手工调大。
6.
TCP优化与BBR启用
编辑/etc/sysctl.conf加入:net.core.default_qdisc=fq; net.ipv4.tcp_congestion_control=bbr; net.core.rmem_max=16777216; net.core.wmem_max=16777216; net.ipv4.tcp_rmem="4096 87380 16777216"; net.ipv4.tcp_wmem="4096 65536 16777216";sysctl -p。
确认:sysctl net.ipv4.tcp_congestion_control;lsmod | grep bbr。
7.
多链路冗余与隧道方案
方法一:建立WireGuard/GRE到其他地区VPS并做负载/备份。
示例WireGuard:wg-quick up wg0,修改路由表把特定流量通过wg0;可用keepalive检测链路健康并切换路由表。
方法二:使用Multipath TCP或基于应用层的负载均衡(haproxy)做会话粘性与故障切换。
8.
监控与告警
部署简单脚本:使用vnstat/ifstat/iftop抓取流量,cron每分钟写入日志;使用prometheus node exporter + grafana可视化。
阈值示例:当5分钟内出入带宽>85%触发告警并自动降级低优先级流量(脚本触发tc调整)。
9.
实践案例:当电信链路出现峰值时的自动化策略
步骤:1) 监控检测短时带宽>阈值;2) 通过iptables打标记;3) tc调整HTB优先级,降低best-effort类ceil;4) 如链路持续不良,触发切换到WireGuard备份链路;5) 记录日志并通知运维。
实现示例脚本片段:使用ifstat获取速率,超过阈值运行 tc class change ... 和 ip rule 修改。
10.
运维注意事项与排障流程
检查点:MTU导致分片影响吞吐、conntrack溢出导致新连接失败、iptables规则顺序错误。
常用排错:tcpdump -i eth0 host X and port Y;iperf3 -c 测试并比对;mtr定位丢包节点。
11.
问:香港VPS对电信链路的主要瓶颈是什么?
答:常见瓶颈包括最后一跳拥塞、跨境链路带宽突发、MTU/分片问题与ISP的QoS限速,建议做mtr/iperf定位并通过多链路或流量整形缓解。
12.
问:如何在VPS上实现快速切换到备份链路?
答:用监控脚本检测主链路延迟/丢包,检测到阈值则通过 ip rule/ip route 调整路由表或启用预配置的WireGuard隧道,并把低优先级流量降速。
13.
问:在高峰期如何保证重要服务可用?
答:优先级策略(iptables标记+tc HTB)、保留带宽给关键端口、启用TCP优化(BBR)与多链路冗余,并配合监控自动触发策略与人工告警。