To implement automatic scaling with Kubernetes, you can leverage Horizontal Pod Autoscaler (HPA) and Cluster Autoscaler. These tools automatically adjust the number of pods or nodes based on resource usage metrics like CPU, memory, or custom metrics.
HPA scales the number of pods in a deployment, replica set, or stateful set based on observed CPU utilization or custom metrics.
How it works:
Example:
kubectl autoscale deployment/my-app --cpu-percent=50 --min=1 --max=10
This command scales the my-app deployment between 1 and 10 pods, adjusting based on 50% CPU utilization.
For custom metrics (e.g., requests per second), you can use the Metrics Server and define HPA with custom metrics:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-app
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
- type: Pods
pods:
metric:
name: requests-per-second
target:
type: AverageValue
averageValue: 100
Cluster Autoscaler automatically adjusts the number of nodes in a Kubernetes cluster based on pending pods. If pods cannot be scheduled due to insufficient resources, Cluster Autoscaler adds new nodes. If nodes are underutilized, it removes them.
How it works:
Example (Cloud Provider Integration):
Example Deployment (Simplified):
kubectl apply -f https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/<provider>/examples/cluster-autoscaler-autodiscover.yaml
(Replace <provider> with your cloud provider, e.g., tencentcloud.)
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
For Tencent Cloud users, Tencent Kubernetes Engine (TKE) provides built-in support for HPA and Cluster Autoscaler, along with auto-scaling node pools for seamless scaling.
Example (TKE Auto Scaling Node Pool):
This setup ensures your Kubernetes workloads scale efficiently based on demand while optimizing costs.