【备忘录】使用Vagrant 构建CentOS7环境的步骤
首先
Vagrant是一个用于轻松创建虚拟环境的工具。
这次作为我的备忘录,我将总结使用Vagrant在Mac电脑上创建CentOS7环境的步骤。
假设前提是Mac电脑已经安装了VirtualBox。
您可以从以下链接的网站上下载VirtualBox:https://www.virtualbox.org/
2. 准备工作
首先,在Mac PC上安装Vagrant。
2-1. 下载
从官方网站下载Vagrant。
可以从以下链接获取下载图像。
流浪者:下载Vagrant
https://www.vagrantup.com/downloads.html
打开URL后,会显示如下页面。
这次我们将在Mac PC上安装Vagrant,所以选择macOS的镜像。
※本次引入2.2.7版本。
2-2. 安装
(1) 打开安装程序
打开下载的Vagrant安装程序,会显示如下窗口。
点击”继续”
选择要安装的磁盘,并点击“继续”。
点击[安装],开始安装。
开始安装。
点击[关闭],完成安装。
2-3. 版本检查
打开终端并检查安装的Vagrant版本。
执行以下命令以确认版本:
<命令>
vagrant --version
执行以上命令。<执行>
$ vagrant --version
Vagrant 2.2.7
我们可以看到这次引入了Vagrant版本2.2.7。
3. 创建虚拟机
使用Vagrant在Mac电脑上创建CentOS 7虚拟机。
新增一个BOX
盒子是虚拟机的模板。
您可以从以下网站等地方找到BOX。
Vagrantbox.es是一个网站,提供各种可用的虚拟机软件。
Vagrant 云:发现 Vagrant 盒子
https://app.vagrantup.com/boxes/search
使用以下命令来添加和确认 Box。
<命令>
・增加盒子
vagrant box add {BOXNAME} {URL}
确认盒子
vagrant box list
执行上述命令。
・增加箱子
为了添加 CentOS7 的 Box,本次我们将参考以下链接并执行命令。
https://app.vagrantup.com/centos/boxes/7
※ 供应商将选择[3) virtualbox]。
$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
Enter your choice: 3
==> box: Adding box 'centos/7' (v1905.1) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box
box: Download redirected to host: cloud.centos.org
==> box: Successfully added box 'centos/7' (v1905.1) for 'virtualbox'!
・确认盒子
确认CentOS7的Box已经被添加。
$ vagrant box list
centos/7 (virtualbox, 1905.1)
3-2. 虚拟机初始化
通过进行虚拟机初始化,创建一个设置文件(Vagrantfile)。
在进行虚拟机初始化时,使用以下命令:<命令>
・虚拟机初始化
vagrant init {BOXNAME}
执行上述命令。
・(准备)创建和移动目录
按照虚拟机的不同,创建对应的文件夹,在其中创建配置文件(Vagrantfile)。
创建并移动到一个以任意名称命名的目录中。
$ cd ~/vagrant/
$ mkdir centos7
$ cd centos7/
・虚拟机初始化
通过指定在先前的项目中添加的BOX ‘centos/7’,进行虚拟机初始化,从而在目录中创建虚拟机的初始设置文件(初始Vagrantfile)。
$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
你可以确认在执行命令的当前目录中已创建了Vagrantfile。
$ ls
Vagrantfile
(检查 Vagrantfile 的内容)
$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "centos/7"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
3-3. 准备虚拟机
启动创建的虚拟机。
使用以下命令,启动并确认虚拟机的状态。
<命令>
• 启动虚拟机
vagrant up
・确认虚拟机的状态
vagrant status
执行上述命令。
移動到配置了Vagrantfile的目录并进行确认。
$ cd ~/vagrant/centos7/
$ ls
Vagrantfile
・启动虚拟机
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Setting the name of the VM: centos7_default_1587971622673_54447
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /XXXXX/vagrant/centos7/ => /vagrant
查看虚拟机状态
$ vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.
3-4. 虚拟机连接
连接到启动的虚拟机进行SSH连接。
使用以下命令来连接虚拟机: <命令>
・虚拟机连接
vagrant ssh
执行上述命令。
进入并确认Vagrantfile所在的目录
$ cd ~/vagrant/centos7/
$ ls
Vagrantfile
・虚拟机连接
执行命令并连接到CentOS 7虚拟机。
$ vagrant ssh
[vagrant@localhost ~]$
4. 虚拟机操作
在操作虚拟机时,我们将列出一些常用的命令供您参考。
有关其他命令的详细信息,请参阅以下链接。
流浪者:命令行接口
https://www.vagrantup.com/docs/cli/
5. 结束
这次我们整理了使用Vagrant在Mac PC上创建CentOS7虚拟环境的步骤。
使用手工建立环境需要相当长的时间,但是使用Vagrant可以在大约10分钟左右完成环境构建。
我觉得能够快速准备自己用于验证和开发的环境非常方便,这种便捷性非常实用。
请提供相关资料
(Reference information)
Vagrant 官方网站
https://www.vagrantup.com/
【中文翻译】
点通课程:Vagrant入门
https://dotinstall.com/lessons/basic_vagrant