k8s学习六-k8s部署go服务
【摘要】 go代码写一个最简单的http服务器:package mainimport ( "flag" "github.com/gin-gonic/gin" "net/http" "os")var version = flag.String("v", "v1", "v1")func main() { router := gin.Default() router.GET("", ...
go代码
写一个最简单的http服务器:
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")
}
复制
测试下能不能用:
go run main.go
复制
启动成功后,curl测试:
(base) tioncico@appledeMacBook-Pro test % curl 127.0.0.1:8080
This is version:v1 running in pod appledeMacBook-Pro.local%
复制
dockerfile
编写go服务的dockerfile,在main.go同级目录编写(多阶段构建dockerfile):
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.0.1"]
复制
构建docker镜像并打包上传
docker build -t go:v1.0.1 . # 编译镜像
docker tag go:v1.0.1 tioncico/go:v1.0.1 # 增加tag
docker push tioncico/go:v1.0.1 # 上传到docker hub
复制
此时,在dockerhub上,即可看到编译好的镜像:
编写k8s yaml文件
新增 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: tioncico/go:v1.0.1
name: go
imagePullPolicy: Always
command: ["./main","-v","v1.0.1"]
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: 30080
type: NodePort
复制
k8s部署
kubectl apply -f go.yaml
复制
即可部署成功,查看pod状态:
root@master:/home/tioncico# kubectl get pods
NAME READY STATUS RESTARTS AGE
go-deployment-86f769995d-6j4pq 1/1 Running 0 2m20s
go-deployment-86f769995d-8pcsd 1/1 Running 0 2m20s
go-deployment-86f769995d-lz7vf 1/1 Running 0 2m20s
nginx-7cbb8cd5d8-w9tn2 1/1 Running 3 (140m ago) 28h
复制
可看到,已经部署了3台,访问 集群任意一个节点ip:30080端口,可查看效果:
root@master:/home/tioncico# curl http://192.168.192.9:30080
This is version:v1.0.1 running in pod go-deployment-86f769995d-lz7vf
root@master:/home/tioncico# curl http://192.168.192.9:30080 ^C
root@master:/home/tioncico# curl http://192.168.192.10:30080
This is version:v1.0.1 running in pod go-deployment-86f769995d-8pcsd
root@master:/home/tioncico#
复制
为什么需要上传到docker hub?
在上面的教程中可以看到,我们先将镜像放到了docker hub,才开始进行k8s部署,这样相当于把自己的项目放到了公共的仓库,十分不安全
原因是: k8s有多个节点,如果我们只在一个节点编译镜像,其他节点就无法获取到这个自己编译后的镜像
解决方案有2种:
1:将所有镜像都编译到其他节点中去
2:创建一个私有的docker仓库,让k8s集群通过私有仓库去进行pull images 即可
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)