作为在 kubernetes 中扩展 pod 的一部分,我想确保在关闭之前优雅地为我的 http 连接提供服务。在某种程度上,我已经在 go 中实现了这段代码:
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/braintree/manners"
)
func main() {
shutdown := make(chan int)
//create a notification channel to shutdown
sigChan := make(chan os.Signal, 1)
//start the http server
http.HandleFunc("/", hello)
server := manners.NewWithServer(&http.Server{Addr: ":80", Handler: nil})
go func() {
server.ListenAndServe()
shutdown <- 1
}()
//register for interupt (Ctrl+C) and SIGTERM (docker)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
fmt.Println("Shutting down...")
server.Close()
}()
<-shutdown
}
func hello(w http.ResponseWriter, r *http.Request) {
// time.Sleep(3000 * time.Millisecond)
io.WriteString(w, "Hello world!")
}
这会寻找 docker SIGTERM 并在现有请求得到服务后正常关闭。当我在具有 10 个实例的 kubernetes 中运行这个容器时,只要我不缩减到单个实例,我就可以毫无意外地扩展和缩减。当我扩展到单个实例时,我看到一组简短的 http 错误,然后一切看起来都很好。
我觉得很奇怪,因为在扩展时我会假设代理首先更新,然后容器被关闭,上面的代码将允许提供请求。
在我当前的设置中,我正在运行 2 个节点,也许问题是当扩展低于节点数量时,并且 etcd 更新存在某种计时问题?对这里发生的事情的任何见解都会非常有用
九州编程
相关分类