Created
December 18, 2022 06:04
-
-
Save yutaroyamanaka/3b77f028bfec131b86f2207714d7306c to your computer and use it in GitHub Desktop.
Graceful Shutdown Experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: graceful-shutdown | |
labels: | |
app: graceful-shutdown | |
spec: | |
replicas: 1 | |
strategy: | |
type: RollingUpdate | |
rollingUpdate: | |
maxSurge: 1 | |
maxUnavailable: 0 | |
selector: | |
matchLabels: | |
app: graceful-shutdown | |
template: | |
metadata: | |
labels: | |
app: graceful-shutdown | |
spec: | |
containers: | |
- name: graceful-shudown | |
args: | |
- --shutdown.delay=5 | |
image: salachike/graceful-shudown:v0.0.1 | |
ports: | |
- containerPort: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:1.19.4-alpine3.16 as builder | |
COPY ./main.go ./ | |
RUN go build -o /app ./main.go | |
FROM alpine:3.16 | |
COPY --from=builder /app . | |
USER nobody | |
ENTRYPOINT ["./app"] | |
CMD ["--shutdown.delay=0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: networking.k8s.io/v1 | |
kind: Ingress | |
metadata: | |
name: graceful-shutdown | |
annotations: | |
nginx.ingress.kubernetes.io/rewrite-target: /$1 | |
spec: | |
rules: | |
- host: graceful.shutdown.test.com | |
http: | |
paths: | |
- path: / | |
pathType: Prefix | |
backend: | |
service: | |
name: graceful-shutdown | |
port: | |
number: 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"log" | |
"net/http" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
) | |
func main() { | |
var t time.Duration | |
flag.DurationVar(&t, "shutdown.delay", 0, "duration until shutdown starts") | |
flag.Parse() | |
srv := http.Server{ | |
Addr: ":8080", | |
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
w.Write([]byte("hello world")) | |
}), | |
} | |
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | |
defer stop() | |
go func() { | |
log.Println("Server is running") | |
if err := srv.ListenAndServe(); err != http.ErrServerClosed { | |
log.Fatal(err) | |
} | |
}() | |
for { | |
select { | |
case <-ctx.Done(): | |
time.Sleep(t) | |
srv.Shutdown(ctx) | |
return | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: Service | |
metadata: | |
labels: | |
app: graceful-shutdown | |
name: graceful-shutdown | |
spec: | |
selector: | |
app: graceful-shutdown | |
type: NodePort | |
ports: | |
- protocol: TCP | |
port: 80 | |
targetPort: 8080 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment