Blog Detail

Container Restart Policy with Podman

By:DALEEP SINGH

2020-May-15 11:05:51

Podman Restart Policy

 

We can use podman to create and run container, basically our application runs as a process in that containerized environment. However, what will happen, if for some reason, the process dies, may be due to some unforeseen reasons. The answer is, Application will stop working.

We have seen how healthchecks can be implemented with podman, however, healthchecks will just ensure that application state is good and it is able to serve the request. It will not get the container to run again, if for some reason, the container exits in unclean state. This can be taken care by container restart policy.

Podman implements container restart with 3 policies namely, no, on-failure & always. It also allow us to define a maximum retry count which is no of times a container will restart when they exit with non-zero exit code.

Let us deploy a container with a restart-policy of on-failure and max retry count of 2.

Running podman inspect checkweb1 shows us that as of now, Restart count is 0, as the container hasn’t restarted yet and also the Restart policy of on-failure with Maximum retry count of 2.

( The output has been truncated to show the required information only. )

Now let us move ahead and kill the process inside the container to simulate a scenario where application exits uncleanly. I used simple method to identify the process ID and passing it to kill command.

The output of podman inspect this time shows that Restart count has now incremented to 1 and the container is in Running state. As we have set the Maximum retry count to two, the container got restarted. I killed the container parent process again by repeating the previous process, which now make our restart count to 2, the set threshold and getting the container process up again.

Now when the process is killed third time, we see that container has not come up again, even when we had the restart policy set for container failure, hence, this time, to get list of container I had to use podman ps -a command.

This small demonstration shows that restart policy is working as intended and this can help application deployer to ensure that application would be running without any human intervention and based on certain failure threshold, the application can then be shutdown.


 

Podman with Systemd service

 

The important point to take note regarding restart policy is, Restart will not restart containers after a system reboot. If you need this functionality with your application, you need to use systemd unit with podman and generate systemd unit files.

Reference: https://github.com/containers/libpod/blob/master/docs/source/markdown/podman-run.1.md

To showcase this functionality, I created a new container, very similar to the one we created earlier with restart policy.

To test my restart policy, I identified the parent process ID and killed it.

It clearly shows that the container was restarted successfully due to restart policy.

Let us restart our host and post reboot, let verify the status of our container. It shows that container is not running post host reboot.

 

We would be using podman generate command to generate a systemd service for our container. It gives us sub-command to either create a service unit or an YAML file for deploying on to a Kubernetes environment.

Our focus here is to generate a systemd service using podman generate systemd.

if we run the command podman generate systemd –name check-systemd, it will generate a systemd service unit file and output will be sent to STDOUT, however, as we want to save it in a file, I am also passing an additional option, -f.

I have not shown the complete output for service unit file.

[root@fedora-host ~]# podman generate systemd -f --name check-systemd

 All the service unit files are available in /etc/systemd/system/ folder. I am copying the generated service unit file to the same location. As of now the service is in inactive and disabled state.

I went ahead to enable and start the service, which automatically started the container also.

Let us reboot our host and check if our service is working correctly and does it start the container automatically for us now.

Output clearly indicates that post reboot, the container was restarted by our service and is up and running now.

This demo was to test and implement restart-policy with podman and create Systemd service unit files. Hope this will help to deploy podman containers and keep them UP & RUNNING..

 

ENJOY  & KEEP READING !!