Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Packer를 통한 AMI 자동 빌드 시스템 구축

Afeb8054efb8e03aab4ed7d90a52f11c?s=47 nacyot
September 05, 2015

Packer를 통한 AMI 자동 빌드 시스템 구축

Packer를 통한 AMI 자동 빌드 시스템 구축
2015. 09. 05 도커 서울 밋업 4번째(Open Container Korea 주최).
by nacyot http://blog.nacyot.com

패커(Packer)는 범용적인 이미지 빌더입니다. 이미지는 일반적으로 가상화된 서버의 상태를 그대로 저장해서 다시 사용할 수 있도록 해줍니다. 하지만 이렇게 만들어진 이미지는 재현도 어렵고 그 수가 많아질 수록 관리하기 어려운 단점이 있습니다. Packer는 서버의 상태를 그대로 저장하는 대신에 Provisioner(셸스크립트, chef, ansible 등)을 사용해 이미지 생성을 관리하고, Builder를 통해서 다양한 플랫폼(AWS AMI, Digital Ocean, Docker 등)에서 이미지를 생성할 수 있도록 지원합니다.

이 발표에서는 Packer를 소개하고 간단한 예제와 실제 사용 사례에 대해서 설명합니다.

Afeb8054efb8e03aab4ed7d90a52f11c?s=128

nacyot

September 05, 2015
Tweet

