1、安装

首先需要安装 VirtualBox,下载地址:https://www.virtualbox.org/wiki/Downloads/
Mac 上安装 virtualbox:

  1. brew install virtualbox

安装 Vagrant,下载地址:http://downloads.vagrantup.com/。
Mac 上安装:

  1. brew install vagrant

2、下载镜像

Vagrant 提供了很多镜像,你可以去网站搜索你想要的镜像(box),然后手动下载。
也可以使用命令下载,例如

  1. $ vagrant box add generic/centos7

按照提示输入5 选择virtualbox:

  1. ==> box: Loading metadata for box 'generic/centos7'
  2. box: URL: https://vagrantcloud.com/generic/centos7
  3. This box can work with multiple providers! The providers that it
  4. can work with are listed below. Please review the list and choose
  5. the provider you will be working with.
  6. 1) docker
  7. 2) hyperv
  8. 3) libvirt
  9. 4) parallels
  10. 5) virtualbox
  11. 6) vmware_desktop
  12. Enter your choice: 5
  13. ==> box: Adding box 'generic/centos7' (v3.3.2) for provider: virtualbox
  14. box: Downloading: https://vagrantcloud.com/generic/boxes/centos7/versions/3.3.2/providers/virtualbox.box
  15. box: Calculating and comparing box checksum...
  16. ==> box: Successfully added box 'generic/centos7' (v3.3.2) for 'virtualbox'!

镜像都被安装在 ~/.vagrant.d/boxes 目录下面,可以查看安装了哪些镜像:

  1. $ vagrant box list
  2. generic/centos7 (virtualbox, 3.3.2)

3、创建虚拟机

创建一个目录,然后初始化一个vagrant虚拟机:

  1. $ mkdir centos7 && cd centos7
  2. $ vagrant init generic/centos7

查看生成的文件:

  1. cat Vagrantfile

去掉注释之后的内容如下:

  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. Vagrant.configure("2") do |config|
  4. config.vm.box = "generic/centos7"
  5. end

4、启动虚拟机

  1. vagrant up

你会看到终端显示了启动过程,启动完成后,我们就可以用 SSH 登录虚拟机了:

  1. vagrant ssh

5、实战

5.1 搭建多节点不同配置

每台机器不同配置:

  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. Vagrant.configure("2") do |config|
  4. # Vagrant Global Config
  5. config.vm.box = "generic/centos7"
  6. config.vm.provision :shell, :path => "bootstrap.sh"
  7. config.vm.define "node1" do |config|
  8. config.vm.network "private_network", ip: "192.168.56.11"
  9. #添加一个桥接网络
  10. config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"
  11. config.vm.hostname = "server"
  12. end
  13. config.vm.define "node2" do |config|
  14. v.vm.network "private_network", ip: "192.168.56.12"
  15. v.vm.hostname = "node1"
  16. end
  17. config.vm.define "node3" do |config|
  18. config.vm.network "private_network", ip: "192.168.56.13"
  19. config.vm.hostname = "node2"
  20. end
  21. end

改为批量配置:

  1. # -*- mode: ruby -*-
  2. # vi: set ft=ruby :
  3. Vagrant.configure("2") do |config|
  4. config.vm.box = "generic/centos7"
  5. config.vm.provision :shell, :path => "bootstrap.sh"
  6. (1..3).each do |i|
  7. config.vm.define "node#{i}" do |config|
  8. config.vm.provider "virtualbox" do |v|
  9. v.customize ["modifyvm", :id, "--name", "node#{i}",
  10. "--memory", "1024",'--cpus', 1]
  11. v.gui = false
  12. end
  13. config.vm.network "private_network", ip: "192.168.56.1#{i}"
  14. config.vm.hostname = "node#{i}"
  15. end
  16. end
  17. end

5.2 配置 SSH 无密码登陆

查看ssh配置:

  1. $ vagrant ssh-config
  2. Host default
  3. HostName 127.0.0.1
  4. User vagrant
  5. Port 2222
  6. UserKnownHostsFile /dev/null
  7. StrictHostKeyChecking no
  8. PasswordAuthentication no
  9. IdentityFile /Users/june/workspace/vagrant/devops/.vagrant/machines/default/virtualbox/private_key
  10. IdentitiesOnly yes
  11. LogLevel FATAL

注意:

  • vagrant将PasswordAuthentication设置为no了,这样就无法通过密码登陆,也不能设置root用户无密码登陆到其他节点,需要改成false

scp 拷贝文件到虚拟机:

  1. scp -i ~/.vagrant/machines/default/virtualbox/private_key test.sql vagrant@192.168.56.100:~

参考:https://stackoverflow.com/questions/10864372/how-to-ssh-to-vagrant-without-actually-running-vagrant-ssh ,这是SSH登陆:

  1. ssh $(vagrant ssh-config | awk 'NR>1 {print " -o "$1"="$2}') localhost