gpt4 book ai didi

ubuntu - 将来自主机网络的静态/DHCP IP 分配给由 Terraform 提供的 KVM VM

转载 作者:行者123 更新时间:2023-12-04 18:26:02 48 4
gpt4 key购买 nike

我有一个测试服务器连接到我的家庭网络,静态 IP 地址,使用 KVM/Libvirt 虚拟化。为了测试我网络中的某些服务(例如使用手机),我想从我的 SOHO 路由器网络中为这些虚拟机分配 IP - 静态地或使用路由器 DHCP。

所以我的目标是:

  1. 在路由器 DHCP 范围之外分配一个静态 IP(DHCP 从 192.168.0.20 开始,我在此示例中使用 192.168.0.10)
  2. 从路由器 DHCP 获取一个动态 IP(可以使用 DNS 访问,所以没问题)

在这两种情况下,虚拟机都没有 IP 地址:

enter image description here

由于这些虚拟机是由 Terraform 自动配置的,我认为 SO 是解决这个问题的好地方。

我的 Terraform POC 文件:

terraform {
required_version = ">= 0.13"
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.6.2"
}
}
}

resource "libvirt_volume" "centos7-img" {
name = "cnx_centos7.qcow2"
pool = libvirt_pool.default.name
source = "/var/lib/libvirt/images/CentOS-7-x86_64-GenericCloud.qcow2"
format = "qcow2"
}
provider "libvirt" {
uri = "qemu:///system"
}
resource "libvirt_pool" "default" {
name = "default"
type = "dir"
path = "/tmp/kvm"
}

data "template_file" "cloudinit_network" {
template = file("network.cfg")
}
data "template_file" "cloudinit_data" {
template = file("cloudinit.cfg")
vars = {}
}

resource "libvirt_cloudinit_disk" "cloudinit" {
name = "cloudinit.iso"
user_data = data.template_file.cloudinit_data.rendered
network_config = data.template_file.cloudinit_network.rendered
pool = libvirt_pool.default.name
}
resource "libvirt_network" "cnx_network" {
name = "cnx_network"
#addresses = ["192.168.0.17/24"]
mode = "bridge"
bridge = "br0"
dhcp {
enabled = true
}
# Enables usage of the host dns if no local records match
dns {
enabled = true
local_only = false
}
}

resource "libvirt_domain" "cnx" {
name = "cnx-poc"
memory = 2048
vcpu = 4
cloudinit = libvirt_cloudinit_disk.cloudinit.id

network_interface {
network_id = libvirt_network.cnx_network.id
hostname = "cnx.fritz.box"
#addresses = ["192.168.0.10"]
# Required to get ip address in the output when using dhcp
wait_for_lease = true
}

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
}
}

output "ips" {
value = libvirt_domain.cnx.*.network_interface.0.addresses
}

Cloudinit network.cfg

version: 2
ethernets:
eth0:
dhcp4: true
dhcp6: false
# addresses:
# - 192.168.0.10
gateway4: 192.168.0.1

Cloudinit cloudinit.cfg

这并不是真正需要的。我只是设置了一个密码,这样我就可以使用 libvirt 控制台访问 VM 并查看 ip 配置,即使在网络无法正常工作时也是如此。

#cloud-config
password: password
chpasswd:
list: |
root:password
centos:password
expire: false

网桥/etc/netplan/50-cloud-init.yaml(主机)

# This file is generated from information provided by
# the datasource. Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
enp6s0:
#addresses: []
dhcp4: no
dhcp6: no
bridges:
br0:
interfaces: [enp6s0]
addresses: [192.168.0.17/24]
gateway4: 192.168.0.1
#mtu: 1500
nameservers:
addresses: [192.168.0.1]
search: ["fritz.box"]
parameters:
stp: true
#forward-delay: 4
dhcp4: no
dhcp6: no
version: 2

测试和应用:

$ sudo netplan generate
$ sudo netplan --debug apply

我尝试过的其他事情

除了配置文件中注释掉的行之外,我还尝试了以下操作:

直接引用网桥

