要讓 JUSTG 雲伺服器符合合理使用規則,只需關注三個指標:1 小時平均 CPU 使用率(不超過 50%)、持續磁碟吞吐(不得在 100 MiB/s 以上持續 4 小時或更久)、網路使用(不得長時間佔用共享連接埠 70% 以上),再加上每月流量。本文介紹如何在 Debian、Ubuntu 和 Rocky Linux / AlmaLinux 上用常見 Linux 工具測量這些指標、在觸及限制前收到提醒,以及如何給自己的任務限速。
合理使用規則與各方案限制
| 資源 | 雲伺服器規則 |
|---|---|
| CPU | 短時間跑滿 100% 沒有問題。若 1 小時平均使用率超過 50%,CPU 會被自動限制在 50%。 |
| 儲存 I/O | 不得在 100 MiB/s 以上持續 4 小時或更久。 |
| 網路連接埠 | 連接埠為共享連接埠:不要長時間佔用 70% 以上。所有方案都是 500 Mbps 連接埠,70% 約為 350 Mbps。 |
每個方案還包含每月流量額度:
| 方案 | CPU | 記憶體 | 每月流量 |
|---|---|---|---|
| mini | 1 核 | 512 MB | 500 GB |
| 01 | 2 核 | 1 GB | 1 TB |
| 02 | 3 核 | 2 GB | 2 TB |
| 03 | 4 核 | 4 GB | 3 TB |
| 04 | 5 核 | 8 GB | 4 TB |
| 05 | 6 核 | 16 GB | 5 TB |
| 06 | 7 核 | 24 GB | 6 TB |
價格和詳細配置請參閱雲伺服器方案說明。
步驟 1:安裝工具
# Debian / Ubuntu
apt update && apt install -y htop sysstat iotop vnstat nload iftop
# Rocky Linux / AlmaLinux (htop, vnstat, nload and iftop come from EPEL)
dnf install -y epel-release && dnf install -y htop sysstat iotop vnstat nload iftop
步驟 2:檢視 CPU 負載與 1 小時平均值
合理使用規則看的是 1 小時平均值,只瞄一眼 top 並不夠。請使用以下命令:
nproc # number of vCPU cores
uptime # load average over 1, 5 and 15 minutes
top -b -n 1 | head -n 5 # %Cpu(s) line: us sy ni id wa ... st
mpstat 60 60 # one hour of 1-minute samples, then an "Average" line
pidstat -u 5 3 # which processes use the CPU
- top 中的 %Cpu(s) 代表整臺 VPS。CPU 使用率等於
100 - id,所以id連續一小時低於 50,就說明已超過限制。 - htop 用一條進度條顯示每個核心。單個程序可能顯示超過 100%,因為 100% 表示佔滿一個核心。按
P按 CPU 排序,按M按記憶體排序。 - Load average 統計的是等待 CPU 和磁碟的程序,因此負載高而 CPU 使用率低時,通常是 I/O 太慢。
- st(steal)是虛擬化層分給其他使用者的時間。如果你自己的使用率很低而 st 一直偏高,請附上命令輸出提交工單。
想檢視過去的時段,請開啟 sar 歷史記錄。它每 10 分鐘取樣一次,事後可以檢視任意一個小時:
# Debian / Ubuntu: turn on history collection (Rocky/Alma: only enable the service)
sed -i 's/^ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
systemctl enable --now sysstat
sar -u # CPU every 10 minutes today, plus an Average
sar -u -s 13:00:00 -e 14:00:00 # one specific hour
sar -d -p # disk throughput history (rkB/s, wkB/s)
sar -n DEV # network history per interface (rxkB/s, txkB/s)
步驟 3:用 iostat、iotop 和 pidstat 測量磁碟 I/O
iostat -xm 5 # per disk (e.g. vda): rMB/s, wMB/s, %util, r_await, w_await
iotop -oPa # only processes doing I/O, with accumulated totals
pidstat -d 5 # per-process kB_rd/s and kB_wr/s
# If iotop warns about CONFIG_TASK_DELAY_ACCT, enable it until the next reboot:
sysctl -w kernel.task_delayacct=1
在 iostat 中,把你那塊硬碟的 rMB/s 與 wMB/s 相加。短時峰值很正常,關鍵是兩者之和是否連續數小時維持在 100 左右或更高。常見原因有:大型備份、資料庫匯入、日誌暴增、小方案頻繁使用 swap,或爬蟲寫入數百萬個檔案。iotop 和 pidstat -d 能找出是哪個程序造成的。
%util 高、await 長但 MB/s 很低,通常是大量小塊隨機寫入:用起來感覺很慢,但並不等於持續高吞吐。步驟 4:用 vnStat 統計每月流量
vnStat 在後臺按網絡卡記錄流量,幾乎不佔資源,重啟後歷史資料也會保留。
systemctl enable --now vnstat
ip -br link # interface name, e.g. eth0 or ens3
vnstat -i eth0 # summary (data appears after about 5 minutes)
vnstat -m # per month, with an estimate for the current month
vnstat -d # per day
vnstat -h # per hour
vnstat -l # live rate in Mbit/s, Ctrl+C to stop
vnstat -m 按月顯示接收(rx)、傳送(tx)和總流量,並給出當月的 estimated 預估值。請把預估值與你方案的流量額度對比。vnStat 統計的是雙向流量;如果不確定你的額度如何計算,請提交工單諮詢。
想實時檢視連接埠占用,可以用下面的命令。若在 500 Mbps 連接埠上長時間維持在 350 Mbps 左右,就已超過 70% 的參考線:
nload eth0 # live in/out graph for one interface
iftop -i eth0 -P # live traffic per connection, with ports
ss -tnp state established | head -n 20 # which processes hold connections
步驟 5:可選的監控面板:Netdata 或 Prometheus node_exporter
Netdata
Netdata 無需配置即可實時繪製 CPU、磁碟、網路和各項服務的圖表。它可能佔用 100 MB 以上記憶體,更適合 2 GB 及以上的方案;mini 和 01 方案建議使用上面的命令列工具。切勿把它的連接埠暴露到公網:
# Debian / Ubuntu (Rocky/Alma: dnf install -y netdata from EPEL)
apt install -y netdata
# /etc/netdata/netdata.conf - listen on localhost only
[web]
bind to = 127.0.0.1
systemctl restart netdata
ss -tlnp | grep 19999 # must show 127.0.0.1:19999, not 0.0.0.0 or *
# On your own computer: open a tunnel, then browse http://localhost:19999
ssh -N -L 19999:127.0.0.1:19999 [email protected]
Prometheus node_exporter
如果你已經在其他伺服器上執行 Prometheus 和 Grafana,VPS 上裝輕量的 node_exporter 就夠了。它預設在所有網絡卡上監聽 9100 連接埠,因此只允許你的 Prometheus 伺服器訪問(Rocky/Alma 請使用 Prometheus 釋出頁提供的官方二進位制檔案):
apt install -y prometheus-node-exporter
ss -tlnp | grep 9100
# Allow only your Prometheus server to scrape it
ufw allow from 203.0.113.30 to any port 9100 proto tcp
# firewalld (Rocky/Alma):
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.30" port port="9100" protocol="tcp" accept'
firewall-cmd --reload
步驟 6:設定簡單的告警
下面這個小指令碼會在以下情況發出提醒:15 分鐘負載超過核心數的一半、根分割槽使用率達到 85%,或當月流量超過額度的 80%。負載只是粗略的早期訊號,真實的 CPU 平均值請用 sar -u 確認。告警會寫入系統日誌,也可以選擇推送到 webhook(聊天工具、郵件閘道器等):
#!/bin/bash
# /usr/local/sbin/vps-alert.sh - simple early warnings, run from cron
QUOTA_GB=1000 # your plan's monthly traffic, e.g. plan 01 = 1 TB
IFACE=eth0
WEBHOOK= # optional, e.g. https://hooks.example.com/REPLACE-ME
alert() {
logger -t vps-alert "$1"
[ -n "$WEBHOOK" ] && curl -fsS -m 10 --data-urlencode "text=$(hostname): $1" "$WEBHOOK" >/dev/null
}
CORES=$(nproc)
LOAD15=$(cut -d' ' -f3 /proc/loadavg)
awk -v l="$LOAD15" -v c="$CORES" 'BEGIN { exit !(l > c * 0.5) }' \
&& alert "15-minute load $LOAD15 on $CORES cores"
DISK=$(df --output=pcent / | tail -n 1 | tr -dc '0-9')
[ "$DISK" -ge 85 ] && alert "root file system ${DISK}% full"
USED=$(vnstat -i "$IFACE" --oneline b | cut -d';' -f11)
[ -n "$USED" ] && [ "$USED" -gt $(( QUOTA_GB * 800000000 )) ] \
&& alert "over 80% of monthly traffic: $(( USED / 1000000000 )) GB"
exit 0
chmod 700 /usr/local/sbin/vps-alert.sh
echo '*/15 * * * * root /usr/local/sbin/vps-alert.sh' > /etc/cron.d/vps-alert
journalctl -t vps-alert --since today # see past warnings
vnstat --oneline b 輸出的第 11 個欄位就是當月總流量(單位為位元組)。請根據你的 VPS 設定 QUOTA_GB 和 IFACE。
步驟 7:給自己的任務限速
大多數合理使用問題都源自某一個重負載任務。只給這個任務限速,而不是拖慢整臺伺服器。
限制單條命令的 CPU 與磁碟
# Lowest CPU and disk priority for a one-off job
nice -n 19 ionice -c3 tar -czf /root/site.tar.gz /var/www
# Hard caps for a one-off job (cgroup v2: Debian 11+, Ubuntu 22.04+, Rocky/Alma 9)
lsblk -d -o NAME,SIZE # disk name, usually vda
systemd-run --scope -p CPUQuota=50% \
-p IOReadBandwidthMax="/dev/vda 50M" -p IOWriteBandwidthMax="/dev/vda 50M" \
tar -czf /root/site.tar.gz /var/www
nice 和 ionice 只是降低優先順序:沒有其他任務競爭時,任務仍會全速執行;而且在虛擬硬碟常用的 none 或 mq-deadline 排程器下,ionice 不起作用。systemd-run 配合 CPUQuota 和 IO*BandwidthMax 才是真正的硬限制。CPUQuota=100% 等於一個完整核心。
為服務設定永久限制
systemctl edit myapp.service
# add in the editor:
[Service]
CPUQuota=100%
IOReadBandwidthMax=/dev/vda 80M
IOWriteBandwidthMax=/dev/vda 80M
systemctl restart myapp.service
systemctl show myapp.service -p CPUQuotaPerSecUSec
網路傳輸限速
rsync -a --bwlimit=20M /var/www/ [email protected]:/srv/www/ # about 170 Mbit/s
wget --limit-rate=20m https://example.com/big.iso
curl --limit-rate 20M -O https://example.com/big.iso
# Cap ALL outgoing traffic on eth0 at 300 Mbit/s (lost on reboot)
tc qdisc replace dev eth0 root tbf rate 300mbit burst 256kb latency 50ms
tc -s qdisc show dev eth0
tc qdisc del dev eth0 root # remove the cap
tc 限速會作用於該網絡卡的全部出站流量,包括 SSH。請在保持登入的狀態下測試,並另開一個會話,以便隨時撤銷。Docker 容器請改用 --cpus 和記憶體限制。步驟 8:升級方案或改用獨立伺服器
限速適合應對偶發的高峰。如果日常負載就讓 CPU 小時平均值超過 50%、需要長時間的高磁碟吞吐,或經常佔滿連接埠或每月流量,說明這臺伺服器已經不夠用了:
- 升級方案:在服務頁面升級,獲得更多核心、記憶體、SSD 和流量。請參閱如何升級 VPS 並擴充套件分割槽。
- 選擇獨立伺服器:適合長期高 CPU、高磁碟或高頻寬的業務,整臺硬體由你獨享。獨立伺服器在東京、首爾和約翰內斯堡 $199/月起,莫斯科 $299/月起。可透過工單聯絡 Sales/Inquiries 獲取建議。
FAQ
VPS 在長時間高負載後變慢,是觸發了 CPU 限制嗎?
有可能:如果 1 小時 CPU 平均使用率超過 50%,CPU 會被自動限制在 50%。用 htop 或 pidstat -u 找出佔用高的程序,降低或限制它的負載,再用 sar -u 檢視每小時的歷史資料。
連接埠 70% 的規則和每月流量額度是一回事嗎?
不是,二者相互獨立。70% 規則針對的是長時間的速率(500 Mbps 連接埠約 350 Mbps),流量額度則是每月的總量。前者用 vnstat -l 或 nload 觀察,後者用 vnstat -m 檢視。
看不懂這些資料,可以找誰幫忙?
請向 Technical support 提交工單,並附上服務 IP、帶時區的時間和命令輸出。JUSTG 提供 7×24 小時 AI 客服(工單),需要人工處理的問題會由工程師跟進。