Good morning. I need to know how can I automatically delete a pod that has not received TCP/UDP requests through the port through which its service is exposed. My intention is that if nobody accesses an application running in a pod for more than, for example, 3 hours, this pod will be automatically deleted to save unused resources, and if a new request is received, the pod will be automatically lifted again. Can anyone help me? Thank you.
How to automatically remove a Kubernetes pod when it does not receive TCP/UDP connections for a certain period of time?
212 Views Asked by Luis Manuel Cortés Tirado AtThere are 2 best solutions below
On
The blog written by Giulia Di Pietro gives the detailed description, if no one accesses an application running in a pod for more than, say, three hours, the pod will be automatically removed to conserve unneeded resources, and if a fresh request is received, the pod will be automatically lifted again.
The Horizontal Pod Autoscaler (HPA) adjusts the number of copies running for each application according to a set of metric thresholds specified by the user. The HPA makes horizontal scaling of Kubernetes container workloads possible. To activate HPA, the Kubernetes cluster must have metrics enabled.
Pod metrics (also known as PodsMetricSource) refer to pod-based metrics at runtime that can be gathered in Kubernetes. A pod's transaction rate per second is one example. If a specific PodsMetricSource has many pods, the values will be collected and averaged before being compared to the desired threshold values.
Based on the below example, mention your requirements:
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: MaxNote that the scaled-down policy is set at 100%, so in 15s, you can remove all the replicas to reach the minimum replica defined in your HPA.
The stabilization window is used to avoid changing the number of replica counts when the metrics used for scaling keep fluctuating. The auto-scaling algorithm uses this window to infer a previous desired state and avoid unwanted changes to the workload scale. For example, in the following snippet, a stabilization window is specified for scaleDown.
behavior: scaleDown: stabilizationWindowSeconds: 300 #You can set based on your requirement
Finally I will use KEDA (https://keda.sh/) to do scale-to-zero when prometheus doesn't detect TCP/UDP connections to a port. Thanks for your comments.