跳转至

常用命令

Terraform

terraform init                              # 下载 provider(第一次、改了 provider / module 之后)
terraform init "-backend-config=backend.hcl"  # remote state(PS 5.1 要加引号)
terraform fmt                               # 格式化
terraform validate                          # 语法检查
terraform plan                              # 预览
terraform plan "-out=x.tfplan"              # 预览并保存
terraform show x.tfplan                     # 查看保存的 plan
terraform apply x.tfplan                    # 按保存的 plan 执行(和看过的内容完全一致)
terraform destroy                           # 全部删除
terraform plan -destroy                     # 预览要删除的资源

terraform state list                        # state 里的资源
terraform state show <地址>                 # 某个资源的详细属性
terraform state pull                        # 输出 remote state 的内容
terraform output                            # output 的值
terraform output -raw <名字>                # sensitive 的值也能看

terraform -chdir=02-compute plan            # 不用 cd 就能对别的目录执行
terraform plan "-generate-config-out=generated.tf"  # import 时自动生成代码

静态检查

tflint --recursive --config "$PWD\.tflint.hcl"
$env:PYTHONUTF8 = '1'
python -m checkov.main -d . --framework terraform --compact

Cloud Shell

# OKE 的 kubeconfig(私有 endpoint)
oci ce cluster create-kubeconfig --cluster-id <OCID> --file $HOME/.kube/config \
  --region ap-osaka-1 --token-version 2.0.0 --kube-endpoint PRIVATE_ENDPOINT

# Bastion session
oci bastion session create-managed-ssh --bastion-id <OCID> --target-resource-id <VM OCID> \
  --target-os-username opc --ssh-public-key-file ~/.ssh/bastion_key.pub --session-ttl 3600 \
  --wait-for-state SUCCEEDED --query 'data.resources[0].identifier' --raw-output

# VM 上读 Secret(Instance Principal;只看长度,不 echo 密码)
oci secrets secret-bundle get --auth instance_principal --secret-id <Secret OCID> \n  --query 'data."secret-bundle-content".content' --raw-output | base64 -d | wc -c

# Ansible
ansible-playbook site.yml --check --diff    # 预览
ansible-playbook site.yml                   # 执行

kubectl

kubectl get nodes -o wide
kubectl get pods -o wide
kubectl get svc <名字> -w                   # 等 EXTERNAL-IP 出来
kubectl apply -f x.yaml
kubectl delete -f x.yaml                    # destroy 前必须先执行(删掉 K8s 建的 LB)
kubectl scale deployment <名字> --replicas=4
kubectl drain <节点> --ignore-daemonsets --delete-emptydir-data
kubectl uncordon <节点>
kubectl rollout restart deployment <名字>

费用

python tools/cost.py             # 最近 7 天,按 service / SKU 汇总
python tools/cost.py --days 30

这个网站

.venv\Scripts\mkdocs serve       # 本机预览 http://127.0.0.1:8000
.venv\Scripts\mkdocs build       # 生成到 site/
python tools/check_ja_code.py    # 日文版嵌入的代码副本(i18n/ja/)和原文件是否一致(改了代码后执行)

# 发布:push 到 OCI DevOps → 自动检查 → build → 上传(build_spec.yaml)
git push                         # remote = oci、branch = main
# 不经过 DevOps,从本机直接发布
python tools/upload_site.py --dry-run   # 只显示要上传 / 删除的文件
python tools/upload_site.py             # 按 MD5 只传变了的文件