博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
深圳哪个区的it工作多_如何创建虚拟IT工作区
阅读量:2533 次
发布时间:2019-05-11

本文共 30243 字,大约阅读时间需要 100 分钟。

深圳哪个区的it工作多

This article is based on a chapter from my free online book, . There’s lots more where that came from at my Bootstrap IT site, including links to my book, , and a hybrid course called that’s made up of more than two hours of video and around 40% of the text of Linux in Action.

本文基于我的免费在线书《 。 在我的Bootstrap IT网站上还有很多其他内容,包括我的书《 链接以及名为的混合课程,该课程由两个多小时的视频和大约40%的Linux文字组成在行动。

Have you ever completely hosed your workstation or main laptop while testing out a new technology? Or do you have so many packages and their dependencies installed that you no longer have any idea what it is that makes your experiments succeed or fail?

在测试新技术时,您是否曾经完全用软管固定过工作站或主笔记本电脑? 还是您安装了如此多的软件包及其依赖项,以至于您不知道是什么导致实验成功或失败?

Virtualization can provide you with a clean, quick, lightweight environment where you can test to your heart’s content. You’re not even restricted to the operating system that’s running on your host machine, so this is also a great way to see how things work on multiple platforms.

虚拟化可以为您提供一个干净,快速,轻便的环境,您可以在其中测试自己的内心世界。 您甚至不局限于主机上运行的操作系统,因此这也是查看事物在多个平台上如何工作的好方法。

When you’re done — or if everything collapses in glorious failure — you can simply kill off your environment and fire up a new one to replace it. No harm done.

完成后-如果一切都因光荣失败而崩溃-您可以简单地销毁您的环境并启动新的环境来替换它。 没有伤害。

In this article, I’ll explore VirtualBox and Linux Containers (LXCs) as tools for providing easily replicated and sharable virtual operating system environments. I’ll finish up with a few thoughts about writing code and building software stacks directly within virtual environments

在本文中,我将探讨VirtualBox和Linux容器(LXC)作为用于提供易于复制和共享的虚拟操作系统环境的工具。 最后,我将对在虚拟环境中直接编写代码和构建软件堆栈的一些想法进行总结。

虚拟盒子 (VirtualBox)

So say “hello” to Oracle’s free VirtualBox cross-platform hypervisor product and, in particular, to some advanced tricks for squeezing more value out of your (no-cost) investment. VirtualBox is something you can use on any operating system to create virtualized computers running just about any flavor of Windows or Linux.

因此,对Oracle的免费VirtualBox跨平台虚拟机管理程序产品打个招呼,尤其是一些高级技巧,可以从(免费)投资中获取更多价值。 您可以在任何操作系统上使用VirtualBox来创建运行几乎任何Windows或Linux风格的虚拟化计算机。

You do still have to get a license for any Windows image you decide to run, although you’re generally free to install and use copies without activating the license for a month or so.
尽管您通常可以自由安装和使用副本,而无需激活许可证大约一个月,但您仍然必须获得决定运行的任何Windows映像的许可证。

If you’ve already read my book and some of this material feels a bit familiar, it’s because this chapter is a scaled down version of Linux in Action’s chapter 2. The content has been made available through kind permission from Manning Publications.

如果您已经阅读了《 一书,并且对其中的某些内容感到有点熟悉,那是因为本章是Linux在Action第2章中的精简版。其内容已通过Manning Publications的许可获得。

VirtualBox入门 (Getting started with VirtualBox)

VirtualBox provides an environment within which you can launch as many virtual computers as your physical system resources can handle. And, of course, it’s a particularly useful tool for safely testing and learning new administration skills — which is our primary goal right now.

VirtualBox提供了一个环境,您可以在该环境中启动物理系统资源可以处理的尽可能多的虚拟计算机。 而且,当然,它是安全测试和学习新管理技能的特别有用的工具-这是我们目前的主要目标。

Installing VirtualBox

安装VirtualBox

Want to try all this out from a Windows PC? Head over to the and download the executable archive. Click the file you’ve downloaded and then work through a few setup steps (the default values should all work). Finally, you’ll be asked whether you’re OK with a possible reset of your network interfaces and then whether you want to install VirtualBox. Of course you are and do.

是否想在Windows PC上尝试所有这些? 转到并下载可执行存档。 单击您已下载的文件,然后完成几个设置步骤(默认值都应该起作用)。 最后,系统会询问您是否可以重新设置网络接口,然后是否要安装VirtualBox。 你当然会做。

Getting VirtualBox happily installed on an Ubuntu Linux machine is even simpler. Just two commands:

在Ubuntu Linux机器上愉快地安装VirtualBox更加简单。 只需两个命令:

sudo apt updatesudo apt install virtualbox

