For a quick test of horizontal autoscaling of an NginX deployment, we need the following ingredients:

  • a metrics server installation to be able to measure the POD’s CPU
  • an nginx deployment with a CPU reservation
  • an auto scaler configuration
  • a process that causes high CPU

This can be tested on: https://killercoda.com/playgrounds/scenario/kubernetes

Install Metrics (quick&insecure way)

curl -s -L https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml | sed 's_\(--cert-dir=/tmp\)_\1\n\ \ \ \ \ \ \ \ - --kubelet-insecure-tls_g' | k apply -f -

Create an NginX Deployment…

…with CPU resource request of 0.1

k create deploy nginx --image=nginx --dry-run=client -o yaml | sed 's_\(resources: \){}_resources:\n\ \ \ \ \ \ \ \ \ \ requests:\n\ \ \ \ \ \ \ \ \ \ \ \ cpu: 0.1_' > nginx.deploy.yaml;
k apply -f nginx.deploy.yaml

Create a horizontal Autoscaler

kubectl autoscale deployment nginx --cpu-percent=50 --min=1 --max=4

Run a high CPU Process

k exec -it deploy/nginx -- bash -c 'while true; do echo -n . >/dev/null; done'

Scaling up Result:

k get hpa --watch
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   0%/50%    1         4         1          51s
nginx   Deployment/nginx   721%/50%   1         4         1          60s
nginx   Deployment/nginx   942%/50%   1         4         4          75s
nginx   Deployment/nginx   228%/50%   1         4         4          90s

Scaling down Result

After stopping the while loop, we see:

k get hpa --watch
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   0%/50%    1         4         4          6m47s
nginx   Deployment/nginx   0%/50%    1         4         4          8m15s
nginx   Deployment/nginx   0%/50%    1         4         1          8m30s

Appendix: A. 1: Next Generation (v2) Autoscaling with Default Behavior

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: null
  name: nginx
spec:
  maxReplicas: 4
  minReplicas: 1
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
      - type: Pods
        value: 4
        periodSeconds: 15
      selectPolicy: Max
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx

Appendix: A. 2: Next Generation (v2) Autoscaling with Custom Behavior

The target here is:

  • scale down quickly, if there is no high CPU anymore (stabilizationWindowSeconds set to 0)
  • scale up one by one every 60 seconds: Pods value 1 and periodSeconds 60
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  creationTimestamp: null
  name: nginx
spec:
  maxReplicas: 4
  minReplicas: 1
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 0
      policies:
      - type: Percent
        value: 100
        periodSeconds: 15
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
      - type: Pods
        value: 1
        periodSeconds: 60
      selectPolicy: Max
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx

we can see that the number of replicas rises one by one from 1 to 4, while the load is high, and the number of replicas goes down quickly if the CPU is below its threshold:

controlplane $ k get hpa --watch
NAME    REFERENCE          TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   0%/80%    1         4         1          32m
nginx   Deployment/nginx   517%/80%   1         4         1          32m
nginx   Deployment/nginx   816%/80%   1         4         2          33m
nginx   Deployment/nginx   414%/80%   1         4         2          33m
nginx   Deployment/nginx   428%/80%   1         4         2          33m
nginx   Deployment/nginx   428%/80%   1         4         2          34m
nginx   Deployment/nginx   419%/80%   1         4         3          34m
nginx   Deployment/nginx   274%/80%   1         4         3          34m
nginx   Deployment/nginx   275%/80%   1         4         3          35m
nginx   Deployment/nginx   279%/80%   1         4         4          35m
nginx   Deployment/nginx   236%/80%   1         4         4          35m
nginx   Deployment/nginx   214%/80%   1         4         4          35m
nginx   Deployment/nginx   204%/80%   1         4         4          36m
nginx   Deployment/nginx   134%/80%   1         4         4          36m
nginx   Deployment/nginx   0%/80%     1         4         4          36m
nginx   Deployment/nginx   0%/80%     1         4         1          36m

More Information:

Next Generation Autoscaling with Behavior Control

 

25 comments

  1. Hello i think that i saw you visited my weblog so i came to Return the favore Im trying to find things to improve my web siteI suppose its ok to use some of your ideas

  2. The unique set of Professional-Data-Engineer dumps is the easiest and the most rewarding content, you ever found on any web page. Your success is guaranteed! The questions and answers format of our dumps is rich with information and provides you also Google Professional Data Engineer Exam latest lab help, enhancing your exam skills. The content is approved by the most distinguished professionals and revised and updated by our experts on regular basis. With these brilliant features it is rated as the most worthwhile, informative and highly exam relevant. In all respects, you will find the Professional-Data-Engineer dumps compatible to your actual preparatory needs. The language is simple and the content is engaging and easy. No more, Professional-Data-Engineer exam is a nightmare.

  3. Somebody essentially lend a hand to make significantly posts I might state That is the very first time I frequented your web page and up to now I surprised with the research you made to create this particular put up amazing Excellent job.

  4. Its like you read my mind You appear to know so much about this like you wrote the book in it or something I think that you can do with a few pics to drive the message home a little bit but other than that this is fantastic blog A great read Ill certainly be back.

  5. I just could not depart your web site prior to suggesting that I really loved the usual info an individual supply in your visitors Is gonna be back regularly to check up on new posts

  6. obviously like your website but you need to test the spelling on quite a few of your posts Several of them are rife with spelling problems and I to find it very troublesome to inform the reality on the other hand Ill certainly come back again

  7. Somebody essentially lend a hand to make significantly articles Id state That is the very first time I frequented your website page and up to now I surprised with the research you made to make this actual submit amazing Wonderful task.

  8. I truly enjoyed what you’ve achieved here. The design is stylish, your written content fashionable, yet you appear to have acquired some apprehension regarding what you intend to present going forward. Undoubtedly, I’ll return more frequently, similar to I have almost constantly, in the event you sustain this ascent.

  9. I truly savored what you’ve accomplished here. The sketch is elegant, your authored material trendy, however, you seem to have developed some trepidation about what you aim to offer next. Certainly, I shall revisit more regularly, just as I have been doing nearly all the time, in case you uphold this ascension.

  10. I truly relished the effort you’ve invested here. The design is tasteful, your authored material fashionable, however, you seem to have acquired some unease about what you intend to present henceforth. Undoubtedly, I’ll revisit more regularly, similar to I have nearly all the time, in the event you sustain this rise.

  11. I genuinely enjoyed the work you’ve put in here. The outline is refined, your written content stylish, yet you appear to have obtained some apprehension regarding what you wish to deliver thereafter. Assuredly, I will return more frequently, akin to I have almost constantly, provided you maintain this climb.

  12. I sincerely enjoyed what you’ve accomplished here. The sketch is fashionable, your written content chic, yet you appear to have developed some apprehension regarding what you aim to offer thereafter. Certainly, I shall return more frequently, just as I have been doing almost constantly, should you uphold this upswing.

  13. I sincerely appreciated the effort you’ve invested here. The sketch is tasteful, your authored material chic, however, you seem to have developed some uneasiness about what you aim to offer henceforth. Certainly, I shall revisit more regularly, just as I have been doing nearly all the time, should you uphold this climb.

  14. Thanks I have recently been looking for info about this subject for a while and yours is the greatest I have discovered so far However what in regards to the bottom line Are you certain in regards to the supply

  15. I truly appreciated the work you’ve put forth here. The sketch is tasteful, your authored material stylish, yet you appear to have developed some nervousness regarding what you intend to deliver next. Rest assured, I’ll return more regularly, much like I’ve done almost constantly, should you maintain this upward trajectory.

  16. I do believe all the ideas youve presented for your post They are really convincing and will certainly work Nonetheless the posts are too short for novices May just you please lengthen them a little from subsequent time Thanks for the post

  17. Thank you I have just been searching for information approximately this topic for a while and yours is the best I have found out so far However what in regards to the bottom line Are you certain concerning the supply

  18. I’ve been visiting this site for years, and it never fails to impress me with its fresh perspectives and wealth of knowledge. The attention to detail and commitment to quality is evident. This is a true asset for anyone seeking to learn and grow.

Comments

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.