Skip to main content

VMware Govc: Manage vCenter Resources with Govc

862 words·
VMware vCenter Govc
Table of Contents

Overview
#

In this tutorial I’m using vSphere 8 and an Ubuntu 24.04 server for the Govc CLI.

192.168.70.9 # vcsa1.vsphere.intern
192.168.30.10 # Govc / Ubuntu 24.04

Govc Setup
#

Installation
#

# Download, extract & move the govc binary
cd /tmp &&
curl -L -o - "https://github.com/vmware/govmomi/releases/latest/download/govc_$(uname -s)_$(uname -m).tar.gz" | sudo tar -C /usr/local/bin -xvzf - govc
# Verify the installation / check version
govc version

# Shelll output:
govc 0.43.0

Add vSphere Root CA Certificate
#

Download vSphere Certificate
#

# Download vSphere Root CA Certificate
cd /tmp &&
wget https://vcsa1.vsphere.intern/certs/download.zip --no-check-certificate
# Install unzip
sudo apt install unzip

# Unzip the certificates
unzip download.zip

Install vSphere Certificate
#

# Create a folder for the certificate
sudo mkdir /usr/share/ca-certificates/vsphere

# Copy the certificate
sudo cp certs/lin/f093c9a0.0 /usr/share/ca-certificates/vsphere

# Rename the certificate
sudo mv /usr/share/ca-certificates/vsphere/f093c9a0.0 /usr/share/ca-certificates/vsphere/94dfc8ac.crt
# Add the vSphere certificate
sudo dpkg-reconfigure ca-certificates

Test the TLS Encryption
#

# Test the TLS encryption with curl
curl https://vcsa1.vsphere.intern/

# Shell output:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
 <head>
...

Export Environment Variables
#

export GOVC_URL="vcsa1.vsphere.intern"
export GOVC_USERNAME="Administrator@vsphere.intern"
export GOVC_PASSWORD="my-secure-pw"
export GOVC_INSECURE=0
  • export GOVC_INSECURE=0 # Set to “1” to disable TLS encryption



Govc Commands
#

Check Connection & List vCenter Details
#

# Retrieve general information about the vCenter server
govc about

# Shell output:
FullName:     VMware vCenter Server 8.0.1 build-22088981
Name:         VMware vCenter Server
Vendor:       VMware, Inc.
Version:      8.0.1
Build:        22088981
OS type:      linux-x64
API type:     VirtualCenter
API version:  8.0.1.0
Product ID:   vpx
UUID:         d3be7958-84d5-4626-894a-e2643120a4c8

List Datacenters
#

# List all datacenters
govc datacenter.info

# Shell output:
Name:                Datacenter
  Path:              /Datacenter
  Hosts:             1
  Clusters:          1
  Virtual Machines:  2
  Networks:          1
  Datastores:        1

List Datastores & Datastore Details
#

# List datastores in datacenter "Datacenter"
govc ls /Datacenter/datastore/

# Shell output:
/Datacenter/datastore/datastore1
# List datastore "datastore1" details
govc datastore.info /Datacenter/datastore/datastore1

# Shell output:
Name:        datastore1
  Path:      /Datacenter/datastore/datastore1
  Type:      VMFS
  URL:       ds:///vmfs/volumes/66f009c7-a6f2dc89-f899-000c2932768f/
  Capacity:  221.8 GB
  Free:      160.4 GB

List Clusters or Resource Pools
#

# List all resource pools and clusters in the datacenter
govc find /Datacenter/host/ -type p

# Shell output:
/Datacenter/host//Cluster1/Resources
# List resource pool details
govc pool.info /Datacenter/host/Cluster1/Resources

# Shell output:
Name:               Resources
  Path:             /Datacenter/host/Cluster1/Resources
  CPU Usage:        408MHz (1.9%)
  CPU Shares:       normal
  CPU Reservation:  21694MHz (expandable=true)
  CPU Limit:        21694MHz
  Mem Usage:        14409MB (41.8%)
  Mem Shares:       normal
  Mem Reservation:  34462MB (expandable=true)
  Mem Limit:        34462MB

List VMs & Folders
#

# List folders & VMs
govc ls /Datacenter/vm/

# Shell output:
/Datacenter/vm/Templates
/Datacenter/vm/k8s-cluster-1
/Datacenter/vm/vCLS
/Datacenter/vm/Discovered virtual machin
# List VMs in folder
govc ls "/Datacenter/vm/Discovered virtual machine"

# Shell output:
/Datacenter/vm/Discovered virtual machine/VMware vCenter Server

List VM Info
#

# List VM info
govc vm.info "/Datacenter/vm/Discovered virtual machine/VMware vCenter Server"

# Shell output:
Name:           VMware vCenter Server
  Path:         /Datacenter/vm/Discovered virtual machine/VMware vCenter Server
  UUID:         564dd379-2190-830e-d789-5ddf42a4ac09
  Guest name:   Other 3.x or later Linux (64-bit)
  Memory:       14336MB
  CPU:          2 vCPU(s)
  Power state:  poweredOn
  Boot time:    2024-09-30 07:38:39 +0000 UTC
  IP address:   192.168.70.9
  Host:         esxi1.jklug.intern

Upload ISO to Datastore
#

Create Folder
#

Create an “ISOs” folder:

# Create an "ISOs" folder in "datastore1"
govc datastore.mkdir -ds datastore1 ISOs
# Verify the folder
govc datastore.ls -ds datastore1

# Shell output:
VMware vCenter Server
vCLS-75b95b2a-252d-42dd-a870-7e62e428c144
ISOs

Upload ISO File
#

# Download ISO file to host
wget https://releases.ubuntu.com/24.04.1/ubuntu-24.04.1-live-server-amd64.iso
# Upload ISO file to "ISOs" folder in "datastore1"
export GOVC_DATASTORE=datastore1
govc datastore.upload ubuntu-24.04.1-live-server-amd64.iso ISOs/ubuntu-24.04.1-live-server-amd64.iso

# Shell output:
[30-09-24 08:31:00] Uploading... OK

Verify Upload / List ISO File
#

# List files in "ISOs" folder
govc datastore.ls -ds datastore1 ISOs/

# Shell output:
ubuntu-24.04.1-live-server-amd64.iso

Templates
#

Create Template Folder
#

# Create a folder
govc folder.create /Datacenter/vm/My-Templates

Create json File
#

# Create json options file
govc import.spec /tmp/ubuntu-2004-kube-v1.28.0.ova | jq . > ubuntu.json

# Edit the json file
vi ubuntu.json

Original:

{
  "DiskProvisioning": "flat",
  "IPAllocationPolicy": "dhcpPolicy",
  "IPProtocol": "IPv4",
  "NetworkMapping": [
    {
      "Name": "sddc-cgw-network-6",
      "Network": ""
    }
  ],
  "Annotation": "Cluster API vSphere image - Ubuntu 20.04 and Kubernetes v1.28.0 - https://github.com/kubernetes-sigs/cluster-api-provider-vsphere",
  "MarkAsTemplate": false,
  "PowerOn": false,
  "InjectOvfEnv": false,
  "WaitForIP": false,
  "Name": null
}

Adopt the following values:

{
  "DiskProvisioning": "flat",
  "IPAllocationPolicy": "dhcpPolicy",
  "IPProtocol": "IPv4",
  "NetworkMapping": [
    {
      "Name": "sddc-cgw-network-6",
      "Network": "VM Network" # Define VM network
    }
  ],
  "Annotation": "Cluster API vSphere image - Ubuntu 20.04 and Kubernetes v1.28.0 - https://github.com/kubernetes-sigs/cluster-api-provider-vsphere",
  "MarkAsTemplate": true, # Convert to template after import
  "PowerOn": false,
  "InjectOvfEnv": false,
  "WaitForIP": false,
  "Name": "ubuntu-2004-kube-v1.28.0.ova" # Define name
}
  • "DiskProvisioning": "thin" Optional define thin provisioning for testing

Without comments:

{
  "DiskProvisioning": "flat",
  "IPAllocationPolicy": "dhcpPolicy",
  "IPProtocol": "IPv4",
  "NetworkMapping": [
    {
      "Name": "sddc-cgw-network-6",
      "Network": "VM Network"
    }
  ],
  "Annotation": "Cluster API vSphere image - Ubuntu 20.04 and Kubernetes v1.28.0 - https://github.com/kubernetes-sigs/cluster-api-provider-vsphere",
  "MarkAsTemplate": true,
  "PowerOn": false,
  "InjectOvfEnv": false,
  "WaitForIP": false,
  "Name": "ubuntu-2004-kube-v1.28.0"
}

Import OVA Template
#

# Import the OVA template & convert the VM to a VM template
govc import.ova -folder /Datacenter/vm/My-Templates -options ubuntu.json /tmp/ubuntu-2004-kube-v1.28.0.ova

# Shell output:
[30-09-24 09:15:58] Uploading ubuntu-2004-kube-v1.28.0-disk1.vmdk... OK
[30-09-24 09:15:58] Marking VM as template...

Verify & List the Template
#

# List VMs and templates
govc ls /Datacenter/vm/My-Templates

# Shell output:
/Datacenter/vm/My-Templates/ubuntu-2004-kube-v1.28.0
# List template details
govc vm.info /Datacenter/vm/My-Templates/ubuntu-2004-kube-v1.28.0

# Shell output:
Name:           ubuntu-2004-kube-v1.28.0
  Path:         /Datacenter/vm/My-Templates/ubuntu-2004-kube-v1.28.0
  UUID:         4219f44f-e3a8-1eb6-fe4c-45f2aebdf3b0
  Guest name:   Ubuntu Linux (64-bit)
  Memory:       2048MB
  CPU:          2 vCPU(s)
  Power state:  poweredOff
  Boot time:    <nil>
  IP address:
  Host:         esxi1.jklug.intern