Tuesday, July 23, 2019

UBUNTU18+KVM+VAGRANT+K8S 16)AUTOSCALE-HORIZONTAL -CPU BASED-HPA-JMETER


This post shows how to autoscale your pods based on cpu metrics.
Hope you get some help!.


I used to below jmeter site to stress wordpress pod(two) to increase.
https://jmeter.apache.org/usermanual/build-web-test-plan.html


1. Two wordpress pod.

 oyj@Workstation-oyj-X555QG ~/apache-jmeter-5.1.1/bin$kubectl get po | grep wordpress
wordpress-6c7f4d4874-bd257                1/1     Running   1          2d21h
wordpress-6c7f4d4874-kxj5s                1/1     Running   1          2d21h
oyj@Workstation-oyj-X555QG ~/apache-jmeter-5.1.1/bin$

2.metrics to autoscale.(https://wnapdlf.blogspot.com/2019/07/ubuntu18kvmvagrantkubernetes-10metrics.html)
oyj@Workstation-oyj-X555QG ~/apache-jmeter-5.1.1/bin$kb get po -n kube-system  | grep metrics
metrics-server-75cb7fd5d7-tq7l8      1/1     Running   14         10d


3.wordpress deploy yaml (resource is important,it resource no exists, then no autoscale is possible)
oyj@Workstation-oyj-X555QG ~/u18kvk8s/k8s/wordpress$cat wp-dp.yaml
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        resources:
          requests:
            cpu: 100m
        env:
        - name: WORDPRESS_DB_HOST
          #Previsous mariadb-master svc, I already created.!
          value: mariadb-master
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mariadb-pass
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: storage-claim-for-wp


#https://wnapdlf.blogspot.com/2019/07/ubuntu18kvmvagrantkubernetes-8.html


4. Create autoscale on wordpress deployment.
oyj@Workstation-oyj-X555QG ~/u18kvk8s/k8s/wordpress$kb autoscale deployment wordpress --cpu-percent=50 --min=2 --max=5
horizontalpodautoscaler.autoscaling/wordpress autoscaled
oyj@Workstation-oyj-X555QG ~/u18kvk8s/k8s/wordpress$kb get hpa
NAME        REFERENCE              TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
wordpress   Deployment/wordpress   <unknown>/50%   2         5         0          4s

oyj@Workstation-oyj-X555QG ~/u18kvk8s/k8s/wordpress$kb describe hpa
Name:                                                  wordpress
Namespace:                                             default
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Wed, 24 Jul 2019 04:55:27 +0900
Reference:                                             Deployment/wordpress
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  1% (1m) / 50%
Min replicas:                                          2
Max replicas:                                          5
Deployment pods:                                       2 current / 2 desired
Conditions:
  Type            Status  Reason               Message
  ----            ------  ------               -------
  AbleToScale     True    ScaleDownStabilized  recent recommendations were higher than current one, applying the highest recent recommendation
  ScalingActive   True    ValidMetricFound     the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
  ScalingLimited  False   DesiredWithinRange   the desired count is within the acceptable range
Events:           <none>
oyj@Workstation-oyj-X555QG ~/u18kvk8s/k8s/wordpress$kb get hpa
NAME        REFERENCE              TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
wordpress   Deployment/wordpress   1%/50%    2         5         2          27s


5. jmeter setting



oyj@Workstation-oyj-X555QG ~/u18kvk8s/k8s/wordpress$watch kubectl get hpa

oyj@Workstation-oyj-X555QG ~/u18kvk8s/k8s/wordpress$kubectl get po -l app=wordpress






 Let's stress like below clip.



As a result pod and cpu usage is increasing.



As cpu usage goes down, then pods will be come back to original value.(2).


According to "https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/"

"Roughly speaking, HPA will increase and decrease the number of replicas (via the deployment) to maintain an average CPU utilization across all Pods of 50% (since each pod requests 200 milli-cores by kubectl run, this means average CPU usage of 100 milli-cores"



Thanks for reading.



No comments:

Post a Comment