Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Perl to automate the management and deployment of virtualized environments

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Perl for automatic management and deployment of virtualized environment". In daily operation, it is believed that many people have doubts about how to use Perl to automate the management and deployment of virtualized environment. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "how to use Perl to automate the management and deployment of a virtualized environment"! Next, please follow the editor to study!

Overview

As the foundation of cloud computing, virtualization is an important trend at present. Virtualization can improve the efficiency and availability of IT resources and applications. After the kernel-based virtual machine KVM was acquired by RedHat in 2008, it has developed in an all-round way under the joint promotion of IBM and RedHat. The KVM virtual machine is fully supported in the newly released RHEL version, and a complete set of libvirt-based management tools (virsh/virt-top/virt-install/virt-manager, etc.) are integrated. The vSphere Client provided by VMware's vSphere virtual environment, a major vendor in the virtualization field, allows users to manage objects such as vCenter/ESX server/datacenter/cluster/VM in an intuitive and graphical way. But when there are a large number of managed objects, it is time-consuming and laborious to use the graphical way of virt-manager or vSphere Client to handle some daily tasks. For this reason, users can write programs to manage these daily affairs through the corresponding API. This article will introduce the application of libvirt API and vSphere SDK for Perl in system management. This article will be helpful to developers, system administrators, and system testers.

Use libvirt to manage the KVM environment

Libvirt is an open source API that implements Linux virtualization capabilities and is 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

The latest release of libvirt,RHEL also includes a series of libvirt-based tools to simplify the maintenance and management of virtual machines:

Virt-install: used to create virtual machines

Virsh: interactive / batch shell that can be used to complete day-to-day management of virtual environments

Virt-manager: a graphical interface for managing Hypervisor and its virtual machines

Virt-clone: for virtual machine cloning

Virt-viewer: a graphical console tool for securely connecting virtual machines

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

Use virsh to manage KVM virtual machines

Virsh is the most commonly used libvirt-based management tool. The format of the virsh command is as follows:

Virsh [OPTION] COMMAND ARG

When no parameters are provided, virsh provides an interactive shell. Administrators can write some simple shell scripts through virsh to complete the configuration of virtual machine / network / storage. The following code snippet shows how the virsh command is used.

Connect to KVM hypervisor using the ssh protocol:

[root@BJGSSLA] # virsh connect qemu+ssh://9.9.9.9/systemroot@9.9.9.9's password:

Enumerate fields (guest VM):

[root@BJGSSLA] # virsh listId Name State--32 rhkvm running33 rhkvm01 running34 xpkvm running

List the details of the domain:

[root@BJGSSLA] # virsh dominfo rhkvmId: 32Name: rhkvmUUID: 9d37e044-b134-c923-bbe6-0db40707ff9bOS Type: hvmState: runningCPU (s): 1CPU time: 92.6sMax memory: 524288 kBUsed memory: 524288 kBPersistent: yesAutostart: disableManaged save: yes

Suspend, continue, restart the domain:

[root@BJGSSLA] # virsh suspend rhkvmDomain rhkvm suspended [root@BJGSSLA] # virsh resume rhkvmDoamin rhkvm resumed [root@BJGSSLA] # virsh reboot rhkvmDomain rhkvm is being rebooted

Snapshot management:

[root@BJGSSLA] # virsh snapshot-create rhkvmDomain snapshot 1336311489 created [root@BJGSSLA] # virsh snapshot-list rhkvmName Create Time State---1336311489 2012-05-06 09:38:09-0400 running [root@BJGSSLA] # virsh snapshot-revert rhkvm 1336311489

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

Write Perl script based on libvirt API

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

The Sys::Virt module on CPAN is the Perl binding of libvirt. First of all, download, compile and install the corresponding Sys-Virt module according to the version of libvirt in KVM environment. Version 0.9.4 of libvirt is used in the experimental environment of this article, so Sys-Virt-0.9.4 is used:

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

Compile and install the Sys::Virt module:

[root@BJGSSLA] # perl Makefile.PL [root@BJGSSLA] # make [root@BJGSSLA] # make install

The following will introduce the use of libvirt API by writing a simple monitoring program. In a production environment, administrators often need to monitor the CPU usage of virtual machines. If you find that the CPU utilization of some virtual machines is abnormal, the administrator can remind the owner of the virtual machine or suspend some virtual machines to ensure the normal operation of the whole virtualized environment.

