Learn How to Install, Configure and Test GOVC?
Audio : Listen to This Blog.
GOVC is a vSphere CLI built on top of govmomi. The CLI is designed to be a user-friendly alternative to the vSphere UI and well suited for automation tasks. govmomi is a “Golang” library for interacting with VMware vSphere APIs (ESXi and/or vCenter). It is built using the VMware vSphere SDK. It’s written in Go language and pre-compiled for Linux, OSX, and Windows. GOVC vSphere CLI is very useful to carry out various vCenter/vSphere operations from the command line.
It’s time to say GoodBye to VMware PowerCLI and welcome “GOVC”
Areas of Automation:
Solutions: VDI Lifecycle | Object Scale Test: | Migration: vMotion Lifecycle |
---|---|---|
|
|
|
Installation & Configuration of govc:
To install govc on Linux machines, we can make use of below shell script.
#!/bin/sh which govc 1>/dev/null 2>/dev/null if [ $? -ne 0 ]; then echo "Install govc.." wget https://github.com/vmware/govmomi/releases/download/v0.19.0/govc_linux_amd64.gz gunzip govc_linux_amd64.gz mv govc_linux_amd64 govc sudo cp -rf govc /usr/local/bin/ sudo chmod +x /usr/local/bin/govc else echo "govc already exists..." fi
To validate govc path and version, use the below script
root@centos:~$ which govc; govc version /usr/local/bin/govc govc 0.19.0
To configure the user environment with GOVC environment variables, update the user’s “.bash_profile” to load the GOVC environment variables.
export GOVC_URL=https://msys-vcsa.dev.msys.com export [email protected] export GOVC_PASSWORD=@Msys123 export GOVC_INSECURE=true source ~/.profile
Let’s make the API call to vSphere environment using govc. “govc ls” command is used to list all the vCenter objects as shown below.
root@centos:~$ govc ls /msys-dc/vm /msys-dc/network /msys-dc/host /msys-dc/datastore
Testing using govc:
Let’s explore creating Datacenter, Cluster, Adding hosts, Datastore, Virtual Machines, Power on/off VMs, Clone VMs using govc.
1 Datacenter creation in vCenter using govc:
root@centos:~$ govc datacenter.create msys-dc root@centos:~$ govc datacenter.info msys-dc Name: msys-dc Path: /msys-dc Hosts: 0 Clusters: 0 Virtual Machines: 0 Networks: 0 Datastores: 0
2 Cluster creation in vCenter using govc:
root@centos:~$ govc cluster.create -dc=msys-dc msys-cluster root@centos:~$ govc datacenter.info msys-dc Name: msys-dc Path: /msys-dc Hosts: 0 Clusters: 1 Virtual Machines: 0 Networks: 0 Datastores: 0
3 Host addition in vCenter using govc:
Adding host in DC: root@centos:~$ govc host.add -hostname init127-9.dev.msys.com -username root -password @Msys123 [20-11-19 02:54:14] adding init127-9.dev.msys.com to folder /msys-dc/host... OK root@centos:~$ govc host.info Name: init127-9.dev.msys.com Path: /msys-dc/host/init127-9.dev.msys.com/init127-9.dev.msys.com Manufacturer: Logical CPUs: 32 CPUs @ 2600MHz Processor type: Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz CPU usage: 1686 MHz (2.0%) Memory: 262098MB Memory usage: 3358 MB (1.3%) Boot time: 2019-08-09 13:09:36.709878 +0000 UTC State: connected Add host in cluster: root@centos:~$ govc cluster.add -cluster msys-cluster -hostname init127-9.dev.msys.com -username root -password @Msys123 [20-11-19 03:02:54] adding init127-9.dev.msys.com to cluster /msys-dc/host/msys-cluster... OK root@centos:~$ govc host.info Name: init127-9.dev.msys.com Path: /msys-dc/host/msys-cluster/init127-9.dev.msys.com Manufacturer: Logical CPUs: 32 CPUs @ 2600MHz Processor type: Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz CPU usage: 0 MHz (0.0%) Memory: 262098MB Memory usage: 0 MB (0.0%) Boot time: 2019-08-09 13:09:36.709878 +0000 UTC State: connected
4 Datastore creation in vCenter using govc:
root@centos:~$ govc datastore.create -type vmfs -name msys-ds -path /msys-dc/datastore -disk="naa.624a9370522f227d0784442d00011039" init127-9.dev.msys.com root@centos:~$ govc datastore.info msys-ds Name: msys-ds Path: /msys-dc/datastore/msys-ds Type: VMFS URL: ds:///vmfs/volumes/5dd514d1-ff6548ba-1540-f8bc123d5c26/ Capacity: 5119.8 GB Free: 5118.3 GB
5 VM Creation in vCenter using govc:
root@centos:~$ govc vm.create -ds=msys-ds -on=false -link=false -net="VM Network" -pool development govc-vm2 root@centos:~$ govc vm.info govc-vm2 Name: govc-vm2 Path: /msys-dc/vm/govc-vm2 UUID: 42315795-6140-58d0-29bf-deee48f04c1d Guest name: Other (32-bit) Memory: 1024MB CPU: 1 vCPU(s) Power state: poweredOff Boot time:IP address: Host: init127-9.dev.msys.com
6 Powering on VM in vCenter using govc:
root@centos:~$ govc vm.power -on=true govc-vm2 Powering on VirtualMachine:vm-3194... OK root@centos:~$ govc vm.info govc-vm2 Name: govc-vm2 Path: /msys-dc/vm/govc-vm2 UUID: 42315795-6140-58d0-29bf-deee48f04c1d Guest name: Other (32-bit) Memory: 1024MB CPU: 1 vCPU(s) Power state: poweredOn Boot time: 2019-11-20 10:52:36.987655 +0000 UTC IP address: Host: init127-9.dev.msys.com
7 Accessing VM via Remote console
Any one of the utilities such as VMRC, VMware Player, VMware Fusion or VMware Workstation must be installed to open VMRC console URLs.
root@centos:~$ govc vm.console govc-vm2 vmrc://clone:cst-VCT-527e9bbb-7ddc-ff54-2e17-04806219b275--tp-11-7B-85-7C-94-F9-1F-86-71-37-13-8B-3F-48-E3-BB-42-7B-E7-8D@raghav-vcsa.dev.msys.com/?moid=vm-3194 (or) root@centos:~$ govc vm.console -capture screen.png govc-vm2 root@centos:~$ govc vm.console -capture - govc-vm2 | display
Remote console view
8 VM clone using govc:
root@centos:~$ govc vm.clone -ds=msys-ds -vm govc-vm2 -link scale-vm1 [20-11-19 04:48:18] Cloning /msys-dc/vm/govc-vm2 to scale-vm1...OK List VM’s root@centos:~$ govc ls /*/vm/* /msys-dc/vm/scale-vm1 /msys-dc/vm/govc-vm2
9 VM Cloning in scale using govc:
root@centos:~$ cat vm_clone.sh i=1 while [ $i -lt 10 ] do govc vm.clone -ds=msys-ds -on=false -vm raghav -link scale-vm-$i & i=`expr $i + 1` done root@centos:~$ ./vm_clone.sh [20-11-19 05:08:50] Cloning /msys-dc/vm/govc-vm2 to scale-vm-1...(95%) [20-11-19 05:08:50] Cloning /msys-dc/vm/govc-vm2 to scale-vm-1...OK [20-11-19 05:08:54] Cloning /msys-dc/vm/govc-vm2 to scale-vm-2...OK [20-11-19 05:08:57] Cloning /msys-dc/vm/govc-vm2 to scale-vm-3...OK [20-11-19 05:09:00] Cloning /msys-dc/vm/govc-vm2 to scale-vm-4...OK [20-11-19 05:09:04] Cloning /msys-dc/vm/govc-vm2 to scale-vm-5...OK [20-11-19 05:09:07] Cloning /msys-dc/vm/govc-vm2 to scale-vm-6...OK [20-11-19 05:09:11] Cloning /msys-dc/vm/govc-vm2 to scale-vm-7...OK [20-11-19 05:09:14] Cloning /msys-dc/vm/govc-vm2 to scale-vm-8...OK [20-11-19 05:09:18] Cloning /msys-dc/vm/govc-vm2 to scale-vm-9...OK root@centos:~$ govc ls /*/vm/* /msys-dc/vm/govc-vm2 /msys-dc/vm/scale-vm-2 /msys-dc/vm/scale-vm-6 /msys-dc/vm/scale-vm-7 /msys-dc/vm/scale-vm-1 /msys-dc/vm/scale-vm-8 /msys-dc/vm/scale-vm-9 /msys-dc/vm/scale-vm1 /msys-dc/vm/scale-vm-3 /msys-dc/vm/scale-vm-4 /msys-dc/vm/scale-vm-5
VM migration in vCenter using govc
vMotion From host init127-9 to host init127-16 root@centos:~$ govc vm.migrate -host init127-16.dev.msys.com govc-vm2 [23-11-19 21:44:33] migrating VirtualMachine:vm-3204... OK root@centos:~$ govc vm.info govc-vm2 Name: govc-vm2 Path: /msys-dc/vm/govc-vm2 UUID: 42315795-6140-58d0-29bf-deee48f04c1d Guest name: Other (32-bit) Memory: 1024MB CPU: 1 vCPU(s) Power state: poweredOn Boot time:IP address: Host: init127-16.dev.msys.com Storage vMotion(VMFS to VMFS) From msys-DS to msys-DS1 root@centos:~$ govc vm.migrate -ds msys-ds1 govc-vm2 [23-11-19 21:51:04] migrating VirtualMachine:vm-3204... OK Storage vMotion(VMFS to vVol) From msys-DS1 to vvol-ds root@centos:~$ govc vm.migrate -ds vvol-ds govc-vm2 [24-11-19 23:45:02] migrating VirtualMachine:vm-1209... OK
Export and Import OVA
Export root@centos:~$ govc export.ovf -i -f -sha 256 -vm govc-vm2 -name=raghav ds:///vmfs/volumes/5dd514d1-ff6548ba-1540-f8bc123d5c26/ [24-11-19 00:59:37] Downloading raghav-disk-0.iso... OK [24-11-19 01:00:06] Downloading raghav-disk-1.vmdk... [24-11-19 01:00:08] Downloading raghav-disk-1.vmdk... OK [24-11-19 01:00:08] Downloading raghav-disk-2.nvram... OK root@centos:~/ds:/vmfs/volumes/5dd514d1-ff6548ba-1540-f8bc123d5c26/raghav$ ls -lrth total 2.9G -rw-rw-r-- 1 root root 600M Nov 24 01:00 raghav-disk-1.vmdk -rw-rw-r-- 1 root root 8.5K Nov 24 01:00 raghav-disk-2.nvram -rw-rw-r-- 1 root root 837M Nov 25 22:50 raghav-disk-0.vmdk -rw-rw-r-- 1 root root 8.5K Nov 25 22:50 raghav-disk-1.nvram -rw-rw-r-- 1 root root 9.2K Nov 25 22:50 raghav.ovf -rw-rw-r-- 1 root root 272 Nov 25 22:50 raghav.mf -rw-rw-r-- 1 root root 1.5G Nov 25 22:53 raghav-disk-0.iso Import root@centos:~/ds:/vmfs/volumes/5dd514d1-ff6548ba-1540-f8bc123d5c26/raghav$ tar -cf /tmp/raghavan_1.ova . root@centos:~$ govc import.ova /tmp/raghavan_1.ova
References: https://github.com/vmware/govmomi/tree/master/govc