IaC and OpenShift Virtualization handshake (using Terraforms for VMs on OCP)
By: Nidhi Bansal
Tags: Openshift, Virtualization, Infrastructure As Code, Terraform

Author: Nidhi Bansal, Vishal Anand, Ranjan Roy

In our last blog, we talked about how to leverage Tekton (OpenShift Pipelines) to perform DevOps for virtualized applications running on VMs with OpenShift Virtualization (a feature of OpenShift container platform).

Provisioning infrastructure has historically been a time consuming and costly manual process. Now infrastructure management has moved away from physical hardware in data centers to virtualization, containers, and cloud computing for various cloud-native use cases.

Infrastructure as Code (IaC) can help an organization manage IT infrastructure needs while also improving consistency and reducing errors and manual configuration. IaC is an important part of implementing DevOps practices and continuous integration/continuous delivery (CI/CD).

Terraform is one of the open-source Infrastructure as Code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. It codifies cloud APIs into declarative configuration files.

While Terraform can manage infrastructure for both public and private cloud services, it can also manage external services like GitHub, Nomad, and Kubernetes pods. Terraform also provides full lifecycle management of OpenShift resources including creation and deletion of pods, replication controllers, and services.

With the advent of OpenShift Virtualization (based on upstream KubeVirt project) as a feature (Operator) of OpenShift container platform, we have to provision to manage and control VMs alongside container resources in our OpenShift container platform (OCP).

But then one goes thinking as how we can leverage the same IaC tools, natively built and working with container resources on OpenShift, manage the legacy VM workload as well, running on same OpenShift cluster. This is what we are going to demonstrate here.

Terraform configuration using KubeVirt provider

Terraform needs the KubeVirt provider along with Kubernetes provider to provision and interact with VM resources running on OpenShift. The provider is created as a custom Terraform plugin.

Terraform should have access to OpenShift cluster along with KubeVirt plugin built and installed in Terraform configuration on host system. The Terraform template .tf files call out these providers first.

After specifying the providers, initialize Terraform. This downloads and installs the latest version of the Terraform Kubernetes provider, locates and picks the KubeVirt provider from the locally installed plugins.

KubeVirt resources like DataVolumes(DV), Virtual Machines(VM) and other OpenShift resources required like Persistent Volume(PV), Persistent Volume Claim(PVC), services, routes etc. can now be created by providing the configuration in Terraform templates.

A DataVolume can be created using multiple definitions like source as PVC, http, local image, image registry.

Below is a snapshot of a DataVolume creation using source as Persistent Volume Claim (PVC) on OCP.

The created DataVolume can be used as a boot disk hosting source OS image required to build Virtual Machine on OCP. Virtual Machine can also be created with DataVolume creation specified as part of the VM resource configuration. There are multiple ways to specify DV within a VM configuration, below shown is a VM resource configuration in .tf file with DV cloned from an existing PVC hosting Windows 2012 OS image.

Apply this Terraform configuration to the OCP cluster.

The plan shows resource addition, updates or deletion as specified by the .tf configuration file. We can verify and choose to perform the required actions.

And there you go, our Virtual Machine creation on OCP is started. A dataVolume is created first, with VM starting showing up in OCP console.

Here is the VM listed in OCP, waiting to initialize with the source image imported from the dataVolume.

Terraform console shows the live update status and finishes when the VM creation is done.

The log of events shown here as visible on the OCP console.

And here is the windows 2012 VM ready and running in OCP within a few minutes.

Login to the newly created windows VM console running on OCP.

This is just one VM demoed here. Spinning-up and managing multiple VMs by a simple click, all with varied OS, configurations, networking, resources, disks, replicated across different environments, pipelines triggers… is at your fingertips now.

Needless to mention, use-cases are endless and so are the advantages.

Happy VM’ing on OpenShift container platform, side by side with containers.

Disclaimer: Views and thoughts expressed are our very own and really personal ones.