定义虚拟机 (Defining a virtual machine)

I’m not sure whether you’ve ever put together a physical computer from components, but it can get involved. Defining a new virtual machine within VirtualBox works pretty much the same way. The only significant difference is that, rather than having to get down on your hands and knees with a flashlight clenched between your teeth to manually add RAM and a storage drive to your box, VirtualBox lets you define your VM’s “hardware” specs by clicking your mouse.

我不确定您是否曾经通过组件组装一台物理计算机,但是它可以参与其中。 在VirtualBox中定义新的虚拟机的工作方式几乎相同。 唯一的显着区别是,不必用手电筒夹在牙齿之间来手动将RAM和存储驱动器添加到框中,VirtualBox可以让您通过单击虚拟机来定义VM的“硬件”规格老鼠。

After clicking New in the VirtualBox interface, you will give the VM you’re about to build a descriptive name and, as you can see in the figure, the software should be able to correctly populate the Type and Version fields automatically. The Type and Version you select here won’t install an actual operating system, but are simply used to apply appropriate hardware emulation settings.

在VirtualBox界面中单击“新建”后,将为您要建立描述性的虚拟机提供名称,并且如图所示,该软件应能够自动正确填充“类型”和“版本”字段。 您在此处选择的类型和版本不会安装实际的操作系统,而只是用于应用适当的硬件仿真设置。

On the next screen you’ll allocate RAM to your VM. Unless you’re planning something particularly demanding — like hosting a container swarm or running a busy web server — the default amount (768 MB) should be fine. You can certainly give it more RAM if necessary, but don’t forget to leave enough over for your host machine and any other VMs that might already live on it. So if your host only has 4 GB of physical RAM, you probably won’t want to give half of that to your VM.

在下一个屏幕上,您将为VM分配RAM。 除非您计划进行一些特别苛刻的操作,例如托管容器群或运行繁忙的Web服务器,否则默认大小(768 MB)应该就可以了。 当然,如果需要,您当然可以为其提供更多的RAM,但是不要忘记为主机和可能已经存在于其上的任何其他VM留出足够的空间。 因此,如果主机仅具有4 GB的物理RAM,则可能不希望将其中的一半分配给VM。

Keep these limits in mind if you eventually decide to run multiple VMs at a time — something that will be useful for testing more complex infrastructure projects. Even if each VM is only using the default amount of memory, two or three of them can start to eat away at RAM needed for normal host operations.

如果最终决定一次运行多个VM,请牢记这些限制-这对于测试更复杂的基础结构项目很有用。 即使每个VM仅使用默认的内存量,它们中的两个或三个也可以开始消耗正常主机操作所需的RAM。

定义您的虚拟硬盘 (Defining your virtual hard disk)

What’s a computer without a hard disk? The VirtualBox setup process will now ask you if you’d like to create a new virtual disk for your VM or use one that already exists. There may be times when you want to share a single disk between two VMs but for this exercise I’m guessing that you’ll want to start from scratch. So select “Create a virtual hard disk now”.

什么是没有硬盘的计算机? 现在,VirtualBox设置过程将询问您是否要为VM创建新的虚拟磁盘或使用已经存在的虚拟磁盘。 有时您可能希望在两个VM之间共享一个磁盘,但是对于本练习,我猜测您将希望从头开始。 因此,选择“立即创建虚拟硬盘”。

The next screen lets you choose a hard disk file format for the disk you’re about to create. Unless you’re planning to eventually export the disk to use within some other virtualization environment, the default VirtualBox Disk Image (VDI) format will work fine.

在下一个屏幕中,您可以为要创建的磁盘选择硬盘文件格式。 除非您打算最终导出磁盘以在其他虚拟化环境中使用,否则默认的VirtualBox磁盘映像(VDI)格式会正常工作。

I’ve also never regretted going with the default “Dynamically allocated” option to determine how the virtual drive will consume space on the host. By “dynamic” they mean that space on the host storage disk will be allocated to the VM only as-needed. Should the VM disk usage remain low, less host space will be allocated.

我也从不后悔使用默认的“动态分配”选项来确定虚拟驱动器将如何占用主机上的空间。 “动态”是指主机存储磁盘上的空间仅根据需要分配给VM。 如果VM磁盘使用率保持较低水平,则将分配较少的主机空间。

A fixed sized disk, on the other hand, will be given its full maximum amount of space right away, regardless of how much it’s actually using. The only advantage of “Fixed size” is application performance, but since I generally only use VirtualBox VMs for testing and experiments, I’m fine avoiding the trade off.

另一方面,无论实际使用多少磁盘,固定大小的磁盘都会立即获得其最大的最大空间量。 “固定大小”的唯一优点是应用程序性能,但是由于我通常只将VirtualBox VM用于测试和实验,因此可以避免这种折衷。

