Run your application on spot instance
Run on spot
Kapstan provides the option to utilize spot instances for Kubernetes workloads, offering potential cost savings for applications that can tolerate interruptions. This feature integrates spot instances with your Kubernetes cluster orchestration.
What are Spot Instances?
Spot instances are spare compute capacity offered at a discounted price compared to on-demand instances. This allow you to run your apps on discounted, interruptible cloud provider instances. This can significantly reduce costs for fault-tolerant workloads while leveraging Kubernetes' scheduling and management capabilities
What are the benefits of enabling spot on Kapstan?
- Lower hardware costs: Spot instances can significantly reduce compute expenses, often up to 70-90% compared to on-demand pricing.
- Scalability: Easily scale workloads that can handle interruptions.
- Automated Management: Kubernetes can handle spot instance interruptions by rescheduling pods.
- Resource Optimization: Ideal for batch jobs, stateless applications, stateless applications and dev/test environments.
How to enable Run on Spot in Kapstan?
Before enabling the spot instance toggle, ensure your application can gracefully handle potential interruptions. For more information, refer to the best practices for running applications on spot instances.
- Navigate to the configuration page for your application (e.g., "api-gateway" in the screenshot).
- Select the "Hardware" tab from the left sidebar.
- Under the "Hardware" section, locate the "Run on Spot" toggle switch.
- Enable the toggle to activate spot instance usage for your workload.
- Click the "Save" button to apply the changes and deploy the application.
Best Practices to run applicatons on Spot Instance
Spot Instances offer cost savings by leveraging unused capacity, but their ephemeral nature requires careful application design. Here are best practices to ensure smooth operation when running applications on Spot Instances in a Kubernetes cluster
-
Stateless Applications: Favor stateless applications, which have no local state and can be easily restarted. This simplifies recovery and avoids data loss when instances are interrupted.
-
Idempotent Tasks: Ensure tasks can be executed multiple times without unintended consequences. This enables Kubernetes to seamlessly reschedule tasks without introducing errors.
-
Checkpointing: For long-running tasks, implement checkpointing to save application state periodically using database(persistent storage). This allows the application to resume from the latest checkpoint, minimizing downtime.
-
Graceful Shutdown: Include a shutdown handler in your container that gracefully terminates the application upon receiving a termination signal(SIGTERM). This involves:
- Draining Active Connections: Stop accepting new connections and allow existing connections to complete.
- Flushing Buffers: Flush all data buffers to persistent storage.
- Saving Application State: Save state to persistent data storage for applications that maintain state.
- Cleanup Tasks: Perform necessary cleanup tasks, such as releasing resources.
-
Keep Container Images Small: Optimize container images for size to improve pod launch times and reduce resource consumption. This can involve minimizing dependencies, using multi-stage builds, and compressing image layers. This would help in bringing up pods faster in case of interruptions.