Always Free 护栏¶
PAYG 账号绑了信用卡,一不小心选错 shape 就会真的扣钱。下面是整个学习过程中遵守的规则。
规则¶
| 资源 | 规则 | 原因 |
|---|---|---|
| Region | 只用 Home Region(ap-osaka-1) |
Always Free 的 Compute 只能建在 Home Region |
| Compute | VM.Standard.A1.Flex 合计 4 OCPU / 24GB 以内,或 VM.Standard.E2.1.Micro 最多 2 台 |
超出就按量收费 |
| Block Volume | boot volume 也算在内,合计 200GB 以内 | boot volume 最小 50GB → 实际上最多 4 台 VM |
| Load Balancer | flexible,带宽 min = max = 10Mbps,只有 1 个 |
默认值或其他带宽要收费 |
| Autonomous DB | is_free_tier = true 写死在代码里,不做成变量 |
漏掉这一行就是收费的 ADB |
| OKE | type = "BASIC_CLUSTER" |
ENHANCED_CLUSTER 的 control plane 按小时收费 |
| Object Storage | 20GB 以内 | state 文件的量可以忽略 |
用代码防止写错¶
光靠记忆不可靠,规则尽量写进代码里:
variable "shape" {
type = string
validation {
condition = contains(["VM.Standard.A1.Flex", "VM.Standard.E2.1.Micro"], var.shape)
error_message = "只允许 Always Free 的 shape:VM.Standard.A1.Flex 或 VM.Standard.E2.1.Micro。"
}
}
- 有 validation 的话,
terraform plan阶段就会报错,不会建出收费资源 - ADB 的
is_free_tier、OKE 的type这种"写错就收费"的值,不做成变量,直接写死
告警和确认¶
- Budget 告警:Compartment
tf-lab$1、tenancy 整体 $5,实际支出超过 1% 就发邮件- Budget 每几个小时才评估一次,不是实时的,不能完全依赖
- 练习完就
terraform destroy,下一个 Phase 要用额度时也先删掉上一个 - 用 Usage API 查费用:本机脚本
tools/cost.py(OCI Python SDK 的request_summarized_usages)- 按 service + SKU 汇总,不用开 Console
- 数据有几小时到 1 天的延迟
结果¶
Phase 0〜6 全部做完,整个 tenancy 的费用是 0 日元。
- A1、E2 Micro、Block Volume、LB、ADB 都有用量记录,但金额都是 0
- NAT GW / Service GW 不出现在明细里(本身没有收费 SKU)
- OKE Basic cluster 的 control plane 也不出现在明细里(免费)
Kubernetes 间接建的资源
OKE 里 Service type=LoadBalancer 会建 OCI LB,PVC 会建 Block Volume。这些不在 Terraform 的 state 里,terraform destroy 删不掉。
要先 kubectl delete,再 terraform destroy。详见 Phase 6。