How can I provide graceful scaling in quaternets?

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)

    //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!")
}

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? , ,

+4

Source: https://habr.com/ru/post/1599175/


All Articles