Packer の使用に関する簡単な説明


公式サイト: https: //developer.bashicorp.com/packer

ドキュメント: https://developer.bashicorp.com/packer/docs

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

序章

テンプレートを通じて構成を定義し、プラグインを使用して AWS、Azure、GCP、Alibaba Cloud、Huawei Cloud、Tencent Cloud、その他のクラウドまたは Saas プラットフォーム システム イメージ用のオープン ソース ツールを構築し、外部プラグインを使用してドキュメントを構成します: https : //developer.bashicorp.com /packer/plugins

インストール

ダウンロード アドレス。このページにはすでにさまざまなシステム インストール手順が含まれています: https://developer.bashicorp.com/packer/downloads

  • マック
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
  • アマゾン・リナックス
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 1.5 以降のバージョンでは、HCL2 (HashiCorp Configuration Language) テンプレートがサポートされており、使用することをお勧めします。拡張子 .pkr.hcl または .pkr.json を持つファイルは HCL2 モードで解析され、それ以外の場合は古い JSON モードが解析に使用されます。

HCL 固有の手順: https://developer.bashicorp.com/packer/docs/templates/hcl_templates

一般的なコマンドの簡単な説明

詳細な手順: https://developer.bashicorp.com/packer/docs/commands

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

AWS

詳細なドキュメント: https://developer.bashicorp.com/packer/plugins/builders/amazon

グーグルクラウド

詳細なドキュメント: https://developer.bashicorp.com/packer/plugins/builders/googlecompute

ファーウェイクラウド

詳細なドキュメント: https://developer.bashicorp.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

おすすめ

転載: blog.csdn.net/dongsong1117/article/details/130284154