Hi, When Kubernetes scale down or if we stopped/ terminated a POD, how can we g . . .

Mahmoud Shahin:
Hi,

When Kubernetes scale down or if we stopped/ terminated a POD, how can we gauarantee that its current functions (tasks in progress) will not be affected? For example, If we are writing file in a pod and while being in the middle of this task, the pod needs to terminate/ stopped? what is the scenario here? Another example to demonstrate my inquiry: kubectl scale replicaset my-new-replica-set --replicas=2 (it was 5 as an example). Three pods will be terminated in this case. Will the termination wait until those three pods finish their tasks? Thank you

Sergei Diachenko:
@Mahmoud Shahin it absolutely depends on your application inside pod. Your application will have chance to finish the current task, but it has time limit. When kubernetes terminates pods it uses algorithm:
• Stop traffic from the service to pod. It prevents to send new requests to the pod.
• Execute <Container Lifecycle Hooks | Kubernetes hook>. You can write here specific actions which should be invoked before your app will be stopped.
• kubernetes sends SIGTERM to all pod’s containers. Usually application correctly release all resources and prepare to finish. But indeed your application might do any actions.
• kubernetes waits graceperiod (30s by default) and if the processes in the pod don’t finish, to the end of this period it kills them. You can manage this time using terminationGracePeriodSeconds option.
And of course if the pod was down suddenly (because power failure of the node for example), no of the above cannot be applied.