When VirtualBox knows it’s Linux you’re after — and because Linux makes such efficient use of storage space — VirtualBox will probably offer you only 8 GB of total disk size on the next screen (shown below). Unless you’ve got unusually big plans for the VM (like, say, you’re going to be working with some serious database operations), that will probably be fine. On the other hand, if you had chosen Windows as your operating system, the default choice would have been 25 GB, and for good reason: Windows isn’t shy about demanding lots of resources. That’s a great illustration of one way Linux is so well suited to virtual environments.

当VirtualBox知道它是Linux时,您就会来了-并且因为Linux如此有效地利用了存储空间-VirtualBox在下一个屏幕上可能会为您提供8 GB的总磁盘大小(如下所示)。 除非您有非常大的虚拟机计划(例如,您将要进行一些严肃的数据库操作),否则可能会很好。 另一方面,如果您选择Windows作为操作系统,则默认选择为25 GB,这有充分的理由:Windows不会要求大量资源。 这很好地说明了Linux非常适合虚拟环境的一种方式。

You can also edit the name and location VirtualBox will use for your disk on this screen.

您还可以在此屏幕上编辑VirtualBox将用于磁盘的名称和位置。

When you’re done, click Create and the new VM will appear in the list of VMs on the left side of the VirtualBox manager. Enjoy the taste of success, but you’re not done: that was just the machine. Now you’ll need an operating system to bring it to life.

完成后,单击“创建”,新的VM将出现在VirtualBox管理器左侧的VM列表中。 享受成功的滋味,但您还没有完成:那仅仅是机器。 现在,您将需要一个操作系统来使其栩栩如生。

下载操作系统 (Downloading an operating system)

Now that you’ve defined your new VM’s virtual hardware profile, here’s what still needs doing:

现在,您已经定义了新VM的虚拟硬件配置文件,这仍然是需要做的事情:

  • Download a file (in ISO format) containing the image of the operating system you want to use.

    下载包含您要使用的操作系统映像的文件(ISO格式)。
  • Boot the new VM using a virtual DVD drive containing the ISO you downloaded

    使用包含您下载的ISO的虚拟DVD驱动器引导新VM
  • Work through the standard OS installation process

    完成标准操作系统安装过程
  • Boot the VM and launch the OS you just installed

    引导VM并启动刚刚安装的OS

You’ll need to download a .ISO file containing the operating system files and installation program. Finding the right file is usually just a matter of searching the internet for the distribution name and the word “download”. In the case of Ubuntu, you could alternatively just go to the page and click on the Downloads tab as you see in the figure. Notice the various flavors of Ubuntu that are available. If you’re going to be using this VM for administration tasks, then the small and fast Server version is probably a better choice than Desktop.

您需要下载一个包含操作系统文件和安装程序的.ISO文件。 查找正确的文件通常只是在互联网上搜索发行名称和单词“ download”的问题。 对于Ubuntu,您也可以转到页面,然后单击“下载”选项卡,如图所示。 请注意可用的各种Ubuntu版本。 如果要使用此VM来执行管理任务,则小型且快速的Server版本可能比Desktop更好。

Validate the ISO archive you downloaded

验证您下载的ISO档案

Large files can sometimes become corrupted during the download process. If even a single byte within your .ISO has been changed, there’s a chance the installation simply won’t work. Because you don’t want to invest time and energy only to discover that there was a problem with the download, it’s always a good idea to immediately calculate the checksum (or hash) for the .ISO you’ve downloaded to confirm that everything is as it was.

大文件有时会在下载过程中损坏。 即使您的.ISO中的单个字节已更改,安装也可能根本无法正常工作。 因为您不想只花时间和精力去发现下载存在问题,所以立即为已下载的.ISO计算校验和(或散列)以确认所有内容都是一个好主意。因为它是。

To do that, you’ll need to get the appropriate SHA or MD5 checksum — which is a long string looking something like this:

为此,您需要获取适当的SHA或MD5校验和-这是一个长字符串,如下所示:

4375b73e3a1aa305a36320ffd7484682922262b3

4375b73e3a1aa305a36320ffd7484682922262b3

In the case of Ubuntu, getting that will mean going to the web page at , clicking the directory matching the version you downloaded, and then clicking one of the links to a checksum (like, for instance, SHA1SUMS).

对于Ubuntu,要获取该信息将意味着转到上的网页,单击与您下载的版本匹配的目录,然后单击指向校验和的链接之一(例如SHA1SUMS)。

You should compare the appropriate string from that page with the results of a command run from the same directory as your downloaded .ISO that might look like this:

您应该将该页面上的适当字符串与从与下载的.ISO相同的目录中运行的命令的结果进行比较,该命令可能如下所示:

