使用 Terraform 编排 Puppet 基础设施的 IaC 示例?
解读
面试官想验证三件事:
- 你是否把 Puppet 当成“被管理对象”而非“管理工具”,真正用 Terraform 去“搭” Puppet 运行所需的一切(Master、CA、Code Manager、PostgreSQL、Bolt 工作节点、网络、IAM、DNS)。
- 你是否熟悉国内云厂(阿里云、腾讯云、华为云)的合规细节:VPC 三段式私网地址、ECS 实例类型族(如阿里云 ecs.c7 代)、按量付费+节省停机模式、SLB 仅支持主动健康检查 5 秒间隔、IAM 最小权限原则、云数据库 RDS 必须开 SSL 且内网地址以 .rds.aliyuncs.com 结尾。
- 你是否能把 Puppet 的“声明式”与 Terraform 的“声明式”做分层:Terraform 负责“有/无”,Puppet 负责“对/错”;两者用 userdata + consul-template 或 cloud-init 做一次性握手,避免循环依赖。
知识点
- Terraform 状态锁:OSS Backend + OTS(表格存储)锁,Region 级别容灾。
- Puppet 7 架构:Masterless 编译节点(compiler pool)+ CA 节点 + PostgreSQL 集群,避免单点。
- 国内云标签规范:Project、Owner、CostCenter、Compliance(等保三级/二级)。
- 证书自动化:Terraform 调用云厂商 KMS 创建私钥,CSR 通过 Puppetserver ca generate --ca-name 内部签发,15 年有效期,满足等保“国密算法或 RSA2048 以上”。
- 合规基线:Terraform provisioner 里内嵌 Bolt plan,把 CIS Linux 基线一次性打到系统盘,后续由 Puppet 持续审计。
- 价格优化:按量实例 + 节省停机模式,Terraform 在 night-hours 自动 stop(用 alicloud_instance 的 auto_stop_time),白天 cron 触发 Terraform apply -replace 拉起。
答案
给出一个可直接落地在阿里云华北 2(cn-beijing)的精简版示例,满足 10 台 Agent 规模,等保三级,节省停机模式。目录结构:
puppet-infra/ ├── env/ │ └── beijing.tfvars ├── main.tf ├── variables.tf ├── outputs.tf ├── modules/ │ ├── puppet-ca/ │ ├── puppet-compiler/ │ └── puppet-db/ └── bolt-plans/ └── cis_apply.pp
main.tf(顶层)
terraform { required_version = ">= 1.5" backend "oss" { bucket = "puppet-terraform-state" prefix = "prod/puppet" region = "cn-beijing" tablestore_endpoint = "https://puppet-terraform-ots.cn-beijing.ots-internal.aliyuncs.com" tablestore_table = "tf_lock" } required_providers { alicloud = { source = "aliyun/alicloud" version = "~> 1.210.0" } } }
provider "alicloud" { access_key = var.access_key secret_key = var.secret_key region = var.region }
data "alicloud_zones" "default" { available_disk_category = "cloud_essd" available_resource_creation = "VSwitch" }
module "vpc" { source = "./modules/vpc" region = var.region vpc_cidr = "10.8.0.0/16" vswitch_cidrs = ["10.8.1.0/24", "10.8.2.0/24"] compliance_tags = var.compliance_tags }
module "puppet-ca" { source = "./modules/puppet-ca" vswitch_id = module.vpc.vswitch_ids[0] instance_type = "ecs.c7.large" key_name = var.key_name compliance_tags = var.compliance_tags puppet_version = "7.17.0" bolt_cis_apply = file("${path.module}/bolt-plans/cis_apply.pp") }
module "puppet-compiler" { source = "./modules/puppet-compiler" count = 2 vswitch_id = module.vpc.vswitch_ids[1] instance_type = "ecs.c7.xlarge" key_name = var.key_name ca_addr = module.puppet-ca.private_ip compliance_tags = var.compliance_tags }
module "puppet-db" { source = "./modules/puppet-db" vswitch_id = module.vpc.vswitch_ids[1] compliance_tags = var.compliance_tags }
modules/puppet-ca/main.tf(核心片段)
resource "alicloud_instance" "ca" { image_id = "centos_7_9_x64_20G_alibase_20230623.vhd" instance_type = var.instance_type instance_name = "puppet-ca" vswitch_id = var.vswitch_id security_groups = [alicloud_security_group.ca.id] key_name = var.key_name system_disk_category = "cloud_essd" system_disk_size = 100 user_data = base64encode(templatefile("${path.module}/user_data.sh", { puppet_version = var.puppet_version ca_name = "puppet-ca-beijing" })) tags = merge(var.compliance_tags, { Role = "puppet-ca" }) auto_stop_time = "23:59Z" // 节省停机 }
resource "alicloud_security_group" "ca" { name = "puppet-ca-sg" vpc_id = data.alicloud_vpcs.default.ids[0] }
resource "alicloud_security_group_rule" "ca_8140" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = "8140/8140" priority = 1 security_group_id = alicloud_security_group.ca.id cidr_ip = "10.8.0.0/16" }
modules/puppet-ca/user_data.sh(关键)
#!/bin/bash exec > >(tee /var/log/user-data.log) 2>&1 set -e
yum install -y puppetserver-{ca_name} --section master /opt/puppetlabs/bin/puppet config set dns_alt_names "${ca_name},puppet,puppet.beijing.local" --section master /opt/puppetlabs/bin/puppet config set autosign true --section master systemctl enable puppetserver systemctl start puppetserver
一次性 CIS 基线
/opt/puppetlabs/bin/bolt plan run cis_apply::apply --targets localhost --no-host-key-check
bolt-plans/cis_apply.pp(节选)
plan cis_apply::apply ( TargetSpec targets, ) { apply_prep(targets) apply($targets, _catch_errors => true) { class { 'cis_rhel7': profile_level => 'server_level_3', } } }
env/beijing.tfvars
access_key = "AKxxxxxxxx" secret_key = "SKxxxxxxxx" region = "cn-beijing" key_name = "puppet-ssh-key" compliance_tags = { Project = "DevOps" Owner = "zhangsan" CostCenter = "BU-9" Compliance = "Level3" }
执行流程
- terraform init -backend-config=env/beijing.tfvars
- terraform plan -var-file=env/beijing.tfvars
- terraform apply -var-file=env/beijing.tfvars -auto-approve
- 输出 puppet_ca_fqdn = puppet-ca.beijing.local,本地 DNS 私有域已自动写入阿里云 PrivateZone。
- Agent 节点只需 puppet agent --test --server puppet-ca.beijing.local 即可注册。
拓展思考
- 多 Region 容灾:用 Terraform workspace 区分 cn-beijing 与 cn-shanghai,通过阿里云 CEN 跨 Region VPC 互通;Puppet CA 采用离线中间 CA 签二级证书,避免跨 Region 复制 CRL 延迟。
- 自动扩缩:结合阿里云 ESS 弹性伸缩组,Terraform 只负责 Launch Template 与 Scaling Policy;Puppet Compiler 池通过 consul-template 动态注册到 SLB,实现编译能力水平扩展。
- 合规审计:Terraform 输出 inventory 到 Security Center,Puppet 每 30 分钟运行一次 fact upload,Security Center 对比 Terraform 基线漂移,产生等保“异常告警”事件。
- GitOps 闭环:Terraform 代码与 Puppet Control-repo 放在同一个阿里 Codeup 仓库,通过 Flow 流水线做 MR 级别的 tf plan 与 puppet parser validate,合并后自动触发 Terraform apply 与 r10k deploy environment,实现“基础设施+配置”一体化审批。