代码之家  ›  专栏  ›  技术社区  ›  Cragmorton

虚拟机没有从Vagrantfile获取静态IP

  •  0
  • Cragmorton  · 技术社区  · 8 年前

    我是网络方面的初学者,而且我刚刚开始使用虚拟机。 我正在做“Ansible for Devops”中的示例,在第3章中,我将创建三个虚拟机,并使用静态ip设置一个专用网络。

    我的流浪者档案看起来像这样:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.box = "geerlingguy/centos7"
      config.ssh.insert_key = false
      config.vm.synced_folder ".", "/vagrant", disabled: true
      config.vm.provider :virtualbox do |v|
        v.memory = 256
        v.linked_clone = true
      end
    
      config.vm.define "app1" do |app|
        app.vm.hostname = "orc-app1.dev"
        app.vm.network :private_network, ip: "192.168.60.4"
      end
    
      config.vm.define "app2" do |app|
        app.vm.hostname = "orc-app2.dev"
        app.vm.network :private_network, ip: "192.168.60.5"
      end
    
      config.vm.define "db" do |db|
        db.vm.hostname = "orc-db.dev"
        db.vm.network :private_network, ip: "192.168.60.6"
      end
    end
    

    流浪日志:

    ❯ vagrant up
    Bringing machine 'app1' up with 'virtualbox' provider...
    Bringing machine 'app2' up with 'virtualbox' provider...
    Bringing machine 'db' up with 'virtualbox' provider...
    ==> app1: Cloning VM...
    ==> app1: Matching MAC address for NAT networking...
    ==> app1: Checking if box 'geerlingguy/centos7' is up to date...
    ==> app1: Setting the name of the VM: 3_app1_1485309004899_30536
    ==> app1: Fixed port collision for 22 => 2222. Now on port 2202.
    ==> app1: Clearing any previously set network interfaces...
    ==> app1: Preparing network interfaces based on configuration...
        app1: Adapter 1: nat
        app1: Adapter 2: hostonly
    ==> app1: Forwarding ports...
        app1: 22 (guest) => 2202 (host) (adapter 1)
    ==> app1: Running 'pre-boot' VM customizations...
    ==> app1: Booting VM...
    ==> app1: Waiting for machine to boot. This may take a few minutes...
        app1: SSH address: 127.0.0.1:2202
        app1: SSH username: vagrant
        app1: SSH auth method: private key
        app1: Warning: Remote connection disconnect. Retrying...
    ==> app1: Machine booted and ready!
    ==> app1: Checking for guest additions in VM...
    ==> app1: Setting hostname...
    ==> app1: Configuring and enabling network interfaces...
    ==> app2: Cloning VM...
    ==> app2: Matching MAC address for NAT networking...
    ==> app2: Checking if box 'geerlingguy/centos7' is up to date...
    ==> app2: Setting the name of the VM: 3_app2_1485309032690_32260
    ==> app2: Fixed port collision for 22 => 2222. Now on port 2203.
    ==> app2: Clearing any previously set network interfaces...
    ==> app2: Preparing network interfaces based on configuration...
        app2: Adapter 1: nat
        app2: Adapter 2: hostonly
    ==> app2: Forwarding ports...
        app2: 22 (guest) => 2203 (host) (adapter 1)
    ==> app2: Running 'pre-boot' VM customizations...
    ==> app2: Booting VM...
    ==> app2: Waiting for machine to boot. This may take a few minutes...
        app2: SSH address: 127.0.0.1:2203
        app2: SSH username: vagrant
        app2: SSH auth method: private key
        app2: Warning: Remote connection disconnect. Retrying...
    ==> app2: Machine booted and ready!
    ==> app2: Checking for guest additions in VM...
    ==> app2: Setting hostname...
    ==> app2: Configuring and enabling network interfaces...
    ==> db: Cloning VM...
    ==> db: Matching MAC address for NAT networking...
    ==> db: Checking if box 'geerlingguy/centos7' is up to date...
    ==> db: Setting the name of the VM: 3_db_1485309060266_65663
    ==> db: Fixed port collision for 22 => 2222. Now on port 2204.
    ==> db: Clearing any previously set network interfaces...
    ==> db: Preparing network interfaces based on configuration...
        db: Adapter 1: nat
        db: Adapter 2: hostonly
    ==> db: Forwarding ports...
        db: 22 (guest) => 2204 (host) (adapter 1)
    ==> db: Running 'pre-boot' VM customizations...
    ==> db: Booting VM...
    ==> db: Waiting for machine to boot. This may take a few minutes...
        db: SSH address: 127.0.0.1:2204
        db: SSH username: vagrant
        db: SSH auth method: private key
        db: Warning: Remote connection disconnect. Retrying...
    ==> db: Machine booted and ready!
    ==> db: Checking for guest additions in VM...
    ==> db: Setting hostname...
    ==> db: Configuring and enabling network interfaces...
    

    和Vagrant SSH配置:

    Host app1
      HostName 127.0.0.1
      User vagrant
      Port 2202
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile /Users/mst/.vagrant.d/insecure_private_key
      IdentitiesOnly yes
      LogLevel FATAL
    
    Host app2
      HostName 127.0.0.1
      User vagrant
      Port 2203
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile /Users/mst/.vagrant.d/insecure_private_key
      IdentitiesOnly yes
      LogLevel FATAL
    
    Host db
      HostName 127.0.0.1
      User vagrant
      Port 2204
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile /Users/mst/.vagrant.d/insecure_private_key
      IdentitiesOnly yes
      LogLevel FATAL
    

    所以你可以看到,这些机器没有得到我为它们设置的静态IP,我无法使用它连接到它们。他们只有一个本地主机IP和一些高端口。在该示例中,我应该使用ansible在该机器上工作,并在清单文件中使用该静态IP,因此它们应该正确设置。

    有什么想法吗?

    马科斯谢拉 流浪汉1.9.1 VirtualBox 5.1.14

    谢谢

    编辑:机器使用CentOS,ip地址输出为:

    [root@orc-app1 vagrant]# ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
        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:dd:23:fa brd ff:ff:ff:ff:ff:ff
        inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
           valid_lft 86067sec preferred_lft 86067sec
        inet6 fe80::a00:27ff:fedd:23fa/64 scope link
           valid_lft forever preferred_lft forever
    3: enp0s8: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
        link/ether 08:00:27:4d:38:fc brd ff:ff:ff:ff:ff:ff
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Olli Tyynelä    8 年前

    尝试使用vagrant 1.9.0。我的同事有一个问题,如果是1.9.1,nfs共享将无法正确装载,这与盒子没有自动添加一个必要的接口有关。

    降级到1.9.0修复了此问题。

    vagrants github上有两个未解决的问题,它们具体涉及rhel/centos 7。

    这是其中之一 https://github.com/mitchellh/vagrant/issues/8138

        2
  •  0
  •   Frederic Henri    8 年前

    我根据示例进行了审查——网络接口文件已由vagrant正确创建

    [vagrant@orc-app2 ~]$ cd /etc/sysconfig/network-scripts
    [vagrant@orc-app2 network-scripts]$ ll
    total 236
    -rw-r--r--. 1 root    root      353 25 janv. 16:06 ifcfg-enp0s3
    -rw-------. 1 vagrant vagrant   214 25 janv. 16:06 ifcfg-enp0s8
    

    这个新网络接口的内容是正确的

    [vagrant@orc-app2 network-scripts]$ more ifcfg-enp0s8
    #VAGRANT-BEGIN
    # The contents below are automatically generated by Vagrant. Do not modify.
    NM_CONTROLLED=no
    BOOTPROTO=none
    ONBOOT=yes
    IPADDR=192.168.60.5
    NETMASK=255.255.255.0
    DEVICE=enp0s8
    PEERDNS=no
    #VAGRANT-END
    

    所以我只是重新启动网络服务来尝试

    [vagrant@orc-app2 network-scripts]$ sudo systemctl restart network
    

    还可以

    [vagrant@orc-app2 network-scripts]$ ip addr
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
        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:dd:23:fa brd ff:ff:ff:ff:ff:ff
        inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
           valid_lft 86391sec preferred_lft 86391sec
        inet6 fe80::a00:27ff:fedd:23fa/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:42:83:e9 brd ff:ff:ff:ff:ff:ff
        inet 192.168.60.5/24 brd 192.168.60.255 scope global enp0s8
           valid_lft forever preferred_lft forever
        inet6 fe80::a00:27ff:fe42:83e9/64 scope link
           valid_lft forever preferred_lft forever
    

    我没有另一个centos 7盒子要测试(仍然与6很好地工作),以确认这是这个盒子或新的centos的问题