$ shasum ubuntu-16.04.2-server-amd64.iso

$ shasum ubuntu-16.04.2-server-amd64.iso

If they match, you’re in business. If they don’t — and you’ve double checked to make sure you’re looking at the right version — then you might have to download the .ISO a second time.

如果他们匹配,则说明您在做生意。 如果它们不支持,并且您已经仔细检查以确保您使用的是正确的版本,那么您可能不得不第二次下载.ISO。

安装操作系统 (Installing an operating system)

Once your .ISO file is in place, head back to VirtualBox. With the VM you just created highlighted in the left panel, click the green Start button at the top of the app. You’ll be prompted to select a .ISO file from your file system to use as a virtual DVD drive. Naturally, you’ll choose the one you just downloaded. The new VM will read this DVD and launch an OS installation.

放置好.ISO文件后,请回到VirtualBox。 在左侧面板中突出显示刚刚创建的VM,然后单击应用顶部的绿色“开始”按钮。 系统将提示您从文件系统中选择一个.ISO文件,以用作虚拟DVD驱动器。 自然,您将选择刚下载的一个。 新的VM将读取此DVD并启动OS安装。

Most of the time the installation process will go fine. However, describing solutions to each of the many small things that could go wrong would require a whole whack of articles. So if you do have trouble you can consult the documentation and guides that are available for your operating system or share your question with the online community.
大多数情况下,安装过程会正常进行。 但是,描述可能会出错的许多小问题的解决方案将需要大量文章。 因此,如果您确实遇到问题,可以查阅适用于您的操作系统的文档和指南,或与在线社区共享您的问题。

When everything is nicely installed, there might still be a few more things to take care of before you can successfully boot into your VM. With your VM’s entry highlighted, click the yellow Settings icon. Here’s where you can play with your VM’s environment and hardware settings.

一切安装妥当后,在成功启动到VM之前,可能还需要处理一些其他事项。 突出显示您的VM条目,然后单击黄色的“设置”图标。 在这里,您可以使用VM的环境和硬件设置。

Clicking on Network, for example, allows you to define network connectivity. If you want your VM to have full internet access through the host machine’s network interface then, as shown below, you can select “Bridged Adapter” from the Attached drop-down, and then the name of your host’s adapter.

例如,单击“网络”可以定义网络连接。 如果希望您的VM通过主机的网络接口具有完全的Internet访问权限,则如下所示,可以从“附件”下拉列表中选择“桥接适配器”,然后选择主机适配器的名称。

Using a bridged adapter might not always be your first choice, and it might sometimes present a security risk. In fact, choosing “NAT Network” is a more common way to provide a VM with internet access. However a bridged network is the easiest way to gain full network connectivity so, for testing at least, it’s a useful approach.
使用桥接适配器可能并不总是您的首选,并且有时可能会带来安全风险。 实际上,选择“ NAT网络”是为VM提供Internet访问的一种更常见的方法。 但是,桥接网络是获得完整网络连接的最简单方法,因此至少对于测试而言,这是一种有用的方法。

These next sections are a bit bonus-y, but who doesn’t like free stuff? I’m going to tell you about two related tricks: how to organize your VirtualBox VMs to make spinning up new ones as quick as possible, and how to use the command line to share VMs across a network.

接下来的几节是一些奖励,但是谁不喜欢免费的东西呢? 我将向您介绍两个相关的技巧:如何组织VirtualBox VM以使新的VM尽快旋转,以及如何使用命令行在网络上共享VM。

克隆虚拟机以快速入门 (Cloning VMs for quick starts)

One of the most obvious advantages of working with VMs is the ability to quickly access a fresh, clean OS environment. But if accessing that environment requires going through the full install process, than I don’t see a whole lot of “quickly.”

使用VM的最明显优势之一是能够快速访问全新,干净的OS环境。 但是,如果要访问该环境需要完成整个安装过程,那么我不会很快看到很多东西。

Until, that is, you throw cloning into the mix. Why not keep your original VM in its clean post-install state, and simply create an identical clone whenever you want to do some real work?

直到将克隆放入混合中。 为什么不将原始VM保持在干净的安装后状态,并在需要进行实际工作时简单地创建相同的克隆?

It’s easy. Take another look at the VirtualBox App. Select the (stopped) VM you want to use as a master copy, click the Machine menu link, and then Clone. You’ll confirm the name you’d like to give your clone and then, after clicking Next, whether you want to create a Full clone (meaning entirely new file copies will be created for the new VM) or Linked clone (meaning the new VM will share all the base files with its master, while maintaining your new work separately).

