This documentation provides a step-by-step guide to scale applications in production using the Timoni project. It delivers concise code examples and insights for expert developers, focusing on utilizing Golang, Cue, Makefile, and shell scripting.
Understanding Production Scaling Concepts
In Kubernetes, scaling applications involves managing the number of active pod replicas based on resource utilization metrics, such as CPU and memory. Timoni employs these scaling metrics by defining configurations in CUE.
Configuration of Replica Scaling
To effectively manage scaling, the minReplicas
and maxReplicas
properties must be set accordingly.
#HorizontalPodAutoscaler: {
minReplicas?: int32 @go(MinReplicas,*int32) @protobuf(2,varint,opt)
maxReplicas: int32 @go(MaxReplicas) @protobuf(3,varint,opt)
}
Key Elements:
minReplicas: The minimum number of replicas the autoscaler can scale down to. Defaults to 1.
maxReplicas: Specifies the upper limit for replicas the autoscaler can scale up to, which must always be greater than or equal to
minReplicas
.
Defining Metrics for Scaling
Metrics play a crucial role in autoscaling. Various metrics can be defined to monitor resource utilization and influence scaling decisions. Here’s how one might set metrics for CPU utilization:
metrics: [...#MetricSpec] @go(Metrics,[]MetricSpec) @protobuf(4,bytes,rep)
Pod and Resource Metrics
When defining your scaling strategy, you could use the following structures to define metrics based on both pods and resources:
#ResourceMetricSource: {
name: v1.#ResourceName @go(Name) @protobuf(1,bytes)
target: #MetricTarget @go(Target) @protobuf(2,bytes)
}
#PodsMetricSourceType: #MetricSourceType & "Pods"
#ResourceMetricSourceType: #MetricSourceType & "Resource"
This allows Kubernetes to evaluate the average metrics of pods and scale them based on configured thresholds.
Code Example for Deployment Scaling
An example of how to set up the scaling configurations can be illustrated by defining a Bundle
in the bundle.cue
file:
bundle: {
_cluster: "dev" @timoni(runtime:string:TIMONI_CLUSTER_NAME)
_env: "dev" @timoni(runtime:string:TIMONI_CLUSTER_GROUP)
apiVersion: "v1alpha1"
name: "fleet-test"
instances: {
"frontend": {
module: {
url: "oci://ghcr.io/stefanprodan/timoni/minimal"
version: "latest"
}
namespace: "fleet-test"
values: {
message: "Hello from cluster \(_cluster)"
if _env == "production" {
replicas: 3
} else {
replicas: 2
}
}
}
}
}
Setting up the Runtime Environment
Defining the runtime context is crucial to aligning your application instances with specific clusters:
runtime: {
apiVersion: "v1alpha1"
name: "fleet-test"
clusters: {
"production": {
group: "production"
kubeContext: "prod-env"
}
"staging": {
group: "staging"
kubeContext: "staging-env"
}
}
}
In this example, Kubernetes will understand when to scale instances based on the defined environments.
Managing Your Build with Makefile
To facilitate the build process and manage dependencies, you can utilize the following commands in your Makefile:
.PHONY: all
all: tidy install setup-envtest
tidy:
go mod tidy
install-envtest:
go get k8s.io/enhancements
setup-envtest:
@echo "Setting up environment for tests"
Running Tests
Before deploying, it’s also beneficial to run tests to ensure all configurations are correct. This might be achieved with a command such as:
make test
Conclusion
By effectively configuring the HorizontalPodAutoscaler
, defining the necessary metrics, and managing deployments through Bundles, developers can harness Timoni’s capabilities to scale applications seamlessly in the production environment.
This documentation is based on direct references from code and configuration files within the Timoni project as maintained in the GitHub repository: stefanprodan/timoni.