Bla bla bla
Graceful shutdown means your OS know you want to close computer. It saves your changes at MS Office. Or you can force shutdown by keep pressing power buton for 10 second. But If you do this you can lost your data at MS Office. Data loss probably the one of the minimum amount of damage that you will get. It can cause hardware problems.
In service scale we won't take that much problems but it don't mean we don't need it. If your cloud orchestrator want your service to stop, you probably want to clear your back before stop like operating systems. This is called graceful shutdown. Even stateless services can need a graceful shutdown.
It was explained on another Lightning Talk by Michael. I will link it after watch.
If critical part of progress fail, you want to stop other tasks. It was basically help you to prevent unnecessary work.
You can use context package in go for cancellation.
Probably state of a service is the most critical part of your program. If kubernetes says your service to stop, you must catch this signal and do a cleanup before your service killed.
One of the way the Operating Systems (UNIX) used for communicate with processes is signals. They are important because they are used for inform your program before process killed.
You can use os/signals package in go for catch signals.
Go signal notification works by sending os.Signal values on a channel. This channel should be buffered.
signal.Notify
registers the given channel to receive notifications of the specified signals.
You must run this on unblocked goroutine. And you can merge this with context package.
(fill with information about this diagram)
There is 3 important signals:
- SIGINT
- It is mostly important for cli applications. Because when you press Ctrl+C you send this signal to the running program.
- SIGTERM
- It is a termination request sended to program. System deamons and cloud orchestrator sends this signals first. If program doesn't close themselves, then sends SIGKILL
- SIGKILL
- It is the brutal way of killing a process and it should only be used as the last resort.
- It kills process on kernel level.
(Maybe add SIGSTOP SIGSTP SIGCONT)
Maybe I can kill some processes with kill -SIGTERM
[ Need More Example in Here ]
In the Kubernetes API Server https://github.com/kubernetes/kubernetes/blob/ea0764452222146c47ec826977f49d7001b0ea8c/staging/src/k8s.io/apiserver/pkg/server/signal.go#L40-L55
When docker stop executes, it sends SIGTERM, waits ten seconds, then sends SIGKILL if it hasn't stopped.
You have to use correct ENTRYPOINT syntax. You must use ["command"] instead of "".
Recall that keyboard interrupts are SIGINT events, not SIGTERM events. If docker run in the foreground, then Ctrl + C, that sends SIGINT to the container.
Interesting Docker bonus feature. You can set e.g. STOPSIGNAL SIGINT in your Dockerfile to change what command Docker will send when you run docker stop.
Kubernetes works same kubectl delete pods mypod
You can send signals https://docs.docker.com/engine/reference/commandline/kill/#send-a-custom-signal-to-a-container
I will write an example signal listener. And containerize it, then kill it.