Master VMware vSphere with govc: A Step-by-Step Tutorial
Note: govmomi and its binaries use Gomodules, i.e. explicitly setting GOPATH is notrequired anymore. To inject build variables (see detailsbelow) used by govc version [-l], GOFLAGS can bedefined and are honored by go get.
You can also build govc following our release process using goreleaser(requires Go toolchain). This will ensure that buildtime variables are correctly injected. Build (linker) flags and injection aredefined in .goreleaser.yaml and automatically set asGOFLAGS when building with goreleaser.
The relative path in this example can only be used if the command canumambigously resolve a datacenter to use as origin for the query. If nodatacenter is specified, govc defaults to the only datacenter, if there isonly one. The datacenter itself can be specified as a pattern as well, enablingthe following arguments: -dc='my*' -ds='*store'. The datastore pattern islooked up and matched relative to the datacenter which itself is specified as apattern.
The -verbose flag writes request and response data to stderr, in a format more compact than the -trace or -debug flags.The output includes the request method name with abbreviated input parameters. The response data is more detailed and may includestructures formatted as Go code, such as Task property updates. The value of some properties will the govc object.collectcommand that can be used to view the actual value.
When connecting to a non-TLS endpoint, Go's http.Client will not send Securecookies, resulting in a NotAuthenticated error. For example, running govcdirectly against the vCenter vpxd endpoint at :8085. Set theenvironment variable GOVMOMI_INSECURE_COOKIES=true to workaround this:
The vSphere web GUI is a nice visual tool, but if you need to retrieve vCenter information in bulk or perform mass operations across VMs, then a command line tool such as govc in invaluable. You can find the repo for govc at , along with installation instructions. govc is written in Go, which means it has support on Linux as well as most other platforms.To perform a quick install on Linux, run this command:$ sudo curl -L -o - \" _$(uname -s)_$(uname \-m).tar.gz" sudo tar -C /usr/local/bin -xvzf - govcNext, you'll want to set up basic connectivity to the vCenter, and for this purpose, you can use a set of environment variables, so the CLI knows how to connect to the vCenter.# vCenter hostexport GOVC_URL=myvcenter.name.com# vCenter credentialsexport GOVC_USERNAME=myuserexport GOVC_PASSWORD=MyP4ss# disable cert validationexport GOVC_INSECURE=trueNext, you can try out a few basic commands:$ govc aboutName: VMware ESXiVendor: VMware, Inc.Version: 6.7.0Build: 8169922OS type: vmnix-x86API type: HostAgentAPI version: 6.7Product ID: embeddedEsxUUID$ govc datacenter.infoName: mydc Path: /mydc Hosts: 1 Clusters: 0 Virtual Machines: 3 Networks: 1 Datastores: 1$ govc ls/mydc/vm/mydc/network/mydc/host/mydc/datastoreNext, set a variable $dc, so that we can use it later:$ dc=$govc ls /)Now you can request various information from the vCenter. For example:Network:$ govc ls -l=true $dc/networkESXi Cluster:# cluster namegovc ls $dc/host# details on cluster, all members and their cpu/mem utilizationgovc host.info [clusterPath]# all members listed (type: HostSystem, ResourcePool)govc ls -l=true [clusterPath]# for each cluster member of type HostSystem, individual statsgovc host.info [memberPath]Datastores:# top level datastores (type: Datastore and StoragePod)govc ls -l=true $dc/datastore# for atomic Datastore type, get capacitygovc datastore.info [datastorePath]# get StoragePod overall utilizationgovc datastore.cluster.info [storagePodPath]# get list of storage pod membersgovc ls [storagePodPath]# then get capacity of each membergovc datastore.info [storagePodMemberPath]VM information:# show basic info on any VM names that start with 'myvm'govc vm.info myvm*# show basic info on single VMgovc vm.info myvm-001# use full path to get detailed VM metadatavmpath=$(govc vm.info myvm-001 grep "Path:" awk 'print $2')govc ls -l -json $vmpathShtudown VM, power up VM:# gracefully shutdown guest OS using toolsgovc vm.power -s=true myvm-001# force immediate powerdowngovc vm.power -off=true myvm-001 # power VM back ongovc vm.power -on=true myvm-001If you found this useful, here's more on the same topic(s) in our blog:Virtual I/O Server lifecycle dates
Workload Partitioning (WPAR) in AIX 6.1
Change default value of hcheck_interval
Renaming a virtual machine domain with virsh
The easiest way to install govc is to get it from Chocolatey PS repository, although it may be a version or two behind the latest. You might want to get it from GitHub instead if being on a current version is important to you. The following commands are run in Powershell.
I have been meaning to write about this neat little feature that was added to the govc CLI late last year that allows you to easily deploy any OVF/OVA without the need of ovftool. You might ask, why not use ovftool? Well, if you just need to perform a very basic OVF/OVA deploy and prefer not to install anything on your desktop, this can be a nice alternative. govc is provided as a simple binary that is platform agnostic and supports Windows, Linux & Mac OS X. govc is built using govmomi which is also known as the vSphere SDK for Go and this also means you can consume this capablitity beyond just the CLI but also programmatically if you wish. Obviously, the CLI is the easiest method which I will demonstrate below.
How to download govc CLI for VMware vSphere
Download govc Linux binary from GitHub releases
Download govc Windows binary from GitHub releases
Download govc Mac binary from GitHub releases
Download govc source code and build from scratch
Download govc Docker image and run as a container
Download govc usage document and learn how to use it
Download govc certificate info and verify vCenter connection
Download govc cluster add command and automate host addition
Download govc datastore info command and get storage details
Download govc vm info command and get virtual machine details
Download govc vm power command and control virtual machine power state
Download govc host info command and get host system details
Download govc network info command and get network details
Download govc datacenter info command and get datacenter details
Download govc folder info command and get folder details
Download govc pool info command and get resource pool details
Download govc device info command and get device details
Download govc snapshot info command and get snapshot details
Download govc guest info command and get guest OS details
Download govc license info command and get license details
Download govc event info command and get event details
Download govc metric info command and get metric details
Download govc about info command and get about details
Download govc version info command and get version details
Download govc import ovf command and import OVF template
Download govc export ovf command and export OVF template
Download govc import spec command and import spec file
Download govc export spec command and export spec file
Download govc import vmdk command and import VMDK disk image
Download govc export vmdk command and export VMDK disk image
Download govc import xva command and import XVA image
Download govc export xva command and export XVA image
Download govc datastore upload command and upload file to datastore
Download govc datastore download command and download file from datastore
Download govc datastore mkdir command and create directory on datastore
Download govc datastore rm command and remove file or directory from datastore
Download govc datastore ls command and list files or directories on datastore
Download govc datastore disk create command and create virtual disk on datastore
Download govc datastore disk attach command and attach virtual disk to VM
Download govc datastore disk detach command and detach virtual disk from VM
Download govc vm create command and create virtual machine
Download govc vm clone command and clone virtual machine
Download govc vm destroy command and destroy virtual machine
Download govc vm migrate command and migrate virtual machine
Download govc vm relocate command and relocate virtual machine
Download govc vm customize command and customize virtual machine
Download govc vm change command and change virtual machine settings
Step 1 - Download the latest govc binary for your platform which can be found here. In this example, I will be running govc on my Mac OS X system and you can easily download it by running the following commands:
Step 4 - To ensure that govc can properly communicate with your vSphere enviornment, you can run the following command which should return details about the system you are connected to:
Step 6 - Now, we just need to open up the JSON file we saved and edit the values before we can use it in our deployment. You may have also noticed that there is an InjectOvfEnv param which means you can use govc to also inject OVF properties when deploying directly to ESXi host ?
VMware has developed a number of tools that can be used to interact with their product; this includes Python, as well as Go bindings to the vSphere API. In this case I will be using govc which is a CLI tool based based on the latter of the two. There's a whole lot of things that can be done in your vSphere environment using this tool, but the focus of this blog will be on sending keystrokes to the guest operating system, as it can be extremely useful in our case.
Below is an example of the script in Bash that will take care of the initial configuration of your Loadbalancer.org appliance. Please bear in mind it doesn't include deploying the Virtual Machine, and you need to make sure that it is Powered On and prompting for the credentials. Hint: It can be done using govc as well. Please refer to the project documentation for more detail.
As it's mostly a collection of the commands to be executed, it was rather easy to reproduce the same in PowerShell. This requires govc to be installed on your workstation. Note, as mentioned previously, the GitHub repository provides ready pre-built binaries. Another option is to install govc using a package manager such as Chocolatey:
You also need to have access to a working VMware infrastructure, supporting VMs with at least hardware version 13.The examples below use the govc command-line tool for remote vSphere provisioning and the ovftool for local Workstation or Fusion provisioning.
An alternative to plain base64 encoding is gzip+base64 as described in the Ignition supported platforms. This is especially useful when submitting the Ignition config via govc as an inline argument. In that case the encoded config is limited to slightly under 128 KiB on Linux, 256 KiB on macOS, and 32 KiB on Windows (8 KiB if using cmd.exe or PowerShell). If your config is larger than that limit, you may be able to submit it inline after compressing it with gzip.
For vSphere the solution is simple because instead of inlining the configuration file within your shell, govc allows you to specify a path to a local file instead with the vm.change-command and will handle reading and writing it internally, circumventing any shell limitations.
On a separate system install PowerShell, PowerCLI, govc and download the base OS to use for your Kubernetes installation. It is recommended to use Ubuntu 18.04LTS as the base OS for Kubernetes on VMware. DHCP should be configured on the subnet that the Kubernetes nodes are being deployed to.
At this point, you can modify the generated configs to your liking if needed.Optionally, you can specify additional patches by adding to the cp.patch.yaml file downloaded earlier, or create your own patch files.
Deploying an OVA/OVF is a typical operation in a vSphere environment, and VMware administrators have plenty of options for that: using the VMware Web or HTML5 clients or techniques based on OVF Tool such as PowerCLI or Ansible. Recently, I have been using the govc CLI when I had to do a basic OVA deployment.
Why precisely this method over a different one? Deploying OVA through the UI can be frustrating (especially when you have unknown errors, or after a vSphere upgrade) and time-consuming. In comparison, govc is easy to use, platform agnostic (supports macOS, Windows and Linux), works without ovftool and seamlessly if you import to vCenter or ESXi hosts.