Creating Dynamic VMs Infrastructure With Xen Hosts
Creating Dynamic VMs Infrastructure With Xen Hosts
In this article, take a look at creating dynamic VMs infrastructure with Xen hosts.
Join the DZone community and get the full member experience.Join For Free
In today's world, the server infrastructure machines are either in on-premise data centers, private data centers, or public cloud data centers. These machines are either physical bare-metal machines, virtual machines (VMs) with hypervisors, or small containers like Docker containers on top of physical or virtual machines. These machines physically might be in the local lab. In a private data center scenario, where your own procured hosts are placed in a physical space that is shared in a third-party data center and connected remotely. Whereas in the public data centers like AWS, GCP, Azure, OCI the machines are either reserved or created on-demand for the highly scalable needs connecting remotely. Each of these have its own advantages w.r.t scalability, security, reliability, management, and costs associated with those infrastructures.
The product development environment teams might need many servers during the SDLC process. Let us say if one had chosen the private data center with their own physical machines along with Xen Servers. Now, the challenge is how the VMs lifecycle is managed for provision or termination in similar to cloud environments with lean and agile processes.
This document is aiming to provide the basic infrastructure model, architecture, minimum APIs and sample code snippets so that one can easily build dynamic infrastructure environments.
First, let's understand the typical sequence of the steps followed in this server's infrastructure process. You can recollect it as below.
- Procurement of the new machines by IT
- Host virtualization - Install Xen Server and Create VM Templates by IT
- Static VMs request by Dev and test teams via (say JIRA) tickets to IT
- Maintain the received VM IPs in a database or a static file or hardcoded in configuration files or CI tools like in Jenkins config.xml
- Monitor the VMs for health checks to make sure these are healthy before using to install the products
- Cleanup or uninstall before or after Server installations
- Windows might need some registry cleanup before installing the product
- Fixed allocation of VMs to an area or a team or dedicated to an engineer might have been done
Now, how can you make this process more lean and agile? Can you eliminate most of the above steps with simple automation?
Yes. In our environment, we had more than 1000 VMs and tried to achieved and mainly the below.
"Disposable VMs on-demand as required during tests execution. Solve Windows cleanup issues with regular test cycles."
As you see below, using the dynamic VMs server manager API service, 6 out of 8 steps can be eliminated and it gives the unlimited infrastructure view for the entire product team. Only the first 2 steps - procure and host virtualization are needed. In effect, this saves in terms of time and cost!
[caption id="attachment_8597" align="alignnone" width="3218"]
Dynamic Infrastructure Model
The below picture shows our proposed infrastructure for a typical server product environments where 80% of docker containers, 15% as dynamic VMs, and 5% as static pooled VMs for special cases. This distribution can be adjusted based on what works most for your environment.
[caption id="attachment_8598" align="alignnone" width="3078"]
From here on, we will discuss more about Dynamic VM server manager part.
Dynamic Server Manager architecture
In the dynamic VMs server manager, a simple API service where the below REST APIs can be exposed and can be used anywhere in the automated process. As the tech stack shows, python 3 and Python-based Xen APIs are used for actual creation of VMs with XenServer host. Flask is being used for the REST service layer creation. The OS can be any of your product supported platforms like windows2016, centos7, centos8, debian10, ubuntu18, oel8, suse15.
[caption id="attachment_8599" align="alignnone" width="3056"]
Save the history of the VMs to track the usage and time to provision or termination can be analyzed further. For storing the JSON document, Couchbase enterprise server, which is a NoSQL document database can be used.
Simple REST APIs
|GET||/showall||Lists all VMs in JSON format|
|GET||/getavailablecount/<os>||Gets the list of available VMs count for the given <os>|
Provisions given <count> VMs of <os>.
cpus count and mem size also can be supported.
expiresin parameter in minutes to get expiry (auto termination) of the VMs.
|Terminates given <count> VMs of <os>|
Pre-Requirements for Dynamic VM Targeted Xen Hosts
- Identify targeted dynamic VM Xen Hosts
- Copy/create the VM templates
- Move these Xen Hosts a separate VLAN/Subnet (work with IT) for IPs recycle
At a high level -
- Create functions each REST API
- Call a common service to perform different REST actions.
- Understand the Xen Session creation, getting the records, cloning VM from template, attaching the right disk, waiting for the VM creation and IP received; deletion of VMs, deletion of disks
- Start a thread for expiry of VMs automatically
- Read the common configuration such as .ini format
- Understand working with Couchbase database and save documents
- Test all APIs with required OSes and parameters
- Fix issues if any
- Perform a POC with few Xen Hosts
The below code snippets can help you to understand even better.
Creation Xen session
Historic Usage of VMs
It is better to maintain the history of all the VMs created and terminated along with other useful data. Here is the example of JSON document stored in the Couchbase, a free NoSQL database server. Insert a new document using the key as the xen opac reference uuid whenever a new VM is provisioned and update the same whenever VM is terminated. Track the live usage time of the VM and also how the provisioning/termination done by each user.
The Dynamic VM server manager service configuration such as Couchbase server, xenhost servers, template details, default expiry, and network timeout values can be maintained in a simple .ini format. Any new Xen Host received, then just add as a separate section. The config is dynamically loaded without restarting the Dynamic VM SM service.
Sample config file: .dynvmservice.ini
Sample REST API Calls Using curl
Jenkins Jobs With Single VM
Jenkins Jobs With Multiple VMs Needed
Here are a few of my observations noted during the process and it is better to handle to make it more reliable.
- Handle Storage name/ID different among different Xen Hosts
- Keep track of VM storage device name in the service input config file.
- Handle partial templates only available on some Xen Hosts while provisioning
- When Network IPs not available and Xen APIs gets the default 169.254.xx.yy on Windows. Wait until getting the non 169 address or timeout.
- Release servers should ignore os template as some of the templates might not be there Xen Hosts
- Provision on a specific given Xen Host reference
- Handle No IPs available or not getting network IPs for some of the VMs created.
- Plan to have a different subnet for dynamic VMs targeted Xen Hosts. The default network DHCP IP lease expiry might be in days (say 7 days) and no new IPs are provided.
- Handle the capacity check to count the in progress as Reserved IPs and should show less count than full at the moment. Otherwise, both in-progress and incoming requests might have issues. One or two VMs (cpus/memory/disk sizes) can be in buffer while creating and checking if any parallel requests.
Some of the key references that help while creating the dynamic VM server manager service.
Hope you had a good reading time!
Disclaimer: Please view this as a reference if you are dealing with Xen Hosts. Feel free to share if you learned something new that can help us. Your positive feedback is appreciated!
Thanks to Raju Suravarjjala, Ritam Sharma, Wayne Siu, Tom Thrush, James Lee for their help during the process.
Opinions expressed by DZone contributors are their own.