【问题标题】:how to install Qemu guest agent on the domain - for KVM network如何在域上安装 Qemu 来宾代理 - 用于 KVM 网络
【发布时间】:2021-11-08 07:18:55
【问题描述】:

我正在尝试通过 terraform 在 KVM 上部署 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 来宾代理?

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

如何确保它正常工作?

【问题讨论】:

    标签: terraform kvm


    【解决方案1】:

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

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

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

    不是,qemu-guest-agent 必须安装在来宾虚拟机上。

    如何确保它正常工作?

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

    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
    

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

    【讨论】:

    • 非常感谢您的详细解答!但是如果来宾尚未创建,我如何在来宾虚拟机上安装 qemu-guest-agent?我的意思是,我试图在通过 terraform 文件创建来宾时为其分配 IP。
    • 现在的输出是:"Changes to Outputs: + ips = [ + [ + "10.100.86.5", ], ]" 但我可以 ping 到 10.100.86.5
    • 我自己准备好自己的 qcow2 映像,然后在 Terraform 中配置它们,但您也可以探索 cloud-init 以通过 Terraform 配置执行此类操作。
    猜你喜欢
    • 2022-06-24
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多