自从用了VirtualBox+Vagrant组合,本地使用虚拟机不知道有多爽。
安装VirtualBox
Oracle VM VirtualBox 是免费的,用起来很顺手。
下载地址:https://www.virtualbox.org/wiki/Downloads
傻瓜式安装。
安装Vagrant
下载地址:https://developer.hashicorp.com/vagrant/downloads
本地新建目录D:\develop\VirtualBox VMs\centos\node01,进入cmd命令
官网镜像:https://app.vagrantup.com/boxes/search
第三方镜像:www.vagrantbox.es。
vagrant init qarham/CentOS7.5-350GB --box-version 1.0
本次使用的官网镜像,速度上很快。第三方镜像需要执行如下命令:
$ vagrant box add {title} {url}
$ vagrant init {title}
$ vagrant up
等待执行完成后,修改目录中的vagrant文件
# -*- 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 = "qarham/CentOS7.5-350GB"
config.vm.box_version = "1.0"
# 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.11"
# 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 = false
vb.cpus=2
# 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
可以把本地文件中的内容和上面的对比。
网络设置
本地执行ipconfig(windows环境)
D:\develop\VirtualBox VMs\centos\node01>ipconfig

可以看到地址,只要不重复,对应修改就可以了。
内存和CPU
vb.cpus=2 # 设置cpu核数
# Customize the amount of memory on the VM:
vb.memory = "1024" # 设置内存(单位:M)
启动
$ vagrant up
默认创建了用户vagrant,密码vagrant。
重启
修改配置,需要重启
$ vagrant reload
关闭
修改配置,需要重启
$ vagrant halt
其他命令可以去官网自行查看。

本文介绍了如何利用VirtualBox和Vagrant组合在本地创建和配置CentOS虚拟机,包括下载工具和镜像,初始化Vagrantbox,设置网络、内存和CPU,以及启动、重启和关闭虚拟机的相关命令。
676

被折叠的 条评论
为什么被折叠?



