Packer 简要使用说明


官网: https://developer.hashicorp.com/packer

文档:https://developer.hashicorp.com/packer/docs

公司现有构建镜像代码库地址:https://gitlab.ushareit.me/sre/packer.git

简介

通过模板定义配置,使用插件构建 AWS、Azure、GCP、阿里云、华为云、腾讯云等多种云或 Saas 平台系统镜像的开源工具,可用外部插件配置文档:https://developer.hashicorp.com/packer/plugins

安装

下载地址,页面已包含各种系统安装说明:https://developer.hashicorp.com/packer/downloads

  • Mac
brew install packer
packer -autocomplete-install
  • CentOS/RHEL
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install packer
packer -autocomplete-install
  • Amazon Linux
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install packer
packer -autocomplete-install

Packer 注意事项

编写镜像定义模板文件,Packer 1.5及以上版本支持并推荐使用 HCL2(HashiCorp Configuration Language)模板。文件名后缀为 .pkr.hcl 或 .pkr.json 解析为 HCL2 模式,其它情况则使用老版 JSON 模式解析。

HCL 具体说明:https://developer.hashicorp.com/packer/docs/templates/hcl_templates

常用命令简单说明

详细说明:https://developer.hashicorp.com/packer/docs/commands

注意事项:命令后[]及包含的内容代表可选项

AWS

详细文档:https://developer.hashicorp.com/packer/plugins/builders/amazon

谷歌云

详细文档:https://developer.hashicorp.com/packer/plugins/builders/googlecompute

华为云

详细文档:https://developer.hashicorp.com/packer/plugins/builders/openstack

AWS EC2 示例

本次使用 AWS 主账号作为示例说明。

1. 安装 Packer CLI 程序。
2. 新建 AWS 程序密钥,授予权限如下:
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ec2:AttachVolume",
        "ec2:AuthorizeSecurityGroupIngress",
        "ec2:CopyImage",
        "ec2:CreateImage",
        "ec2:CreateKeypair",
        "ec2:CreateSecurityGroup",
        "ec2:CreateSnapshot",
        "ec2:CreateTags",
        "ec2:CreateVolume",
        "ec2:DeleteKeyPair",
        "ec2:DeleteSecurityGroup",
        "ec2:DeleteSnapshot",
        "ec2:DeleteVolume",
        "ec2:DeregisterImage",
        "ec2:DescribeImageAttribute",
        "ec2:DescribeImages",
        "ec2:DescribeInstances",
        "ec2:DescribeInstanceStatus",
        "ec2:DescribeRegions",
        "ec2:DescribeSecurityGroups",
        "ec2:DescribeSnapshots",
        "ec2:DescribeSubnets",
        "ec2:DescribeTags",
        "ec2:DescribeVolumes",
        "ec2:DescribeVpcs",
        "ec2:DetachVolume",
        "ec2:GetPasswordData",
        "ec2:ModifyImageAttribute",
        "ec2:ModifyInstanceAttribute",
        "ec2:ModifySnapshotAttribute",
        "ec2:RegisterImage",
        "ec2:RunInstances",
        "ec2:StopInstances",
        "ec2:TerminateInstances"
      ],
      "Resource": "*"
    }
  ]
}
3. 设置环境变量关联 AWS 程序密钥或者运行命令 aws configure 直接永久保存密钥配置:
export AWS_ACCESS_KEY_ID=申请的AK
export AWS_SECRET_ACCESS_KEY=申请的SK
4. 新建模板配置文件:aws.pkr.hcl
variable "ImageVersion" {
  type    = string
}

data "amazon-ami" "main" {
  filters = {
    name                = "amzn2-ami-kernel-*-hvm-*-x86_64-gp2"
    root-device-type    = "ebs"
    virtualization-type = "hvm"
  }
  most_recent = true
  owners      = ["137112412989"]
  region      = "ap-southeast-1"
}

source "amazon-ebs" "main" {
  ami_block_device_mappings {
    delete_on_termination = true
    device_name           = "/dev/xvda"
    volume_type           = "gp3"
  }
  ami_description           = "awscli lrzsz node_exporter obsutil openssh tmux"
  ami_name                  = "dongsong-test-v${var.ImageVersion}"
  ami_regions               = ["ap-south-1"]
  ami_users                 = ["404486105145"]
  instance_type             = "t3.medium"
  region                    = "ap-southeast-1"
  source_ami                = "${data.amazon-ami.main.id}"
  ssh_clear_authorized_keys = true
  ssh_username              = "ec2-user"
  subnet_id                 = "subnet-0a95dbf475604da5d"
  tags = {
    "sgt:env"      = "prod"
    "sgt:group"    = "SGT"
    "sgt:project"  = "image"
    "sgt:subgroup" = "SRE"
  }
}

build {
  sources = ["source.amazon-ebs.main"]

  provisioner "shell" {
    scripts = ["image-init.sh", "aws-init.sh"]
  }

}
5. 格式化配置:
packer fmt aws.pkr.hcl
6. 检查语法:
packer validate -var "ImageVersion=1" aws.pkr.hcl
7. 构建镜像:
packer build -var "ImageVersion=1" aws.pkr.hcl
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值