Automating CLI Command Execution with Paramiko

I spend my time always playing around with environments. Sometimes it’s simple spinning up multiple VMs with Terraform, configuring with Ansible, or just running simple stress tests. In this particular case, I had spun up four CentOS VMs all of which have the same application running Folding@Home. Incase you don’t know what Folding@Home is:

Folding@home is a distributed computing project which studies protein folding, misfolding, aggregation, and related diseases. We use novel computational methods and large scale distributed computing to simulate timescales thousands to millions of times longer than previously achieved.”

I figured running this application in my Nutanix environment would be a fun project. Currently, I’m running playbooks from my Prism Central instance to automate powering on and off of these VMs based on pre-defined hours of the day. But what if I want to spin them up without logging into Prism Element? While you can define the power state of VMs with Terraform, sadly, you’re not able to yet with Nutanix. I decided to use an old friend, Python with Paramiko.

Note: Ok, I lied. It’s a combination of Python, Paramiko, and Nutanix ACLI.

What you’ll need:
1. CVM IP Address
2. Login credentials for said CVM
3. Python 3.0 installed
4. Image UUID

Alrighty, SSH into any of your CVMs with the appropriate user credentials. From here, you’ll need to gather the VMs you’d like to power on. Use the command: acli image.list

Copy to a clipboard the Image UUID.

Go to your editor of choice and use my already created script as a template.

I use Microsoft Visual Studio Code for my editing, but anything really works.

import paramiko
import sys
import config

username = sys.argv[1] # First command after your script
password = sys.argv[2] # Second command after your script

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('10.48.2.15', port=22, username=username, password=password)
stdin, stdout, stderr = client.exec_command("acli vm.on cbd69a63-c0a1-404c-969a-9816e085372f && acli vm.on b637a621-6cf2-442c-a513-5caeb108e96f && acli vm.on 28d4c186-c05c-4709-814f-eb635b4f269d")
lines = stdout.read()

print(lines)
client.close()

The script is pretty standard from what you’ll get on Paramiko’s documentation site. There are two points that I want to highlight for your future uses:

1. In the documentation, you’d need to store your username and password in plain text on the same script. This works, but it is a HUUUGE no no as it is a security issue. You can store your passwords in a separate file, but I decided to import sys so I can use the sys.argv[] feature to input the username and password on the command line instead.

2. client.exec_command(“”) is where you’ll need to enter your Nutanix acli commands. Since I want to power on three VMs at the same time, I added “&&” in between each command so they will all be run at the same time. The commands being acli vm.off <Image_UUID>

When you’re done making your edits, go ahead and run the script from your command line.

python PowerOnVM.py admin password

After the python script, there is the username (admin) and password (password) that you need to enter to log into the CVM and execute the acli commands.

And that’s it! Simple way to execute terminal commands on a remote host.

Playing around with Kubernetes and Nutanix Karbon

Nutanix’s Karbon automates the deployment and scaling of Kubernetes infrastructure on the Nutanix platform. If you’ve ever setup Kubernetes manually, you know from experience that the setup itself is time consuming and cumbersome. As much as Kubernetes makes orchestration of all containers easier, the setup process is entirely complex and can be prone to errors.

So if you want to play around with Karbon, but don’t quite know where to start, here you go!

Assuming you haven’t installed Kubernetes, follow this link to the installation process from kubernetes.io.

When you’ve verified that kubectl is installed, be sure to check out the video setup from Nutanix on creating a cluster.

I took some time to create a Github repo with instructions on how to deploy a dashboard gui, and a cluster using both a YAML file and regular commands.

https://github.com/mathurin186/NutanixKarbon

To deploy a fun dashboard in Karbon, open up your terminal application (MacOS/Linux) and run the command below:

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml

Followed by:

$ kubectl proxy

The commands enables the dashboard to begin to run for you to access and get a user friendly look at managing your cluster. From here, all you need to do is open a browser and past the following link into it:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

When you want to deploy you an application, you can do it two ways:
1. Through commands
2. Through YAML files
You can do both, which I’ll show you. However, if you want a quick deployment, then I’d recommend that you stick with deploying via YAML files. Scaling your cluster and deploying applications via command line is great to learn, but if you want something working and live then go with YAML.

$ kubectl create deployment --image nginx my-nginx
$ kubectl get pods
$ kubectl get deployment
$ kubectl scale deployment --replicas 2 my-nginx
$ kubectl get pods
$ kubectl expose deployment my-nginx --port=80  type=NodePort
$ kubectl get services

Deploying your cluster via YAML is just as simple. What you need to do is create a directory where you will house your YAML files. Then change to your new directory and clone the github repo posted earlier.

$ mkdir Karbon
$ cd Karbon/
$ git clone https://github.com/mathurin186/NutanixKarbon.git

You should be seeing this:

The last two commands will deploy the YAML files to create a Kubernetes cluster on Nutanix Karbon. Running “get pods” verifies that the deployment was a success.

$ kubectl apply -k .
$ kubectl get pods

From this post, you should know how to deploy a Karbon cluster, and provision applications on demand. This will give you the necessary experience to see how easy it is to run Kubernetes in Nutanix.

Enjoy!