AutoScaler - HPA
HPA, VPA, CA
1. Metrics Server 설치
1-1) Metrics Server 다운 및 설치
kubectl apply -f https://raw.githubusercontent.com/k8s-1pro/install/main/ground/k8s-1.27/metrics-server-0.6.3/metrics-server.yaml
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.6/components.yaml
Metrics Deployment 수정
# 추가 작업 없음
# args 에 `kubelet-insecure-tls` 추가
kubectl edit deployment metrics-server -n kube-system
#spec:
# containers:
# - args:
# - --cert-dir=/tmp
# - --secure-port=443
# - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
# - --kubelet-use-node-status-port
# - --metric-resolution=15s
# - --kubelet-insecure-tls <-- 추가
# args 에 `kubelet-insecure-tls` 와 `kubelet-preferred-address-types=InternalIP` 추가
kubectl edit deployment metrics-server -n kube-system
#spec:
# containers:
# - args:
# - --cert-dir=/tmp
# - --secure-port=4443
# - --kubelet-insecure-tls <-- 추가
# - --kubelet-preferred-address-types=InternalIP <-- 추가
설치 확인 (True값 확인)
kubectl get apiservices |egrep metrics
------------------------
v1beta1.metrics.k8s.io kube-system/metrics-server True 28m
------------------------
메트릭 값 확인 (1~2분 후)
kubectl top node
------------------------
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
k8s-master 485m 9% 4852Mi 32%
k8s-node1 413m 8% 4929Mi 33%
k8s-node2 554m 11% 4672Mi 31%
------------------------
2. HPA (Horizontal Pod Autoscaler)
2-1) Target Deployment (CPU) / Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: stateless-cpu1
spec:
selector:
matchLabels:
resource: cpu
replicas: 2
template:
metadata:
labels:
resource: cpu
spec:
containers:
- name: container
image: kubetm/app:v1
resources:
requests:
cpu: 10m
limits:
cpu: 20m
---
apiVersion: v1
kind: Service
metadata:
name: stateless-svc1
spec:
selector:
resource: cpu
ports:
- port: 8080
targetPort: 8080
nodePort: 30001
type: NodePort
2-2) HPA - Resource (Utilization)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-resource-cpu
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: stateless-cpu1
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
kubectl get hpa -w
while true;do curl 192.168.56.30:30001/hostname; sleep 0.01; done
while true;do curl 192.168.0.30:30001/hostname; sleep 0.01; done
kubectl delete horizontalpodautoscalers.autoscaling hpa-resource-cpu
2-3) Target Deployment (Memory) / Service
apiVersion: apps/v1
kind: Deployment
metadata:
name: stateless-memory1
spec:
selector:
matchLabels:
resource: memory
replicas: 2
template:
metadata:
labels:
resource: memory
spec:
containers:
- name: container
image: kubetm/app:v1
resources:
requests:
memory: 10Mi
limits:
memory: 20Mi
---
apiVersion: v1
kind: Service
metadata:
name: stateless-svc2
spec:
selector:
resource: memory
ports:
- port: 8080
targetPort: 8080
nodePort: 30002
type: NodePort
2-4) HPA - Resource (AverageValue)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-resource-memory
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: stateless-memory1
metrics:
- type: Resource
resource:
name: memory
target:
type: AverageValue
averageValue: 5Mi
while true;do curl 192.168.56.30:30002/hostname; sleep 0.01; done
while true;do curl 192.168.0.30:30002/hostname; sleep 0.01; done
kubectl delete horizontalpodautoscalers.autoscaling hpa-resource-memory
Sample yaml
* HPA for - Pods
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-pods
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: stateless-app1
metrics:
- type: Pods
pods:
metric:
name: packets-per-second
target:
type: AverageValue
averageValue: 10
HPA - Object
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: hpa-object
spec:
maxReplicas: 10
minReplicas: 2
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: stateless-app1
metrics:
- type: Object
object:
metric:
name: requests-per-second
target:
type: Value
value: 10
describedObject:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
name: ingress-hpa
Referenece
Kubernetes
- Horizontal Pod Autoscaler : https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
- Horizontal Pod Autoscaler Walkthrough : https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/
Others
- Best Practices for Cluster Autoscaler, HPA and VPA : https://www.replex.io/blog/kubernetes-in-production-best-practices-for-cluster-autoscaler-hpa-and-vpa
- Cluster Autoscaler, Horizontal Pod Autoscaler, and Vertical Pod Autoscaler : https://levelup.gitconnected.com/kubernetes-autoscaling-101-cluster-autoscaler-horizontal-pod-autoscaler-and-vertical-pod-2a441d9ad231
- How to autoscale apps on Kubernetes with custom metrics : https://learnk8s.io/autoscaling-apps-kubernetes