HPAの設定
現在、クラスタ ーには水平ポッド自動スケーリングを可能にするリソースはなく、以下のコマンドで確認できます:
~$kubectl get hpa -A
No resources found
今回はuiサービスを利用し、CPU使用率に基づいてスケールさせます。まず最初に、uiのPod仕様を更新して、CPU requestとlimitの値を指定します。
- Kustomize Patch
- Deployment/ui
- Diff
~/environment/eks-workshop/modules/autoscaling/workloads/hpa/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ui
spec:
template:
spec:
containers:
- name: ui
resources:
limits:
cpu: 250m
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app.kubernetes.io/created-by: eks-workshop
app.kubernetes.io/type: app
name: ui
namespace: ui
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/component: service
app.kubernetes.io/instance: ui
app.kubernetes.io/name: ui
template:
metadata:
annotations:
prometheus.io/path: /actuator/prometheus
prometheus.io/port: "8080"
prometheus.io/scrape: "true"
labels:
app.kubernetes.io/component: service
app.kubernetes.io/created-by: eks-workshop
app.kubernetes.io/instance: ui
app.kubernetes.io/name: ui
spec:
containers:
- env:
- name: JAVA_OPTS
value: -XX:MaxRAMPercentage=75.0 -Djava.security.egd=file:/dev/urandom
- name: METADATA_KUBERNETES_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: METADATA_KUBERNETES_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: METADATA_KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
envFrom:
- configMapRef:
name: ui
image: public.ecr.aws/aws-containers/retail-store-sample-ui:1.2.1
imagePullPolicy: IfNotPresent
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 45
periodSeconds: 20
name: ui
ports:
- containerPort: 8080
name: http
protocol: TCP
resources:
limits:
cpu: 250m
memory: 1.5Gi
requests:
cpu: 250m
memory: 1.5Gi
securityContext:
capabilities:
add:
- NET_BIND_SERVICE
drop:
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- mountPath: /tmp
name: tmp-volume
securityContext:
fsGroup: 1000
serviceAccountName: ui
volumes:
- emptyDir:
medium: Memory
name: tmp-volume
name: http
protocol: TCP
resources:
limits:
+ cpu: 250m
memory: 1.5Gi
requests:
cpu: 250m
memory: 1.5Gi
次に、HPAがワークロードをスケールする方法を決定するためのパラメータを定義するHorizontalPodAutoscalerリソースを作成する必要があります。
~/environment/eks-workshop/modules/autoscaling/workloads/hpa/hpa.yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: ui
namespace: ui
spec:
minReplicas: 1
maxReplicas: 4
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: ui
targetCPUUtilizationPercentage: 80
A
常に少なくとも1つのレプリカを実行する
B
4つ以上のレプリカにはスケールしない
C
HPAにui Deploymentのレプリカ数を変更するよう指示する
D
CPU使用率のターゲットを80%に設定する
この設定を適用しましょう:
~$kubectl apply -k ~/environment/eks-workshop/modules/autoscaling/workloads/hpa