Using govc with vcsim in Kubernetes
The vCenter Simulator
vcsim
is a powerful Go application to simulate VMware vCenter
Server
environments. I personally use it a lot together with Github Actions as part of
continuous integration pipelines, e.g. in the Tanzu Sources for
Knative and VMware Event
Broker
Appliance.
Recently a colleague reached out to me for some help to deploy and use vcsim
to verify an upstream contribution. As part of her testing, she wanted to
trigger a vCenter
event (VmPoweredOffEvent
) so her code could react
accordingly.
Her code and vcsim
were supposed to run inside a Kubernetes environment. This
isn’t well documented so she wasn’t quiet sure how to call the vcsim
APIs
manually from a local command line outside a Kubernetes cluster to trigger the
desired event.
So here’s a quick step-by-step tutorial how to use
govc
, a vSphere CLI
built on top of govmomi
to perform API
calls against vcsim
inside a Kubernetes cluster.
Create a Kubernetes Cluster
For this demo, we’re using kind (Kubernetes in Docker) which quickly has become one of the most important assets in my toolbox to spin up local Kubernetes environments.
Let’s go ahead and create a cluster.
|
|
kind
will correctly set the Kubernetes context (i.e. target cluster), but
let’s quickly verify that.
|
|
Deploy vcsim
We can use the official vcsim
Docker container
image, which makes it dead simple to
deploy vcsim
in a Kubernetes environment. The following command deploys
vcsim
in the default
namespace.
|
|
We can verify that the deployment succeeded with kubectl wait
.
|
|
kubectl get deploy,rs,pod
. You should
see a Deployment, ReplicaSet and Pod successfully deployed.Create Port-Forwarding
To keep this post simple and to not assume any load-balancing infrastructure, we
do not create a Kubernetes
Service to
expose vcsim
to the outside world.
Instead, we can create a
port-forwarding
to directly talk to the deployment from the local machine where we are going to
run the govc
commands.
|
|
The above command will forward traffic against the host-local address
127.0.0.1:8989
to the pod backing the vcsim
deployment inside Kubernetes.
Set up govc
Environment Variables
As a last step, we need to give govc
information about the vCenter (Simulator)
API endpoint.
Since kubectl port-forward
is a blocking call and needs to remain running 1, the
following steps are performed in a separate terminal.
|
|
Now verify that we can establish a connection.
|
|
Trigger an Event
As a final exercise, let’s trigger an event by powering off a virtual machine.
To see the events, open another terminal and set the environment variables as described above again.
|
|
Now go ahead and power off a VM.
|
|
Once you powered off the VM via the command below you should see the following events in the other console.
|
|
When you’re done you can throw away the environment in one command.
|
|
Wrap Up
I really like how easy it is to spin up even complex infrastructure environments on your local machine with the great open source tooling around us these days.
The vCenter Simulator vcsim
is a great tool to simplify access to vCenter APIs
and can be easily integrated into CI environments, e.g. with Github Actions. To
dig deeper, check out the earlier mentioned repositories Tanzu Sources for
Knative and VMware Event
Broker
Appliance for
some inspiration how we use vcsim
with Kubernetes in Github Actions.
Credits
Photo by ThisisEngineering RAEng on Unsplash
-
You could also send and resume the command to the background, but I am assuming you know how to perform this in your
$SHELL
, since you’re asking đ ↩︎