As part of the scaling of containers in the kubernetes, I want me to gracefully serve my http connections until close. To this extent, I implemented this code in go:
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/braintree/manners"
)
func main() {
shutdown := make(chan int)
sigChan := make(chan os.Signal, 1)
http.HandleFunc("/", hello)
server := manners.NewWithServer(&http.Server{Addr: ":80", Handler: nil})
go func() {
server.ListenAndServe()
shutdown <- 1
}()
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) {
io.WriteString(w, "Hello world!")
}
This displays the SIGTERM dockers and gracefully exits after existing requests have been submitted. When I run this container in 10-instance kubernetes, I can scale up and down without incident until I scale to one instance. When I scale to a single instance, I see a short set of HTTP errors, then everything looks great.
It seems strange to me that when scaling, I assume that the proxy server is updated first, then the containers are closed, and the code above allows you to submit requests.
2 , , , , - etcd? , ,