我尝试直接在 VM 中引用网桥,而不像这样定义 libvirt 网络:

resource "libvirt_domain" "cnx" {
name = "cnx-poc"
memory = 2048
vcpu = 4
cloudinit = libvirt_cloudinit_disk.cloudinit.id

network_interface {
bridge = "br0"
addresses = ["192.168.0.10"]
}
# ...

这不起作用,我认为这与 this problem of the missing qemu-guest-agent package 有关.我不能简单地解决这个问题,因为我需要网络访问才能安装它,但这是行不通的。我将尝试研究是否可以添加两个 NIC(1x NAT 1x 网桥)来连接互联网。

但这似乎不是一个好的解决方法。并且在票证中建议创建一个单独的网络。如果该解决方法可行,我不会有任何问题,但到目前为止我还没有运气。

不使用 cloudinit/network

因为我 run into this problem some time ago ,我尝试不指定网络配置:

network_config = data.template_file.cloudinit_network.rendered

我认为这可能会导致虚拟机使用 DHCP,或者至少我从 Terraform 静态分配的 IP 在这种情况下似乎不起作用。

调查生成的 kvm 对象

生成的网络如下所示:

$ virsh net-dumpxml cnx_network
<network connections='1'>
<name>cnx_network</name>
<uuid>${removed}</uuid>
<forward mode='bridge'/>
<bridge name='br0'/>
</network>

这在查看 articles like this 时似乎完全有效,谁在解释如何在 Ubuntu 上使用 KVM 和 netplan 手动设置。

此外,使用 virsh dumpxml cnx-poc 检查的 VM 的 xml 中的 interface type='network' 元素看起来不错:

<interface type='network'>
<mac address='${mac}'/>
<source network='cnx_network'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

关于环境的一些信息

主持人

  • Ubuntu 18.04.5 LTS
  • 静态 IP 192.168.0.17/24
  • Terraform v0.13.6
  • 提供商 registry.terraform.io/dmacvicar/libvirt v0.6.2
  • virsh 4.0.0

虚拟机

  • CentOS 7 云镜像
  • 192.168.0.10 测试为静态 IP(未保留给 DHCP 范围)

最佳答案

原来他的问题是由于libvirt provider plugin的变化引起的.最近我从 0.5.2 更新到 0.6.2。对于所有高于 0.4.2 的版本,default behavior was changed :

Until terraform-provider-libvirt 0.4.2, qemu-agent was used by defaultto get network configuration. However, if qemu-agent is not running,this creates a delay until connecting to it times-out.

In current versions, we default to not to attempt connecting to it,and attempting to retrieve network interface information from theagent needs to be enabled explicitly with qemu_agent = true, furtherdetails here. Note that you still need to make sure the agent isrunning in the OS, and that is unrelated to this option.

Note: when using bridge network configurations you need to enable theqemu_agent = true. otherwise you will not retrieve the ip adresses ofdomains.

Be aware that this variables may be subject to change again in futureversions.

qemu-guest-agent CentOS云镜像中已经安装,无需下载。但是由于更改了 libvirt 提供程序行为,它没有被使用。我还没有注意到这一点,因为我的虚拟机是使用 NAT 构建的。它现在只与我对桥接网络的更改有关。

事实上,这意味着我只需像这样将属性添加到我的 vm 域中:

resource "libvirt_domain" "cnx" {
name = "cnx-poc"
memory = 2048
vcpu = 4
cloudinit = libvirt_cloudinit_disk.cloudinit.id
# Required for bridged networks for libvirt provider plugin > 0.4.2
qemu_agent = true
# ...
}

现在我的虚拟机已经构建完成,它们获得了我的 DHCP 服务器的 IP 地址:

Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

Outputs:

ips = [
[
"192.168.0.163",
"fe80::5054:ff:fe62:2adf",
],
]
ips_db2 = [
[
"192.168.0.162",
"fe80::5054:ff:fe8a:ac6a",
],
]

关于ubuntu - 将来自主机网络的静态/DHCP IP 分配给由 Terraform 提供的 KVM VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65945531/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com