How to scale down pods to zero if there is no traffic to the application pods?

Solution Verified - Updated -

Environment

  • Red Hat Openshift Container Platform (OCP 4)
  • Red Hat OpenShift Service on AWS (ROSA 4)
  • Red Hat Openshift Dedicated 4 (OSD 4)
  • Azure Red Hat Openshift (ARO 4)

Issue

  • As a cost optimization strategy, customer wants to scale down the application pod replicas to zero on weekends.
  • Is it possible to idle applications to reduce resource consumption?

Resolution

In OpenShift, we have a concept of idling applications where cluster administrators can idle applications to reduce resource consumption.

If any scalable resources are not in use, the user can idle them, scaling their replicas to 0, using oc idle. The next time network traffic is directed to the resources, the resources are unidled by scaling up the replicas, and normal operation continues.

For more information about idling applications in OpenShift, please refer to our official documentation.

For automated scaling to zero, it is recommended to use OpenShift Serverless, which supports Scale-to-zero.

Additionally, the Custom Metrics Autoscaler allows OpenShift to scale down resources to zero based on custom Prometheus or Kafka metrics.

Customers can also create cron jobs to schedule the above process as per their requirements.

Alternate solution:

An alternative to pod-idling may be a script such as an ansible playbook that the customer can run through a cronjob from an external system such as a bastion host.

For example:

One could say on Friday evening bring the replicas down to 0 and say Monday morning run another playbook to scale up the workloads.

  • Example playbook to scale deployment to 0 replicas:
- name: Scale deployment
  hosts: localhost
  gather_facts: false
  connection: local

  tasks:

  - name: Get access token  
    k8s_auth:  
      username: cluster-admin 
      password: <password>
      host: https://<openshift-api>:6443 
      validate_certs: true 
    register: k8s_auth_results

  - name:  Scale down deployment 
    k8s_scale:
      api: apps/v1
      api_key: "{{ k8s_auth_results.k8s_auth.api_key }}"  
      kind: Deployment
      name: "{{ item.deployment }}"
      namespace: "{{ item.name_space }}"
      replicas: "{{ item.replicas }}"
      wait: false 
    loop:
      - name_space: test1
        deployment: simple-deployment 
        replicas: 0

      - name_space: test2
        deployment: simple-deployment
        replicas: 0

      - name_space: test3
        deployment:  simple-deployment
        replicas: 0

      - name_space: test5
        deployment: simple-deployment
        replicas: 0
  • Change the replica count and re-run the playbook when you desire to scale workloads:
- name: Scale deployment
  hosts: localhost
  gather_facts: false
  connection: local

  tasks:

  - name: Get access token  
    k8s_auth:  
      username: cluster-admin 
      password:  <password>
      host: https://<api.url> 
      validate_certs: true 
    register: k8s_auth_results

  - name:  Scale down deployment 
    k8s_scale:
      api: apps/v1
      api_key: "{{ k8s_auth_results.k8s_auth.api_key }}"  
      kind: Deployment
      name: "{{ item.deployment }}"
      namespace: "{{ item.name_space }}"
      replicas: "{{ item.replicas }}"
      wait: false 
    loop:
      - name_space: test1
        deployment: simple-deployment 
        replicas: 2 <-

      - name_space: test2
        deployment: simple-deployment
        replicas: 4 <-

      - name_space: test3
        deployment:  simple-deployment
        replicas: 8 <-

      - name_space: test5
        deployment: simple-deployment
        replicas: 6 <- 

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments