Add skeleton for autoscaling example and documentation#439
Draft
Calinachoo wants to merge 5 commits into
Draft
Conversation
added 4 commits
July 10, 2026 19:19
There was a problem hiding this comment.
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 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 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 |
| # Deploy infrastructure using the raw Unikraft Cloud REST API | ||
| # Usage: ./api/deploy.sh | ||
|
|
||
| TOKEN="${UKC_TOKEN}" |
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 |
| # Configure autoscale policy using the raw Unikraft Cloud REST API | ||
| # Usage: ./api/scale.sh | ||
|
|
||
| TOKEN="${UKC_TOKEN}" |
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 } | ||
| ] | ||
| } | ||
| ] | ||
| }" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WIP: Adding autoscaling example for CDL project.