Skip to main content

Bash

An example of deploying and managing Virtual Servers using programmatic access to the Kubernetes REST API with Bash.

Overview

Example script

Follow along by pulling the example script from CoreWeave's GitHub.

In this example, the curl and jq tools are used to perform the following actions via the Kubernetes REST API using Bash:

  1. Create a CoreWeave Virtual Server, which:
    1. Runs Ubuntu 20.04
    2. Has 2 CPUs
    3. Has 2Gi of memory
  2. List the created Virtual Server.
  3. Delete the created Virtual Server.

Using curl and jq to deploy a Virtual Server

This script makes calls to two different API endpoints separately:

The Kubevirt endpoint

Kubevirt is an open-source project that allows running virtual systems on the Kubernetes cluster. The script defines and leverages the following functions to call to the Kubevirt API:

VM

  • start_vm() - Starts a Virtual Machine and creates a Virtual Machine Instance (VMI)
  • stop_vm() - Stops the Virtual Machine, then deletes the Virtual Machine Instance (VMI)
  • list_vm() - Lists all the Virtual Machines (VMs) in the namespace
  • get_vm() - Prints formatted JSON details about the Virtual Machine (requires jq)

VMI

  • list_vmi() - Lists all the Virtual Machine Instances (VMIs) in the designated namespace
  • get_vmi() - Prints formatted JSON details about the Virtual Machine Instance (VMI)

The Virtual Server endpoint

The Virtual Server endpoint is a Kubernetes Custom Resource that allows deploying a virtual system and interacting with Kubevirt with ease.

Functions calling the Virtual Server API

The Bash script defines and leverages the following functions to call the Virtual Server API:

  • create_vs() - Creates a Virtual Server (VS)
  • delete_vs() - Deletes a Virtual Server (VS)
  • list_vs() - Lists of all the Virtual Servers (VS) in the designated namespace
  • get_vs() - Prints formatted JSON details about the Virtual Server (requires jq)
  • wait_until_vs_status() - Loops until the expected condition of the Virtual Server (VS) is met

Installation

Before invoking the script, the jq and curl commands must be installed and available from the PATH.

Environment variables

In invoking this script, TOKEN and NAMESPACE are exported as environment variables. The value of NAMESPACE should be set to the desired namespace. The value of TOKEN should be replaced with the value of 'token:' generated in the kubeconfig file.

Run

The script is invoked using the following command and environment variable exports:

TOKEN=<token> NAMESPACE=<namespace> ./run.sh
Additional Resources

The latest resource details, such as statuses and conditions, are available in the Virtual Servers reference API.

The general description of the Kubernetes RESTful API is available in the official documentation of the Kubernetes API Overview.

The basic concepts of the API are described in the official documentation of the Kubernetes API Concepts.

The Kubevirt Python client, kubevirt-py, can list all of the Kubevirt RESTful API endpoints for both VMs and VMIs.