vi hpa-dp.yaml 명세서 작성

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ex-dp
  labels:
    hpa: test
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources:
           requests:
              cpu: 200m
        ports:
        - containerPort: 80
          protocol: TCP

 

Deployment 생성 및 확인

root@master1:~/hpa# kubectl create -f hpa-dp.yaml 
deployment.apps/ex-dp created

root@master1:~/hpa# kubectl autoscale deployment ex-dp --cpu-percent=50 --min=1 --max=5
horizontalpodautoscaler.autoscaling/ex-dp autoscaled

root@master1:~/hpa# kubectl get hpa
NAME    REFERENCE          TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
ex-dp   Deployment/ex-dp   <unknown>/50%   1         5         0          12s

root@master1:~/hpa# kubectl get hpa ex-dp -o yaml
apiVersion: autoscaling/v1
	kind: HorizontalPodAutoscaler
	metadata:
	  annotations:
		autoscaling.alpha.kubernetes.io/conditions: '[{"type":"AbleToScale","status":"True","lastTransitionTime":"2020-12-04T06:35:03Z","reason":"SucceededGetScale","message":"the
		  HPA controller was able to get the target''s current scale"},{"type":"ScalingActive","status":"False","lastTransitionTime":"2020-12-04T06:35:03Z","reason":"FailedGetResourceMetric","message":"the
		  HPA was unable to compute the replica count: unable to get metrics for resource
		  cpu: no metrics returned from resource metrics API"}]'
	  creationTimestamp: "2020-12-04T06:34:48Z"
	  managedFields:
	  - apiVersion: autoscaling/v1
		fieldsType: FieldsV1
		fieldsV1:
		  f:spec:
			f:maxReplicas: {}
			f:minReplicas: {}
			f:scaleTargetRef:
			  f:apiVersion: {}
			  f:kind: {}
			  f:name: {}
			f:targetCPUUtilizationPercentage: {}
		manager: kubectl-autoscale
		operation: Update
		time: "2020-12-04T06:34:48Z"
	  - apiVersion: autoscaling/v1
		fieldsType: FieldsV1
		fieldsV1:
		  f:metadata:
			f:annotations:
			  .: {}
			  f:autoscaling.alpha.kubernetes.io/conditions: {}
		  f:status:
			f:currentReplicas: {}
		manager: kube-controller-manager
		operation: Update
		time: "2020-12-04T06:35:03Z"
	  name: ex-dp
	  namespace: default
	  resourceVersion: "454553"
	  selfLink: /apis/autoscaling/v1/namespaces/default/horizontalpodautoscalers/ex-dp
	  uid: db7c37d2-1a31-46b4-bf5f-032db6ec26d3
	spec:
	  maxReplicas: 5
	  minReplicas: 1
	  scaleTargetRef:
		apiVersion: apps/v1
		kind: Deployment
		name: ex-dp
	  targetCPUUtilizationPercentage: 50
	status:
	  currentReplicas: 2
	  desiredReplicas: 0
      
root@master1:~/hpa# kubectl describe hpa ex-dp
Name:                                                  ex-dp
	Namespace:                                             default
	Labels:                                                <none>
	Annotations:                                           <none>
	CreationTimestamp:                                     Fri, 04 Dec 2020 06:34:48 +0000
	Reference:                                             Deployment/ex-dp
	Metrics:                                               ( current / target )
	  resource cpu on pods  (as a percentage of request):  <unknown> / 50%
	Min replicas:                                          1
	Max replicas:                                          5
	Deployment pods:                                       2 current / 0 desired
	Conditions:
	  Type           Status  Reason                   Message
	  ----           ------  ------                   -------
	  AbleToScale    True    SucceededGetScale        the HPA controller was able to get the target's current scale
	  ScalingActive  False   FailedGetResourceMetric  the HPA was unable to compute the replica count: did not receive metrics for any ready pods
	Events:
	  Type     Reason                        Age                From                       Message
	  ----     ------                        ----               ----                       -------
	  Warning  FailedGetResourceMetric       28s (x2 over 43s)  horizontal-pod-autoscaler  unable to get metrics for resource cpu: no metrics returned from resource metrics API
	  Warning  FailedComputeMetricsReplicas  28s (x2 over 43s)  horizontal-pod-autoscaler  invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: unable to get metrics for resource cpu: no metrics returned from resource metrics API
	  Warning  FailedGetResourceMetric       12s                horizontal-pod-autoscaler  did not receive metrics for any ready pods
	  Warning  FailedComputeMetricsReplicas  12s                horizontal-pod-autoscaler  invalid metrics (1 invalid out of 1), first error is: failed to get cpu utilization: did not receive metrics for any ready pods

 

창을 더 띄워서 지속적으로 모니터링한다.

# kubectl get hpa -w

 

부하주기위해 스크립트 작성한다.

vi  cpuhog 
#!/bin/bash

while :
do
x=1
done
ubuntu@master1:~$ chmod +x cpuhog 
ubuntu@master1:~$ kubectl cp cpuhog ex-dp-{tab}:/bin #@ /bin 밑에 복사
ubuntu@master1:~$ kubectl exec ex-dp-{tab} -- /bin/cpuhog

부하 종료 후 스케일-인이 이루어 지는지 확인한다.

#@ replicas를 활용하여 
root@master1:~# kubectl get hpa -w
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
ex-dp   Deployment/ex-dp   0%/50%    1         5         2          95s

ex-dp   Deployment/ex-dp   41%/50%   1         5         2          4m35s
ex-dp   Deployment/ex-dp   249%/50%   1         5         2          5m36s
ex-dp   Deployment/ex-dp   249%/50%   1         5         4          5m51s
ex-dp   Deployment/ex-dp   249%/50%   1         5         5          6m6s

#@ POD가 늘어난 것을 확인할 수 있다.
NAME                                 READY   STATUS    RESTARTS   AGE
pod/ex-dp-d67954d46-5qlxj            1/1     Running   0          93s
pod/ex-dp-d67954d46-ghmhk            1/1     Running   0          78s
pod/ex-dp-d67954d46-jn77k            1/1     Running   0          7m22s
pod/ex-dp-d67954d46-mkpt7            1/1     Running   0          93s
pod/ex-dp-d67954d46-rrgn4            1/1     Running   0          7m22s

'클라우드 컴퓨팅 & NoSQL > k8s' 카테고리의 다른 글

k8s 개념 확인  (0) 2020.12.06
RBAC  (0) 2020.12.06
AutoScaling  (0) 2020.12.06
monitoring 구축  (0) 2020.12.06
Monitoring  (0) 2020.12.06

+ Recent posts