Cloud-init
Cloud-init scripts let you optionally automate the initialization of virtual machines.
Setting a cloud-init script
To set a cloud-init script, click on the "Click for more advanced options" green link when deploying your virtual machine.
This is the default cloudinit configuration we pass to the hypervisor:
The script that you provide simply gets appended to the end of this file.
Examples
Example 1: Preventing the automatic update of NVIDIA drivers
NVIDIA drivers autoupdate on the first boot. Once this happens, they become unusable. By including this cloud-init script, you'll lock the NVIDIA driver versions so that they do not autoupdate
Objectives
Write a file using cloud-init
write_files
in base64 encoding that usesapt-mark
to hold NVIDIA packages from being automatically updatedThe file will have the following contents:
dpkg-query -W --showformat='${Package} ${Status}\n' | grep -v deinstall | awk '{ print $1 }' | grep -E 'nvidia.*-[0-9]+$' | xargs -r -L 1 apt-mark hold
Run a file
Process
We'll set this as our cloud-init script:
Debrief
Explanation:
First, our server will write the new file,
prevent_update.sh
, in the user's newtensordock_scripts
directoryThen, our server run this file, locking in our NVIDIA driver versions to prevent autoupdates
Example 2: Hosting a website with Apache2
Objectives
Install a package through cloud-init
packages
Write a file using cloud-init
write_files
in plaintext
Process
By default, the Apache2 webserver listens on port 80. As such, we'll first forward an external port into that internal port.
Then, we'll set this as our cloud-init script:
Note the indentation of the custom HTML we've defined above! When writing files, we must append four (4) spaces in front of each text block.
Debrief
Explanation:
First, our server will install the
apache2
packageThen, our server will overwrite the we create an index.html file with the custom HTML we've defined
Now, let's try accessing our web server!
Example 3: Hosting a simple Docker container
Objectives
Host a simple Docker container through the Docker CLI
TensorDock operating system templates come preinstalled with Docker
Process
For networking, we forward an external port into an internal port in the virtual machine. Then, a Docker container forwards a virtual machine's port into its own internal network.
By default, the NGINX webserver listens on port 80 within the Docker container, but we can have the Docker container listen on any port. As such, we'll first forward an external port (e.g. 20018) into itself.
Then, we'll set this as our cloud-init script:
Note that we have our Docker container listen on port 20018 because we forwarded external port 20018 into the virtual machine on port 20018
Debrief
Explanation:
First, our server will write a file to a new directory containing our custom HTML
Then, our server will run a Docker container that passes through the directory of our HTML page into the NGINX contiainer
When users access the external port 20018, our hypervisor forwards that to the virtual machine's port 20018, and then Docker forwards that to the NGINX container on port 80
Now, let's try accessing our web server!
Example 4: Running Valdi hosting software on TensorDock
Valdi.ai is an excellent managed machine learning platform that you can provide compute power to.
Objectives
Run persistent Valdi.ai hosting workloads on TensorDock, automated through a cloudinit-script
Process
First, copy the following code block and replace the YOUR_EMAIL_HERE
, YOUR_USERNAME_HERE
, and YOUR_PASSWORD_HERE
entries with your Valdi.ai
This is the script that we'll have our virtual machine run on boot.
For cloud-init to properly run this script, we'll convert it into a base64 format. You can use a service like https://www.base64encode.org/ to encode it.
You should see an output similar to:
Then, include that in line 15 of the script below:
Your final result should be something like:
Debrief
Explanation:
First, we'll prevent NVIDIA's default automatic driver updates (see Example 1: Preventing the automatic update of NVIDIA drivers)
Then, we create a file that defines a service to run Valdi's hosting binary
Next, we create a script that installs the Valdi software and configures systemd to run the binaries on boot as a service.
Last updated