A simplified implementation algorithm is provided in the documentation of the virtual machine performance monitoring tool virt-top developed based on libvirt:

Periodically sample the cpuTime of the virtual machine. This value can be obtained from the get_info method of the Sys::Virt::Domain class. It records the elapsed CPU time since boot time in nanoseconds.

Assume that the actual time of the two samples is T1 and T2, and the running time of the virtual machine CPU is vt1 and vt2

The CPU usage of this virtual machine can be calculated as follows:

% CPU = 100% * (vt2-vt1) / ((t2-v1) * # _ of_cores * 10 ^ 9)

The actual sampling time (T1 and T2) in the above formula can be obtained by the gettimeofday method of the Perl standard library module Time::HiRes. # _ of_cores indicates the number of CPU cores of the host. This parameter can be obtained through the get_node_info method of the Sys::Virt class.

Listing 1. Hypervisor connection information and virtual machine name GetOptions ('uri=s' = >\ $uri,' dom=s' = >\ $dom) from command line arguments # get the hypervisor connection object my $conn = Sys::Virt- > new (address = > $uri) or die\ "Cannot connect to the hypervisor: $uri. $!\ n "; # get the my object $vm = $conn- > get_domain_by_name ($dom) or die\" Cannot find the domain: $dom. $!\ n "; my ($start_time, $end_time, $start_vtime, $end_vtime); # actual time to get the first sample $start_time = Time::HiRes::gettimeofday (); # get the virtual machine CPU time for the first sample $start_vtime = $vm- > get_info ()-> {'cpuTime'}; # wait for the next sample sleep (1) # actual time to get the second sampling $end_time = Time::HiRes::gettimeofday (); # get the virtual machine CPU time for the second sampling $end_vtime = $vm- > get_info ()-> {'cpuTime'}; # get the number of cores of the host CPU my $n_cores = $conn- > get_node_info ()-> {' cores'} # calculate the utilization of virtual machine CPU my $util_rate = 100 * ($end_vtime-$start_vtime) / (($end_time -\ $start_time) * 1000000000 * $n_cores); printf "CPU utilization of $dom in $uri is:% .2f%%\ n", $util_rate

The following is the result of a run:

[root@BJGSSLA] # / vcpu_util.pl-- uri qemu:///system-- dom rhkvmCPU utilization of rhkvm in qemu:///system is: 4.46%

Use vSphere SDK to manage the VMware environment

The libvirt described in the previous section is a common set of API, so it can also be used to manage the VMware environment. But in this respect, the function of vSphere SDK provided by VMware itself is more targeted. Next we will learn how to use vSphere SDK for Perl to manage the VMware virtualized environment.

Build vSphere SDK for Perl development environment

The latest vSphere SDK for Perl can be found on the official site of VMware. Readers need to sign up for a VMware account and choose the appropriate platform version to download according to their needs.

Before you start using vSphere SDK for Perl, you need to make sure that the connection between the development environment and vSphere works properly. We can test the connectivity by visiting Managed Object Browser (MOB):

Https:///mob

MOB is a server-side (ESX/vCenter) program based on Web, which is used to browse the properties and methods of various objects on the server side. If the browser correctly displays the following page, the connection between the development environment and vSphere is normal.

Figure 1. Use MOB to check connection status

After ensuring that the connection to the VMware environment is working properly, we need to install vSphere SDK for Perl. Make sure that tools such as OpenSSL/LibXML2/e2fsprogs that it depends on are installed before installation. The installation process of vSphere SDK for Perl is very simple. Extract the installation package, execute vmware-install.pl, and accept the default parameters:

[root@BJGSSLA] # tar-zxvf VMware-vSphere-CLI-4.X.X-XXXXX.i386.tar.gz [root@BJGSSLA] # / / sudo vmware-vsphere-cli-distrib/vmware-install.pl

After installation, you can run some of vSphere SDK for Perl's sample programs to test the availability of the development environment, such as datacenterlisting.pl. This program gets a list of ESX/ESXi hosts and virtual machines on those hosts:

[root@BJGSSLA] # cd / usr/share/doc/vmware-vcli/samples/discovery/ [root@BJGSSLA] # perl datacenterlisting.pl-server\-datacenter\-username-password

In the experimental environment of this paper, the running result of the script is:

[root@BJGSSLA] # perl datacenterlisting.pl-- server 9.115.66.131-- datacenter MyDC\-password passw0rdHosts found:1: 9.115.208.49VM's found:1: WIN2K32: WIN2K8R23: RHEL5

General process for interacting with the VMware environment

The interaction process between the vSphere SDK for Perl and vSphere environments is roughly divided into the following four steps:

Verify command line parameters

Connect to the remote server and pass parameters

Perform user-defined operations, such as querying objects from remote servers, collecting information about server-side objects, obtaining or modifying remote server-side object states, etc.

Disconnect

Understanding this interaction process will help us to understand the logic of the sample program and organize the automation scripts developed by ourselves more clearly. This interaction is illustrated by parsing a simple piece of code.

Listing 2. Simple_flow.pl

#! / usr/bin/perl-wuse strict;use warnings;# imports the running support module of vSphere SDK for Perl # this module is used to complete server-client data mapping, load interaction functions between client and server, etc. Use VMware::VIRuntime # hash structure% opts stores custom command line parameters my% opts = (entity = > {type = > "= s", variable = > "VI_ENTITY", help = > "ManagedEntity type: HostSystem, etc", required = > 1,},); # vSphere SDK for Perl provides some basic command line parameters for all scripts, such as-- server,--url and other # Opts::add_options methods to add user-defined parameters Opts::add_options (% opts) # parsing the command line parameter Opts::parse (); # verifying the command line parameter Opts::validate (); # connecting to the remote server, vCenter or ESX serverUtil::connect (); # extracting the value of the command line parameter entity my $entity_type = Opts::get_option ('entity') # query server-side objects based on entity values. # Vim::find_entity_views returns the Perl view my $entity_views = Vim::find_entity_views (view_type= > $entity_type) corresponding to server-side objects; # outputs the Perl view information of server-side objects, such as foreach my $entity_view (@ $entity_views) {my $entity_name = $entity_view- > name;Util::trace (0, "Found $entity_type: $entity_name\ n") } # disconnect from the remote server Util::disconnect ()

Simple_flow.pl shows the general structure of vSphere SDK for Perl scripts, as well as the use of some common functions in the Opts package, the Util package, and the Vim package. The running results of simple_flow.pl in the experimental environment of this paper are as follows:

[root@BJGSSLA] #. / simple_flow.pl-- server 9.115.66.131-- username administrator\-- password passw0rd-- entity HostSystemFound HostSystem: 9.115.208.49 [root@BJGSSLA] #. / simple_flow.pl-- server 9.115.66.131-- username administrator\-- password passw0rd-- entity VirtualMachineFound VirtualMachine: WIN2K3Found VirtualMachine: WIN2K8R2Found VirtualMachine: RHEL55

VMware server-side objects and Perl views

Users write vSphere SDK for Perl scripts to access and modify server-side objects, such as virtual machines, clusters, snapshots, and so on. To do this, we need to understand the organization of vSphere server-side objects and the relationship between server-side objects and local Perl objects.

The vSphere server-side object is called managed object. Each managed object has a property set (properties) and provides related services (methods). The following figure shows the inheritance structure (part) of managed object:

Figure 2. Server-side managed object inheritance structure (part)

The abstract class ManagedEntity defines the most basic set of properties (such as name,parent, etc.) and methods (such as Reload,Rename_Task, etc.) for server-side objects. Common server-side objects, such as ResoucePool, HostSystem, Datacenter, VirtualMachine, etc., are inherited from ManagedEntity and provide specific properties and methods. Interested readers can learn more about the details of various managed object by visiting the MOB or vSphere SDK API online reference.

VSphere SDK for Perl can map the properties and methods of server-side objects to local Perl objects, that is, the local Perl view of server-side objects, so the Perl view can be regarded as a local copy of server-side objects, whose properties and methods correspond to server-side objects one by one. It is important to note, however, that the server-side objects are updated in real time, and the Perl view is a static copy, so users must explicitly refresh the Perl view using Vim::update_view_data when the latest status information is needed.

We can usually use the Vim::find_entity_views and Vim::find_entity_view functions to get the local Perl view (see the example in simple_flow.pl). Sometimes Vim::find_entity_views returns too many results, and the first result in the result set returned by Vim::find_entity_view may not be what we need, so we can use the filter parameter to refine the query conditions and control the query results. The following code snippet uses the guestFullName attribute to filter to get a view of all Windows virtual machines:

Listing 3. Filter using the filter parameter

.. my $vm_views = Vim::find_entity_views (view_type = > 'VirtualMachine',filter = > {# config (VirtualMachineConfigInfo class) is the property of VirtualMachine class # guestFullName (xsd:string class) is the property of VirtualMachineConfigInfo class' config.guestFullName' = > qr/Windows/}); foreach my $vm (@ $vm_views) {print "Name:". $vm- > name. "\ n";}.

Most server-side managed object have a large number of attributes, and we tend to be interested in only some of them when writing task scripts. It is very inefficient to construct a complete Perl view to get a property value, so we can specify the desired properties in the Perl view through the properties parameter:

Listing 4. Use the properties parameter to get the necessary properties

.. my $vm_view = Vim::find_entity_view (view_type = > 'VirtualMachine',filter = > {' name' = > 'foo'}, # get only the powerState attribute in the runtime attribute properties = > [' runtime.powerState']); # read the powerState property my $state = $vm_view- > runtime- > powerState;... of the Perl view

Use vSphere SDK for Perl to deploy and customize the virtual machine

Virtual machine template is a kind of reusable virtual machine image. Deploying virtual machine through template can avoid a lot of repetitive operations in the process of installing the system and greatly improve the efficiency. But even if we use templates to deploy the system, we still need to manually do some customized work for each virtual machine, such as configuring IP, hostname, and so on. This customization can be done using Customization Wizard or Customization Specification files provided by vSphere when deploying the template. Customization Wizard allows users to enter custom information in the form of a wizard, and users can also save the custom information as a Customization Specification file for later reuse. These tools are sufficient for small-scale deployments. However, when the scale becomes larger, manually entering, editing, and maintaining this information will also consume a lot of energy. The sample code given below automates virtual machine deployment and network configuration.

W2k8_deploy.pl demonstrates how to automatically deploy virtual machines and configure their networks through Window 2008R2 templates. This script applies to later Windows versions of XP/2003, including Windows 2008/Windows 2008 R2/Vista/Win 7 sp1. The customization of Windows 2008R2 and Win7 sp1 requires vCenter 4.1 update 1. For specific Windows Guest OS customization support, please refer to the relevant vSphere documentation.

Listing 5. W2k8_deploy.pl

#! / usr/bin/perl-wuse strict;use warnings;use VMware::VIRuntime;Opts::parse (); Opts::validate (); Util::connect (); # deploy the Windows target virtual machine and configure the network deploy_W2K8 (); Util::disconnect (); Virt-top documentationsub deploy_W2K8 {my $vmhost = "9.115.208.49"; # address of the ESX server where the target virtual machine is located my $ds = "datastore1"; # datastoremy $vm_template = "WIN2K8R2" of the target virtual machine # deploy template for target virtual machine my $respool = "ResPool"; # resource poolmy $vm_name = "WIN2K8R2A" where target virtual machine is located; # virtual machine name of target virtual machine # get template view my $vm_template_view = Vim::find_entity_view (view_type = > 'VirtualMachine',filter = > {name = > $vm_template}) # get ESX server view my $vmhost_view = Vim::find_entity_view (view_type = > 'HostSystem',filter = > {name = > $vmhost}); # get resource pool view my $respool_view = Vim::find_entity_view (view_type = >' ResourcePool',filter = > {name = > $respool}); # get datastore view my $ds_view = Vim::find_entity_view (view_type = > 'Datastore',filter = > {name = > $ds}) # Target virtual machine relocation information, specify the target virtual machine datastore/host/resource poolmy $relocate_spec = VirtualMachineRelocateSpec- > new (datastore = > $ds_view,host = > $vmhost_view,pool = > $respool_view,); # Custom Guest Windows os network configuration information # hostname / DNS domain / IP address / gateway / subnet mask / DNS server / user and organization / serial number / password my $host_name = "WIN2K8R2A" My $domain = "cn.ibm.com"; my $ip_address = "9.115.208.62"; my @ gateway = ("9.115.208.1"); my $netmask = "255.255.255.0"; my @ dnsServers = ("9.181.2.101", "9.181.2.102"); my $full_name = "IBMCN"; my $org_name = "IBMCN"; my $prod_ID = ""; my $password = "passw0rd" The customization of # Windows Guest OS does not need to specify the GlobalIP setting my $cust_global_settings = CustomizationGlobalIPSettings- > new (); # join the workgroup group. # if you use domain configuration, you need to provide domainAdmin/domainAdminPassword/joinDomain parameters my $cust_identification = CustomizationIdentification- > new (joinWorkgroup = > "workgroup",); my $cust_gui_unattended = CustomizationGuiUnattended- > new (autoLogon = > 1dint autoLogonCount = > 1department timezone = > 190J password = > CustomizationPassword- > new (plainText = > "true", value = > $password),); my $cust_user_data = CustomizationUserData- > new (computerName = > CustomizationFixedName- > new (name = > $host_name), fullName = > $full_name,orgName = > $org_name,productId = > $prod_ID,) My $win_prep = CustomizationSysprep- > new (guiUnattended = > $cust_gui_unattended,identification = > $cust_identification,userData = > $cust_user_data,); my $cust_IP_settings = CustomizationIPSettings- > new (dnsDomain = > $domain,dnsServerList = >\ @ dnsServers,ip = > CustomizationFixedIp- > new (ipAddress = > $ip_address), gateway = >\ @ gateway,subnetMask = > $netmask,); my $cust_adapter_mapping = CustomizationAdapterMapping- > new (adapter = > $cust_IP_settings,); my $cust_adapter_mapping_list = [$cust_adapter_mapping] My $cust_spec = CustomizationSpec- > new (globalIPSettings = > $cust_global_settings,identity = > $win_prep,nicSettingMap = > $cust_adapter_mapping_list,); my $clone_spec = VirtualMachineCloneSpec- > new (powerOn = > 1motif template = > 0charge location = > $relocate_spec,customization = > $cust_spec,); # launch task $vm_template_view- > CloneVM_Task (folder = > $vm_template_view- > parent,name = > $vm_name,spec= > $clone_spec);}

The CloneVM_Task method performs customized template deployment, in which the folder parameter determines the directory of the target virtual machine, the name parameter determines the virtual machine name of the target virtual machine, and the spec parameter of VirtualMachineCloneSpec type is used to customize the process of virtual machine cloning (including virtual machine hardware configuration customization, Guest OS customization, etc.). You can see that the previous large piece of code is all about constructing VirtualMachineCloneSpec objects. The following class diagram shows the relationship between the custom objects:

Figure 3. VirtualMachineCloneSpec composition

The customization process for windows 2003/xp and previous versions is slightly different. The main manifestations are as follows:

When constructing a CustomizationSysprep, you need to provide a CustomizationLicenseFilePrintData object as a parameter. As for the specific construction methods of CustomizationLicenseFilePrintData objects, readers can refer to the vSphere SDK API online documentation.

You need to prepare the sysprep file on the vCenter side (the OS after vista/2008 has a built-in sysprep, so you no longer need to prepare it on the vCenter side). Locate DEPLOY.cab from the OS installation media and extract it to the relevant OS directory of vCenter sysprep:

If vCenter is installed on 2003 Server, this path is:

C:\ Documents and Settings\ All Users\ Application Data\ VMware\ VMware VirtualCenter\ sysprep\\

If vCenter is installed on 2008 Server, this path is:

C:\ ProgramData\ VMware\ VMware VirtualCenter\ sysprep\\

In practical applications, considering the reusability of the code, it is more appropriate to put the parameters that need to be customized into external files (such as organizing with XML format), and then only need to change the necessary parts every time the virtual machine is deployed in batches. But to highlight the point and use as simple examples as possible to illustrate the logical flow of automated configuration, the parameters are still written directly in the configuration process.

The customization process of Linux is roughly the same as that of Windows, and I will not repeat it in this article. The main difference is that the CustomizationLinuxPrep object is passed in as a parameter when constructing CustomizationSpec. Both CustomizationLinuxPrep and CustomizationSysprep inherit from CustomizationIdentitySettings.

At this point, the study on "how to use Perl to automate the management and deployment of virtualized environments" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report