这很容易。 再看看VirtualBox应用程序。 选择要用作主副本的(停止的)VM,单击“计算机”菜单链接,然后单击“克隆”。 您将确认要提供克隆的名称,然后单击“下一步”后,是要创建完整克隆(意味着将为新的VM创建全新的文件副本)还是链接克隆(意味着将新的副本VM将与其主数据库共享所有基本文件,同时分别维护新工作)。

Selecting the Linked option will go much faster and take up much less room on your hard disk. The only down side is that you’ll be unable to move this particular clone to a different computer later. It’s your choice.

选择链接选项将更快,并且占用的硬盘空间更少。 唯一的缺点是您以后将无法将该特定克隆移动到另一台计算机。 这是你的选择。

Now click Clone, and a new VM will show up in the VM panel. Start it the way you normally would and then log in using the same credentials you set on the master.

现在单击“克隆”,新的VM将显示在“ VM”面板中。 以通常的方式启动它,然后使用在主服务器上设置的相同凭据登录。

从命令行管理VM (Managing VMs from the command line)

VirtualBox comes with its own command line shell that’s invoked using vboxmanage. Why bother with the command line? Because, among other benefits, it will allow you to work on remote servers - which can greatly increase the scope of possible projects. To see how vboxmanage works, use list vms to list all the VMs currently available on your system. Here's how that looks on my machine:

VirtualBox带有自己的命令行外壳程序,可使用vboxmanage调用。 为什么要打扰命令行? 因为除其他好处外,它还使您可以在远程服务器上工作-这可以大大增加可能的项目范围。 要查看vboxmanage工作方式,请使用list vms列出系统上当前可用的所有VM。 这是我的机器上的样子:

$ vboxmanage list vms“Ubuntu-16.04-template” {c00d3b2b-6c77–4919–85e2–6f6f28c63d56}“centos-7-template” {e2613f6d-1d0d-489c-8d9f-21a36b2ed6e7}“Kali-Linux-template” {b7a3aea2–0cfb-4763–9ca9–096f587b2b20}“website-project” {2387a5ab-a65e-4a1d-8e2c-25ee81bc7203}“Ubuntu-16-lxd” {62bb89f8–7b45–4df6-a8ea-3d4265dfcc2f}

vboxmanage clonevm will pull off the same kind of clone action I described above using the GUI. Here, I'm making a clone of the Kali-Linux-template VM, naming the copy "newkali":

vboxmanage clonevm将使用GUI进行上述类型的克隆操作。 在这里,我正在克隆Kali-Linux-template VM,将副本命名为“ newkali”:

$ vboxmanage clonevm Kali-Linux-template --name newkali

That will work nicely as long as I only need to use the new VM here on my local computer. But suppose I wanted other members of my team to have an exact copy of that VM — perhaps so they could test something I’ve been working on. For that, I’ll need to convert the VM to some standardized file format. Here’s how I might export a local VM to a file using the Open Virtualization Format (.OVA):

只要我只需要在本地计算机上使用新的VM,就可以很好地工作。 但是假设我希望团队中的其他成员拥有该VM的精确副本,也许是这样,他们就可以测试我一直在研究的东西。 为此,我需要将VM转换为某种标准文件格式。 这是使用开放虚拟化格式(.OVA)将本地VM导出到文件的方法:

$ vboxmanage export website-project -o website.ova0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%Successfully exported 1 machine(s).

Next, you’ll need to copy the .OVA file to your colleague’s computer. Bear in mind that the file won’t, by any standard, be considered small and dainty. If you haven’t got network bandwidth to spare for a multiple GB transfer, then consider moving it via a USB device.

接下来,您需要将.OVA文件复制到您同事的计算机上。 请记住,无论如何,该文件都不会被视为小巧精致。 如果没有足够的网络带宽来进行多GB传输,请考虑通过USB设备移动它。

Once the transfer is complete, all that’s left is, from the remote computer, to import the VM into that machine’s VirtualBox. The command is simple:

传输完成后,剩下的就是从远程计算机将VM导入到该计算机的VirtualBox中。 该命令很简单:

$ vboxmanage import docker.ova

Confirm that the import operation worked using list vms and try launching the VM from the desktop.

确认使用list vms虚拟机执行导入操作,然后尝试从桌面启动虚拟机。

$ vboxmanage list vms“docker-project” {30ec7f7d-912b-40a9–8cc1-f9283f4edc61}

If you don’t need any fancy remote access, you can also share a VM from the GUI. With the machine you want to share highlighted, click on the File menu and then on Export Appliance.

如果不需要任何高级远程访问,也可以从GUI共享VM。 将要共享的计算机突出显示,单击“文件”菜单,然后单击“导出设备”。

Coming next: the wondrous and mysterious world of LXC.

下一步:LXC奇妙而神秘的世界。

