Skip to content

Instantly share code, notes, and snippets.

@yusufpapurcu
Last active April 29, 2022 18:53
Show Gist options
  • Save yusufpapurcu/b6ced71660465035dc27074e84d562d7 to your computer and use it in GitHub Desktop.
Save yusufpapurcu/b6ced71660465035dc27074e84d562d7 to your computer and use it in GitHub Desktop.
Lightning Talk - Graceful Shutdowns (Cancellation and Signals)

1st Slide

About Me

Bla bla bla

2nd Slide

What is graceful shutdown?

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.

What is cancellation?

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.

What is OS signals?

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.

3rd Slide

Example context usage

(fill with information about this diagram)

4th Slide

Short informations about signals?

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)

Examples

Maybe I can kill some processes with kill -SIGTERM

5th Slide

[ 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

6th Slide

How Kubernetes & Docker uses signals?

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

Examples

I will write an example signal listener. And containerize it, then kill it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment