在带有 Terraform/KVM 的 CentOS 7 上使用 Cloud-Init 设置静态 IP [英] Set static IP using Cloud-Init on CentOS 7 with Terraform/KVM

查看:167
本文介绍了在带有 Terraform/KVM 的 CentOS 7 上使用 Cloud-Init 设置静态 IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有 KVM 的 Terraform 创建了一个 CentOS 7 VM.作为测试服务器,这个虚拟机应该有一个静态 IP,应该可以使用 云初始化:

I create a CentOS 7 VM using Terraform with KVM. As testserver, this VM should have a static IP which should be possible using Cloud-Init:

version: 2
ethernets:
  eth0:
    # match:
    #   name: eth0
    dhcp4: false
    dhcp6: false
    addresses: 
      - 10.18.3.2/24
    gateway4: 10.18.3.1

现在我们重新创建虚拟机并打开它的控制台:

Now we re-create the VM and open its console:

terraform destroy -auto-approve
terraform apply -auto-approve
terraform console centos

这会从 Cloud-Init 产生以下输出

This produces the following output from Cloud-Init

[   10.073544] cloud-init[626]: Cloud-init v. 18.5 running 'init-local' at Wed, 15 Jan 2020 17:34:43 +0000. Up 10.04 seconds.
[  OK  ] Started Initial cloud-init job (pre-networking).
[  OK  ] Reached target Network (Pre).
         Starting LSB: Bring up/down networking...
[FAILED] Failed to start LSB: Bring up/down networking.
See 'systemctl status network.service' for details.
         Starting Initial cloud-init job (metadata service crawler)...
[  OK  ] Reached target Network.
         Starting Postfix Mail Transport Agent...
         Starting Dynamic System Tuning Daemon...
[  OK  ] Started Dynamic System Tuning Daemon.
[  310.701064] cloud-init[820]: Cloud-init v. 18.5 running 'init' at Wed, 15 Jan 2020 17:39:44 +0000. Up 310.67 seconds.
[  310.724568] cloud-init[820]: ci-info: +++++++++++++++++++++++++++++++++++Net device info+++++++++++++++++++++++++++++++++++
[  310.727151] cloud-init[820]: ci-info: +--------+------+---------------------------+-----------+-------+-------------------+
[  310.729810] cloud-init[820]: ci-info: | Device |  Up  |          Address          |    Mask   | Scope |     Hw-Address    |
[  310.732602] cloud-init[820]: ci-info: +--------+------+---------------------------+-----------+-------+-------------------+
[  310.735405] cloud-init[820]: ci-info: |  eth0  | True | fe80::5054:ff:fe6a:ca6/64 |     .     |  link | 52:54:00:6a:0c:a6 |
[  310.738253] cloud-init[820]: ci-info: |   lo   | True |         127.0.0.1         | 255.0.0.0 |  host |         .         |
[  310.740787] cloud-init[820]: ci-info: |   lo   | True |          ::1/128          |     .     |  host |         .         |
[  310.743060] cloud-init[820]: ci-info: +--------+------+---------------------------+-----------+-------+-------------------+

[FAILED] Failed to start LSB: Bring up/down networking 线上花了大约 5 分钟.正如我们所见,机器没有按照 Cloud-Init 的要求分配 ipc4 地址.

It took about 5 minutes at the line [FAILED] Failed to start LSB: Bring up/down networking to fail. As we can see, the machine doesn't have a ipc4 address assigned, as requested via Cloud-Init.

为什么我的静态 IP 地址没有分配?我还尝试将 ens3 作为接口名称,但没有成功.

Why is my static IP address not assigned? I also tried ens3 as interface name, without success.

provider "libvirt" {
  uri = "qemu:///system"
}
resource "libvirt_pool" "test_pool" {
  name = "test_pool"
  type = "dir"
  path = "/tmp/kvm_test"
}
resource "libvirt_volume" "centos7-img" {
  name    = "centos7.qcow2"
  pool    = libvirt_pool.test_pool.name
  source =  "/var/lib/libvirt/images/CentOS-7-x86_64-GenericCloud.qcow2"
  format =  "qcow2"
}
data "template_file" "cloudinit_data" {
  template = file("cloudinit.cfg")
}
data "template_file" "cloudinit_network" {
  template = file("network.cfg")
}
resource "libvirt_cloudinit_disk" "cloudinit" {
  name           = "cloudinit.iso"
  # https://github.com/hashicorp/terraform/issues/7919#issuecomment-320816276
  user_data      = data.template_file.cloudinit_data.rendered
  network_config = data.template_file.cloudinit_network.rendered
  pool           = libvirt_pool.test_pool.name
}

resource "libvirt_network" "test_network" {
   name = "test_network"
   addresses = ["10.18.3.0/24"]
   dhcp {
      enabled = false
   }
}
resource "libvirt_domain" "centos" {
  name   = "centos"
  memory = "1024"
  vcpu   = 4
  cloudinit = libvirt_cloudinit_disk.cloudinit.id

  network_interface {
    #network_name = "default"
    network_id = libvirt_network.test_network.id
  }

  disk {
    volume_id = libvirt_volume.centos7-img.id
  }

  console {
    type = "pty"
    target_type = "serial"
    target_port = "0"
  }
  console {
    type        = "pty"
    target_type = "virtio"
    target_port = "1"
  }

  graphics {
    type = "spice"
    listen_type = "address"
    autoport = true
  }
}

cloudinit.cfg

#cloud-config
# https://cloudinit.readthedocs.io/en/latest/topics/modules.html
timezone: Europe/Berlin

fqdn: myhost.internal
manage_etc_hosts: true
resize_rootfs: true

ssh_authorized_keys:
  - ssh-rsa mykey

ssh_pwauth: true
password: password
chpasswd:
  list: |
    root:password
    centos:password
  expire: false

# Here I set the yum proxy and update all packages
# runcmd:
# ...

网络服务日志

# systemctl status network.service -l
● network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
   Active: failed (Result: timeout) since Wed 2020-01-15 18:39:44 CET; 3min 11s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 654 ExecStart=/etc/rc.d/init.d/network start (code=killed, signal=TERM)
   CGroup: /system.slice/network.service
           └─1000 /sbin/dhclient -q -lf /var/lib/dhclient/dhclient--eth0.lease -pf /var/run/dhclient-eth0.pid -H myhost eth0

Jan 15 18:40:50 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6 (xid=0x60f0f449)
Jan 15 18:40:56 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6 (xid=0x60f0f449)
Jan 15 18:41:02 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 11 (xid=0x60f0f449)
Jan 15 18:41:13 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 20 (xid=0x60f0f449)
Jan 15 18:41:33 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 20 (xid=0x60f0f449)
Jan 15 18:41:53 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 17 (xid=0x60f0f449)
Jan 15 18:42:10 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 17 (xid=0x60f0f449)
Jan 15 18:42:27 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 13 (xid=0x60f0f449)
Jan 15 18:42:40 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 9 (xid=0x60f0f449)
Jan 15 18:42:49 myhost dhclient[1000]: DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 17 (xid=0x60f0f449)

解决方法:不要使用 Cloud-Init 进行网络配置

目前我发现的唯一解决方法是禁用 Cloud-Init 的网络部分,这意味着不呈现 network.cfg 文件.相反,在 network_interface 部分设置静态 IP 是可行的:

Workaround: Dont use Cloud-Init for network config

Currently the only workaround I found is disabling the network part of Cloud-Init, which means not rendering the network.cfg file. Instead, setting a static IP in the network_interface section works:

resource "libvirt_domain" "centos" {
  name   = "centos"
  memory = "1024"
  vcpu   = 4
  cloudinit = libvirt_cloudinit_disk.cloudinit.id

  network_interface {
    network_id = libvirt_network.test_network.id

    hostname  = "centos"
    addresses = ["10.18.3.2"]
  }
  # ...
}

推荐答案

我遇到了同样的场景,简单地在配置文件中指定 dhcp4 或 dhcp6 会导致虚拟机尝试通过 dhcp 拉地址(即使值设置为 false.)

I ran into this exact same scenario and simply specifying dhcp4 or dhcp6 in the configuration file would cause the virtual machine to try to pull an address via dhcp (even with the value set to false.)

对我来说,解决方法是将 dhcp4 和 dhcp6 从配置文件中删除,并且在快速启动和静态网络配置到位的情况下一切正常.

Workaround for me was to leave dhcp4 and dhcp6 out of the configuration file and everything worked as expected with a fast bootup and static network configuration in place.

这篇关于在带有 Terraform/KVM 的 CentOS 7 上使用 Cloud-Init 设置静态 IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