Cloud Native 13 min read

How to Run Knative Serverless on Alibaba Cloud Spot Instances for Cost Savings

Learn how to integrate Alibaba Cloud's low‑cost spot (preemptible) instances with Knative serverless workloads, configure pod annotations, handle automatic replacement, monitor eviction events, and apply best‑practice YAML settings to achieve cost‑effective, resilient deployments on a cloud‑native platform.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
How to Run Knative Serverless on Alibaba Cloud Spot Instances for Cost Savings

Background

Knative is an open‑source, cloud‑native, cross‑platform Serverless orchestration framework built on Kubernetes. Spot (preemptible) instances are low‑cost, bid‑based compute resources that can be reclaimed when the market price exceeds the bid or inventory runs out. Combining Knative with spot instances can further reduce resource costs.

Knative and Spot Instance Integration

Knative Service provides automatic request‑driven scaling, including scaling to zero. By adding specific pod annotations, Knative can schedule workloads onto spot instances via the Virtual Node, which automatically requests the appropriate Elastic Container Instance (ECI) specifications.

Advantages of Using Spot Instances with Knative

Serverless scenarios: Short‑lived web requests that can be spun up on demand and released immediately after use.

Graceful termination support: Virtual Node replaces pods by first deleting the pod, then the workload controller creates a new spot instance. Knative adds a queue‑proxy sidecar that waits for in‑flight requests before the business container is terminated.

Cost sensitivity: Users of Knative are often cost‑conscious, making spot instances an attractive option.

Handling Spot Instance Reclamation

When a spot instance is about to be reclaimed (price exceeds bid or insufficient inventory), the ECI service emits a SpotToBeReleased event and sets the pod condition ContainerInstanceExpired to true. Applications can use these signals to perform graceful shutdown and pod rotation.

Spot Instance Eviction Notification

The eviction event is sent three minutes before the instance is reclaimed. The pod’s Conditions and Events fields reflect this, for example:

Events:
  Type    Reason               Age    From          Message
  ----    ------               ----   ----          -------
  Warning SpotToBeReleased    3m32s  kubelet, eci  Spot ECI will be released in 3 minutes

Graceful Eviction Process

Virtual Node receives SpotToBeReleased and calls the Eviction API.

The API server checks the associated PodDisruptionBudget.

If allowed, the pod is marked for termination; the kubelet performs graceful shutdown respecting terminationGracePeriodSeconds and PodDisruptionBudget constraints.

After the grace period, the kubelet force‑terminates the pod and informs the API server, which finally deletes the pod resource.

Knative’s queue‑proxy sidecar ensures in‑flight requests finish before the business container is removed.

Configuration Annotations

To create spot instances in a Knative Service, add the following annotations to the pod metadata:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
spec:
  template:
    metadata:
      labels:
        alibabacloud.com/eci: "true"
      annotations:
        k8s.aliyun.com/eci-use-specs: "ecs.c6.large"               # ECS instance type
        k8s.aliyun.com/eci-spot-strategy: "SpotAsPriceGo"          # Automatic market‑price bidding
        k8s.aliyun.com/eci-spot-duration: "1"                     # 1‑hour protection period
        k8s.aliyun.com/eci-spot-fallback: "true"                  # Fallback to pay‑as‑you‑go if no spot capacity
    spec:
      containers:
      - env:
        - name: TARGET
          value: "Knative"
        image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56

Key Annotation Details

k8s.aliyun.com/eci-spot-strategy

: SpotAsPriceGo lets the system bid automatically at the current market price. k8s.aliyun.com/eci-spot-duration: Sets the protection period (in hours) during which the spot instance will not be reclaimed. k8s.aliyun.com/eci-spot-fallback: When set to true, the pod will fall back to a standard pay‑as‑you‑go instance if spot capacity is unavailable.

Example Configurations

Example 1 – Spot with Price Limit

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
spec:
  template:
    metadata:
      labels:
        alibabacloud.com/eci: "true"
      annotations:
        k8s.aliyun.com/eci-use-specs: "ecs.c6.large"
        k8s.aliyun.com/eci-spot-strategy: "SpotWithPriceLimit"
        k8s.aliyun.com/eci-spot-price-limit: "0.25"
    spec:
      containers:
      - env:
        - name: TARGET
          value: "Knative"
        image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56

This creates a spot instance of type ecs.c6.large with a maximum hourly price of 0.25 CNY. If the market price exceeds the limit or inventory is insufficient, creation fails.

Example 2 – Fallback to Pay‑As‑You‑Go

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
spec:
  template:
    metadata:
      labels:
        alibabacloud.com/eci: "true"
      annotations:
        k8s.aliyun.com/eci-use-specs: "ecs.c6.large"
        k8s.aliyun.com/eci-spot-strategy: "SpotWithPriceLimit"
        k8s.aliyun.com/eci-spot-price-limit: "0.05"
        k8s.aliyun.com/eci-spot-fallback: "true"
    spec:
      containers:
      - env:
        - name: TARGET
          value: "Knative"
        image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56

If spot capacity is unavailable, the pod automatically falls back to a standard pay‑as‑you‑go instance, which will not be reclaimed automatically.

Best Practices

Use the automatic market‑price bidding strategy ( SpotAsPriceGo) to follow real‑time prices.

Set a protection period (e.g., 1 hour) to guarantee stable operation before possible reclamation.

Enable fallback to pay‑as‑you‑go instances to ensure service continuity when spot capacity is exhausted.

Conclusion

Knative combined with Alibaba Cloud’s Virtual Node now supports automatic replacement of spot instances, providing a cost‑effective yet resilient serverless deployment model. By configuring the appropriate pod annotations and following the outlined best practices, users can minimize expenses while maintaining high availability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ServerlessKubernetesAlibaba CloudKnativespot instances
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.