【发布时间】:2021-05-02 20:16:56
【问题描述】:
我有一个使用静态 IP 地址连接到我的家庭网络的测试服务器,使用 KVM/Libvirt 进行虚拟化。为了测试我的网络中的一些服务(例如使用手机),我想从我的 SOHO 路由器网络中分配这些虚拟机 IP - 静态或使用路由器 DHCP。
所以我的目标是:
- 在路由器 DHCP 范围之外分配一个静态 IP(DHCP 从 192.168.0.20 开始,在本例中我使用 192.168.0.10)
- 从路由器 DHCP 获取动态 IP(可使用 DNS 访问,因此没问题)
在这两种情况下,VM 都没有 IP 地址:
由于这些虚拟机是由 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
我认为这可能会导致 VM 使用 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 范围保留)
【问题讨论】:
-
请勿发布代码、数据、错误消息等的图片 - 将文本复制或输入到问题中。 How to Ask
标签: ubuntu networking terraform kvm libvirt