Packer and Terraform: A powerful tool for efficient DevOps deployment
This article discusses how to use open source DevOps tools Packer and Terraform to collaborate on building and managing infrastructure to achieve efficient application deployment.
Core points:
Alibaba Cloud released a wonderful white paper on DevOps, which states: "DevOps is not just about simply implementing agile principles to manage infrastructure, John Willis and Damon Edwards use CAMS (culture, automation, measurement and sharing) to Defining DevOps. DevOps is designed to facilitate collaboration between development and operation teams. "This reflects a new role or mindset in DevOps that connects software development and infrastructure management, requiring both knowledge and making full use of the growing importance cloud computing model. But DevOps practice is not limited to large enterprises, and developers can easily incorporate it into their daily work. This tutorial demonstrates how to easily orchestrate the entire deployment process using only a small number of configuration files and run applications on Alibaba Cloud Elastic Computing Services (ECS) instances.
Packer introduction:
Packer is an open source DevOps tool developed by HashiCorp. It can create images from a single JSON configuration file to facilitate long-term tracking of changes. The software is cross-platform compatible and can create multiple images in parallel. Installation using Homebrew is very simple: brew install packer
. Packer creates "ready-to-use" images containing the additional software required by the operating system and applications, just like creating a custom distribution. For example, you can easily create a Debian image containing a custom PHP application.
Introduction to Terraform:
Deployment consists of two major tasks: packaging the application into the right environment (creating an image), and creating the infrastructure (server) that runs the application. Terraform is also from HashiCorp, based on the same principles as Packer, allowing you to build infrastructure in Alibaba Cloud using only a single TF format profile, which facilitates version control and has a clear understanding of how the underlying application works. For the installation of Terraform and the configuration of Alibaba Cloud official provider, please refer to other related articles.
Target:
This tutorial will create and deploy simple PHP applications in DevOps, covering everything from running software to supporting infrastructure.
Step:
To simplify the process, we will create a docker-compose-based application to get METAR weather data from the airport (using ICAO airport code and obtaining data from the US National Weather Service). We will then create the image using Ubuntu and Packer and deploy the infrastructure using that image and Terraform.
PHP Application:
For convenience, we provide an off-the-shelf application. You can view the source code (including index.php, 2 JavaScript files for decoding METAR data, a small amount of CSS and a PNG image). The app is based on docker-compose, which we will install as a dependency using Packer later.
Build images with Packer:
Create a folder named ~/metar-app on your computer, then go to that folder and create a file named meta-build.json, as follows:
{ "variables": { "access_key": "{{env `ALICLOUD_ACCESS_KEY`}}", "region": "{{env `ALICLOUD_REGION`}}", "secret_key": "{{env `ALICLOUD_SECRET_KEY`}}" }, "builders": [ { "type": "alicloud-ecs", "access_key": "{{user `access_key`}}", "secret_key": "{{user `secret_key`}}", "region":"{{user `region`}}", "image_name": "metar_app", "source_image": "ubuntu_16_0402_64_20G_alibase_20180409.vhd", "ssh_username": "root", "instance_type": "ecs.t5-lc1m1.small", "internet_charge_type": "PayByTraffic", "io_optimized": "true" } ], "provisioners": [ { "type": "shell", "script": "base-setup" } ] }
In the same directory, create a file named base-setup, with the following content:
#!/usr/bin/env bash apt-get update && apt-get install -y apt-transport-https ca-certificates curl git-core software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" apt-get update && apt-get install -y docker-ce docker-compose curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose mkdir /var/docker git clone https://github.com/roura356a/metar.git /var/docker/metar
When you prepare these two files, run packer build metar-build.json
and wait for them to complete. Note that for this to take effect, you need to set three environment variables in your computer: ALICLOUD_REGION
, ALICLOUD_ACCESS_KEY
, and ALICLOUD_SECRET_KEY
. This step takes some time because it creates an ECS instance, installs all the software on it, then stops the instance, creates a snapshot of its, and finally creates an image of the entire system. After the image is created, Packer will output ==> Builds finished
.
Deploy infrastructure with Terraform:
Now, in the same folder, create a file named main.tf, with the following content:
provider "alicloud" {} data "alicloud_images" "search" { name_regex = "metar_app" } data "alicloud_instance_types" "search" { instance_type_family = "ecs.xn4" cpu_core_count = 1 memory_size = 1 } data "alicloud_security_groups" "search" {} data "alicloud_vswitches" "search" {} resource "alicloud_instance" "app" { instance_name = "metar_app" image_id = "${data.alicloud_images.search.images.0.image_id}" instance_type = "${data.alicloud_instance_types.search.instance_types.0.id}" vswitch_id = "${data.alicloud_vswitches.search.vswitches.0.id}" security_groups = [ "${data.alicloud_security_groups.search.groups.0.id}" ] internet_max_bandwidth_out = 100 password = "Test1234!" user_data = "${file("user-data")}" } output "ip" { value = "${alicloud_instance.app.public_ip}" }
In the same directory, create a file named user-data, with the following content:
#!/usr/bin/env bash cd /var/docker/metar && docker-compose up -d
Now, your file structure should look like this:
<code>metar_app/ ├── metar-build.json ├── base-setup ├── main.tf └── user-data</code>
Runterraform init
, then run terraform plan
to check if everything is normal, and finally run terraform apply
to start the deployment process.
After the infrastructure is built, Terraform will output the IP address of the newly created ECS instance, for example: 111.111.111.111.111.
Test:
If all goes well, you can check out the latest weather report for San Sebastian Airport (located in northern Spain with beautiful entry routes).
Summary:
You have almost no effort to complete the full DevOps deployment of your application. This will greatly simplify the maintenance release cycle, infrastructure updates, and improve system availability without having to directly process host and Linux commands.
Frequently Asked Questions about Packer and Terraform:
The above is the detailed content of How to Deploy Apps Effortlessly with Packer and Terraform. For more information, please follow other related articles on the PHP Chinese website!