packer插件_如何使用Packer和Terraform轻松部署应用程序

packer插件

This article was originally published on Alibaba Cloud. Thank you for supporting the partners who make SitePoint possible.

本文最初发表在阿里云上 。 感谢您支持使SitePoint成为可能的合作伙伴。

Think you got a better tip for making the best use of Alibaba Cloud services? Tell us about it and go in for your chance to win a Macbook Pro (plus other cool stuff). Find out more here.

认为您有更好的技巧来充分利用阿里云服务吗? 告诉我们,并争取获得Macbook Pro的机会(还有其他很棒的东西)。 在这里找到更多 。

Alibaba Cloud published a very neat white paper about DevOps that is very interesting to read. It shows how “DevOps is a model that goes beyond simple implementation of agile principles to manage the infrastructure. John Willis and Damon Edwards defined DevOps using the term CAMS: Culture, Automation, Measurement, and Sharing. DevOps seeks to promote collaboration between the development and operations teams“.

阿里云发布了关于DevOps的非常整洁的白皮书 ,非常有趣。 它显示了“ DevOps是如何超越简单地运用敏捷原则来管理基础架构的模型。 John Willis和Damon Edwards使用CAMS术语定义了DevOps:文化,自动化,度量和共享。 DevOps旨在促进开发和运营团队之间的协作

This means, roughly, that there is a new role or mindset in a team that aims to connect both software development and infrastructure management. This role requires knowledge of both worlds and takes advantage of the cloud paradigm that nowadays grows in importance. But DevOps practices are not limited to large enterprises. As developers, we can easily incorporate DevOps in our daily tasks. With this tutorial you will see how easy is to orchestrate a whole deployment with just a couple of config files. We will be running our application on an Alibaba Cloud Elastic Compute Service (ECS) instance.

大致上,这意味着团队中有一个新角色或新思维模式,旨在将软件开发和基础架构管理联系在一起。 这个角色需要两个世界的知识,并且要利用当今日益重要的云范例。 但是DevOps的实践不仅限于大型企业。 作为开发人员,我们可以轻松地将DevOps纳入我们的日常任务中。 通过本教程,您将看到仅使用几个配置文件来编排整个部署是多么容易。 我们将在阿里云弹性计算服务(ECS)实例上运行我们的应用程序。

什么是包装工? (What Is Packer?)

Packer is an open source DevOps tool made by Hashicorp to create images from a single JSON config file, which helps in keeping track of its changes in the long run. This software is cross-platform compatible and can create multiple images in parallel.

Packer是由Hashicorp制造的开源DevOps工具,用于从单个JSON配置文件创建图像,从长远来看,它有助于跟踪其更改。 该软件是跨平台兼容的,可以并行创建多个图像。

If you have Homebrew, just type brew install packer to install it.

如果您有Homebrew,只需键入brew install packer进行安装。

It basically creates ready-to-use images with the Operating System and some extra software ready to use for your applications, like creating your own distribution. Imagine you want Debian but with some custom PHP application you made built-in by default. Well, with Packer this is very easy to do, and in this how-to, we will create one.

它基本上是通过操作系统和一些准备用于您的应用程序的额外软件来创建即用型映像, 例如创建自己的发行版 。 假设您想要Debian,但是默认情况下您内置了一些自定义PHP应用程序。 好吧,使用Packer可以很容易地做到这一点,在本操作方法中,我们将创建一个。

什么是地形? (What Is Terraform?)

When deploying we have two big tasks to complete. One is to pack the actual application in a suitable environment, creating an image. The other big task is to create the underlying infrastructure in where the application is going to live, this is, the actual server to host it.

部署时,我们有两个大任务要完成。 一种是将实际应用程序打包在合适的环境中,以创建映像。 另一个大任务是在应用程序将要驻留的地方创建底层基础结构,也就是托管它的实际服务器。

For this, Terraform, made by the same company as Packer, Hashicorp, came to existence as a very interesting and powerful tool. Based in the same principles as Packer, Terraform lets you build infrastructure in Alibaba Cloud by just using a single config file, in the TF format this time, also helping with versioning and a clear understanding of how all the bits are working beneath your application.

为此,与Packer,Hashicorp相同的公司制造的Terraform成为一种非常有趣且功能强大的工具。 基于与Packer相同的原理,Terraform允许您仅使用单个配置文件(这次为TF格式)在阿里云中构建基础架构,还有助于进行版本控制并清楚地了解应用程序下所有位的工作方式。

To install Terraform and the Alibaba Cloud Official provider, please follow the instructions in this other article.

要安装Terraform和阿里云官方提供程序,请按照另一篇文章中的说明进行操作。

我们想要实现的目标 (What We Want to Achieve)

As mentioned at the top of the article, we are going to create and deploy a simple PHP application in a pure DevOps way — that is, taking care of every part of the deployment, from the software running it to the subjacent infrastructure supporting it.

如本文顶部所述,我们将以纯DevOps方式创建和部署一个简单PHP应用程序-也就是说,照顾到部署的每个部分,从运行它的软件到支持它的底层基础结构。

脚步 (Steps)

For the sake of KISS principle, we will create a docker-compose based application to retrieve the METAR weather data from airports, using its ICAO airport code and pulling the data from the National US Weather Service. Then we will create the image with Packer using Ubuntu and deploy the infrastructure using the image with Terraform.

出于KISS原则,我们将创建一个基于docker-compose的应用程序,使用其ICAO机场代码并从美国国家气象局获取数据,以从机场检索METAR气象数据。 然后,我们将使用Ubuntu的Packer创建映像,并使用Terraform的映像部署基础架构。

PHP应用 (PHP Application)

For the sake of simplicity I created the application ready to use. You can find the source code here to have a look, which includes an index.php, 2 JavaScript files to decode the METAR data and a bit of CSS and a PNG image to make it less boring. It even indicates the direction of wind! But don’t worry, you won’t need to clone the repository at this point.

为了简单起见,我创建了可以使用的应用程序。 您可以在此处找到源代码,其中包括一个index.php,2个JavaScript文件以解码METAR数据以及一些CSS和一个PNG图像,以使它看起来更无聊。 它甚至指示风向! 但是不用担心,此时您无需克隆存储库。

The application is based in docker-compose and is something we will install as a dependency with Packer later.

该应用程序基于docker-compose ,稍后我们将作为Packer的依赖项进行安装。

使用Packer构建映像 (Building the Image with Packer)

Let’s get our hands dirty! For this, we need to create a folder somewhere in our computer, lets say ~/metar-app. Then lets cd in and create a file named metar-build.json with the following contents:

让我们弄脏双手吧! 为此,我们需要在计算机中的某个位置创建一个文件夹,例如~/metar-app 。 然后让cd进入并创建一个名为metar-build.json的文件,其内容如下:

{
  "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"
    }
  ]
}

And right next to it, a file named base-setup with the following:

在它旁边,是一个名为base-setup的文件,其中包含以下内容:

#!/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

Once you have these two files ready, you can run packer build metar-build.json and wait for it to finish. Please note that, for this to work, you need to have 3 environment variables in your machine with the relevant values for you: ALICLOUD_REGION, ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY. This step will take a while, as it creates an ECS, installs all the software in it, then stops the instance, creates a snapshot of it, and finally creates the image of the whole system.

准备好这两个文件后,即可运行packer build metar-build.json并等待其完成。 请注意,要使其正常工作,您的计算机中需要具有3个环境变量,并具有相关的值: ALICLOUD_REGIONALICLOUD_ACCESS_KEYALICLOUD_SECRET_KEY 。 创建ECS,在其中安装所有软件,然后停止实例,为其创建快照并最终创建整个系统的映像时,此步骤将花费一些时间。

After the image is completed, packer will output ==> Builds finished. Good, we have an image ready to use! We are ready to continue to the next step.

映像完成后,打包程序将输出==> Builds finished 。 好,我们有一个可供使用的图像! 我们准备继续下一步。

使用Terraform部署基础架构 (Deploying the Infrastructure with Terraform)

It’s time to create the ECS Instance. For this, in the same folder, we will create a file named main.tf with the following content:

现在该创建ECS实例了。 为此,在同一文件夹中,我们将创建一个名为main.tf的文件,其内容如下:

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}"
}

And, right next to it, create a file named user-data with the following:

然后,在其旁边,创建一个名为user-data的文件,其中包含以下内容:

#!/usr/bin/env bash

cd /var/docker/metar && docker-compose up -d

To be clear, at this moment we should have a file structure like this:

为了清楚起见,此刻我们应该具有如下文件结构:

metar_app/
├── metar-build.json
├── base-setup
├── main.tf
└── user-data

Ready to deploy. Run terraform init, then terraform plan to check that everything is fine, and terraform apply to launch the process.

准备部署。 运行terraform init ,然后运行terraform init terraform plan以检查一切都很好,然后terraform apply启动该过程。

When the infrastructure is built, Terraform will output the IP of the newly created ECS instance. Let’s say, 111.111.111.111.

构建基础架构后,Terraform将输出新创建的ECS实例的IP。 假设是111.111.111.111

测试中 (Testing)

If everything went well, you will be able to go to http://111.111.111.111/LESO and visualise, in this case, the latest weather report from the San Sebastián airport, an airport with one of the most beautiful approaches in the world, located in the north of Spain.

如果一切顺利,您将可以转到http://111.111.111.111/LESO进行可视化,在这种情况下,您可以查看来自圣塞巴斯蒂安机场的最新天气报告,该机场是世界上最美丽的航道之一,位于西班牙北部。

结语 (Wrapping Up)

See, with almost no effort you just created a full DevOps deployment for your application. This means that will be so much easier for you and your team to maintain anything related to its release cycles, infrastructure updates and will improve its uptime. No need to fiddle anymore directly with hosts and Linux commands in the normal day-to-day.

几乎无需费力,您就可以为应用程序创建完整的DevOps部署 。 这意味着您和您的团队可以更轻松地维护与其发布周期,基础架构更新相关的任何内容,并可以改善其正常运行时间。 在正常的日常情况下,不再需要直接与主机和Linux命令打交道。

翻译自: https://www.sitepoint.com/how-to-deploy-apps-effortlessly-with-packer-and-terraform/

packer插件

你可能感兴趣的:(java,docker,python,linux,大数据)