Skip to content

Add skeleton for autoscaling example and documentation#439

Draft
Calinachoo wants to merge 5 commits into
unikraft-cloud:mainfrom
Calinachoo:calin/add-autoscaling-example
Draft

Add skeleton for autoscaling example and documentation#439
Calinachoo wants to merge 5 commits into
unikraft-cloud:mainfrom
Calinachoo:calin/add-autoscaling-example

Conversation

@Calinachoo

Copy link
Copy Markdown

WIP: Adding autoscaling example for CDL project.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an initial autoscaling example under autoscaling/ intended to demonstrate deploying a simple service and configuring an autoscaling policy via both KraftKit CLI and the raw Unikraft Cloud REST API.

Changes:

  • Introduces a minimal Go HTTP app plus Kraftfile/Dockerfile for deployment as a unikernel workload.
  • Adds KraftKit-based helper scripts to create a service group, deploy an instance, and configure CPU-based autoscaling.
  • Adds REST API-based helper scripts to create a service group and configure autoscaling (with token/metro env support).

Reviewed changes

Copilot reviewed 7 out of 10 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
autoscaling/README.md Placeholder documentation file (currently empty).
autoscaling/unikraft/deploy.sh Placeholder script file (currently empty).
autoscaling/unikraft/scale.sh Placeholder script file (currently empty).
autoscaling/kraft/deploy.sh KraftKit CLI deploy script for creating a service group and deploying an instance.
autoscaling/kraft/scale.sh KraftKit CLI script for initializing autoscaling and adding a CPU policy.
autoscaling/app/main.go Minimal HTTP server returning instance hostname for observing scaling behavior.
autoscaling/app/Kraftfile Kraftfile configuration for the autoscaling example app.
autoscaling/app/Dockerfile Multi-stage Docker build for producing the app rootfs/binary.
autoscaling/api/deploy.sh REST API script to create a service group.
autoscaling/api/scale.sh REST API script to configure autoscaling policy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread autoscaling/app/main.go
Comment on lines +1 to +20
package main

import (
"fmt"
"net/http"
"os"
)

// The handler responds with the current instance's hostname.
// Under heavy load, when the autoscaler creates clones, we will see different hostnames here.
func handler(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()
fmt.Fprintf(w, "Hello from KraftCloud! Responding instance: %s\n", hostname)
}

func main() {
http.HandleFunc("/", handler)
fmt.Println("Server starting on port 8080...")
http.ListenAndServe(":8080", nil)
}
Comment thread autoscaling/app/Dockerfile Outdated
Comment on lines +5 to +6
# Unikraft requires a dynamically linked binary (PIE - Position Independent Executable)
RUN CGO_ENABLED=0 GOOS=linux go build -buildmode=pie -a -installsuffix cgo -o /autoscaling-test main.go
Comment on lines +8 to +10
# Stage 2: Final image
# We use debian-slim instead of scratch to provide the dynamic linker (ld-linux.so) required by the PIE binary above
FROM debian:bookworm-slim
Comment on lines +7 to +16
NAME=${1:-my-first-instance}
GROUP=${2:-my-scaling-group}

echo "[+] Creating service group $GROUP (if it doesn't exist)..."
# Create the service group. We use '|| true' so the script doesn't fail if the group already exists.
kraft cloud service create -n $GROUP 443:8080/http+tls || true

echo "[+] Deploying instance $NAME to service group $GROUP..."
# We append ./app at the end to explicitly tell KraftKit where the Kraftfile and Dockerfile are located.
kraft cloud deploy -M 512M -g $GROUP --name $NAME ./app No newline at end of file
Comment on lines +7 to +20
GROUP=${1:-my-scaling-group}
TEMPLATE=${2:-my-first-instance}

echo "[+] Initializing autoscale configuration for service group $GROUP using template $TEMPLATE..."
# The template was already successfully created, so we just ensure the autoscaler is initialized
kraft cloud scale init $GROUP --min-size 0 --max-size 1 --template $TEMPLATE || true

echo "[+] Configuring CPU autoscaling policy..."
kraft cloud scale add $GROUP \
--name my-cpu-policy \
--metric cpu \
--adjustment percent \
--step 600:800/50 \
--step 800:/100 || true No newline at end of file
Comment thread autoscaling/api/deploy.sh
# Deploy infrastructure using the raw Unikraft Cloud REST API
# Usage: ./api/deploy.sh

TOKEN="${UKC_TOKEN}"
Comment thread autoscaling/api/deploy.sh
Comment on lines +16 to +31
echo "[+] Creating service group '$GROUP_NAME' via Unikraft Cloud API..."
curl -s -X POST "${API_URL}/services" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"${GROUP_NAME}\",
\"services\": [
{
\"port\": 443,
\"destination_port\": 8080,
\"handlers\": [\"http\", \"tls\"]
}
]
}"

echo -e "\n[+] Service group deployment request sent successfully." No newline at end of file
Comment thread autoscaling/api/scale.sh
# Configure autoscale policy using the raw Unikraft Cloud REST API
# Usage: ./api/scale.sh

TOKEN="${UKC_TOKEN}"
Comment thread autoscaling/api/scale.sh
Comment on lines +19 to +43
curl -s -X POST "${API_URL}/services/autoscale" \
-H "Authorization: Bearer ${TOKEN}" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"${GROUP_NAME}\",
\"min_size\": 0,
\"max_size\": 1,
\"create_args\": {
\"template\": {
\"name\": \"${TEMPLATE_NAME}\"
}
},
\"policies\": [
{
\"name\": \"my-cpu-policy\",
\"type\": \"step\",
\"metric\": \"cpu\",
\"adjustment_type\": \"percent\",
\"steps\": [
{ \"lower_bound\": 600, \"upper_bound\": 800, \"adjustment\": 50 },
{ \"lower_bound\": 800, \"adjustment\": 100 }
]
}
]
}"
@razvand razvand self-assigned this Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants