设为首页 收藏本站
查看: 862|回复: 0

[经验分享] Vagrant基础简要记录

[复制链接]

尚未签到

发表于 2017-6-23 10:35:14 | 显示全部楼层 |阅读模式
  Vagrant是一种开源软件,它为跨众多操作系统构建可重复的开发环境提供了一种方法。Vagrant使用提供者(provider)来启动隔离的虚拟环境。默认的提供者是Virtualbox
  Vagrant ( http://www.vagrantup.com/ ) is a powerful development tool, which
  lets you manage and support the virtualization of your development environment.
  Instead of running all your projects locally on your own computer, having to juggle
  the different requirements and dependencies of each project, Vagrant lets you run
  each project in its own dedicated virtual environment.
  Vagrant uses Providers to integrate with the third-party virtualization software,
  which provides the virtualized machines for our development environment. The
  default provider is for Oracle's VirtualBox however, there are providers to work
  with Amazon Web Services and VMware Fusion. The entire configuration is stored
  in simple plain text files. The Vagrant configuration (Vagrantfile), Puppet, and Chef
  manifests are simply written in text files in a Ruby Domain Specific Language. This
  means we can easily share the configurations and projects with colleagues, using
  Version Control Systems such as Git or Subversion.
Docker vs Vagrant
  http://stackoverflow.com/questions/16647069/should-i-use-vagrant-or-docker-for-creating-an-isolated-environment
  http://ju.outofmemory.cn/entry/52470
  vagrant可以帮助用户管理/部署虚拟机的程序。docker是一个帮助用户创建/运行/管理基于lxc的linux container的程序。coreos是一个专门为运行linux container而设计的发行版。
安装和使用
  https://atlas.hashicorp.com/boxes/search
  https://docs.vagrantup.com/v2/providers/ VMWare和Virtualbox等各种提供者
  Vagrant can be installed on Linux, Windows, and Mac OS X, and although it
  uses Ruby, the package includes an embedded Ruby interpreter. The only other
  requirement is a virtualization tool such as Oracle's VirtualBox. The Oracle's
  VirtualBox provider is available for free, and is included built-in with Vagrant
  https://www.virtualbox.org/
  http://downloads.vagrantup.com
  Each virtual machine starts with what Vagrant calls a base box. This is a specially
  packaged version of an operating system with some specific configurations in
  place. The number of configurations and packages installed on this packaged
  operating system is typically minimal (containing only a few tools which allow it
  to communicate with Vagrant).
初始化
  Ubuntu的几个box
  Ubuntu10
  ·  Lucid32 is available at  http://files.vagrantup.com/lucid32.box
  ·  Lucid64 is available at  http://files.vagrantup.com/lucid64.box
  Ubuntu12
  ·  Precise32 is available at  http://files.vagrantup.com/precise32.box
  ·  Precise64 is available at  http://files.vagrantup.com/precise64.box
  Ubutnu14
  vagrant init ubuntu/trusty64; vagrant up --provider virtualbox
  vagrant init precise64 http://files.vagrantup.com/precise64.box
  Vagrantfile生成[ruby]
  vagrant init base64
  vagrant init
  vagrant box add <name> <url> [--provider provider] [--force]
Powering up
  vagrant up
  Vagrant will then perform the following:
  ·  Copy the base box
  ·  Create a new virtual machine with the relevant provider (the default
  being VirtualBox)
  ·  Forward any configured ports; by default, it will forward port 22 (SSH) on
  the VM to port 2222 on the host; this will allow us to connect to the VM
  ·  Boot (power up) the VM
  ·  Configure and enable networking, so that we can communicate with the VM
  ·  Map shared folders between the host and the guest (by default, it will map
  the folder containing the Vagrant project to  /vagrant on the guest machine)
  ·  Run any provisioning tools that are set up such as Puppet, Chef, or
  SSH provisioning
  vagrant suspend
  vagrant resume
  vagrant halt
  vagrant destroy
  vagrant ssh
和HOST机器共享
  Port forwarding
  Vagrantfile 文件中进行端口映射
  config.vm.network :forwarded_port, guest: 80, host: 8888
  Synced folders
  config.vm.synced_folder "/Users/michael/assets/" "/var/www/assets"
  The first parameter is the path to the folder on our machine, the second being the
  mount point on the VM.
  Networking
  config.vm.network :private_network, ip: "192.168.1.100"
  Auto-running commands
  config.vm.provision :shell, :inline => "sudo apt-get update"
  config.vm.provision :shell, :path => "provision.sh"  (the location of the
  script specified is relative to our project root, that is, /vagrant )
Provisioning
  Puppet modules
  http://docs.puppetlabs.com/references/latest/type.html
  http://forge.puppetlabs.com/
  puppet apply --modulepath=/home/michael/provision/modules /home/michael/provision/manifests/default.pp
  独立运行模式
  config.vm.provision :puppet do |puppet|
  puppet.manifests_path = "provision/manifests"
  puppet.manifest_file = "default.pp"
  puppet.module_path = "provision/modules"
  end
  client/server模式
  config.vm.provision :puppet_server do |puppet|
  puppet.puppet_server = "puppet.internal.michaelpeacock.co.uk"
  puppet.puppet_node = "vm.internal.michaelpeacock.co.uk"
  end
  SSH
  config.vm.provision :shell, :path => "provision/setup.sh"
  config.vm.provision :shell, :inline => "apt-get install apache2"
  vagrant provision 命令可以重新进行provisioning
multiple virtual machines
  Vagrantfile的配置
  # -*- mode: ruby -*-
  # vi: set ft=ruby :
  Vagrant.configure("2") do |config|
  config.vm.define :server1 do |server1|
  server1.vm.box = "precise64"
  server1.vm.network :private_network, ip: "10.11.1.100"
  end
  config.vm.define :server2 do |server2|
  server2.vm.box = "precise64"
  server2.vm.network :private_network, ip: "10.11.1.101"
  end
  end
  1. Power up the project ( vagrant up )
  2. Connect to  server1 ( vagrant ssh server1 )
  3. Ping  server2 from  server1 ( ping 10.11.1.101
  不同的配置
  # -*- mode: ruby -*-
  # vi: set ft=ruby :
  Vagrant.configure("2") do |config|
  config.vm.define :server1 do |server1|
  server1.vm.box = "precise64"
  server1.vm.network :private_network, ip: "10.11.1.100"
  server1.vm.provision :puppet do |puppet|
  puppet.manifests_path = "provision/manifests"
  puppet.manifest_file = "server1.pp"
  puppet.module_path = "provision/modules"
  end
  end
  config.vm.define :server2 do |server2|
  server2.vm.box = "precise64"
  server2.vm.network :private_network, ip: "10.11.1.101"
  server2.vm.provision :puppet do |puppet|
  puppet.manifests_path = "provision/manifests"
  puppet.manifest_file = "server2.pp"
  puppet.module_path = "provision/modules"
  end
  end
  end
LAMP
  |-- provision
  | |-- manifests
  | | -- init.pp
  | -- modules
  -- Vagrantfile
  根下的Vagrantfile文件内容
  # -*- mode: ruby -*-
  # vi: set ft=ruby :
  Vagrant.configure("2") do |config|
  config.vm.provision :shell, :inline => "apt-get update"
  config.vm.box = "precise64"
  config.vm.network :forwarded_port, guest: 80, host: 8080
  config.vm.provision :puppet do |puppet|
  puppet.manifests_path = "provision/manifests"
  puppet.module_path = "provision/modules"
  puppet.manifest_file  = "default.pp"
  end
  end
  目录provision下是Puppet使用的各模块的安装和配置文件
  http://pan.baidu.com/s/1ntKFSVn#path=%252Fshare vagrant_lamp_stack.zip
建立自己的Box
  1.安装VirtualBox虚拟机
  a) 网络需要设置为NAT
  b) 虚拟机名字:vagrant-ubuntu-raring
  c) hostname : vagrant-ubuntu-raring
  d) Domain:  vagrantup.com
  e) Root password:  vagrant
  f) Main account username:  vagrant
  g) Main account password:  vagrant
  h) install  openssh-server
  2.Install Guest Additions
  a) sudo apt-get install linux-headers-$(uname -r) build-essential
  b) sudo mount /dev/cdrom /media/cdrom
  c) sudo sh /media/cdrom/VBoxLinuxAdditions.run
  3.Vagrant authentication
  a) sudo groupadd admin
  b) sudo usermod -a -G admin vagrant
  4.vi sudo 修改
  a) %admin ALL=(ALL) NOPASSWD: ALL
  b) Defaults env_keep="SSH_AUTH_SOCK"
  c) #Default requiretty
  5.ssh无秘码登陆
  a) wget https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub –o ~/.ssh/authorized_hosts
  b) chmod 0644 ~/.ssh/authorized_keys
  6.Provisioners
  a) sudo apt-get install puppet
  b) Chef

  • sudo apt-get install ruby ruby-dev libopenssl-ruby rdoc ri irb build-essential wget ssl-cert curl
  • cd /tmp
  • curl -O http://production.cf.rubygems.org/rubygems/rubygems-1.8.10.tgz
  • tar zxf rubygems-1.8.10.tgz
  • cd rubygems-1.8.10
  • sudo ruby setup.rb --no-format-executable
  • sudo gem install chef --no-ri --no-rdoc
  7.Cleanup
  a) rm –rf /tmp/*
  b) sudo apt-get clean
  8.Export
  a) vagrant package --base vagrant-ubuntu-raring
  b) website: http://docs.vagrantup.com/v2/cli/package.html

测试自己的box
$vagrant box add /../../my.box
$vagrant init my
$vagrant up

如上的过程就可建立自己的box
  https://atlas.hashicorp.com/boxes/search 这里有很多开放的box

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-387220-1-1.html 上篇帖子: Archlinux安装和使用技巧 下篇帖子: WildFly 9.0.2+mod_cluster-1.3.1 集群配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表