目标
镜像名: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