使用Vagrant和VirtualBox创建虚拟机
首先
因为我在研究流浪者(vagrant)方面的许多内容,所以我打算整理一下我所调查的内容。
环境
-
- ホストOS : Windows10(64bit)
-
- Vagrant : 2.2.6
-
- 仮想S/W : virtualbox 6.0.14
- ゲストOS : CentOS8
作业的步骤
本次将创建用于执行Ansible和terraform的环境。
(步骤)
1. 创建Box
2. 创建Vagrantfile
3. 环境构建
4. 登录
1. 制作盒子
Box是指用作虚拟机操作系统(Guest OS)的原始镜像。
这次选择了最新版本的CentOS8,虽然并没有特别的意义。
我通常在写上add之后的参数时(虽然我不确定是否属于正规路径),会参考Vagrant Cloud页面上的内容进行填写。
> vagrant box add generic/centos8
==> box: Loading metadata for box 'generic/centos8'
box: URL: https://vagrantcloud.com/generic/centos8
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) docker
2) hyperv
3) libvirt
4) parallels
5) virtualbox
6) vmware_desktop
Enter your choice: 5 ### <=今回はvirtualboxなので"5"を選択 ###
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/virtualbox.box
box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
box:
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'virtualbox'!
如果在list命令中看到CentOS 8,则表示成功。
> vagrant box list
generic/centos8 (virtualbox, 2.0.6)
2. 创建Vagrantfile
创建一个用于定义虚拟机构建的Vagrantfile。(顺便提一下,注意大小写的区分。)
你可以从头创建文件,也可以使用init命令创建一个示例(模板)。
> vagrant init
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.
这次我们不使用样本,而是使用事先准备好的。
Vagrant.configure('2') do |config| ### <= Vagrantファイルのバージョン。"2"を指定
config.vm.box = "centos/8" ### <= ゲストOSのソースとなるbox
config.vm.hostname = "ansible.local" ### <= 仮想マシンのホスト名
config.vm.network "private_network",ip: "192.168.33.10" ### <= 仮想マシンのIPアドレス
config.vm.synced_folder '.', '/vagrant', disabled: true ### <= フォルダ同期をOFFに設定
config.vm.define 'ansible' do |host|
end
config.vm.provider "virtualbox" do |vb| ### <= 仮想マシンを実行する仮想S/W。今回はvirtualbox
vb.name = "ansible.local" ### <= vitrualbox上の仮想マシン名
end
config.vm.provider "virtualbox" do |vb|
config.vm.provision "shell", inline: <<-SHELL ### <= 起動後に実行するコマンド
yum install epel-release -y
yum install ansible -y
yum install wget unzip python3 git -y
SHELL
end
end
选项1: IP地址的补充
如果只是要创建一个环境(为了使用端口转发进行连接),那么不需要设置IP地址,但是为了连接到其他机器进行验证,我要配置Ansible和Terraform。我会设置IP地址与VirtualBox的Host-Only Adapter的IP地址范围相匹配。(可以通过ipconfig等命令来确认Host-Only Adapter的IP地址)。
补充2:关于文件夹同步。
在中国,Folder Sync OFF不是必需的,但默认情况下,“同步放置Vagrantfile的文件夹到虚拟机的/Vagrant目录下(使用rsync)”。
起初进行同步后,随后的vagrant up命令永远无法完成,我以为它在做什么,结果它试图完全同步用户家目录下(包括Document、Download等巨大文件存放位置)。因此,除非有特殊需求,否则建议将其设为OFF。
(很少有文章提到这一点,可能是因为最近的更新,或者用户家目录下没有人做这样的事情,具体原因不明)。
3. 环境建设
执行vagrant up命令后,输出一些内容后即可登录。
> vagrant up
Bringing machine 'ansible' up with 'virtualbox' provider...
==> ansible: Importing base box 'centos/8'...
==> ansible: Matching MAC address for NAT networking...
==> ansible: Checking if box 'centos/8' version '1905.1' is up to date...
==> ansible: Setting the name of the VM: ansible.local
==> ansible: Clearing any previously set network interfaces...
==> ansible: Preparing network interfaces based on configuration...
ansible: Adapter 1: nat
ansible: Adapter 2: hostonly
==> ansible: Forwarding ports...
ansible: 22 (guest) => 2222 (host) (adapter 1)
==> ansible: Booting VM...
==> ansible: Waiting for machine to boot. This may take a few minutes...
ansible: SSH address: 127.0.0.1:2222
ansible: SSH username: vagrant
ansible: SSH auth method: private key
ansible:
ansible: Vagrant insecure key detected. Vagrant will automatically replace
ansible: this with a newly generated keypair for better security.
ansible:
ansible: Inserting generated public key within guest...
ansible: Removing insecure key from the guest if it's present...
ansible: Key inserted! Disconnecting and reconnecting using new SSH key...
==> ansible: Machine booted and ready!
==> ansible: Checking for guest additions in VM...
ansible: No guest additions were detected on the base box for this VM! Guest
ansible: additions are required for forwarded ports, shared folders, host only
ansible: networking, and more. If SSH fails on this machine, please install
ansible: the guest additions and repackage the box to continue.
ansible:
ansible: This is not an error message; everything may continue to work properly,
ansible: in which case you may ignore this message.
==> ansible: Setting hostname...
==> ansible: Configuring and enabling network interfaces...
==> ansible: Running provisioner: shell...
(中略)
ansible: Complete!
4. 登录
登录非常方便,只需要一条命令就能完成(无需密码)。
> vagrant ssh
[vagrant@ansible ~]$ cat /etc/centos-release
CentOS Linux release 8.0.1905 (Core)
[vagrant@ansible ~]$
如果要对root进行su操作,需要输入密码,但默认密码是”vagrant”(顺便提一下,vagrant用户的密码也是一样的)。