在K8s上部署Go服务

这篇博客详细介绍了如何在Kubernetes(K8s)上部署Go语言开发的服务。首先,创建一个简单的Go服务,提供HTTP接口。接着,编写Dockerfile以便构建Docker镜像。随后,构建go.yaml文件用于K8s部署。最后,通过参数调整和重新打包,完成服务的部署。
摘要由CSDN通过智能技术生成

目标

镜像名:foxliang/go:v
镜像内容:二进制文件,跑起来是个http服务,监听8080端口,接到请求会打印 This is version:{v} running in pod {hostname},其中v是可变参数代表版本(字符串1.0这种),可以通过传参数进去
 
1、将上面的二进制部署成一个服务跑起来
2、服务需要包含3个副本
3、可以外部访问这个服务
4、可以对服务的版本进行滚动更新(通过传参进去)


1.先添加一个最简单的golang服务

写一个main.go文件提供了一个 接口:

package main
 
import (
    "flag"
 
    "github.com/gin-gonic/gin"
    "net/http"
    "os"
)
 
var version = flag.String("v", "v1", "v1")
func main() {
    router := gin.Default()
 
    router.GET("", func(c *gin.Context) {
        flag.Parse()
        hostname, _ := os.Hostname()
 
        c.String(http.StatusOK, "This is version:%s running in pod %s",*version,hostname)
    })
 
 
    router.Run(":8080")
}

访问 http://127.0.0.1:8080

This is version:1.3 running in pod f06afe0eabc4

2.为上面的golang服务写一个Dockerfile文件:(与main.go同目录)

FROM golang:latest AS build
 
WORKDIR /go/src/test
COPY . /go/src/test
RUN go env -w GOPROXY=https://goproxy.cn,direct
RUN CGO_ENABLED=0 go build -v -o main .
 
FROM alpine AS api
RUN mkdir /app
COPY --from=build /go/src/test/main /app
WORKDIR /app
ENTRYPOINT ["./main", "-v" ,"1.3"]

3.然后构建Docker镜像并打包上传

$ docker build  -t go:v1.3 . 
$ docker tag go:v1.3 foxliang/go:v1.3 
$ docker push foxliang/go:v1.3

4.构建go.yaml文件

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-deployment
  labels:
    app: go
spec:
  selector:
    matchLabels:
      app: go
  replicas: 3
  minReadySeconds: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: go
    spec:
      containers:
      - image: foxliang/go:v1.3
        name: go
        imagePullPolicy: Always
        command: ["./main","-v","v1.3"]
        ports:
        - containerPort: 8080
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: go-service
  labels:
    app: go
spec:
  selector:
    app: go
  ports:
    - name: go-port
      protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 31080
  type: NodePort
$ kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
go-deployment-8675897977-6r9rd   1/1     Running   0          3m25s
go-deployment-8675897977-c7kv2   1/1     Running   0          3m47s
go-deployment-8675897977-f7dn2   1/1     Running   0          3m47s


$ minikube service list
|----------------------|---------------------------|--------------|---------------------------|
|      NAMESPACE       |           NAME            | TARGET PORT  |            URL            |
|----------------------|---------------------------|--------------|---------------------------|
| default              | go-service                | go-port/8080 | http://192.168.79.2:31080 |

最后可编辑传参数 然后重新打包上传并部署可以得到下面的结果

for i in `seq 1 10`     
do
  curl http://192.168.79.2:31080
  echo " "
# echo $i
done
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 

for i in `seq 1 10`
do
  curl http://192.168.79.2:31080
  echo " "
# echo $i
done
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 
This is version:1.4 running in pod go-deployment-8675897977-c7kv2 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 
This is version:1.4 running in pod go-deployment-8675897977-c7kv2 
This is version:1.3 running in pod go-deployment-c4fd9b576-mb9b2 
This is version:1.3 running in pod go-deployment-c4fd9b576-l7l6b 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值