본문 바로가기

퍼블릭 클라우드

Packer 설치 및 이미지 빌드

반응형

Packer 설치 및 이미지 빌드

Packer는 인프라 자동화 도구로, 가상 머신 이미지 및 컨테이너 이미지를 자동으로 빌드하고 관리하는 데 사용됩니다. 다양한 가상화 및 클라우드 플랫폼에서 이미지 빌드를 지원하며, 코드로 이미지를 정의하고 구성할 수 있습니다.

출처-https://d2908q01vomqb2.cloudfront.net/972a67c48192728a34979d9a35164c1295401b71/2021/09/02/Picture2.png

AWS IAM 사용자 생성

사용자 이름 : sangchulkr-packer

p2
p3

태그 Name : sangchulkr-packer

p4
p5
p6

packer 패키지 설치

ec2 인스턴스(Amazon Linux 2)에 Packer 설치하기 : https://scbyun.com/1126

packer 서버에 AWS 액세스 키 등록하기

aws configure --profile sangchukr

AWS CLI로 액세스 키(Access Key, Secret Access Key) 설정 : https://scbyun.com/1121

728x90

Amazon Machine Image(AMI) 빌드

Packer 템플릿 작성

더보기

Packer source 클론

https://github.com/anti1346/packer.git

git clone https://github.com/anti1346/packer.git

---

packer.json 편집

{
    "variables": {
        "aws_profile": "{{env `AWS_PROFILE`}}",
        "access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
        "secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
        "region": "{{env `AWS_REGION`}}",
        "source_ami": "{{env `AWS_SOURCE_AMI`}}",
        "instance_type": "{{env `INSTANCE_TYPE`}}",
        "ssh_user": "{{env `SSH_USER`}}",
        "ami_name": "{{env `AMI_NAME`}}",
        "vpc_id": "{{env `VPC_ID`}}",
        "subnet_id": "{{env `SUBNET_ID`}}",
        "security_group_id": "{{env `SECURITY_GROUP_ID`}}"
    },
    "builders": [
        {
            "type": "amazon-ebs",
            "profile": "{{user `aws_profile`}}",
            "access_key": "{{user `access_key`}}",
            "secret_key": "{{user `secret_key`}}",
            "region": "{{user `region`}}",
            "source_ami": "{{user `source_ami`}}",
            "instance_type": "{{user `instance_type`}}",
            "ssh_username":  "{{user `ssh_user`}}",
            "ami_name": "{{user `ami_name`}}-{{timestamp}}",
            "launch_block_device_mappings": [
                {
                    "device_name": "/dev/xvda",
                    "volume_size": 8,
                    "volume_type": "gp3",
                    "delete_on_termination": true
                }
            ],
            "vpc_id": "{{user `vpc_id`}}",
            "subnet_id": "{{user `subnet_id`}}",
            "security_group_id": "{{user `security_group_id`}}",
            "associate_public_ip_address": true,
            "ssh_timeout": "10m",
            "tags": {
                "Name": "{{user `ami_name`}}-{{timestamp}}-image",
                "Owner" : "sangchul",
                "Creator" : "packer",
                "Release": "latest",
                "Description": "packer image {{timestamp}}"
            },
            "run_tags": {
                "Name": "packer-build-instance"
            },
            "run_volume_tags": {
                "Name": "{{user `ami_name`}}-{{timestamp}}-volume"
            },
            "snapshot_tags": {
                "Name": "{{user `ami_name`}}-{{timestamp}}-snapshot"
            },
            "ami_description": "Sangchul.kr Linux Image"
        }
    ],
    "provisioners": [
        {
            "type": "shell",
            "inline": [
                "sudo yum install -y -q git",
                "sudo rpm -qa | egrep git"
            ]
        },
        {
            "type": "file",
            "source": "provision/init-script.sh",
            "destination": "~/init-script.sh"
        },
        {
            "type": "shell",
            "remote_folder": "~",
            "inline": [
                "bash ~/init-script.sh",
                "rm ~/init-script.sh"
            ]
        }
    ]
}

환경 변수 편집

cd packer
cp env_tmp .env
vim .env
AWS_PROFILE=sangchulkr
AWS_REGION=us-east-1
AWS_SOURCE_AMI=ami-02e136e904f3da870
INSTANCE_TYPE=t3a.medium
SSH_USER=ec2-user
AMI_NAME=packer-sangchul-web
VPC_ID=vpc-0a45
SUBNET_ID=subnet-0875
SECURITY_GROUP_ID=sg-0842

이미지 빌드

./packer-build.sh packer.json
$ ./packer-build.sh packer.json
amazon-ebs: output will be in this color.

==> amazon-ebs: Prevalidating any provided VPC information
==> amazon-ebs: Prevalidating AMI Name: packer-sangchul-web-1633860380
    amazon-ebs: Found Image ID: ami-02e136e904f3da870
==> amazon-ebs: Creating temporary keypair: packer_6162bb1d-0df6-bcf9-611f-5729703c700d
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Adding tags to source instance
    amazon-ebs: Adding tag: "Name": "packer-build-instance"
    amazon-ebs: Adding tag: "Name": "packer-sangchul-web-1633860380-volume"
    amazon-ebs: Instance ID: i-00bc09d9e229658da
==> amazon-ebs: Waiting for instance (i-00bc09d9e229658da) to become ready...
==> amazon-ebs: Using SSH communicator to connect: 52.71.171.150
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with shell script: /tmp/packer-shell2935123381
==> amazon-ebs: Existing lock /var/run/yum.pid: another copy is running as pid 2383.
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory : 154 M RSS (446 MB VSZ)
==> amazon-ebs:     Started: Sun Oct 10 10:06:48 2021 - 00:07 ago
==> amazon-ebs:     State  : Running, pid: 2383
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory : 155 M RSS (448 MB VSZ)
==> amazon-ebs:     Started: Sun Oct 10 10:06:48 2021 - 00:09 ago
==> amazon-ebs:     State  : Running, pid: 2383
==> amazon-ebs: Existing lock /var/run/yum.pid: another copy is running as pid 2400.
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory :  34 M RSS (251 MB VSZ)
==> amazon-ebs:     Started: Sun Oct 10 10:06:48 2021 - 00:11 ago
==> amazon-ebs:     State  : Running, pid: 2400
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory : 110 M RSS (329 MB VSZ)
==> amazon-ebs:     Started: Sun Oct 10 10:06:48 2021 - 00:13 ago
==> amazon-ebs:     State  : Running, pid: 2400
    amazon-ebs: lm_sensors-libs-3.4.0-8.20160601gitf9185e5.amzn2.x86_64
    amazon-ebs: git-core-doc-2.32.0-1.amzn2.0.1.noarch
    amazon-ebs: net-tools-2.0-0.22.20131004git.amzn2.0.2.x86_64
    amazon-ebs: screen-4.1.0-0.27.20120314git3c2946.amzn2.x86_64
    amazon-ebs: python-pillow-2.0.0-21.gitd1c6db8.amzn2.0.1.x86_64
    amazon-ebs: crontabs-1.11-6.20121102git.amzn2.noarch
    amazon-ebs: git-core-2.32.0-1.amzn2.0.1.x86_64
    amazon-ebs: git-2.32.0-1.amzn2.0.1.x86_64
==> amazon-ebs: Uploading provision/init-script.sh => ~/init-script.sh
    amazon-ebs: init-script.sh 38 B / 38 B [=====================================] 100.00% 0s
==> amazon-ebs: Provisioning with shell script: /tmp/packer-shell2993481597
    amazon-ebs: Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
==> amazon-ebs: Existing lock /var/run/yum.pid: another copy is running as pid 2595.
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory : 151 M RSS (368 MB VSZ)
==> amazon-ebs:     Started: Sun Oct 10 10:07:07 2021 - 00:04 ago
==> amazon-ebs:     State  : Running, pid: 2595
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory : 153 M RSS (370 MB VSZ)
==> amazon-ebs:     Started: Sun Oct 10 10:07:07 2021 - 00:06 ago
==> amazon-ebs:     State  : Running, pid: 2595
    amazon-ebs: Package wget-1.14-18.amzn2.1.x86_64 already installed and latest version
    amazon-ebs: Nothing to do
==> amazon-ebs: Stopping the source instance...
    amazon-ebs: Stopping instance
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating AMI packer-sangchul-web-1633860380 from instance i-00bc09d9e229658da
    amazon-ebs: AMI: ami-0877390a79b358493
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Modifying attributes on AMI (ami-0877390a79b358493)...
    amazon-ebs: Modifying: description
==> amazon-ebs: Modifying attributes on snapshot (snap-075b34929dc9b5ac9)...
==> amazon-ebs: Adding tags to AMI (ami-0877390a79b358493)...
==> amazon-ebs: Tagging snapshot: snap-075b34929dc9b5ac9
==> amazon-ebs: Creating AMI tags
    amazon-ebs: Adding tag: "Name": "packer-sangchul-web-1633860380-image"
    amazon-ebs: Adding tag: "Owner": "sangchul"
    amazon-ebs: Adding tag: "Creator": "packer"
    amazon-ebs: Adding tag: "Release": "latest"
    amazon-ebs: Adding tag: "Description": "packer image 1633860380"
==> amazon-ebs: Creating snapshot tags
    amazon-ebs: Adding tag: "Name": "packer-sangchul-web-1633860380-snapshot"
==> amazon-ebs: Skipping Enable AMI deprecation...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished after 4 minutes 902 milliseconds.

==> Wait completed after 4 minutes 902 milliseconds

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
us-east-1: ami-0877390a79b358493

웹 콘솔

Elastic Block Store > 스냅샷

i1

이미지 > AMI

i2

 

참고URL

- https://aws.amazon.com/ko/blogs/mt/migrating-from-hashicorp-packer-to-ec2-image-builder/

 

728x90
반응형