gin 获取主机名

28 篇文章 2 订阅
  • gin.go
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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值