我们的目标
今天,试一下在k8s里面跑自己编写的go web服务。
1、编写可运行的go服务
首先,第一步当然是写一个简单的go服务,这里简单写了一个http的服务。
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", hello) server := &http.Server{ Addr: ":8888", } fmt.Println("server startup...") if err := server.ListenAndServe(); err != nil { fmt.Printf("server startup failed, err:%v\n", err) } } func hello(w http.ResponseWriter, _ *http.Request) { w.Write([]byte("hello layu!")) }
一个最简单的http服务,监听8080端口,返回 "hello layu" 字符串。
2、制作docker镜像
接着,我们需要制作镜像,制作docker镜像需要写 Dockerfile 文件
FROM golang:alpine ENV GO111MODULE=on \ CGB_ENABLED=0 \ GOOS=linux \ GOARCH=amd64 WORKDIR /build COPY . . RUN go mod init main RUN go build -o app . EXPOSE 8888 CMD ["/