Scale Tanzu VMs using a simple shell script

VMware’s Tanzu Portfolio offers enough products that can help you on your modernisation journey. They also have something for traditional vSphere guys like me πŸ™‚ Yes, vSphere with Tanzu offers first hand Kubernetes experience without compromising on the chill and thrill that vSphere has been able to give us for so long πŸ™‚

In today’s blog, I would like to discuss about a particular offering in vSphere with Tanzu called “VM Service”. VM Service APIs allow vSphere admins to declaratively deploy (traditional) VMs into Kubernetes namespaces. Make no mistake, you will still be deploying a VM and not a container/pod. You can find more about Tanzu VMs here.

Most recently, I was tasked with performing a scale test of VMs deployed through VM Service and that’s how I ended up here. I was curious to know if there’s a “kubectl scale” like command to dynamically increase the count of VMs thereby deploying them at scale. Though not an apple to apple comparison, it definitely would have been a nice feature to have.

Hmm, now that we have established we don’t have a scale command, how do we achieve this without having to create multiple manifests individually? SHELL SCRIPT TO THE RESCUE πŸ˜€

VM Service Deployment

Pre-Requisites

A typical VM deployment would include creating a manifest and using kubectl create/apply against it. A sample deployment file can be found here. We will use one such manifest to scale our VMs as well.

Config Map

As you would have noticed in the sample manifest, each VM would reference a ConfigMap to instruct what needs to be done to a VM once its deployed. Since I was performing only a scale test, I referenced all my VMs to use the same configmap that I created using the following manifest. I had also pre-created a vSphere Namespace: β€œscale-test” and added relevant permissions, storage classes, VM classes and content libraries.

apiVersion: v1
kind: ConfigMap
metadata:
    name: scale-test-cm
    namespace: scale-test
data:
  user-data: |
    #cloud-config
    ssh_pwauth: true
    users:
      - name: vmware
        sudo: ALL=(ALL) NOPASSWD:ALL
        lock_passwd: false
        passwd: '$1$salt$SOC33fVbA/ZxeIwD5yw1u1'
        shell: /bin/bash
    write_files:
      - content: |
          VMSVC Says Hello World
        path: /helloworld

The config map instructs the machine to use cloud init to create a file called “helloworld” in the root directory and adds a user named “vmware” with password “Admin!23”.

Other pre-requisites include:

  1. Access to vSphere with Tanzu supervisor cluster from a jump box
  2. “kubectl” plugin which can be downloaded from here
  3. Login to the supervisor context

Shell Script

So, here’s the snippet that I used to create 200 VMs at once. It should work for any number that you choose to run it against. It is advisable to create a separate folder for this shell script, since it will create ‘n’ number of manifest files for deployment, n being the number you input. For the context of this blog, I will use 5 as the number.

#!/bin/bash
read -p 'Enter the number of VMs to be deployed: ' vmcount
for (( c=1; c<=$vmcount; c++ ))
do
    filename="deployvm_${c}.yaml"
    cat <<EOF > "$filename"
apiVersion: vmoperator.vmware.com/v1alpha1
kind: VirtualMachine
metadata:
  name: scale-test-vm-$c
  namespace: scale-test
spec:
  imageName: ubuntuova
  className: best-effort-xsmall
  powerState: poweredOn
  storageClass: vwt-storage-policy
  networkInterfaces:
  - networkName: vlan-20-pg
    networkType: vsphere-distributed
  vmMetadata:
      configMapName: scale-test-cm
      transport: CloudInit
EOF
    echo "Created $filename"
#    echo "Creating VM using kubectl apply"
    k apply -f $filename
done

That’s it, all your VMs should be ready in few minutes, if there’s nothing on the template. For what its worth, it roughly took about 10 minutes for all 200 VMs to be up and running.

Screenshots

Configmap Creation

Script Execution

VM Deployment Screenshots

That’s it for today, folks!

Was this what you were expecting when you read the title of this blog? Would love to hear your feedback on this one πŸ™‚

Happy learning!

Please follow and like my content:

3 thoughts on “Scale Tanzu VMs using a simple shell script”

Leave a Comment

error

Enjoy this blog? Please spread the word :)