部署go项目到k8s集群
目录
部署go项目到k8s集群
go项目
项目代码
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "welcome jeff!!!",
})
})
r.Run(":8888")
}
Dockerfile
FROM docker.sharkgulf.cn/public/alpine-ca:3.10
RUN mkdir -p /app /app/log
COPY ./bikesvc /app/
WORKDIR /app
VOLUME [ "/app/log" ]
RUN chmod +x ./bikesvc
EXPOSE 8888
ENTRYPOINT ["./bikesvc"]
go交叉编译linux可执行文件
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o . .
docker编译镜像
// docker build -t 标签:版本 go可执行文件位置
docker build -t docker.sharkgulf.cn/shark-bs/gin:v1 .
k8s部署
因为用的是本地镜像测试,所以在每一台node节点创建生成镜像
bs-namespace.yaml
# 名称空间
kind: Namespace
apiVersion: v1
metadata:
name: sg-bs
labels:
release: stable
bs-service.yaml
# NodePort模式service
apiVersion: v1
kind: Service
metadata:
name: bs-service
namespace: sg-bs
spec:
selector:
release: stable
type: NodePort
ports:
- name: http
port: 8888
targetPort: 8888
protocol: "TCP"
- name: https
port: 443
targetPort: 443
protocol: "TCP"
bs-deployment.yaml
# Deployment控制器
kind: Deployment
apiVersion: apps/v1
metadata:
name: bs-deployment
namespace: sg-bs
spec:
replicas: 1
selector:
matchLabels:
release: stable
template:
metadata:
labels:
release: stable
spec:
containers:
- name: bikesvc
image: docker.sharkgulf.cn/shark-bs/gin:v1
imagePullPolicy: Never # 用本地镜像
NodePort模式访问
http://192.168.0.216:31720/ping
{"message":"welcome jeff!!!"}
选择了IT,必定终身学习