📜 ⬆️ ⬇️

Quick build OS images with Packer

Repetitio est mater studiorum
Old Latin proverb

If you periodically have to collect images of Linux-based operating systems for different virtualization environments or even clouds, or, especially, you use Vagrant , then you should look at the new tool from Mitchell Hashimoto (this is the creator of Vagrant) Packer .

Packer is a tool for creating identical OS images for different platforms from the same description.
')


Long ago, Patrick Deboise (this is the man who coined the term DevOps) wrote Veewee - a tool that allows you to automatically create an image for VirtualBox, KVM and VMWare.

Packer went further, and allows you to do the same for common cloud providers: Amazon , DigitalOcean , OpenStack and GCE . Also Packer allows you to create containers for Docker .

Immediately answer the main question - why it may be needed. Recently, the topic has been actively discussed.
unchanged server ( Immutable Server ). We even dedicated her to the release of the podcast Devops Deflop . The fact is that all configuration management systems do not allow controlling the “identicalness” of different machines to the end. Then the idea arose to create machines and never change their configuration. And if necessary, create them again with a new configuration.

Packer is exactly what makes it easy to create images of cars in automatic mode.

Let's make an image with Ubuntu 12.04 for VirtualBox to see how easy it is.

First, we need to put VirtaulBox and packer versions older than 0.5.1 . I am sure this step will not cause difficulties.

Next you need to create a configuration file for the packer, which should look something like this:

{ "provisioners": [ { "type": "shell", "scripts": [ "scripts/postinstall.sh" ], "override": { "virtualbox-iso": { "execute_command": "echo 'vagrant'|sudo -S sh '{{.Path}}'" } } } ], "builders": [ { "type": "virtualbox-iso", "boot_command": [ "<esc><esc><enter><wait>", "/install/vmlinuz noapic preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg <wait>", "debian-installer=en_US auto locale=en_US kbd-chooser/method=us <wait>", "hostname=ubuntu <wait>", "fb=false debconf/frontend=noninteractive <wait>", "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false <wait>", "initrd=/install/initrd.gz -- <enter><wait>" ], "boot_wait": "4s", "disk_size": 51200, "guest_os_type": "Ubuntu_64", "http_directory": "http", "iso_checksum_type": "md5", "iso_url": "http://releases.ubuntu.com/precise/ubuntu-12.04.4-server-amd64.iso", "iso_checksum": "e83adb9af4ec0a039e6a5c6e145a34de", "ssh_username": "vagrant", "ssh_password": "vagrant", "ssh_port": 22, "http_directory" : ".", "http_port_min" : 9001, "http_port_max" : 9001, "ssh_wait_timeout": "10000s", "shutdown_command": "echo 'shutdown -P now' > shutdown.sh; echo 'vagrant'|sudo -S sh 'shutdown.sh'", "guest_additions_path": "VBoxGuestAdditions_{{.Version}}.iso", "virtualbox_version_file": ".vbox_version", "vboxmanage": [ [ "modifyvm", "{{.Name}}", "--memory", "512" ], [ "modifyvm", "{{.Name}}", "--cpus", "1" ] ] } ], "post-processors": [ "vagrant" ] } 


It says that we take a standard Ubuntu image ( iso_url key), install it using a set of commands from preseed.cfg , and then execute the postinstall.sh script inside, which installs a chef and prepares the box for working with the vagrant. It makes no sense to bring all the files here, it's easier to see the repository on the githab (it contains all the necessary files in the packer folder ).

Next, execute the packer build ubuntu64.json , and carefully observe how the installer automatically puts Ubuntu.

Put up Ubuntochka

If everything went well, then as a result you will have a file packer_virtualbox-iso_virtualbox.box - an image that can be used in Vagrant.

Learn more about Vagrant in our previous article . And on March 15, as always free of charge, the next DevOps Moscow community meeting will be held in the office of Parallels , which will be dedicated to the vagrant. If you sign up at meetup.com , then notifications about new meetings will come to you automatically.

We import our fresh box in Vagrant and initialize the clean Vagrant environment.

 vagrant box add from_packer packer_virtualbox-iso_virtualbox.box cd .. vagrant init 


After that we will fix one line in Vagrantfile , namely config.vm.box = "from_packer" (all this is in the test repository).

 vagrant up vagrant ssh Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.8.0-29-generic x86_64) * Documentation: https://help.ubuntu.com/ Last login: Sat Feb 8 18:01:24 2014 from 10.0.2.2 vagrant@ubuntu:~$ 


So, it is easy and simple, we have Ubunt 12.04 with the latest updates and installed the most recent chef.

With the help of simple actions, the postinstall.sh script can be replaced with any other script, as well as launching chef-solo , puppet apply, and much more .

With packer, the creation of new images for different environments is transformed from a complex and dreary job into a simple and repeatable process with a predictable result.

For an easy start, Chef has prepared a large number of packer templates for various Linux distributions and called them bento .

Do not forget that the current members of the crew of the Express 42 spaceship will land at Evil Martians on March 22 and 23 to brainwash everyone , namely, to tell and show how to use DevOps practices and tools in harmony with the Great Cosmos.

List of articles on the subject of the Packer in English, which seemed to me interesting.

Source: https://habr.com/ru/post/212085/


All Articles