feat: statefulsets

This commit is contained in:
loveuer 2024-04-03 14:02:28 +08:00
parent cd4df6ce5f
commit 4ae56d1726
7 changed files with 213 additions and 1 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
.vscode
.idea
.DS_Store

113
deployment/echo.yaml Normal file
View File

@ -0,0 +1,113 @@
apiVersion: v1
kind: Namespace
metadata:
name: echo-app
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
namespace: echo-app
name: echo
spec:
replicas: 1
selector:
matchLabels:
app: echo
template:
metadata:
labels:
app: echo
spec:
containers:
- name: system
image: loveuer/echo_app:v0.0.1
imagePullPolicy: IfNotPresent
command: ["/app/echo_app"]
ports:
- containerPort: 80
resources:
limits:
memory: 60Mi
cpu: 1
volumeMounts:
- mountPath: /data
name: echo-data
volumes:
- name: echo-data
persistentVolumeClaim:
claimName: echo-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: echo-app
name: echo-pvc
spec:
accessModes: ["ReadWriteMany"]
storageClassName: "echo-storage"
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
namespace: echo-app
name: echo-pv
spec:
capacity:
storage: 1Gi
volumeMode: Filesystem # Filesystem文件系统 Block
accessModes:
- ReadWriteMany
# persistentVolumeReclaimPolicy: Delete
persistentVolumeReclaimPolicy: Retain
hostPath:
path: /data/echo-app
type: ''
storageClassName: "echo-storage"
volumeMode: Filesystem
---
apiVersion: v1
kind: Service
metadata:
namespace: echo-app
name: echo
# annotations:
# metallb.universe.tf/loadBalancerIPs: 10.220.10.48
spec:
selector:
app: echo
# type: LoadBalancer
type: ClusterIP
ports:
- name: echo-http
port: 80
targetPort: 80
# nodePort: 31997
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ing-echo
namespace: echo-app
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
ingressClassName: nginx
rules:
- host: "echo.zyp.dev.com"
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: echo
port:
number: 80

View File

@ -7,6 +7,7 @@ docker run \
--restart=always \ --restart=always \
-e SSL_CERT_DIR=/apps/ssl \ -e SSL_CERT_DIR=/apps/ssl \
--add-host raw.githubusercontent.com:185.199.109.133 \ --add-host raw.githubusercontent.com:185.199.109.133 \
-v ./data:/data \
-p 20443:443 \ -p 20443:443 \
-p 80:30080 \ -p 80:30080 \
-p 443:30443 \ -p 443:30443 \

23
service/echo/Dockerfile Normal file
View File

@ -0,0 +1,23 @@
FROM golang:1.20.14-alpine3.19 AS builder
WORKDIR /app/build
COPY go.mod .
COPY go.sum .
COPY service/echo/main.go .
ENV GOPROXY https://goproxy.io
RUN go mod download && go build -ldflags='-s -w' -o echo_app .
FROM alpine
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && apk add curl
ENV TZ Asia/Shanghai
WORKDIR /app
COPY --from=builder /app/build/echo_app .
CMD [ "/app/echo_app" ]

72
service/echo/main.go Normal file
View File

@ -0,0 +1,72 @@
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"time"
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
)
var (
addr string
hostname string
root string
)
func init() {
flag.StringVar(&addr, "addr", ":80", "")
flag.StringVar(&root, "root", "/data", "save file root dir")
flag.Parse()
hostname = os.Getenv("HOSTNAME")
if hostname == "" {
hostname = fmt.Sprintf("unknown-%d", time.Now().UnixNano())
}
}
func main() {
app := nf.New()
app.Use(func(c *nf.Ctx) error {
c.SetHeader("U-HOSTNAME", hostname)
return c.Next()
})
app.Post("/echo/file", func(c *nf.Ctx) error {
type Req struct {
Filename string
Content string
}
var (
err error
req = new(Req)
file *os.File
)
if err = c.BodyParser(req); err != nil {
return resp.Resp400(c, err.Error())
}
filename := path.Join(root, path.Base(req.Filename))
if file, err = os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644); err != nil {
return resp.Resp400(c, err.Error())
}
defer file.Close()
if _, err = file.WriteString(req.Content); err != nil {
return resp.Resp500(c, err.Error())
}
return resp.Resp200(c, nf.Map{"filename": filename})
})
log.Fatal(app.Run(addr))
}

View File

@ -4,7 +4,7 @@ WORKDIR /app/build
COPY go.mod . COPY go.mod .
COPY go.sum . COPY go.sum .
COPY main.go . COPY service/hello/main.go .
ENV GOPROXY https://goproxy.io ENV GOPROXY https://goproxy.io