Transcript

  1. PACKERܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ 1 — Packerܳ

    ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  2. Daekwon Kim SMARTSTUDY SOFTWARE & SYSTEM ENGINEER 2 — Packerܳ

    ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  3. 3 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ ©

    nacyot 2015 -
  4. PACKER HASHICORP 4 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ

    ҳ୷ © nacyot 2015 -
  5. PACKER IS A TOOL FOR CREATING MACHINE AND CONTAINER IMAGES

    FOR MULTIPLE PLATFORMS FROM A SINGLE SOURCE CONFIGURATION. 5 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  6. PACKER IS A TOOL FOR CREATING MACHINE AND CONTAINER IMAGES

    FOR MULTIPLE PLATFORMS FROM A SINGLE SOURCE CONFIGURATION. 6 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  7. PACKER ߧਊ੸ ੉޷૑ ࠽؊ 7 — Packerܳ ాೠ AMI ੗ز

    ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  8. IMAGE 8 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷

    © nacyot 2015 -
  9. MACHINE TO IMAGE ੘ز઺ੋ ݠन੄ ౠ੿ ࢚కܳ ੷ 9 —

    Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  10. IMAGE ▸ ݠन੄ ౠ੿ ࢚కܳ ੷੢ೡ ࡺ ▸ ੌױ جইоח

    ࢚కܳ ੷੢ ▸ ೒ۖಬ р ҕਬೞӝ য۰਑ ▸ ੤അೞӝ য۰਍ ҃਋о ݆਺ ▸ ҙܻ࠺ਊ੉ ੉޷૑ࣻী ࠺۹ 10 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  11. Build! 11 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷

    © nacyot 2015 -
  12. BUILDER AND PROVISIONER 12 — Packerܳ ాೠ AMI ੗ز ࠽٘

    दझమ ҳ୷ © nacyot 2015 -
  13. BUILDER যڃ ೒ۖಬ੄ ੉޷૑ܳ ٜ݅ Ѫੋо 13 — Packerܳ ాೠ

    AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  14. Amazon EC2 (AMI) DigitalOcean Docker Google Compute Engine OpenStack Parallels

    QEMU VirtualBox VMware Custom 14 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  15. PROVISIONER যڃ بҳ۽ ੉޷૑ܳ ٜ݅ Ѫੋ 15 — Packerܳ ాೠ

    AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  16. Remote Shell Local Shell PowerShell Windows Shell Ansible Chef Puppet

    Salt Windows Restart Custom 16 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  17. TEMPLATE PACKER੄ ׮নೠ ஹನք౟ܳ ੿੄ೞח JSON ౵ੌ 17 — Packerܳ

    ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  18. TEMPLATE { "builders": [{ // ... }], "provisioners": [{ //

    ... }], "post-processors": [{ // ... }] } 18 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  19. DOCKER 19 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷

    © nacyot 2015 -
  20. EXAMPLE - SHELL / DOCKER { "builders": [{ "type": "docker"

    // ... }], "provisioners": [{ "type": "shell" // ... }], "post-processors": [{ "type": "docker-import" // ... }] } 20 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  21. BUILDER - DOCKER { "type": "docker", "image": "ubuntu:14.04", "export_path": "nacyot-ubuntu-wget.tar"

    } 21 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  22. PROVISIONER - SHELL { "type": "shell", "inline": [ "apt-get update",

    "apt-get install -y wget" ] } 22 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  23. POST-PROCESSOR - DOCKER IMPORT { "type": "docker-import", "repository": "nacyot/ubuntu", "tag":

    "wget" } 23 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  24. { "builders": [{ "type": "docker", "image": "ubuntu:14.04", "export_path": "nacyot-ubuntu-wget.tar" }],

    "provisioners": [{ "type": "shell", "inline": [ "apt-get update", "apt-get install -y wget" ] }], "post-processors": [{ "type": "docker-import", "repository": "nacyot/ubuntu", "tag": "wget" }] } 24 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  25. packer build ./template.json docker output will be in this color.

    ==> docker: Creating a temporary directory for sharing data... ==> docker: Pulling Docker image: ubuntu:14.04 docker: 14.04: Pulling from ubuntu ... 25 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  26. Build End ... ==> docker: Exporting the container ==> docker:

    Killing the container: f0e28c4f ==> docker: Running post-processor: docker-import docker (docker-import): Importing image: Container docker (docker-import): Repository: nacyot/ubuntu:wget docker (docker-import): Imported ID: 6b773d2f Build 'docker' finished. $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE nacyot/ubuntu wget 6b773d2f87b4 About a minute ago 190.5 MB 26 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  27. NEW DOCKER IMAGE nacyot/ubuntu:wget 27 — Packerܳ ాೠ AMI ੗ز

    ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  28. ubuntu:14.04 $ docker run -it ubuntu:14.04 bash root@79b1000960e5:/# wget --version

    bash: wget: command not found nacyot/ubuntu:wget ec2-user at ip-172-17-103-250 in ~ $ docker run -it nacyot/ubuntu:wget bash root@2767bc99a7fc:/# wget --version GNU Wget 1.15 built on linux-gnu. 28 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  29. AWS AMI 29 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ

    ҳ୷ © nacyot 2015 -
  30. EXAMPLE - ANSIBLE / AMAZON AMI { "builders": [{ "type":

    "amazon-ebs" // ... }], "provisioners": [{ "type": "ansible-local" // ... }] } 30 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  31. BUILDER - AMAZON AMI { "type": "amazon-ebs", "access_key": "<AWS_ACCESS_KEY>", "secret_key":

    "<AWS_SECRET_KEY>", "region": "ap-northeast-1", "source_ami": "ami-cbf90ecb", "instance_type": "m3.medium", "ssh_username": "ec2-user", "ami_name": "CustomImage {{isotime | clean_ami_name}}" } 31 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  32. PROVISIONER - ANSIBLE { "type": "ansible-local", "playbook_file" : "ansible/playbook.yml", "playbook_dir":

    "/Users/../ansible" } 32 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  33. { "builders": [{ "type": "amazon-ebs", "access_key": "<AWS_ACCESS_KEY>", "secret_key": "<AWS_SECRET_KEY>", "region":

    "ap-northeast-1", "source_ami": "ami-cbf90ecb", "instance_type": "m3.medium", "ssh_username": "ec2-user", "ami_name": "CustomImage {{isotime | clean_ami_name}}" }], "provisioners": [{ "type": "ansible-local", "playbook_file" : "ansible/playbook.yml", "playbook_dir": "/Users/../ansible" }] } 33 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  34. packer build ./template.json 34 — Packerܳ ాೠ AMI ੗ز ࠽٘

    दझమ ҳ୷ © nacyot 2015 -
  35. Build Start $ packer build ./tempalte.json amazon-ebs output will be

    in this color. ==> amazon-ebs: Inspecting the source AMI... ==> amazon-ebs: Creating temporary keypair: packer 55e9b978-5a49... ==> amazon-ebs: Creating temporary security group for this instance... ==> amazon-ebs: Authorizing SSH access on the temporary security group... ==> amazon-ebs: Launching a source AWS instance... amazon-ebs: Instance ID: i-12345678 ==> amazon-ebs: Waiting for instance (i-12345678) to become ready... ... 35 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  36. Build End ... ==> amazon-ebs: Stopping the source instance... ==>

    amazon-ebs: Waiting for the instance to stop... ==> amazon-ebs: Creating the AMI: CustomImage 2015-09-04T15-32-08Z amazon-ebs: AMI: ami-12345678 ==> amazon-ebs: Waiting for AMI to become ready... ==> amazon-ebs: Terminating the source AWS instance... ==> amazon-ebs: Deleting temporary security group... ==> amazon-ebs: Deleting temporary keypair... Build 'amazon-ebs' finished. ==> Builds finished. The artifacts of successful builds are: --> amazon-ebs: AMIs were created: ap-northeast-1: ami-12345678 36 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  37. NEW AMI IMAGE ami-12345678 37 — Packerܳ ాೠ AMI ੗ز

    ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  38. INFRASTRUCTURE AS CODE ੷੢ࣗীࢲ ҙܻೞӝ 38 — Packerܳ ాೠ AMI

    ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  39. REPOSITORY PACKER TEMPLATE ANSIBLE PLAYBOOK 39 — Packerܳ ాೠ AMI

    ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  40. IMAGE UPDATE? BASE IMAGE ߸҃ ANSIBLE PLAYBOOK ߸҃ 40 —

    Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  41. BUILDING IMAGE WITH CI 41 — Packerܳ ాೠ AMI ੗ز

    ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  42. 42 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ ©

    nacyot 2015 -
  43. ALWAYS Latest IMAGE 43 — Packerܳ ాೠ AMI ੗ز ࠽٘

    दझమ ҳ୷ © nacyot 2015 -
  44. VAGRANT ࢤпա૑ ঋਵࣁਃ? 44 — Packerܳ ాೠ AMI ੗ز ࠽٘

    दझమ ҳ୷ © nacyot 2015 -
  45. PACKER BUILDER / PROVISIONER VAGRANT PROVIDER / PROVISIONER 45 —

    Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  46. VAGRANT DEVELOPMENT 46 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ

    ҳ୷ © nacyot 2015 -
  47. PACKER DEPLOYMENT 47 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ

    ҳ୷ © nacyot 2015 -
  48. Vagrantfile Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = 'dummy' config.vm.provider :aws do

    |aws, override| # ... end config.vm.provision 'ansible' do |ansible| # ... end end 48 — Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  49. UP $ vagrant Up UPDATE $ vagrant provision 49 —

    Packerܳ ాೠ AMI ੗ز ࠽٘ दझమ ҳ୷ © nacyot 2015 -
  50. THANK YOU! @NACYO_T 50 — Packerܳ ాೠ AMI ੗ز ࠽٘

    दझమ ҳ୷ © nacyot 2015 -