VirtualBox is great for running operations requiring kernel access, for when you need GUI desktop sessions, or for testing niche market edge-case operating systems. But if you’re on a Linux machine and you just need fast access to a clean Linux environment and you’re not looking for any special release version, then you’d be hard pressed to beat Linux Containers.

VirtualBox非常适合用于运行需要内核访问权限的操作,在需要GUI桌面会话时进行测试,或者用于测试特定市场的边缘情况操作系统。 但是,如果您使用的是Linux机器,并且只需要快速访问干净的Linux环境,并且不需要任何特殊的发行版本,那么您将很难击败Linux容器。

Just how fast are LXC containers? You’ll see for yourself soon enough. But, because they skillfully share many system resources with both the host and other containers, they work like full-bore standalone servers while using only minimal storage space and memory.

LXC容器的速度有多快? 您很快就会看到自己的。 但是,由于它们巧妙地与主机和其他容器共享许多系统资源,因此它们像全口径独立服务器一样工作,而仅使用最少的存储空间和内存。

NOTE: We’re going to discuss the classic LXC, but you should be aware that there’s a similar environment from the same developers called LXD. LXD is essentially a newer implementation of the LXC interface. It still uses LXC under the hood, but provides a different set of commands that extend functionality to remote network administration.
注意:我们将讨论经典的LXC,但是您应该知道,来自相同开发人员的类似环境称为LXD。 LXD本质上是LXC接口的较新实现。 它仍然使用LXC,但提供了一组不同的命令,这些命令将功能扩展到远程网络管理。

LXC入门 (Getting started with LXC)

Install LXC on your Ubuntu workstation? Piece of cake:

在您的Ubuntu工作站上安装LXC? 小菜一碟:

sudo apt updatesudo apt install lxc

That’s it. We’re ready to get down to business. The basic LXC skill set is actually quite simple. I’m going to show you the three or four commands you’ll need to make it all work, and then an insider tip that, once you understand how LXC organizes itself, will just blow you away.

而已。 我们准备开始做生意。 LXC的基本技能实际上很简单。 我将向您展示使它们全部正常运行所需的三个或四个命令,然后向您提供内幕提示,一旦您了解LXC的自身组织方式,那将使您大吃一惊。

启动您的第一个容器 (Launch your first container)

Why not dive right in and create your first container? The value given to -nsets the name I want to use for the container, and -t tells LXC to build the container from the Ubuntu template.

为什么不直接潜水并创建您的第一个容器? -n的值设置了我要用于容器的名称, -t告诉LXC从Ubuntu模板构建容器。

$ sudo lxc-create -n mycont -t ubuntu

If you decided to create, say, a CentOS container, then you should make a note of the final few lines of the output, as it contains information about the password you should use to log in:

如果您决定创建一个CentOS容器,那么您应该记下输出的最后几行,因为它包含有关您用于登录的密码的信息:

$ sudo lxc-create -n centos_lxc -t centos [...]The temporary root password is stored in:        '/var/lib/lxc/centos_lxc/tmp_root_pass'

You will log in using the user name “root” and the password contained in that file. If, on the other hand, your container used the Ubuntu template, then you’ll use “ubuntu” for both your user name and password. Naturally, if you plan to use this container for anything serious, you’ll want to change that password right away.

您将使用用户名“ root”和该文件中包含的密码登录。 另一方面,如果您的容器使用Ubuntu模板,则您的用户名和密码都将使用“ ubuntu”。 自然,如果您打算将此容器用于任何严重的事情,则需要立即更改该密码。

Use lxc-ls --fancy to check the status of your container:

使用lxc-ls --fancy检查容器的状态:

$ sudo lxc-ls --fancyNAME    STATE   AUTOSTART GROUPS IPV4    IPV6 mycont  STOPPED 0         -      -       -

Well, it exists, but apparently it needs starting. As before, the -n specifies by name the container you want to start. -d stands for "detach" - meaning you don't want to be automatically dropped into an interactive session as the container starts.

好的,它存在,但是显然它需要启动。 和以前一样, -n通过名称指定要启动的容器。 -d代表“分离”-意味着您希望在容器启动时自动进入交互式会话。

$ sudo lxc-start -d -n mycont

Listing your containers should now display something like this:

列出您的容器现在应该显示如下内容:

$ sudo lxc-ls --fancyNAME    STATE   AUTOSTART GROUPS IPV4        IPV6mycont  RUNNING 0         -      10.0.3.142  -

This time, the container is running and has been given an IP address. You could use this address to log in using a secure shell session.

这次,容器正在运行,并已获得IP地址。 您可以使用该地址通过安全的Shell会话登录。

$ ssh ubuntu@10.0.3.142

Alternatively, you can launch a root shell session within a running container using lxc-attach.

另外,您可以使用lxc-attach在正在运行的容器中启动root shell会话。

