一个专注于IT互联网运维的技术博客

Vagrant的配置文件Vagrantfile介绍

2019.05.18

使用vagrant init命令初始化 Vagrant 环境会生成一个 Vagrantfile 文件,里面包含有大量的虚拟机配置信息,通过它可以定义虚拟机的各种配置,如网络、内存、主机名等。还可以使用 Ruby 的语法编写 Vagrantfile 文件,批量生成多个虚拟机,快速部署开发环境。另外,类似 Dockerfile,共享 Vagrantfile 文件可以在别的电脑上生成一模一样的虚拟机。

默认的 Vagrantfile 文件

使用vagrant init命令初始化 Vagrant 环境,生成默认的 Vagrantfile 文件:

[admin@ityoudao k8s]$ vagrant init centos7-standard
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.
[admin@ityoudao k8s]$ ll
total 4
-rw-rw-r-- 1 admin admin 3023 Apr 27 18:12 Vagrantfile

查看 Vagrantfile 文件内容:

[admin@ityoudao k8s]$ cat Vagrantfile |grep -vE "^#|^  #|^$"
Vagrant.configure("2") do |config|
  config.vm.box = "centos7-standard"
end
[admin@ityoudao k8s]$ cat Vagrantfile 
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "centos7-standard"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL
end

自定义 Vagrantfile 创建虚拟机

下面是一个自定义的 Vagrantfile 配置文件:

[admin@ityoudao k8s]$ cat > Vagrantfile <<EOF
Vagrant.configure("2") do |config|
  (1..3).each do |i|
    config.vm.define "master-00#{i}" do |node|
      node.vm.box = "centos7-standard"
      node.vm.box_check_update = false
      node.vm.hostname = "master-00#{i}"
      node.vm.network "private_network", ip: "192.168.31.10#{i}" 
    end
  end
end
EOF
[admin@ityoudao k8s]$ vagrant up

3、在 Vagrantfile 中使用初始化脚本

使用上面的 Vagrantfile 启动虚拟机之后遇到了问题,node.vm.network "private_network"设置的 IP 地址没有生效,查看创建的虚拟机的网络配置:

[vagrant@master-001 ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:c5:46:4e brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 86126sec preferred_lft 86126sec
    inet6 fe80::a00:27ff:fec5:464e/64 scope link 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:e4:f4:58 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::a00:27ff:fee4:f458/64 scope link 
       valid_lft forever preferred_lft forever
[vagrant@master-001 ~]$ nmcli con show
NAME           UUID                                  TYPE            DEVICE 
enp0s8         30ec36ac-beda-40fd-a6bd-48b1e65a5bb8  802-3-ethernet  enp0s8 
System enp0s8  00cb8299-feb9-55b6-a378-3fdc720e0bc6  802-3-ethernet  --     
enp0s3         93cc844d-2d38-4894-ae01-0e89d392bc7d  802-3-ethernet  enp0s3 
[vagrant@master-001 ~]$ nmcli con show System\ enp0s8|grep "ipv4.addresses"
ipv4.addresses:                         { ip = 192.168.31.101/24, gw = 0.0.0.0 }
[vagrant@master-001 ~]$ cat /etc/sysconfig/network-scripts/ifcfg-enp0s3 
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=enp0s3
UUID=93cc844d-2d38-4894-ae01-0e89d392bc7d
ONBOOT=yes
HWADDR=08:00:27:C5:46:4E
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
[vagrant@master-001 ~]$ cat /etc/sysconfig/network-scripts/ifcfg-enp0s8
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=yes
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.31.101
NETMASK=255.255.255.0
DEVICE=enp0s8
PEERDNS=no
#VAGRANT-END
[vagrant@master-001 ~]$ sudo nmcli con up System\ enp0s8
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[vagrant@master-001 ~]$ nmcli con show
NAME           UUID                                  TYPE            DEVICE 
enp0s8         d55b8e76-0cf7-4b04-ae0c-2ea54b4c34cf  802-3-ethernet  --     
System enp0s8  00cb8299-feb9-55b6-a378-3fdc720e0bc6  802-3-ethernet  enp0s8 
enp0s3         93cc844d-2d38-4894-ae01-0e89d392bc7d  802-3-ethernet  enp0s3 
  • Vagrant 配置的网络“System enp0s8”被“enp0s8”覆盖了,导致设置的 Private Network 不起作用;
  • 添加auto_config: true也不行,因为该参数默认就是 true。估计是 CentOS 7 的 Network Manager 的原因,暂时使用一个初始化脚本配置虚拟机的网络,如下

    [admin@ityoudao k8s]$ cat > Vagrantfile <<EOF
    Vagrant.configure("2") do |config|
    (1..3).each do |i|
    config.vm.define "master-00#{i}" do |node|
      node.vm.box = "centos7-standard"
      node.vm.box_check_update = false
      node.vm.hostname = "master-00#{i}"
      node.vm.network "private_network", ip: "192.168.31.10#{i}"
      node.vm.provision "shell", path: "post-deploy.sh" ,run: "always"
    end
    end
    (1..3).each do |i|
    config.vm.define "node-00#{i}" do |node|
      node.vm.box = "centos7-standard"
      node.vm.box_check_update = false
      node.vm.hostname = "node-00#{i}"
      node.vm.network "private_network", ip: "192.168.31.20#{i}"
      node.vm.provision "shell", path: "post-deploy.sh" ,run: "always"
    end
    end
    end
    EOF
    

初始化脚本post-deploy.sh有两个功能,一是配置 hosts 文件,二是启用“System enp0s8”网络:

[admin@ityoudao k8s]$ cat > post-deploy.sh <<"EOF"
#!/bin/bash
value=$( grep -ic "entry" /etc/hosts )
if [ $value -eq 0 ]
then
echo "
################ kubernetes host entry ############
192.168.31.101  master-001
192.168.31.102  master-002
192.168.31.103  master-003
######################################################
" >> /etc/hosts
fi
if [ -e /etc/redhat-release ]
then
  nmcli connection up System\ enp0s8
fi
EOF
[admin@ityoudao k8s]$ vagrant up

参考文档:

Vagrant 官方文档 Vagrantfile

发表评论