Vargrant + virtualbox 初探

发布 : 2021-11-03 分类 : 运维 浏览 :

Vargrant及virtbox初探

环境准备

软件 版本 备注
系统centos7 7
vargrant 2.2.18
virtualbox 6.1-6

先安装下依赖软件,以下软件安装后重启下服务器

1
2
# yum -y install gcc kernel kernel-devel
# reboot

vargrant安装

其它系统参考官方文档

1
2
3
# yum install -y yum-utils
# yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# yum -y install vagrant

个人感觉vagrant和docker-compose类似,都可通过配置文件实现工作流的自动化,但是区别是docker-compose只支持docker,而vagrant支持多种虚拟化平台,例如VirtualBox、 Hyper-V和Docker等等,默认情况vagrant使用的是virtualbox,本文中也是使用virtualbox做相关测试

virtualbox安装

其它系统参考官方文档

1
# yum install https://download.virtualbox.org/virtualbox/6.1.28/VirtualBox-6.1-6.1.28_147628_el7-1.x86_64.rpm

执行以下命令编译内核,输出内容无报错则正常

1
2
3
4
# /sbin/vboxconfig
vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Starting VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.

测试

运行以下两个命令后,您将在 VirtualBox 中拥有一个完全运行的虚拟机,该虚拟机运行 Ubuntu 18.04 LTS 64 位。

  1. 初始化vagrant

这里执行后会生成一个Vagrantfile,启动时会根据Vagrantfile工作流进行启动

1
2
# mkdir vargrant && cd vargrant
# vagrant init hashicorp/bionic64

  1. 启动虚拟机

首次启动时会去下载虚拟机镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'hashicorp/bionic64' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'hashicorp/bionic64'
default: URL: https://vagrantcloud.com/hashicorp/bionic64
==> default: Adding box 'hashicorp/bionic64' (v1.0.282) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/hashicorp/boxes/bionic64/versions/1.0.282/providers/virtualbox.box
==> default: Successfully added box 'hashicorp/bionic64' (v1.0.282) for 'virtualbox'!
==> default: Importing base box 'hashicorp/bionic64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/bionic64' version '1.0.282' is up to date...
==> default: Setting the name of the VM: vargrant_default_1635909887818_89726
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:

https://www.virtualbox.org/manual/ch04.html#sharedfolders

This option can be disabled globally with an environment variable:

VAGRANT_DISABLE_VBOXSYMLINKCREATE=1

or on a per folder basis within the Vagrantfile:

config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> 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: Warning: Connection reset. Retrying...
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: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 6.0.10
default: VirtualBox Version: 6.1
==> default: Mounting shared folders...
default: /vagrant => /root/vargrant

  1. 连接主机

执行以下命令可以直接进入虚拟机

1
# vagrant ssh

进入主机后查看目录信息,发现有一个/vagrant目录,此目录与物理机的vagrant工作目录为共享目录

1
2
3
4
5
6
7
8
9
10
$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 463M 0 463M 0% /dev
tmpfs tmpfs 99M 5.0M 94M 5% /run
/dev/mapper/vagrant--vg-root ext4 62G 1.6G 58G 3% /
tmpfs tmpfs 493M 0 493M 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 493M 0 493M 0% /sys/fs/cgroup
vagrant vboxsf 40G 5.4G 35G 14% /vagrant
tmpfs tmpfs 99M 0 99M 0% /run/user/1000

我们可以尝试在虚拟机的/vagrant目录创建一个文件文件,之后在物理机的工作目录查看

1
2
3
4
5
6
$ touch /vagrant/123.txt
$ exit
logout
Connection to 127.0.0.1 closed.
# ls
123.txt Vagrantfile

  1. 删除主机
1
2
3
4
# vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

总结

经过简单的尝试,vagrant通过读取Vagrantfile启动虚拟机,并且可以根据vagrantfile做好相应的配置及程序安装

本文作者 : WGY
原文链接 : http://geeklive.cn/2021/11/03/vagrant-virtualbox/undefined/vagrant-virtualbox/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
留下足迹