一个 goroutine 在其 Kubernetes 容器终止后是否终止?

huangapple go评论78阅读模式
英文:

Does a goroutine terminates after its Kubernetes container gets terminated?

问题

例如,一个容器运行主线程和一个 goroutine。主线程遇到问题并终止。需要注意的是,在 Golang 中,主线程的终止不会导致 goroutine 的自动终止。

  1. 由于主线程已经终止,容器会被杀死并重新创建吗?还是容器会继续运行,因为 goroutine 仍在运行?

  2. 如果主线程终止后容器会被杀死并重新创建,这会导致 goroutine 也被终止吗?还是 goroutine 会无限继续运行,现在没有简单的方法来终止它?

英文:

For example, a container runs the main thread and a goroutine. The main thread encounters an issue and terminates. Note that for Golang, termination of the main thread does not result in auto-termination of the goroutine.

  1. As the main thread has been terminated, will the container be killed and re-created? Or will the container continue running due to the goroutine is still running?

  2. If the container will be killed and re-created after the main thread has been terminated, will this result in the goroutine getting terminated as well? Or will the goroutine continue running indefinitely and there is no easy way to terminate it now?

答案1

得分: 2

如果存在主要函数,程序将停止运行。不会再执行任何操作。它会释放任何已使用的资源,如文件描述符和数据库连接。

在下面的程序中,我们永远不会看到“done”被打印出来。

func main() {
	go func() {
		time.Sleep(time.Minute)
		fmt.Println("done")
	}()

	time.Sleep(time.Second * 3)
}

如果具有该主要函数的程序是容器的前台进程,则容器将按照容器的标准行为关闭。

如果运行下面的示例,您可以观察到容器在睡眠结束后立即关闭。

$ docker run --name sample busybox sleep 3 && docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS                              PORTS     NAMES
fd4319261a0d   busybox   "sleep 3"   4 seconds ago   Exited (0) Less than a second ago             sample

如果容器中运行的程序被关闭,这几乎相当于拔掉计算机的电源插头。您的计算机上将不再运行任何程序。这是不可能的。

我鼓励您自己创建一些测试场景并验证这一点。

英文:

If the main functions exists, the program is stopped. Nothing will run any more. It will release any used resource, like file descriptors and database connections.

In the below program, we will never see done being printed.

func main() {
	go func() {
		time.Sleep(time.Minute)
		fmt.Println("done")
	}()

	time.Sleep(time.Second * 3)
}

https://play.golang.com/p/kPKZDdMcduS

If the program with that main function was the foreground process of the container, then the container shuts down as its standard behaviour with containers.

If you run the below example, you can observe how the container shuts down as soon as the sleep finishes.

$ docker run --name sample busybox sleep 3 && docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS                              PORTS     NAMES
fd4319261a0d   busybox   "sleep 3"   4 seconds ago   Exited (0) Less than a second ago             sample

If the container, the program is running in, is shut down, it's more or less as if you would pull the plug of your computer. Nothing will run on your computer any more. It's impossible.

I would encourage you to create some test scenarios yourself and validate this.

huangapple
  • 本文由 发表于 2022年2月9日 04:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/71040563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定