Kubernetes Cost Optimization
/ 3 min read
Understanding Kubernetes Costs
Cost optimization in Kubernetes involves managing resource allocation, scaling strategies, and infrastructure choices to maximize efficiency while minimizing expenses.
Resource Optimization
1. Resource Requests and Limits
apiVersion: v1kind: Podmetadata: name: optimized-podspec: containers: - name: app image: nginx resources: requests: memory: "64Mi" cpu: "250m" limits: memory: "128Mi" cpu: "500m"2. Vertical Pod Autoscaling
apiVersion: autoscaling.k8s.io/v1kind: VerticalPodAutoscalermetadata: name: my-app-vpaspec: targetRef: apiVersion: "apps/v1" kind: Deployment name: my-app updatePolicy: updateMode: "Auto" resourcePolicy: containerPolicies: - containerName: '*' minAllowed: cpu: "100m" memory: "50Mi" maxAllowed: cpu: "1" memory: "500Mi"Cluster Optimization
1. Node Pool Management
apiVersion: v1kind: Nodemetadata: name: optimized-node labels: node-size: optimized workload-type: generalspec: taints: - key: workload-type value: general effect: NoSchedule2. Pod Scheduling
apiVersion: v1kind: Podmetadata: name: cost-aware-podspec: nodeSelector: node-size: optimized tolerations: - key: workload-type operator: Equal value: general effect: NoSchedule containers: - name: app image: myapp:1.0Cost Monitoring
1. Prometheus Metrics
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata: name: cost-metricsspec: selector: matchLabels: app: cost-exporter endpoints: - port: metrics interval: 30s namespaceSelector: matchNames: - monitoring2. Cost Allocation
apiVersion: v1kind: Namespacemetadata: name: team-a labels: cost-center: team-a department: engineering---apiVersion: v1kind: ResourceQuotametadata: name: team-a-quota namespace: team-aspec: hard: requests.cpu: "4" requests.memory: "8Gi" pods: "20"Scaling Strategies
1. Horizontal Pod Autoscaling
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: cost-efficient-hpaspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-app minReplicas: 1 maxReplicas: 5 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70 behavior: scaleDown: stabilizationWindowSeconds: 3002. Cluster Autoscaling
apiVersion: cluster.k8s.io/v1alpha1kind: MachineDeploymentmetadata: name: worker-nodesspec: replicas: 3 template: spec: providerSpec: value: instanceType: t3.medium diskSize: 50 taints: - key: workload-type value: general effect: NoScheduleStorage Optimization
1. Storage Classes
apiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: optimized-storageprovisioner: kubernetes.io/aws-ebsparameters: type: gp3 iops: "3000" throughput: "125"reclaimPolicy: DeleteallowVolumeExpansion: true2. Volume Management
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: optimized-pvcspec: accessModes: - ReadWriteOnce storageClassName: optimized-storage resources: requests: storage: 10GiCost Analysis Tools
1. Kubecost Configuration
apiVersion: apps/v1kind: Deploymentmetadata: name: kubecostspec: template: spec: containers: - name: cost-analyzer image: kubecost/cost-analyzer:latest env: - name: PROMETHEUS_SERVER_ENDPOINT value: http://prometheus:9090 - name: CLOUD_PROVIDER_API_KEY valueFrom: secretKeyRef: name: cloud-secret key: api-key2. Resource Reports
apiVersion: cost.k8s.io/v1alpha1kind: Reportmetadata: name: monthly-cost-reportspec: timeframe: start: "2024-12-01" end: "2024-12-31" aggregateBy: - namespace - label:cost-center format: CSVOptimization Policies
1. Resource Quotas
apiVersion: v1kind: ResourceQuotametadata: name: compute-quotaspec: hard: requests.cpu: "4" requests.memory: "8Gi" limits.cpu: "8" limits.memory: "16Gi" pods: "20"2. Limit Ranges
apiVersion: v1kind: LimitRangemetadata: name: default-limitsspec: limits: - default: cpu: "300m" memory: "256Mi" defaultRequest: cpu: "200m" memory: "128Mi" type: ContainerBest Practices
1. Node Affinity
apiVersion: v1kind: Podmetadata: name: cost-optimizedspec: affinity: nodeAffinity: requiredDuringSchedulingIgnoredDuringExecution: nodeSelectorTerms: - matchExpressions: - key: instance-type operator: In values: - spot - preemptible2. Pod Disruption Budget
apiVersion: policy/v1kind: PodDisruptionBudgetmetadata: name: app-pdbspec: minAvailable: 2 selector: matchLabels: app: critical-appMonitoring and Alerts
1. Cost Alerts
apiVersion: monitoring.coreos.com/v1kind: PrometheusRulemetadata: name: cost-alertsspec: groups: - name: cost.rules rules: - alert: HighCostSpike expr: | sum(rate(container_cpu_usage_seconds_total[1h])) by (namespace) > 1000 for: 1h labels: severity: warning annotations: summary: High cost detected in namespace2. Resource Monitoring
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata: name: resource-monitorspec: selector: matchLabels: app: resource-metrics endpoints: - port: metrics interval: 30s namespaceSelector: matchNames: - monitoringConclusion
Cost optimization in Kubernetes requires a comprehensive approach involving proper resource allocation, efficient scaling strategies, and continuous monitoring. By implementing these practices and regularly reviewing costs, organizations can maintain efficient and cost-effective Kubernetes clusters.
Series Navigation
- Previous: Kubernetes Policy Management
- Series Complete! Start from Introduction to Kubernetes