你的第一个无家可归的盒子

    科技2025-04-01  21

    Virtual Machines, or VMs for short, are fairly popular in the Software industry and among developers. And for good reason — who would mind a disposable OS, within an OS! Right? They are fairly easy to use, allow you to run multiple OS’s on a single host OS, completely independent of each other (okay, there are some limitations in performance compared to bare metal performance, but let’s leave that aside for now).

    虚拟机(简称VM)在软件行业和开发人员中相当流行。 并且有充分的理由-谁会介意在一个OS中使用一次性OS! 对? 它们相当易于使用,允许您在彼此完全独立的单个主机操作系统上运行多个操作系统(好吧,与裸机性能相比,性能存在一些限制,但现在让我们暂且不谈)。

    One of the bigger problems with using VMs that I felt prevented them from being as convenient as, say, an actual local environment, was the initial setup. Setting up a VM and getting it ready to use for development inside a VM manager like VirtualBox wasn’t exactly a click-click-done process. You needed to:

    我认为使用VM时遇到的最大问题之一就是它们的初始安装无法像实际的本地环境那样方便。 设置虚拟机并准备将其准备用于VirtualBox之类的虚拟机管理器中的开发并非完全是单击即可完成的过程。 您需要:

    Find and download an installation image file for your OS of choice.

    查找并下载所选操作系统的安装映像文件。 Create your VM.

    创建您的VM。 Boot up your VM, and install each development software and package you needed.

    启动您的VM,并安装所需的每个开发软件和软件包。

    Yeah, that takes quite some time. But when you’re someone who wants a consistent development environment without having to install an infinite amount of software, from programming language runtimes to database servers on your local machine, VMs are definitely a very attractive solution. If you’re someone like this, and you’ve been unaware, allow me to introduce you to the extreme convenience that is Vagrant.

    是的,这需要相当长的时间。 但是,当您想要一个一致的开发环境而不必安装无限数量的软件时(从编程语言运行时到本地计算机上的数据库服务器),VM绝对是一个非常有吸引力的解决方案。 如果您是这样的人,并且您一直不知道,请允许我向您介绍Vagrant的极大便利。

    Using Vagrant requires you to install a single installable package, which you can get from here for your OS. The installation process is trivial really, but do remember to ensure that virtualization support is enabled in your BIOS settings. Vagrant also requires a VM “provider”, such as VirtualBox or VMWare Fusion to be installed on your system. We’ll be using the free and open-source VirtualBox.

    要使用Vagrant,您需要安装一个可安装的程序包,您可以从此处从中获取该程序包。 安装过程确实很简单,但请务必确保在BIOS设置中启用了虚拟化支持。 Vagrant还需要在系统上安装VM“提供程序”,例如VirtualBox或VMWare Fusion。 我们将使用免费的开源VirtualBox。

    First and foremost, what is Vagrant? The official documentation briefly calls it “ the command line utility for managing the lifecycle of virtual machines”. That’s all you really need to know. Let’s work with it to gain an idea of what it can do for you.

    首先,什么是流浪汉? 官方文档将其简称为“用于管理虚拟机生命周期的命令行实用程序”。 这就是您真正需要知道的。 让我们使用它来了解它可以为您做什么。

    A Vagrant box, which is nothing but a Vagrant-controlled VM, is brought up based on a configuration file, which is called, by convention, a Vagrantfile (Yes, like a Dockerfile!). A Vagrantfile has 5 main config blocks:

    一个Vagrant框就是一个由Vagrant控制的VM,它是基于一个配置文件启动的,该配置文件按照惯例被称为Vagrantfile (是的,就像Dockerfile!)。 一个Vagrantfile有5个主要的配置块:

    box: The “box” to start with. A box is like a template that forms the base for your Vagrant box. Essentially, what “images” are to a Dockerfile, a “box” is to a Vagrantfile. They contain a base OS and, optionally, additional configurations and installed software/packages. To see openly available Vagrant boxes, you can go here.

    box:以“ box”开头。 盒子就像模板一样,构成了Vagrant盒子的基础。 本质上,对于Dockerfile,什么是“映像”,对于Vagrantfile,是一个“盒子”。 它们包含基本OS,以及可选的其他配置和已安装的软件/软件包。 要查看公开可用的Vagrant框,请转到此处。

    provider: As mentioned earlier, Vagrant supports a number of VM managers or “providers”, including but not limited to VirtualBox, VMWare Fusion, and Docker. Not all providers support all base boxes, but you do not need to worry about that yet. The documentation can help you understand more about the supported providers.

    提供者: 如前所述,Vagrant支持许多VM管理器或“提供程序”,包括但不限于VirtualBox,VMWare Fusion和Docker。 并非所有提供程序都支持所有基本框,但是您不必为此担心。 该文档可以帮助您了解有关支持的提供程序的更多信息。

    network: This is the network configuration, such as forwarding ports to the host machine, binding the VM to particular private IP, etc.

    网络:这是网络配置,例如将端口转发到主机,将VM绑定到特定的专用IP等。

    synced_folder: Synced folders parallel volume bind mounts from docker. What they allow, essentially, is to sync directories between your host and the VM. This could allow you to have setups where you may, for example, edit code on your local code editor but see changes reflected in real-time inside the VM.

    synced_folder:从docker同步的文件夹并行卷绑定挂载。 从本质上讲,它们允许在主机和VM之间同步目录。 这可以让您进行一些设置,例如,可以在本地代码编辑器上编辑代码,但是可以实时查看虚拟机中反映的更改。

    provision: A vanilla base box is rarely of any use without necessary packages installed and the environment configured. The provision block is where you “provision” your VM with necessary changes, and installations through, say, shell scripts.

    规定:如果没有安装必需的软件包和配置环境,则很少使用香草基盒。 供应块是您通过必要的更改和安装(例如通过shell脚本)“供应” VM的地方。

    You don’t need to use all the blocks in all cases. Using Vagrant regularly, you’ll eventually gain a feel of what you need and what you don’t. The documentation is always there to help you.

    您无需在所有情况下都使用所有块。 定期使用Vagrant,您最终会感觉到您需要什么和不需要什么。 该文档始终可以为您提供帮助。

    Bored with the theory? Let’s write some config files! Create a text file names Vagrantfile (no extensions). Now, Vagrantfiles are Ruby-based, I believe. But fret not. I do not know Ruby. You don’t need to know Ruby either.

    厌倦了理论? 让我们写一些配置文件! 创建一个名称为Vagrantfile的文本文件(无扩展名)。 我相信,现在,Vagrantfiles是基于Ruby的。 但不用担心。 我不认识露比您也不需要了解Ruby。

    Let’s write up the Vagrantfile:

    让我们来编写Vagrantfile:

    Don’t be confused. It’s super simple. Let’s go line by line (or block by block).

    不要困惑。 非常简单。 让我们逐行(或逐块)进行。

    The outermost block simply specifies the Vagrant configuration version. Until Vagrant updates a major version, this should remain the same across Vagrantfiles. Inside it, you define the 5 blocks for your Vagrantfile.

    最外面的块仅指定Vagrant配置版本。 在Vagrant更新主版本之前,此版本在Vagrantfile中应保持相同。 在其中,您为Vagrantfile定义了5个块。 Vagrant.configure("2") do |config| # # Rest of the config goes here #end

    Next, we define the box to use as the base. We use an Ubuntu 18.04 box, listed on https://app.vagrantup.com.

    接下来,我们定义要用作基础的框。 我们使用https://app.vagrantup.com上列出的Ubuntu 18.04框。

    config.vm.box = "ubuntu/bionic64" We now have told the Vagrantfile the OS we want. But Vagrant can’t create VMs itself. It needs a provider. Let’s tell Vagrant to use VirtualBox. The provider box will also allow us to configure the resources used by the VM. Here, we’ll explicitly tell Vagrant to use 2GB of memory and 4 vCPUs.

    现在我们已经告诉Vagrantfile我们想要的操作系统。 但是,Vagrant本身无法创建VM。 它需要一个提供者。 让我们告诉Vagrant使用VirtualBox。 provider框还将允许我们配置VM使用的资源。 在这里,我们将明确告诉Vagrant使用2GB内存和4个vCPU。 config.vm.provider "virtualbox" do |vb| vb.memory = 2048 vb.cpus = 4 end The VM would work even without a networking block. But just so we could enjoy our final result later, we’ll bind the VM to an address in the “private_network”.

    即使没有网络障碍,虚拟机也可以正常工作。 但是,这样我们以后才能享受最终结果,我们将VM绑定到“ private_network”中的地址。 config.vm.network "private_network", ip: "192.168.33.10" The synced_folder config, like the network config, is one of those configs which you use as needed. It can be left out if you feel you don’t need to sync directory(s) between the host and the VM. We bing a folder in our working directory names “var-www-html” to the directory /var/www/html inside the VM, and set up file and directory permissions in the :mount_options block. I believe this syntax is ruby-based, so it isn’t the most intuitive, but hopefully, you can make sense of what is happening.

    像网络配置一样,synced_folder配置是您根据需要使用的那些配置之一。 如果您认为不需要在主机和VM之间同步目录,则可以将其忽略。 我们在工作目录中将文件夹“ var-www-html”绑定到VM内的目录/ var / www / html,并在:mount_options块中设置文件和目录权限。 我相信此语法是基于ruby的,因此它不是最直观的,但是希望您可以理解正在发生的事情。 config.vm.synced_folder "./var-www-html", "/var/www/html", :mount_options => ["dmode=777", "fmode=666"] Finally, we can configure the vanilla VM. This we do via a bash script. I’ll use a bash script to install LAMP server packages in the VM, named “bootstrap.sh” kept in the working directory alongside the Vagrantfile.

    最后,我们可以配置原始VM。 这是通过bash脚本完成的。 我将使用bash脚本在VM中安装LAMP服务器软件包,名为“ bootstrap.sh”,该软件包与Vagrantfile一起保存在工作目录中。 config.vm.provision "shell", path: "bootstrap.sh"

    The shell script is trivial. It is nothing but a series of apt-get install commands. Note that the script is run as root, so using sudo is unnecessary:

    Shell脚本很简单。 它不过是一系列的apt-get install命令。 请注意,该脚本以root用户身份运行,因此无需使用sudo:

    apt-get updateapt-get install apache2 -yapt-get install mysql-server -yapt-get install php libapache2-mod-php php-opcache php-cli php-gd php-curl php-mysql -ysystemctl restart apache2

    What we should have now is a directory structure looking like this:

    我们现在应该拥有的目录结构如下所示:

    | var-www-html/| bootstrap.sh| Vagrantfile

    Now, we let Vagrant do its magic. Go to your command line and type in the magic words.

    现在,我们让Vagrant发挥其魔力。 转到命令行,然后输入魔术字。

    vagrant up

    What you should see is a ton of console output going by, ending with you back in your terminal prompt. Checking VirtualBox will tell you that the VM definitely up (the VM’s name may vary).

    您应该看到的是大量控制台输出,最后在终端提示符下返回。 检查VirtualBox会告诉您VM确实已启动(VM的名称可能有所不同)。

    And since we installed the Apache server on the VM as well, visiting the IP you used in your network config (192.168.33.10 in my case) should greet you with the default Apache page. That is confirmation that out provision (or at least some of it) worked.

    并且由于我们也在VM上安装了Apache服务器,因此访问您在网络配置中使用的IP(在我的情况下为192.168.33.10)应该会向您显示默认的Apache页面。 这证实了拨备(或至少其中的一部分)有效。

    Feels magical? You haven’t yet noticed the index.html file that magically appeared inside var-www-html/. The file was created during the Apache server’s installation and represents the above page, which gets served to you when you visit the VM’s private IP. Let’s change the index.html file.

    感觉神奇吗? 您尚未注意到神奇地出现在var-www-html /中的index.html文件。 该文件是在Apache服务器的安装过程中创建的,并代表上述页面,当您访问VM的专用IP时,该页面将提供给您。 让我们更改index.html文件。

    Replace all content inside var-www-html/index.html with:

    将var-www-html / index.html中的所有内容替换为:

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>My Own Default Page</title></head><style>body { background: #333; color: #fff;}</style><body> <h1>Hello World</h1></body></html>

    Go back to the VM IP inside your browser, or simply reload the page if you’re already there. You should now see your very own page. “Hello World” in dark mode 💯.

    返回浏览器中的VM IP,或者如果已经存在,则只需重新加载页面即可。 现在,您应该看到自己的页面。 在黑暗模式下“ Hello World””。

    It really is cool, isn’t it? The benefits of declarative configuration approaches cannot be overstated. The rise of declarative configurations in technologies like Terraform, Docker, Kubernetes, and Ansible among many others is testimony to their usefulness.

    真的很酷,不是吗? 声明性配置方法的优点不能被夸大。 诸如Terraform,Docker,Kubernetes和Ansible等技术中的声明式配置的兴起证明了它们的实用性。

    Using VMs may or may or be for you, and their need may vary from person to person and company to company. But there is no denying the convenience Vagrant brings in.

    使用VM可能会或可能会或适合您,并且VM的需求可能因人而异,因公司而异。 但是无可否认,Vagrant带来了便利。

    I’ll leave you with some other basic commands used for Vagrant VMs. Most of these need you to be in the same working directory as the Vagrantfile.

    我将为您提供用于Vagrant VM的其他一些基本命令。 其中大多数都需要您与Vagrantfile位于同一工作目录中。

    vagrant destroy forcefully destroys the VM. You won’t be able to reboot it. vagrant up again should create a new VM.

    vagrant destroy强力破坏虚拟机。 您将无法重新启动它。 再次vagrant up ,应该创建一个新的VM。

    vagrant halt halts or suspends the VM, so that you may later start it again with vagrant resume

    vagrant halt 停止或挂起VM,以便您以后可以使用vagrant resume再次启动它

    vagrant init will generate for you a basic Vagrantfile with helpful comments. It may be a good starting point for your VMs.

    vagrant init 将为您生成带有有用注释的基本Vagrantfile。 对于您的VM来说,这可能是一个很好的起点。

    vagrant ssh ssh’s you into the VM, if you feel the need to do so.

    vagrant ssh 如果您有需要,可以将ssh引导到VM中。

    That’s it for now, dear reader. Until the next time I get excited about something enough to write about it, take care and happy learning 😄

    亲爱的读者,仅此而已。 直到下次我对足以写些东西的东西感到兴奋,保重和愉快的学习😄

    翻译自: https://medium.com/swlh/your-first-vagrant-box-becaf150167c

    相关资源:四史答题软件安装包exe
    Processed: 0.016, SQL: 8