Automated Provisioning with Terraform on Libvirt and KVM — DevOps Diaries
Regardless of public cloud or on-premise environments, today’s enterprise infrastructure demands more automation than ever. Luckily, Infrastructure as Code (IaC) tools such as Terraform and Ansible provide solutions for both. This article provides a scenario where an on-prem KVM-based virtual environment is present and Terraform being utilised for automated provisioning. In addition, a useful article of a public cloud use-case with Terraform can be found here.
Terraform is the IaC tool of choice when it comes to the automation of infrastructure provisioning. Although primarily a configuration tool, Ansible, also does the job. However, popular analogy of hammer and nail is useful to remember and a good discussion about using them in the right use-cases can be found here.
KVM is an open-source type I hypervisor which uses the Linux kernel and libvirt library for virtualization. High performance, low-cost and Linux native nature makes it a good choice among the others. There are also enterprise-level virtualization software products such as Red Hat Virtualization and Proxmox built on these advantages.
Terraform is designed with providers and modules which enable the resource creation. The libvirt provider is developed and maintained by dmacvicar-led contributors. It provides several resources which are based on KVM terminology to do the job such as domain, network and volume. One especially useful resource is the cloudinit which enables the initialization of cloud images.
The scenario consists of a typical environment with three tiers. First one is the application load balancer layer, second is the application layer, and the third one database layer. Instead of provisioning all three layers with mouse clicks on virtualization software UI, a dynamic setup is developed in which all required specs of VMs such as CPU, memory and network configuration are parameterized.
This simulation does not have deployment of an application for simplicity’s sake. It would be quite easy to have one with the use of cloudinit resource. However, Ansible is probably a better choice for this task after provisioning.
The primary advantage to use an IaC tool such as Terraform is, VMs in the topology given above takes less than thirty seconds to provision regardless of their number. Moreover, any VM in any tier can be added or discarded with changing a couple of variables. Also, the specs of the existing VMs can be changed with the same practicality. Below, live screen capture is provided for illustration.
Source code and the instructions to deploy are available on my GitHub repo. Hope it provides a starting point for people in similar need.
PS: If you liked the article, please support it with claps. Cheers!