Automate management of virtualized environments with Perl

Source: Internet
Author: User
Tags ssh virtual environment kvm hypervisor

Overview

Virtualization, as the foundation of cloud computing, is an important trend at present. Virtualization can improve the efficiency and availability of IT resources and applications. The kernel based virtual machine KVM was RedHat acquired in 2008 and was fully developed with the joint impetus of IBM and RedHat. KVM virtual machines are fully supported in the latest release of RHEL, and a complete set of libvirt management tools (Virsh/virt-top/virt-install/virt-manager, etc.) are integrated. The main manufacturer of virtualization, VMware's vsphere Virtual environment provides vsphere client that allows users to manage objects such as Vcenter/esx SERVER/DATACENTER/CLUSTER/VM visually and graphically. But when the number of managed objects is large, it can be time-consuming and laborious to use Virt-manager or vsphere Client graphics to handle some of the day-to-day business. This allows users to write programs to manage these day-to-day transactions through the appropriate APIs. This article describes the application of the Libvirt API and the vsphere SDK for Perl in System management. This article will help developers, system administrators, and system testers.

Managing KVM environments with Libvirt

Libvirt is a set of Open-source APIs to implement Linux virtualization, designed to provide a single way to manage many different virtualization scenarios. Currently Libvirt supports the following hypervisor:

Kvm/qemu
Xen
LXC
Openvz
VirtualBox
VMware ESX, workstation, player
Microsoft Hyper-V

Along with Libvirt,rhel's latest release also includes a series of libvirt based tools for simplifying maintenance management of virtual machines:

Virt-install: for creating virtual machines
Virsh: Interactive/batch shell, can be used to complete the day-to-day management of the virtual environment
Virt-manager: A graphical interface for the management of Hypervisor and its virtual machines
Virt-clone: for virtual machine clones
Virt-viewer: A graphical console tool for securely connecting virtual machines

Although Libvirt itself is developed by C, it provides a variety of mainstream language bindings. System administrators are free to choose the languages they are familiar with, such as Python, Perl, Ruby, Java, PHP, and so on.

Using Virsh to manage KVM virtual machines

The most common use of libvirt based management tools is Virsh. The format of the Virsh command is as follows:

Virsh [OPTION] COMMAND ARG

Virsh provides an interactive shell when no arguments are supplied. The administrator can write some simple shell scripts to complete the virtual machine/network/storage configuration through Virsh. The following code fragment shows how the Virsh command is used.

To connect to the KVM hypervisor using the SSH protocol:

[root@bjgssla]# Virsh Connect Qemu+ssh://9.9.9.9/system
root@9.9.9.9 ' s Password:

Enumerate domains (guest VM):

[root@bjgssla]# Virsh List
Id Name State
------------ --------------
RHKVM Running
RHKVM01 Running
XPKVM Running

Enumerate the details of the domain:

[root@bjgssla]# Virsh Dominfo RHKVM
Id:32
Name:rhkvm
uuid:9d37e044-b134-c923-bbe6-0db40707ff9b
OS TYPE:HVM
State:running
CPU (s): 1
CPU time:92.6s
Max memory:524288 KB
Used memory:524288 KB
Persistent:yes
Autostart:disable
Managed Save:yes

Suspend, continue, restart domain:

[root@bjgssla]# Virsh suspend RHKVM
Domain RHKVM suspended
[root@bjgssla]# Virsh Resume RHKVM
Doamin RHKVM resumed
[root@bjgssla]# Virsh reboot RHKVM
Domain RHKVM is being rebooted

Snapshot management:

[root@bjgssla]# Virsh snapshot-create RHKVM
Domain Snapshot 1336311489 created
[root@bjgssla]# Virsh snapshot-list RHKVM
Name Create Time State
------------------------------------------------- ----
1336311489 2012-05-06 09:38:09-0400 Running
[root@bjgssla]# virsh Snapshot-revert RHKVM 1336311489

Note: The physical host is called a node in a KVM virtual environment, and each guest is referred to as domain.

Write Perl scripts based on the Libvirt API

Virsh can do most of the day-to-day work, and more complex requirements can be achieved through Libvirt API programming. This section describes how to obtain finer virtual machine information based on how to use Libvirt for Perl.

The Sys::virt module on the CPAN is the Perl binding of Libvirt. First, the corresponding Sys-virt module is downloaded, compiled and installed according to the Libvirt version in the KVM environment. The 0.9.4 version of Libvirt is used in the experimental environment of this article, so the sys-virt-0.9.4 is used:

http://search.cpan.org/~danberr/Sys-Virt-0.9.4/

To compile and install the Sys::virt module:

[Root@bjgssla]# Perl makefile.pl
[root@bjgssla]# Make
[root@bjgssla]# make Install

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.