跳到主要内容

域名管理与SSL证书自动化

1. 域名生命周期

注册 → 实名 → 备案(国内服务器需要)→ 解析 → 续费 → (或转出 / 释放)

注册商选择:

  • 阿里云万网(国内主流,中文支持)
  • 腾讯云 / 华为云
  • Cloudflare Registrar(成本最低,无溢价)
  • Namecheap / Porkbun(国外)

2. DNS 服务商分离

注册商 ≠ DNS 服务商。可以在 A 注册,B 解析。

国内 DNS 推荐:

  • 阿里云 DNS(免费版够用)
  • DNSPod(腾讯,国内速度快)
  • Cloudflare DNS(免费 + 全球 Anycast)

设置 NS 记录指向 DNS 厂商。

3. DNS 记录类型速查

类型用途
A域名 → IPv4
AAAA域名 → IPv6
CNAME域名 → 域名(不能根域)
MX邮件服务器
TXT验证、SPF
NS域名服务器
CAA限制谁可签证书
SRV服务发现

3.1 SPF(防伪造邮件)

example.com. TXT "v=spf1 include:_spf.google.com ~all"

3.2 CAA(限定证书签发者)

example.com. CAA 0 issue "letsencrypt.org"
example.com. CAA 0 iodef "mailto:security@example.com"

只有 Let's Encrypt 能给这个域签证书。防止恶意 CA 签发。

4. SSL 证书获取

4.1 免费:Let's Encrypt

# certbot + nginx
sudo certbot --nginx -d example.com -d www.example.com

# 通配符(要 DNS-01)
sudo certbot certonly --manual --preferred-challenges dns \
-d "*.example.com" -d "example.com"

90 天有效期,自动续期。

4.2 阿里云免费证书

控制台 → SSL 证书 → 申请免费证书(DV)。1 年有效。

每年最多 20 张免费证书/账户。

4.3 商业 SSL

DigiCert / Sectigo / GeoTrust。OV/EV 适合金融、电商。

5. cert-manager(K8s 自动签发)

# ClusterIssuer
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-prod
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory
email: ops@example.com
privateKeySecretRef:
name: letsencrypt-prod
solvers:
- http01:
ingress:
ingressClassName: nginx
# DNS-01(通配符必须)
- dns01:
cloudflare:
apiTokenSecretRef:
name: cloudflare-api-token
key: api-token

# Ingress 加 annotation
metadata:
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
tls:
- hosts: [app.example.com]
secretName: app-tls

cert-manager 自动签发 + 90 天续期。

6. 监控证书过期

最常见生产事故。监控方案:

6.1 Prometheus blackbox_exporter

# blackbox.yml
modules:
http_2xx:
prober: http
timeout: 5s
http:
tls_config:
insecure_skip_verify: false
preferred_ip_protocol: ip4
# prometheus.yml
- job_name: blackbox-https
metrics_path: /probe
params:
module: [http_2xx]
static_configs:
- targets:
- https://app.example.com
- https://api.example.com
relabel_configs:
- source_labels: [__address__]
target_label: __param_target
- source_labels: [__param_target]
target_label: instance
- target_label: __address__
replacement: blackbox-exporter:9115

告警:

- alert: SSLCertExpiringSoon
expr: probe_ssl_earliest_cert_expiry - time() < 30 * 24 * 3600
for: 10m
annotations:
summary: "&#125;&#125; $labels.instance &#125;&#125; 证书 30 天内过期"

6.2 商业监控

UptimeRobot / Pingdom / 阿里云监控都有 SSL 过期监控。

7. 证书部署最佳实践

  • 私钥 chmod 600
  • 不进 git
  • 多机器复制走 secrets 工具(Ansible Vault、K8s Secret)
  • 过期前 30 天告警,7 天紧急告警
  • 续期自动化(certbot --post-hook 重载 nginx)
  • HSTS preload 慎重(一旦上不可逆)

8. 域名转出 / 切换 DNS

切 NS 期间用户随机解析到新旧两套,要保证两套都能正常服务一段时间:

  1. 新 DNS 建好相同记录
  2. 改 NS(注册商处)
  3. 等全球 NS 更新(48 小时)
  4. 旧 DNS 保留至少 7 天再下线

切 IP / 域名前一周把 TTL 调小到 60 秒。

9. 常见反模式

  • NS 改了不等 48 小时直接关旧:部分用户解析失败
  • TTL 长期 86400:故障切换要 24 小时生效
  • 证书人工续期:忘记一次全站挂
  • HSTS preload 没准备好就上:撤销极慢
  • CAA 没配:第三方 CA 可签发该域名证书(攻击向量)
  • 私钥 chmod 644:本机其他用户能读
  • 证书不监控:等用户报警才发现过期
  • 多域名一证书没用 SAN:浏览器拒绝

10. 延伸阅读