package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/shirou/gopsutil/host"
)
func main() {
r := gin.Default()
nInfo, _ := host.Info()
r.GET("/sys", func(context *gin.Context) {
context.String(http.StatusOK, "主机名%v\n", nInfo.Hostname)
})
//3.监听端口,默认8080
r.Run(":80")
}
Dockerfile多阶级构建
cat > Dockerfile <<-'EOF'
FROM golang:1.20.8 as builder
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct
WORKDIR /data
COPY . .
RUN go mod init app
RUN go mod tidy
#RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -o app .
#RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
ENV TZ='Asia/Shanghai'
ENV TIMEZONE Asia/Shanghai
RUN echo "https://mirrors.ustc.edu.cn/alpine/latest-stable/main/" > /etc/apk/repositories && \
echo "https://mirrors.ustc.edu.cn/alpine/latest-stable/community/" >> /etc/apk/repositories && \
apk update && apk upgrade && \
apk add tzdata bash-doc bash && \
ln -snf /usr/share/zoneinfo/$TIMEZONE /etc/localtime && \
echo $TIMEZONE > /etc/timezone
WORKDIR /data
COPY --from=builder /data/app .
CMD ["./app"]
EOF
docker build -t app:latest .
我的镜像
registry.cn-shenzhen.aliyuncs.com/jbjb/logs:ginv01
K8S YAML文件
tee gin.yaml <<-'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: mygin-v1
labels:
app: mygin
spec:
replicas: 3
selector:
matchLabels:
app: mygin
template:
metadata:
labels:
app: mygin
spec:
containers:
- name: mygin
image: registry.cn-shenzhen.aliyuncs.com/jbjb/logs:ginv01
resources:
requests:
memory: 256Mi
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: mygin-v1
labels:
app: mygin
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: mygin
EOF
while true; do curl 192.168.8.62:26964/sys; sleep 1; done
hosts:mygin-v1-6c57fc598-t9kgd
hosts:mygin-v1-6c57fc598-t9kgd
hosts:mygin-v1-6c57fc598-ncnfd
hosts:mygin-v1-6c57fc598-9cbzt
hosts:mygin-v1-6c57fc598-t9kgd
hosts:mygin-v1-6c57fc598-ncnfd
hosts:mygin-v1-6c57fc598-9cbzt
hosts:mygin-v1-6c57fc598-t9kgd
hosts:mygin-v1-6c57fc598-t9kgd
hosts:mygin-v1-6c57fc598-ncnfd
hosts:mygin-v1-6c57fc598-t9kgd
hosts:mygin-v1-6c57fc598-9cbzt
hosts:mygin-v1-6c57fc598-9cbzt
hosts:mygin-v1-6c57fc598-t9kgd
tee gin-ingress.yaml <<-'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: gin-app
annotations:
kubernetes.io/ingress.class: nginx
kubernetes.io/elb.port: '80'
spec:
rules:
- host: jb.jbjb2.com
http:
paths:
- path: '/'
pathType: Prefix
backend:
service:
name: mygin-v1
port:
number: 80 <<<----svc端口
EOF
while true; do curl jb.jbjb2.com/sys; sleep 1; done