moles-packer

tech2022-08-29  128

moles-packer

tl;dr; – Is Packer a tool you’ll need in your tool box as a PHP developer? It depends.

tl; dr; – Packer是PHP开发人员在工具箱中所需的工具吗? 这取决于。

If you do a lot of server work for your clients or on the job, along with development work, then yes. Packer can help you a lot.

如果您为客户或工作做很多服务器工作以及开发工作,那么可以。 Packer可以为您提供很多帮助。

If you are only a developer and don’t really do much work on the server directly, then no. Packer won’t be very helpful.

如果您只是一名开发人员,而实际上并没有直接在服务器上做太多工作,则不会。 Packer不会很有帮助。

That being said, it is wise for any PHP developer to learn the basics of creating server environments. You will run into these technologies in your career in one way or another (everything you create runs on them!). This specialized knowledge will help your career in the future for sure! At a minimum, you’ll understand your dev-ops colleagues and the work they do much better.

话虽如此,对于任何PHP开发人员来说,学习创建服务器环境的基础知识都是明智的。 您将以一种或另一种方式在您的职业生涯中使用这些技术(您创造的一切都取决于它们!)。 这些专业知识必将对您将来的职业有所帮助! 至少,您会了解开发人员的同事以及他们做得更好的工作。

If your interest has been piqued, or if you do a lot of manual server work, please do read on.

如果您的兴趣激起了,或者您做了很多手动服务器工作,请继续阅读。

时光倒流 (A Look Back in Time)

tl;dr; #2 – If you have a command of Vagrant and understand it’s purpose as a tool, please jump to this section of the article.

tl; dr; #2 –如果您掌握Vagrant的命令并了解其作为工具的用途, 请跳至本文的本部分 。

Only 10 years ago, developing PHP applications basically worked like this: the developer would throw some code up on a web server via FTP and see if it worked. If it didn’t, they would troubleshoot it, edit in a change in some text editor, reload the affected file via FTP again and see if the changes fixed the problem. In that case, they’d go to the next feature or issue, wash, rinse, repeat (and hope nothing they just worked on broke). They might have had some synchronization going with FTP, too.

仅在10年前,开发PHP应用程序的工作原理基本上是这样的:开发人员将通过FTP在Web服务器上投放一些代码,然后查看其是否有效。 如果不是,他们将对其进行故障排除,在某些文本编辑器中进行更改,再次通过FTP重新加载受影响的文件,然后查看更改是否解决了问题。 在这种情况下,他们将转到下一个功能或问题,进行清洗,漂洗,重复(并希望他们没有做的任何事情都崩溃了)。 他们可能也与FTP进行了一些同步。

This was the workflow of the average lone PHP developer pushing stuff to a development server or, heaven help them, directly to a production server (and still is the same process for some developers today, actually). Building the web server itself back then was usually the work of someone else.

这是普通PHP开发人员将工作推到开发服务器或直接帮助他们直接到生产服务器的工作流程(实际上,对于今天的某些开发人员来说,这是相同的过程)。 那时,构建Web服务器本身通常是其他人的工作。

Also, in that not-so-distant past, to get teams of developers to work together, there might have been some form of concurrent versioning system and maybe the team even had an auto-deployment set up. When changes were committed to the repository, it would deploy to either a staging or a production system automatically. This scenario was actually the best technology for web application development back then.

同样,在那个不那么遥远的过去,为了使开发团队能够一起工作,可能存在某种形式的并发版本控制系统,也许团队甚至建立了自动部署功能。 将更改提交到存储库后,它将自动部署到暂存或生产系统。 当时,这种情况实际上是Web应用程序开发的最佳技术。

In those good old days, things like continuous integration, virtual machines, cloud computing, infrastructure orchestration, data-center automation and even the term “devops” were basically unknown. They were just coming to life. Amazon was just starting their Web Services business. And remember, this was only 10 years ago!

在过去的美好时光中,诸如连续集成,虚拟机,云计算,基础架构流程,数据中心自动化甚至“发展”一词基本上是未知的。 他们刚刚变得栩栩如生。 亚马逊刚刚开始其Web服务业务。 请记住,这只是十年前!

Ahhh, how simple life was back then, right?

啊,那时的生活多么简单,对吗?

Well, no. Back then, development was slower and more tedious, and also more error prone. Servers also always had to be “put up” for testing, which often meant developers had to wait until the “network guys” got a server ready for use with, say, the newest version of PHP. It was often a fight in a lot of cases and for the “network guys”, keeping servers “in shape” was often like fighting fires.

好吧,不。 那时,开发速度较慢,更乏味,而且也更容易出错。 服务器也总是必须“准备好”进行测试,这通常意味着开发人员不得不等到“网络专家”准备好要与最新版本PHP一起使用的服务器。 在很多情况下,这通常是一场战斗,对于“网络专家”而言,保持服务器“处于良好状态”就像是扑救大火。

当今的计算与虚拟化 (Computing Today and Virtualization)

Today, with the computer technology within everyone’s grasp, developers can carry a much more sophisticated development infrastructure right with them on their own laptop. The computing power of a laptop or PC today is considerably more than what was available just 10 years ago in a web server.

如今,随着人人掌握的计算机技术,开发人员可以在自己的笔记本电脑上随身携带更加复杂的开发基础架构。 今天的笔记本电脑或PC的计算能力比10年前在Web服务器中所具有的强大得多。

In fact, many of the smart phones we have today are more powerful than the servers of 10 years ago.

实际上,我们今天拥有的许多智能手机都比10年前的服务器功能强大。

Technology is amazing, isn’t it?

技术是惊人的,不是吗?

Because of the growing amount of computing power available, more and more people started to realize that this computing power could be split off into separate “machines”. This allowed for totally different things to happen on them at the same time. More importantly, the computing power could be much better utilized, saving resources. These were the main drivers for the virtualization of computing power.

由于可用的计算能力不断增长,越来越多的人开始意识到可以将这种计算能力分解为单独的“机器”。 这允许完全不同的事情同时发生在他们身上。 更重要的是,可以更好地利用计算能力,从而节省资源。 这些是计算能力虚拟化的主要驱动力。

In other words, today, with the ability to make virtual machines, developers can carry their own development environments with them and are no longer restricted by another party of people, who have to “give” them a proper environment to work on. More importantly, they can locally build a complete development environment, which mimics a full web server.

换句话说,如今,有了制造虚拟机的能力,开发人员可以随身携带自己的开发环境,而不再受到另一方人员的限制,而另一方人员必须“给他们”一个适当的环境以进行工作。 更重要的是,他们可以在本地构建一个完整的开发环境,以模仿完整的Web服务器。

Not only does this make developers much more autonomous, there is another big advantage to this kind of local development environment: it is called “development-production parity” – a concept we discuss at length in the newbie friendly “Jump Start PHP Environment” book. Dev-prod parity basically means that the developer can develop on a system virtually identical to the one the app will end up on when deployed live.

这不仅使开发人员更加自主,而且这种本地开发环境还有另一个很大的优势:它被称为“ 开发-生产对等 ” –我们在新手友好的“ Jump Start PHP Environment”一书中详细讨论了这个概念。 。 Dev-prod奇偶性基本上意味着开发人员可以在与实际部署应用程序时最终可以使用的系统几乎相同的系统上进行开发。

The Production Parity Advantage – it allows the developer to produce higher quality code, because they can do the right things, the right way, the first time.

生产奇偶校验优势–由于开发人员可以在第一时间以正确的方式,正确的方式进行操作,因此可以使开发人员生成更高质量的代码。

Today, a developer can have everything they need to develop highly complex applications within a highly complex environment, right in their own backpack.

如今,开发人员可以在自己的背包中拥有在高度复杂的环境中开发高度复杂的应用程序所需的一切。

With this ability obviously comes complexity. The virtual servers have to be built and the software has to be installed and configured. All of this can be quite time consuming and time is, as you know, money. It also means that the developer might have to learn a lot to get such environments running. This is also time consuming.

显然,有了这项功能,复杂性就会提高。 必须构建虚拟服务器,并且必须安装和配置软件。 所有这些都可能是非常耗时的,而且时间就是金钱。 这也意味着开发人员可能必须学习很多知识才能使此类环境运行。 这也是费时的。

进来无业游民 (In Comes Vagrant)

To help the developer with this “building” of the development environment, a program called Vagrant was brought to life by Hashicorp Inc. With Vagrant, the development environment configuration is code and can be shared, rolled back and rolled forward with ease. A developer is free to try new things, like the latest version of PHP, without worrying that a server might take hours to set up or a failure take hours to unravel. The developer doesn’t have to wait for anyone to make this environment, nor would they bother anyone, should it be completely screwed up.

为了帮助开发人员“构建”开发环境, Hashicorp Inc.引入了名为Vagrant的程序。 使用Vagrant,开发环境配置是代码,可以轻松共享,回滚和前滚。 开发人员可以自由尝试新事物,例如最新版本PHP,而不必担心服务器可能需要花费数小时来设置或失败要花费数小时才能解决。 如果完全搞砸了,开发人员不必等待任何人来创建这个环境,也不必打扰任何人。

For More on Vagrant – Re-introducing Vagrant: The Right Way to Start with PHP

有关Vagrant的更多信息– 重新引入Vagrant:从PHP开始的正确方法

But there is still another problem to be solved. Vagrant, although it is really good for building development environments, takes a considerable amount of time to run, especially when there is a lot of provisioning (more on provisioning below). Depending on the software involved, the building of a box and the provisioning of software can take anywhere from 2 to 20 minutes.

但是还有另一个问题需要解决。 Vagrant尽管对构建开发环境确实非常有用,但需要花费大量时间才能运行,尤其是在有很多配置的情况下(更多有关以下配置)。 根据所涉及的软件,构建盒子和提供软件可能需要2到20分钟。

This time consumption is compounded by the fact that Vagrant can only build a single box for a single virtualization system (called “the provider”) at one time. If you want to build your environment locally and also build one on AWS, for instance, you’d have to create two different Vagrant files and run them separately. This isn’t really practical. Simply put, Vagrant builds single development environments very well and that’s about it.

由于Vagrant一​​次只能为一个虚拟化系统(称为“提供程序”)构建一个盒子,这使时间消耗更加复杂。 例如,如果要在本地构建环境并在AWS上构建一个环境,则必须创建两个不同的Vagrant文​​件并分别运行它们。 这不是很实际。 简而言之,Vagrant可以很好地构建单个开发环境。

在包装工来 (In Comes Packer)

To continue to follow along, you now have to get more into the shoes of someone in a DevOps role. Packer puts the chore of creating virtualized machine images into a relatively simple central configuration, so you get the same advantages Vagrant has in automating development environments, but also now for developing base images.

为了继续跟进,您现在必须让更多的人参与DevOps角色的工作 。 Packer将创建虚拟机映像的繁琐工作放在相对简单的中央配置中,因此您可以获得Vagrant在自动化开发环境中所具有的相同优势,但现在也可以用于开发基础映像。

A virtual machine image is also called a virtual appliance. It is a pre-built copy of a virtual machine, which can be used to create a new virtual machine. These images are called “boxes” in Vagrant terms.

虚拟机映像也称为虚拟设备 。 它是虚拟机的预构建副本,可用于创建新的虚拟机。 这些图像在流浪汉中称为“盒子”。

If you know about the process of creating your own images, you know it is a PITA to do manually. If you don’t, you’re lucky, because you get to skip the headaches of building them manually and can start using Packer now. Though, you will still have to learn some basics of image/ server building. Again, this is valuable knowledge as a developer and any developer really should take the time to get to know the basics.

如果您知道创建自己的图像的过程,则知道它是手动执行的PITA。 如果您不这样做,那么您会很幸运,因为您无需手动进行构建,就可以立即开始使用Packer。 但是,您仍然必须学习一些映像/服务器构建的基础知识。 同样,这对于开发人员来说是宝贵的知识,任何开发人员确实都应该花些时间来了解基础知识 。

Once you get started with Packer, and you are already using Vagrant, the first question that will hit you is, “What the heck is the difference between Packer and Vagrant?” We started to go over the differences a bit above. In the end, Packer creates images, which Vagrant can use to build development environments. If you have used a Vagrant box from Hashicorp before, more than likely it was also built with Packer.

一旦您开始使用Packer,并且已经在使用Vagrant,将遇到的第一个问题是:“ Packer和Vagrant有什么区别?” 我们开始讨论以上差异。 最后,Packer创建映像,Vagrant可以使用它们来构建开发环境。 如果您以前使用过Hashicorp的Vagrant盒子,那么很有可能也是Packer制作的。

Working as a DevOps person armed with Packer, you can offer images of the production environment to the development team, which they can use to develop on with ease. This, again, supports development-production parity and simplifies the development process.

作为配备Packer的DevOps人员,您可以向开发团队提供生产环境的图像,他们可以轻松地进行开发。 再次,这支持了开发与生产的均等,并简化了开发过程。

We mentioned saving time earlier. Packer can also build these images and deploy them in parallel. So you could build images for different hosts, your private cloud and for Vagrant, practically all at the same time. Additionally, these “pre-baked” images start up much faster than provisioned boxes in Vagrant. Much faster! If your team is “up”ing and destroying boxes all the time, the images made by Packer with your pre-baked software and configuration will save your team a lot of time.

我们之前提到过节省时间。 Packer还可以构建这些映像并并行部署它们。 因此,您几乎可以同时为不同的主机,私有云和Vagrant构建映像。 此外,这些“预烘焙”映像的启动速度比Vagrant中预配置的框快得多。 快多了! 如果您的团队一直都在“装箱”并销毁盒子,那么Packer使用预先烘焙的软件和配置制作的图像将为您的团队节省大量时间。

The Packer process makes creating the base images reproducible too. This means higher quality in your production system, because the recreation of the image as a VM can be tested up front and automated. In other words, Packer helps reduce the chance of human error.

Packer流程还使创建基础图像也具有可复制性。 这意味着您的生产系统将具有更高的质量,因为可以预先测试并自动化将映像重新生成为VM。 换句话说,Packer有助于减少人为错误的机会。

While building images, there are also several sub-processes which you can control. We’ll get to them a little further down. You can also read a lot more about the concepts and features of Packer in the Packer docs. Just be aware that you can build for a good number of different platforms. You can also provision software with a whole slew of provisioners like Chef, Puppet, Ansible, or with Shell scripts. And, you can do post-processing, like creating a Vagrant box or pushing an image to the Docker registry (and a whole lot more).

在构建图像时,还可以控制几个子过程。 我们将进一步介绍他们。 您还可以在Packer文档中阅读有关Packer概念和功能的更多信息。 请注意,您可以为许多不同的平台进行构建 。 您还可以通过大量的预配器(例如Chef,Puppet,Ansible)或Shell脚本来预配软件。 而且,您可以进行后处理,例如创建一个Vagrant框或将映像推送到Docker注册表(以及更多)。

Phew….that was a lot of theory, wasn’t it?

ew……这是很多理论,不是吗?

Let’s do something practical, to help clarify the concepts of Packer and how to use it.

让我们做一些实际的事情,以帮助阐明Packer的概念以及如何使用它。

一些打包程序工作流程 (Some Packer Workflow)

This is a visualization of what we are going to accomplish.

这是我们将要完成的工作的可视化。

Although Packer doesn’t need Vagrant to make its images, Vagrant was added in the graphic above to give you a better understanding of where it fits in “the pipeline”.

尽管Packer不需要Vagrant制作图像,但在上图中添加了Vagrant,以使您更好地了解它在“管道”中的位置。

To get you going with the basics of Packer, the goal of the following exercise is to create both an image capable of running on Rackspace and a Virtualbox “box” file. The goal is to show you that you can produce the same image and provisioning to develop with and have on your (theoretical) production server.

为了使您了解Packer的基础知识,以下练习的目的是创建一个能够在Rackspace上运行的映像和一个Virtualbox“ box”文件。 目的是向您展示您可以生成相同的映像并进行配置,以与您的(理论上的)生产服务器一起使用并在其上进行开发。

We’re using Rackspace because they have a really simple back-end for managing computing infrastructure and because they are using Openstack, which is a standard system for infrastructure management.

我们之所以使用Rackspace,是因为它们具有用于管理计算基础结构的非常简单的后端,并且因为它们使用的是Openstack,这是用于基础结构管理的标准系统。

We are also using Virtualbox, because it is a free and widely used local virtualization system. You can use other systems like AWS and VMWare if you want, however, you’d need to modify the materials offered in this tutorial, as they are made specifically for these two systems.

我们还使用Virtualbox,因为它是一个免费且广泛使用的本地虚拟化系统。 如果需要,您可以使用其他系统,例如AWS和VMWare,但是,您需要修改本教程中提供的材料,因为它们是针对这两个系统专门制作的。

PLEASE NOTE: The virtual servers we are building in this article are in no way “production ready”. They are only for demonstration purposes.

请注意:我们在本文中构建的虚拟服务器绝不是“生产就绪”的。 它们仅用于演示目的。

The initial image we will be building with Packer will be a Debian Jessie (64bit) image, with Nginx and PHP7 pre-installed.

我们将使用Packer构建的初始映像将是Debian Jessie(64位)映像,其中预装有Nginx和PHP7。

Requirements: Before we get going, you’ll need Vagrant and the virtualization software Virtualbox (Version 5.0+) installed on your computer. You’ll also need to have a 64bit computer. You should also have Git for Windows installed, if you are on a Windows system, so we can use its bash console.

要求:开始之前,您需要在计算机上安装Vagrant和虚拟化软件Virtualbox (版本5.0+)。 您还需要一台64位计算机。 如果您使用的是Windows系统,则还应该安装Windows版Git ,以便我们使用其bash控制台。

As I mentioned above, we are using Rackspace for this tutorial, so you’ll need an account there. It’s relatively quick and easy to set one up.

就像我上面提到的,我们在本教程中使用Rackspace,因此您需要在此处开设一个帐户 。 设置起来相对比较容易。

You’ll also need a relatively fast internet connection, as the image download for the Virtualbox build is pretty big (approximately 250MB). Lastly, you’ll need about 12GB of free disk space on your computer for the whole thing.

您还需要一个相对较快的Internet连接,因为Virtualbox构建的图像下载量很大(大约250MB)。 最后,整个计算机将需要约12GB的可用磁盘空间。

This article will assume you also know some basics of working with Vagrant, using SSH and Shell commands and have some basic knowledge of Linux and Virtual Machines.

本文将假定您还知道使用SSH和Shell命令使用 Vagrant的一些基础知识,并且具有Linux和虚拟机的一些基础知识 。

设置计算机以运行Packer (Set up your computer to run Packer)

Please follow the instructions to install Packer on your computer.

请按照说明在计算机上安装Packer。

As of this writing, Packer 0.11.0 had just been released.

在撰写本文时,Packer 0.11.0刚刚发布。

Once installed, enter packer in your (git bash) console. You should see something like this.

安装后,在(git bash)控制台中输入packer 。 您应该会看到类似这样的内容。

Now we’ll go to Rackspace and spin up a server to then create a base image. First, log into the Rackspace control panel to create the server. NOTE: If you have AWS or some other cloud computing host, which Packer supports as a builder platform, the steps you will need to take will be similar. We are covering Rackspace in this tutorial. Also note, creating servers will cause some charges to your hosting account. The amount will be small, however. Following this tutorial shouldn’t cost more than one dollar.

现在,我们将转到Rackspace并启动服务器,然后创建基本映像。 首先, 登录Rackspace控制面板以创建服务器。 注意:如果您具有Packer作为构建器平台支持的AWS或其他云计算主机,则需要采取的步骤将相似。 在本教程中,我们将介绍Rackspace。 另请注意,创建服务器将对您的托管帐户产生一些费用。 但是,数量很少。 遵循本教程的费用不应超过一美元。

Go ahead and build a Jessie 8 server and call it “Packer Test”. Rackspace is pretty self explanatory. Fire up a 1GB server to create the image from.

继续构建Jessie 8服务器,并将其称为“包装测试”。 Rackspace很容易说明。 启动1GB服务器以从中创建映像。

After the server is active, create an image of the newly made server (Actions button, top right corner). Once the image is built and saved, you can delete the server.

服务器激活后,创建新制作的服务器的映像(“操作”按钮,右上角)。 构建并保存映像后,即可删除服务器。

Go to “Server -> Images” and you should see your new image. Click on it to go to the image’s detail page. We will need the image ID.

转到“服务器->图像”,您应该会看到新图像。 单击它以转到图像的详细信息页面。 我们将需要图像ID。

Ok. Now that you have a base image ready to use on Rackspace, we’ll start to build our own images. This process is quite involved actually, especially for those who have only a limited knowledge about server environments and Packer. So to help out a bit, we’ve created a demo Packer repo for you to download. Please clone this repo from your (Git bash) console.

好。 现在您已经准备好在Rackspace上使用基本图像,我们将开始构建自己的图像。 实际上,这个过程相当复杂,特别是对于那些对服务器环境和Packer知识了解有限的人。 为了帮助您,我们创建了一个演示Packer仓库供您下载。 请从您的(Git bash)控制台克隆此仓库。

git clone https://github.com/smolinari/PHP-Packer-Demo

Move down into the newly created “PHP-Packer-Demo” directory.

移至新创建的“ PHP-Packer-Demo”目录。

cd PHP-Packer-Demo

Use your favorite editor and open up the jessie64.json file, then enter your Rackspace user credentials and the image ID.

使用您喜欢的编辑器并打开jessie64.json文件,然后输入您的Rackspace用户凭据和图像ID。

Quick side note: you can also add your Rackspace API key instead of your password. If you really get into Packer usage, you can also move the access credentials to a separate file and Git ignore them. Doing so goes a bit beyond the scope of this article though.

快速提示:您也可以添加Rackspace API密钥而不是密码。 如果您真的喜欢Packer的用法,还可以将访问凭据移动到单独的文件中,而Git则忽略它们。 但是,这样做超出了本文的范围。

The jessie64.json file is the heart of Packer. This is the configuration file which controls the actions Packer takes. It is similar in nature to Vagrantfile used in Vagrant (and which we’ve also given you in the download).

jessie64.json文件是Packer的核心。 这是控制Packer采取的操作的配置文件。 它本质上类似于Vagrant中使用的Vagrantfile(在下载中我们也提供了它)。

Now run Packer with the following command.

现在,使用以下命令运行Packer。

packer build jessie64.json

This build process takes several minutes. Since we are in the middle of a tutorial, let’s actually go over what is happening inside of Packer, while we wait for it to finish. This process shouldn’t take more than about 30 minutes or the output in the console should never stop for more than 10 minutes. If it does, something went wrong.

此构建过程需要几分钟。 由于我们在本教程的中间,因此让我们仔细研究Packer内部的情况,然后等待它完成。 该过程不应超过30分钟,否则控制台中的输出不应停止超过10分钟。 如果是这样,则出了点问题。

建造者 (The Builders)

First, Packer is starting the build processes with the base images we have defined. The images are started as servers themselves, which we also defined within the jessie64.json file. You can find the particular JSON values for the “builders” at the top of our jessie64.json file.

首先,Packer使用我们定义的基础映像开始构建过程。 映像作为服务器本身启动,我们也在jessie64.json文件中定义了这些jessie64.json 。 您可以在jessie64.json文件顶部找到“构建器”的特定JSON值。

"builders": [ { "type": "openstack", "username": "ENTER_RACKSPACE_USERNAME_HERE", "password": "ENTER_RACKSPACE_PASSWORD_HERE", "region": "IAD", "identity_endpoint": "https://identity.api.rackspacecloud.com/v2.0", "ssh_username": "root", "image_name": "Test Packer Image", "source_image": "ENTER_YOUR_IMAGE_ID_HERE", "flavor": "2" }, { "type": "virtualbox-iso", "boot_command": [ "<esc><wait>", "install <wait>", "preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/vanilla-debian-8-jessie-preseed.cfg <wait>", "debian-installer=en_US <wait>", "auto <wait>", "locale=en_US <wait>", "kbd-chooser/method=us <wait>", "netcfg/get_hostname={{ .Name }} <wait>", "netcfg/get_domain=vagrantup.com <wait>", "fb=false <wait>", "debconf/frontend=noninteractive <wait>", "console-setup/ask_detect=false <wait>", "console-keymaps-at/keymap=us <wait>", "keyboard-configuration/xkb-keymap=us <wait>", "<enter><wait>" ], "disk_size": "10140", "guest_additions_mode": "disable", "guest_os_type": "Debian_64", "hard_drive_interface": "sata", "headless": true, "http_directory": "http", "iso_checksum": "dd25bcdde3c6ea5703cc0f313cde621b13d42ff7d252e2538a11663c93bf8654", "iso_checksum_type": "sha256", "iso_url": "http://cdimage.debian.org/cdimage/release/8.3.0/amd64/iso-cd/debian-8.3.0-amd64-netinst.iso", "shutdown_command": "sudo /sbin/halt -p", "ssh_password": "vagrant", "ssh_username": "vagrant", "ssh_port": "22", "ssh_wait_timeout": "10000s", "vm_name": "debian-jessie", "vboxmanage": [ ["modifyvm", "{{.Name}}", "--memory", "1024"], ["modifyvm", "{{.Name}}", "--cpus", "1"] ] } ],

The builders are responsible for making the images you want compatible with the environments you are targeting. For instance, with an AWS AMI, you could actually build your Virtualbox image to be used with Vagrant and with AWS.

建设者负责使您想要的图像与目标环境兼容。 例如,使用AWS AMI,您实际上可以构建Virtualbox映像以与Vagrant和AWS一起使用。

In our exercise, we are putting together “openstack” and “virtualbox-iso” images. Unfortunately, Packer cannot use the openstack image to build a Virtualbox image, so we are creating the virtualbox image additionally.

在我们的练习中,我们将“ openstack”和“ virtualbox-iso”图像放在一起。 不幸的是,Packer无法使用openstack映像来构建Virtualbox映像,因此我们正在另外创建virtualbox映像。

You’ll notice the “boot_command” section in the JSON code above. This is where we are actually automating the entering of the necessary commands in order to install Debian Jessie 8 as an OS in the Virtualbox VM. If you want, you can even watch the installation happen within Virtualbox, as Packer does its building.

您会在上面的JSON代码中注意到“ boot_command”部分。 我们实际上是在此处自动输入必要的命令,以将Debian Jessie 8作为OS安装在Virtualbox VM中。 如果需要,您甚至可以在Packer进行构建时观看Virtualbox中的安装过程。

Granted, this is a lot of “magic” happening and you’d have to really know the internals of Linux installations and the internals of Packer to put such a Virtualbox image together manually. Thankfully, there are many cool DevOps people who put such Packer .json files up for general use.

当然,这是很多“魔术”事件,您必须真正了解Linux安装的内部和Packer的内部,才能手动将这样的Virtualbox映像放在一起。 值得庆幸的是,有很多很棒的DevOps人士将此类Packer .json文件放到一般用途中 。

供应者 (The Provisioners)

Once the builders have gotten the images up and running in a VM in Rackspace and in Virtualbox, Packer will start to provision the software, which is defined in the shell scripts. With Packer, you can also use systems like Puppet, Chef or Ansible and a number of others to orchestrate the provisioning. We are only using Shell scripts, which you can find in the /scripts directory.

一旦构建者在Rackspace和Virtualbox中的虚拟机中启动了映像并运行后,Packer将开始配置软件,该软件在Shell脚本中定义。 借助Packer,您还可以使用Puppet,Chef或Ansible等系统以及许多其他系统来编排配置。 我们仅使用Shell脚本 ,您可以在/scripts目录中找到它们。

This is the provisioning section in our configuration file.

这是我们配置文件中的供应部分。

"provisioners": [ { "type": "file", "source": "transfer_files/", "destination": "/tmp" }, { "type": "shell", "pause_before": "5s", "execute_command": "echo 'vagrant'|sudo -S sh '{{.Path}}'", "override": { "virtualbox-iso": { "scripts": [ "scripts/base.sh", "scripts/vagrant.sh", "scripts/virtualbox.sh", "scripts/setup.sh", "scripts/cleanup.sh", "scripts/minimize.sh" ] }, "openstack": { "scripts": [ "scripts/setup.sh" ] } } } ],

The “file” provisioner is how we get our configuration onto the VM. It loads config files from our machine to the /tmp directory on the VMs. There are commands in the shell scripts which copy the configuration files to the proper locations on the VMs later in the provisioning process. We have files like the “default” configuration files for Nginx or a modified php.ini file for PHP. You can find the files we are using in the /transfer_files folder.

“文件”配置程序是我们将配置获取到VM的方式。 它将配置文件从我们的计算机加载到VM上的/tmp目录。 Shell脚本中有一些命令,可在配置过程的稍后部分将配置文件复制到VM上的正确位置。 我们有Nginx的“默认”配置文件或PHP的修改后的php.ini文件之类的文件。 您可以在/transfer_files文件夹中找到我们正在使用的文件。

You’ll also notice the “override” section above. This is telling Packer to only run certain scripts for each image build. In our exercise, we need to do less work with the “openstack” image to get it to be “in parity” with the Virtualbox image, so only the setup.sh script is being processed.

您还会注意到上面的“替代”部分。 这告诉Packer仅针对每个映像构建运行某些脚本。 在我们的练习中,我们需要对“ openstack”映像进行更少的工作,以使其与Virtualbox映像“处于同等地位”,因此仅处理setup.sh脚本。

Lastly, there is the post-provision processor section.

最后,还有后置处理器部分。

"post-processors": [ { "type": "vagrant", "only": ["virtualbox-iso"], "output": "jessie64.box" } ]

This is responsible for actually building the Vagrant box.

这负责实际构建Vagrant框。

It’s important to remember that everything we provision now is “pre-baked” into the images. Packer saves us (or our dev team) a lot of time with this bit of automation!

重要的是要记住,我们现在提供的所有内容都已“预烘焙”到图像中。 Packer通过这种自动化方式为我们(或我们的开发团队)节省了大量时间!

内置图像 (The Built Images)

After Packer provisions all the software we’ve configured in the jessie64.json file, it will shut down the VMs and store the new images of these VMs. For the Rackspace image, you’ll find the new image under the “Saved Images” section of the Rackspace admin control panel. For the Virtualbox image, you’ll find a “jessie64.box” file in your current directory.

Packer设置完我们在jessie64.json文件中配置的所有软件后,它将关闭VM,并存储这些VM的新映像。 对于Rackspace图像,您可以在Rackspace管理控制面板的“保存的图像”部分下找到新图像。 对于Virtualbox映像,您将在当前目录中找到一个“ jessie64.box”文件。

That’s it. Packer has done its job!

而已。 Packer已经完成工作!

怎么办? (Now what?)

You can now use the Rackspace image to create a new server. Try it. Go to the “Servers” section and select “Create Server”, then go to “Saved Images”. You should see your newly saved image under “Deleted Servers”. Select it and start the server. Once it is active, you can take the IP address given to you and enter it in your browser. You should get the Nginx welcome screen.

现在,您可以使用Rackspace映像创建新服务器。 试试吧。 转到“服务器”部分,然后选择“创建服务器”,然后转到“保存的图像”。 您应该在“已删除的服务器”下看到新保存的图像。 选择它并启动服务器。 一旦激活,您就可以获取提供给您的IP地址,并将其输入到浏览器中。 您应该会看到Nginx欢迎屏幕。

For the Virtualbox image, we have a bit more work to do with Vagrant. Follow along carefully now. This is going to get really difficult!

对于Virtualbox映像,我们需要处理更多与Vagrant相关的工作。 现在,请仔细遵循。 这将变得非常困难!

创建Virtualbox VM (Create the Virtualbox VM)

In the directory with the Vagrant file, enter these commands.

在包含Vagrant文​​件的目录中,输入以下命令。

vagrant box add jessie64 jessie64.box vagrant up

That’s it! Ok, granted, we did help you a bit by creating the Vagrant file for you. You’d need to learn more about Vagrant, in order to create a working file yourself. As mentioned before, if you are a serious developer, this should be something you should learn.

而已! 好的,当然,我们确实为您创建了Vagrant文​​件,对您有所帮助。 您需要了解有关Vagrant的更多信息 ,以便自己创建工作文件。 如前所述,如果您是一名认真的开发人员,那么这应该是您应该学习的东西。

You should now be seeing the new image as a VM in Virtualbox’s UI.

现在,您应该在Virtualbox的UI中将新映像作为VM看到。

The first command above adds the new box into Vagrant, so it knows where to use it from. We’ve given the box the name “jessie64” and are using the box we just created. The second command tells Vagrant to create the virtual machine in Virtualbox and run it.

上面的第一个命令将新框添加到Vagrant中,因此它知道从何处使用它。 我们为该框指定了名称“ jessie64”,并使用了刚创建的框。 第二个命令告诉Vagrant在Virtualbox中创建虚拟机并运行它。

You’ll also notice, this initial start of the boxes takes only a minute or two. Had we been provisioning everything in Vagrant, it would have taken up to 10-20 minutes. This is time saving again.

您还会注意到,盒子的初始启动只需要一两分钟。 如果我们一直在Vagrant中配置所有内容,则可能要花费10到20分钟。 这又节省了时间。

Once the “vagrant up” command is finished, enter the URL http://192.168.33.10 in your browser and you should see the same Nginx welcome page. This is your local web server, a perfect copy of the web server on Rackspace.

完成“ vagrant up”命令后,在浏览器中输入URL http://192.168.33.10 ,您应该看到相同的Nginx欢迎页面。 这是您的本地Web服务器,是Rackspace上Web服务器的完美副本。

In both systems, you also have SSH with the “vagrant” user and the password “vagrant”. These servers are not secure!

在这两个系统中,您还具有带有“ vagrant”用户和密码“ vagrant”的SSH。 这些服务器不安全!

我们还没有完成! (We’re not finished quite yet!)

Let’s do some more fun stuff.

让我们做一些更有趣的事情。

As you are doing this next step, imagine how many times you’ve done this or something similar. We are now automating the process of software installation and configuration!

在进行下一步时,请想象您完成了多少次或类似的操作。 现在,我们正在自动化软件安装和配置过程!

Let’s say we do a lot of Symfony development work. Let’s go ahead and set up our images to automatically have a version of Symfony installed and ready for us.

假设我们做了很多Symfony开发工作。 让我们继续进行设置,以自动安装一个版本的Symfony映像并为我们准备好。

Before we get going, delete the server on Rackspace. (We don’t want to spend any unnecessary money.)

开始之前,请删除Rackspace上的服务器。 (我们不想花任何不必要的钱。)

Also, in your console, let’s kill and remove the Vagrant box we just loaded. Enter these commands:

另外,在您的控制台中,让我们杀死并删除刚刚加载的Vagrant框。 输入以下命令:

vagrant destroy vagrant box remove jessie64

安装Symfony(和演示)只是为了好玩–(或者是吗?) (Installing Symfony (and the Demo) Just For Fun – (or is it?))

Again, let’s imagine we have a team of devs and they work with Symfony a lot. Let’s automate setting up a Symfony-ready server. We’re only going to load the Symfony demo app, but this extra work should give you a pretty good idea of where and how Packer can help with automation to support development and infrastructure orchestration work.

再次,让我们假设我们有一个开发人员团队,他们与Symfony经常合作。 让我们自动设置支持Symfony的服务器。 我们将只加载Symfony演示应用程序,但是这项额外的工作将使您清楚地了解Packer在何处以及如何帮助自动化以支持开发和基础架构流程。

Go into the /scripts directory and open the setup.sh script with your favorite editor and add this Shell script code to the bottom.

进入/scripts目录,并使用您喜欢的编辑器打开setup.sh脚本,并将此Shell脚本代码添加到底部。

# Restart PHP-FPM (only needed so the Symfony demo install works properly) service php7.0-fpm restart #Install SQLite Database apt-get install -y sqlite3 #Install Symfony Installer echo "Installing Symfony" echo "Downloading Symfony installer" curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony echo "Changing permissions on symfony installer for execution" chmod a+x /usr/local/bin/symfony #Install Symfony Demo echo "Installing Symfony Demo" echo "Changing to www directory" cd /var/www/html echo "Creating the demo" symfony demo #Change ownership of the project to the www-data user echo "Changing ownership of symfony_demo" chown -R www-data:www-data /var/www/html/symfony_demo #Change configuration files to preconfigured ones #To get Nginx to work with Symfony and move the doc root to Symfony's /web directory echo "Copying new Nginx config file" cp /tmp/config/default-symfony /etc/nginx/sites-available/default

The code above is basically what we’d need to do to set up Symfony, PHP, and Nginx properly.

上面的代码基本上是我们需要正确设置Symfony,PHP和Nginx的代码。

Comments have been added to explain what will be happening during these provisioning steps.

添加了注释,以解释在这些设置步骤中将发生的情况。

Once you’ve added the code above and saved it, let’s let Packer do the heavy lifting once more.

一旦添加了上面的代码并保存了代码,就让Packer再次完成繁重的工作。

Go back up a level in the file system to where we have our jessie64.json file, and enter the command we entered earlier.

返回文件系统中的某个级别到我们拥有jessie64.json文件的位置,然后输入我们之前输入的命令。

packer build jessie64.json

You can now go and take care of other business or get another coffee (or do some walking on your treadmill). This building of the new Symfony demo images will take a while.

现在,您可以去做其他事情,或者去喝杯咖啡(或者在跑步机上散步)。 新Symfony演示图像的构建将花费一些时间。

Once done, you should be greeted with a screen looking something like this.

完成后,您应该会看到类似如下的屏幕。

Now that the images are built and ready for us, we can start the openstack image as a new server again on Rackspace.

现在已经构建了映像并为我们准备好了,我们可以再次在Rackspace上将openstack映像作为新服务器启动。

For your Virtualbox VM, enter the two commands again, which were mentioned above under “Create the Virtualbox VM”.

对于您的Virtualbox VM,再次输入两个命令,以上在“创建Virtualbox VM”中已提到。

With the Rackspace server, you will get a new IP. Enter

使用Rackspace服务器,您将获得一个新IP。 输入

http://new.server.ip

and you should see the Symfony demo app running.

并且您应该看到Symfony演示应用程序正在运行。

On your local VM, you can enter the following:

在本地VM上,您可以输入以下内容:

http://192.168.33.10

And you will see the same thing.

并且您将看到相同的事情。

Cool, huh?

酷吧?

结论 (Conclusion)

We’ve gone down memory lane and then came back to the present. Hopefully, we got you to understand the reasons for computer virtualization, and for the creation of software like Vagrant and Packer, as well as the connection between the two.

我们走了记忆的道路,然后回到现在。 希望我们能使您了解计算机虚拟化的原因,以及诸如Vagrant和Packer之类的软件的创建以及两者之间的联系。

If you were successful in creating the two servers, then you can also imagine the advantages Packer can offer you both as a member of DevOps and as a developer. They include but aren’t limited to:

如果成功创建了两个服务器,那么您也可以想象Packer作为DevOps成员和开发人员都可以为您提供的优势。 它们包括但不限于:

Production parity – a development environment pretty much exactly like the one in production.

生产平价 –一种非常类似于生产环境的开发环境。

Time savings – the images are “pre-baked” with software and configuration, which you no longer have to manually add.

节省时间 –使用软件和配置“预烘焙”图像,而无需手动添加。

Environment testing – The finished images can be considered “complete” and tested, avoiding human error.

环境测试 –可以将完成的图像视为“完整”的图像并进行了测试,避免了人为错误。

Environment versioning – As you change the environment, with Git, you can version the different packer builds.

环境版本控制 –当您使用Git更改环境时,可以对不同的打包程序版本进行版本控制。

Supports “Immutable Architecture” – where instead of building on or modifying an existing VM, which can be a source of mistakes, the VM instances are simply “destroyed” and new ones are created.

支持“不可变的体系结构” -在此基础上,VM实例被“破坏”并创建了新的实例,而不是在现有VM上进行构建或修改(这可能是错误的来源)。

Hopefully, you’ve enjoyed the article and the exercise of building images with Packer. Let us know what you think about Packer in the comments below.

希望您喜欢这篇文章以及使用Packer构建图像的练习。 在下面的评论中,让我们知道您对Packer的看法。

翻译自: https://www.sitepoint.com/hashicorps-packer-is-it-something-for-php-developers/

moles-packer

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)