Skip to content

Commit c5e1b3b

Browse files
committed
[deployment] feat: add deployment liveness tutorial.
1 parent d49e629 commit c5e1b3b

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

deployment/liveness/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Dockerfile
2+
FROM golang:1.16-buster AS builder
3+
RUN mkdir /src
4+
ADD . /src
5+
WORKDIR /src
6+
7+
RUN go env -w GO111MODULE=auto
8+
RUN go build -o main .
9+
10+
FROM gcr.io/distroless/base-debian10
11+
12+
WORKDIR /
13+
14+
COPY --from=builder /src/main /main
15+
EXPOSE 3000
16+
ENTRYPOINT ["/main"]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: hellok8s-deployment
5+
spec:
6+
strategy:
7+
rollingUpdate:
8+
maxSurge: 1
9+
maxUnavailable: 1
10+
replicas: 3
11+
selector:
12+
matchLabels:
13+
app: hellok8s
14+
template:
15+
metadata:
16+
labels:
17+
app: hellok8s
18+
spec:
19+
containers:
20+
- image: guangzhengli/hellok8s:liveness
21+
name: hellok8s-container
22+
livenessProbe:
23+
httpGet:
24+
path: /healthz
25+
port: 3000
26+
initialDelaySeconds: 3
27+
periodSeconds: 3

deployment/liveness/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"time"
8+
)
9+
10+
func hello(w http.ResponseWriter, r *http.Request) {
11+
io.WriteString(w, "[v2] Hello, Kubernetes!")
12+
}
13+
14+
func main() {
15+
started := time.Now()
16+
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
17+
duration := time.Since(started)
18+
if duration.Seconds() > 15 {
19+
w.WriteHeader(500)
20+
w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
21+
} else {
22+
w.WriteHeader(200)
23+
w.Write([]byte("ok"))
24+
}
25+
})
26+
27+
http.HandleFunc("/", hello)
28+
http.ListenAndServe(":3000", nil)
29+
}

0 commit comments

Comments
 (0)