$ sudo lxc-attach -n mycontroot@mycont:/#

When you’re done playing with your new container, you can either run exitto log out leaving the container running:

完成使用新容器的操作后,您可以运行exit以使容器运行:

root@mycont:/# exitexit

…or shut the container down using shutdown -h now.

…或使用shutdown -h now关闭容器。

But before you do that, let’s find out just how blazing fast LXC containers are. The -hflag I added to shutdown just before stands for "halt". If I would use rinstead, rather than shutting down for good, the container would reboot. So let's run reboot and then try to log in again right away to see how long it takes for the container to get back up on its feet.

但是在您这样做之前,让我们了解一下快速的LXC容器的性能如何。 我在shutdown之前添加了-h标志,表示“暂停”。 如果我改用r ,而不是永久关闭,容器将重新启动。 因此,让我们运行重新启动,然后尝试立即再次登录,以查看该容器重新站起来需要多长时间。

root@mycont:/# shutdown -r nowsudo lxc-attach -n mycont

How did that go? I’ll bet that, by the time you managed to retype the lxc-attachcommand, mycont was awake and ready for action. But did you know that hitting the up arrow key in Bash will populate the command line with the previous command. Using that would make it even faster to request a login. In my case, there was no noticeable delay. The container shut down and fully rebooted in less than 2 seconds!

怎么样了 我敢打赌,当您设法重新键入lxc-attach命令时,mycont醒了,可以采取行动了。 但是您是否知道击中Bash中的向上箭头键将使用先前的命令填充命令行。 使用它可以使请求登录的速度更快。 就我而言,没有明显的延迟。 容器关闭并在不到2秒的时间内完全重新启动!

Linux containers are also really easy on system resources. Unlike my experience with VirtualBox VMs — where running three concurrently already starts to seriously impact my 8GB host workstation performance — I can launch all kinds of LXC containers without suffering any slowdown.
Linux容器在系统资源上也非常容易。 与我使用VirtualBox VM的经验不同(在该环境中,同时运行三个虚拟机已经开始严重影响我的8GB主机工作站性能),我可以启动各种LXC容器而不会受到任何影响。

固定一个冷的LXC容器 (Fixing a cold LXC container)

Now what about that insider tip I promised you? Well, back in a terminal on the host machine (as opposed to the container), you’ll need to open an administrator shell using sudo su. From here on in - until you type exit - you will be sudo full-time.

现在,我向您保证的内幕消息呢? 好了,回到主机上的终端(而不是容器),您需要使用sudo su打开管理员外壳。 从这里开始-直到您键入exit您将成为sudo全职。

$ sudo su[sudo] password for username:#

Now change directory to /var/lib/lxc/ and list the contents. You should see a directory with the name of your container. If you've got other containers on the system, they'll have their own directories as well.

现在将目录更改为/var/lib/lxc/并列出内容。 您应该看到包含容器名称的目录。 如果您的系统上还有其他容器,它们也将具有自己的目录。

# cd /var/lib/lxc# lsmycont

Move to your container directory and list its contents. There will be a file called “config” and a directory called “rootfs”. The “fs” stands for “file system”.

移至您的容器目录并列出其内容。 将有一个名为“ config”的文件和一个名为“ rootfs”的目录。 “ fs”代表“文件系统”。

# cd mycont# lsconfig rootfs

It’s the rootfs directory that I really want you to see right now:

我真的希望您现在看到的是rootfs目录:

# cd rootfs# lsbin   dev  home  lib64  mnt  proc  run   srv  tmp  varboot  etc  lib   media  opt  root  sbin  sys  usr

All those subdirectories that fill rootfs…do they look familiar to you? Of course! They’re all part of the Linux Filesystem Hierarchy Standard. This is essentially the container’s root (/) directory…but within the host’s file system. As long as you have admin permissions on the host, you’ll be able to browse through those directories and edit any files you want — even when the container isn’t running.

所有填充rootfs的子目录……您对它们看起来很熟悉吗? 当然! 它们都是Linux Filesystem Hierarchy Standard的一部分。 本质上,这是容器的根(/)目录,但是在主机的文件系统中。 只要您对主机具有管理员权限,就可以浏览这些目录并编辑所需的任何文件-即使容器未运行。

There are all kinds of things you’ll be able to do with this access, but here’s one that can quite possibly save your (professional) life one day. Suppose you do something dumb on a container and lock yourself out, there’s now nothing stopping you from navigating through the file system, fixing the configuration file that you messed up, and getting back to work. Go ahead: tell me that’s not cool.

使用此访问权限,您可以进行各种各样的事情,但是这里的事情很可能会挽救您(专业)一天的生命。 假设您在容器上做一些愚蠢的操作并将自己锁定在外面,那么现在没有什么可以阻止您浏览文件系统,修复您弄乱的配置文件并恢复工作。 继续:告诉我那不酷。

Need to mount a single process on a stopped container? Just .

是否需要在停止的容器上安装单个进程? 只需 。

But it gets better. It’s true that the has gained many layers of features and sophistication since the technology moved out from under LXC’s shadow some years ago. Under the hood, however, it’s still built on top of a basic structural paradigm that will be instantly recognizable to anyone familiar with LXC.

但它会变得更好。 自从几年前该技术从LXC的阴影下移出以来, 已经获得了许多层的功能和完善性。 然而,在幕后,它仍然建立在基本的结构范例之上,熟悉LXC的任何人都可以立即识别它。

Which means that, should you be inclined to test drive the fastest-growing virtualization technology of the decade, you’ve already got skin in the game.

这意味着,如果您倾向于试驾十年来发展最快的虚拟化技术,那么您已经拥有了自己的皮肤。

在远程服务器上编写代码 (Writing code on a remote server)

Now that you’ve got those virtual environment thingies all figured out, what can you do with ‘em? Well, it’s obvious that such setups are perfect for playing with system tools and architectures.

现在,您已经弄清了所有虚拟环境,您将如何处理它们? 好吧,很明显,这样的设置非常适合使用系统工具和体系结构。

Suppose you’re not into system stuff, but you would like a safe place to build applications. What do I mean by “safe?” I’m talking about an environment where you can freely install libraries and dependency packages without having to worry about destabilizing your personal work computer.

假设您不喜欢系统知识,但是您想要一个安全的地方来构建应用程序。 我所说的“安全”是什么意思? 我说的是一种环境,您可以在其中自由安装库和依赖项程序包,而不必担心破坏个人工作计算机的稳定性。

But there are limits to how far that can take you. There’s a lot going on beneath the surface of IDEs like Eclipse or Visual Studio, and people become very attached to their convenience. But expecting to be able to easily run all those layers of complexity over a remote connection — and especially on a headless remote server — is perhaps a bit ambitious.

但是可以带你走多远。 像Eclipse或Visual Studio这样的IDE的底层有很多东西,人们变得非常重视其便利性。 但是,期望能够通过远程连接(尤其是在无头的远程服务器上)轻松地运行所有这些复杂性层,也许有些雄心勃勃。

But still, wouldn’t it be nice to work on the IDE on your laptop and have the code saved, compiled, and run remotely…say, on a hosted VM or cloud instance? That way you could build your applications on the servers where they’ll actually be run without having to risk the stability of your own workstation.

但是,仍然可以在笔记本电脑上的IDE上运行并保存,编译和远程运行代码,而不是在托管的VM或云实例上运行,这不是很好吗? 这样,您可以在将要实际运行它们的服务器上构建应用程序,而不必担心自己的工作站的稳定性。

It can be done. Generally, the trick is to get your IDE to ride on top of a Secure Shell (SSH) session. For details, search the internet for something like “eclipse edit java code on remote server”.

可以办到。 通常,诀窍是使您的IDE处于安全Shell(SSH)会话之上。 有关详细信息,请在Internet上搜索“在远程服务器上蚀编辑Java代码”之类的内容。

This article is based on a chapter from my free online book, . There’s lots more where that came from at my Bootstrap IT site, including links to my book, , and a hybrid course called that’s made up of more than two hours of video and around 40% of the text of Linux in Action.

本文基于我的免费在线书《 。 我的Bootstrap IT站点还有很多其他内容,包括我的书《 链接以及名为的混合课程,该课程由两个多小时的视频和大约40%的Linux文字组成在行动。

翻译自:

深圳哪个区的it工作多

转载地址:http://rcwzd.baihongyu.com/

你可能感兴趣的文章
Settings app简单学习记录
查看>>
SQLAlchemy
查看>>
多线程
查看>>
使用缓存的9大误区(下)转载
查看>>
appium键值对的应用
查看>>
MyEclipse 8.X 通用算法
查看>>
selenium.Phantomjs设置浏览器请求头
查看>>
分布式数据库如何选择,几种分布式数据库优缺点一览
查看>>
BZOJ 4443: 小凸玩矩阵【二分图】
查看>>
苹果 OS X制作u盘启动盘
查看>>
Jquery便利对象
查看>>
MVC: Connection String
查看>>
idea常用设置汇总
查看>>
Node.SelectNodes
查看>>
Lambda表达式语法进一步巩固
查看>>
Vue基础安装(精华)
查看>>
Git 提交修改内容和查看被修改的内容
查看>>
PAT - 1008. 数组元素循环右移问题 (20)
查看>>
请求出现 Nginx 413 Request Entity Too Large错误的解决方法
查看>>
配置php_memcache访问网站的步骤
查看>>