Vagrant使用指南:Vagrant第一个Helloworld例子

这里写图片描述
Vagrant是用ruby写的一个工具, 它的出现是为了更加容易的解决开发环境的一致性问题. 我们将会在本系列教程中学习到如何使用vagrant。本文将会使用vagrant官方给出的例子,如何简单的使用vagrant init和vagrant up就可以简单的跑起来第一个vagrant的例子。

安装

安装在devops工具系列的介绍中,已经写过,基本上安装virtulbox之后就可以了。详细可以参照如下文章,其中还做成了一个安装的脚本,可以在centos7.2上直接执行。

安装方法 http://blog.csdn.net/liumiaocn/article/details/52254049

vagrant init

命令: vagrant init hashicorp/precise64

[root@host31 ~]# vagrant init hashicorp/precise64
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.
[root@host31 ~]#

结果确认: 其实就是生成了一个Vagrantfile

[root@host31 ~]# ll Vagrantfile
-rw-r--r--. 1 root root 3023 Aug 25 22:20 Vagrantfile
[root@host31 ~]#

Vagrantfile的内容确认,我们去掉注释和空行,发现这个vagrantfile只有如下寥寥数行的内容

[root@host31 ~]# cat Vagrantfile |grep -v '#' |grep -v '^$'
Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
end
[root@host31 ~]#

vagrant up

命令:vagrant up
说明:因为此命令需要下载一个box,然后把这个box加入到virtulbox中,然后设定nat网络,启动它,接下来我们就可以通过其提供的ssh用户名来连接到这台机器了,对于现在很多动辄就是需要3台以上机器才能做试验的各种新的框架,k8s/mesos/dcos等等,vagrant是在是一个不错的现阶段的可用软件。

[root@host31 ~]# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> 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: 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: 4.2.0
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => /root
[root@host31 ~]#

因为之前hashicorp/precise64已经下载过了,所以最开始没有进行下载,如果你是第一次执行,可能下载会有点慢。从上面的log中需要注意有这些重要信息:

内容 详细
ssh user vagrant
网络类型 nat
ssh address 127.0.0.1:2222
共享目录 /vagrant => /root

连接到刚启动的vm中

命令:vagrant ssh

[root@host31 ~]# vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
New release '14.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$

确认用户

vagrant@precise64:~$ id
uid=1000(vagrant) gid=1000(vagrant) groups=1000(vagrant),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),110(sambashare),999(admin)
vagrant@precise64:~$

确认OS

vagrant@precise64:/vagrant$ uname -a
Linux precise64 3.2.0-23-generic #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
vagrant@precise64:/vagrant$

确认共享目录:试图在/vagrant下创建一个文件,结果发现因为权限不足而无法做到,设定权限之后应该没有问题,就不再多做试验了。因为通过另外一个文件就是前面我们分析过的Vagrantfile,这里也看到了,说明这个目录确实是和宿主机可以进行共享的目录。

vagrant@precise64:/vagrant$ echo "hello world" >vagrant_host.inf
-bash: vagrant_host.inf: Permission denied
vagrant@precise64:/vagrant$ ls -l Vagrantfile
-rw-r--r-- 1 vagrant vagrant 3023 Aug 25 22:20 Vagrantfile
vagrant@precise64:/vagrant$

你可能感兴趣的:(#,Vagrant)