如何在域上安装 Qemu 来宾代理 - 适用于 KVM 网络

how to install Qemu guest agent on the domain - for KVM network

我正在尝试通过 KVM 上的 terraform 部署 VM。

我想让我的虚拟机在主机网络中获得一个IP,我的主机是10.100.86.180。所以我正在使用 Bridge(当我手动部署 VM 时效果很好)

但是对于 terraform - 在“terraform apply”之后它无法获得 IP,

我做错了什么?

这是我的 main.tf :

terraform {
  required_providers {
    libvirt  = {
      source = "dmacvicar/libvirt"
    }
  }
}

provider "libvirt" {
    uri = "qemu:///system"
}

resource "libvirt_volume" "centos7-qcow2" {
    name   = "centos7.qcow2"
    pool   = "default"
    source = "http:///14.7.1/output/KVMdisk1.qcow2"
    format = "qcow2"

}

data "template_file" "user_data" {
  template = "${file("${path.module}/cloud_init.cfg")}"
}

resource "libvirt_cloudinit_disk" "commoninit" {
  name         = "commoninit.iso"
  user_data    = "${data.template_file.user_data.rendered}"
}

resource "libvirt_network" "my_network" {
  name = "default"
  mode = "bridge"
  addresses = ["10.100.86.0/24"]
  bridge = "br0"
  dhcp {
     enabled = true
       }
}


resource "libvirt_domain" "gw" {
  name   = "gw"
  memory = "8192"
  vcpu   = 4
  
  qemu_agent = true
  
  network_interface {
   # network_id     = libvirt_network.my_network.id
   addresses = ["10.100.86.5"]
   bridge = "br0"   
   wait_for_lease = true
  }

  boot_device {
   dev = [ "hd", "network"]
  }

  disk {
    volume_id = "${libvirt_volume.centos7-qcow2.id}"
  }

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

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

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

它抛出这个错误:

╵
╷
│ Error: Error: couldn't retrieve IP address of domain id: c49d77eb-62c4-4532-93c2-7d3f351b26e7. Please check following:
│ 1) is the domain running proplerly?
│ 2) has the network interface an IP address?
│ 3) Networking issues on your libvirt setup?
│  4) is DHCP enabled on this Domain's network?
│ 5) if you use bridge network, the domain should have the pkg qemu-agent installed
│ IMPORTANT: This error is not a terraform libvirt-provider error, but an error caused by your KVM/libvirt infrastructure configuration/setup
│  timeout while waiting for state to become 'all-addresses-obtained' (last state: 'waiting-addresses', timeout: 5m0s)
│
│   with libvirt_domain.gw,
│   on main.tf line 41, in resource "libvirt_domain" "gw":
│   41: resource "libvirt_domain" "gw" {

我正在使用 Bridge - 我发现必须安装 Qemu 来宾代理并且 运行 在域内

为了发现连接到 LAN 的所有网络接口的 IP 地址。

如何在域上安装 Qemu 来宾代理?

我已经在我的主机上安装了它,够了吗?

如何确保它正常工作?

how can I install the Qemu guest agent on the domain?

sudo yum install qemu-guest-agent
sudo systemctl enable qemu-guest-agent --now

I have already install it on my Host, is it enough?

不是,qemu-guest-agent 必须安装在 guest vm 上。

How can I ensure it is working properly?

例如,您可以通过以下方式检查:

sudo systemctl status qemu-guest-agent

我个人无法做到

wait_for_lease = true 

在桥接网络上工作。就我而言,它仅适用于 libvirt 网络。可能需要配置桥接:

modprobe bridge
modprobe br_netfilter
cat >> /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
EOF
sysctl -p /etc/sysctl.conf

如果您希望 运行 您的机器通过 Terraform 和桥接网络,只需删除:

wait_for_lease = true

您还定义了连接到网桥的网络,然后您不使用它,而是将接口直接连接到网桥。如果要将接口直接连接到网桥,请删除网络定义。如果您想定义网络,请使用它。