package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"golang.org/x/sync/errgroup"
)
var (
g errgroup.Group
)
func router01() http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.GET("/", func(c *gin.Context) {
c.String(200, "New New New Server>>111111")
})
return e
}
func router02() http.Handler {
e := gin.New()
e.Use(gin.Recovery())
e.GET("/", func(c *gin.Context) {
c.String(200, "Old Old Old Server<<2222222")
})
return e
}
func main() {
server01 := &http.Server{
Addr: ":8181",
Handler: router01(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
server02 := &http.Server{
Addr: ":8282",
Handler: router02(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
g.Go(func() error {
return server01.ListenAndServe()
})
g.Go(func() error {
return server02.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
编译
//设置国内代理
go env -w GOPROXY=https://goproxy.cn,direct
go get -u golang.org/x/sync/errgroup
go build weball.go
//生成mod
go mod init example.com/m
go mod tidy
Dockerfile
tee Dockerfile <<-'EOF'
FROM alpine:latest
ENV TZ='Asia/Shanghai'
ENV TIMEZONE Asia/Shanghai
RUN mkdir /lib64 && \
ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2
WORKDIR /data
ADD weball /data/weball
EXPOSE 8181
EXPOSE 8282
CMD ["./weball"]
EOF
YAML
tee k8s.yaml <<-'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: weball-v1
labels:
app: weball
spec:
replicas: 1
selector:
matchLabels:
app: weball
template:
metadata:
labels:
app: weball
spec:
containers:
- name: weball
image: registry.cn-shenzhen.aliyuncs.com/lolos/loloss:weball-00
ports:
- containerPort: 8181
---
apiVersion: v1
kind: Service
metadata:
name: weball-v1
labels:
app: weball
spec:
type: NodePort
ports:
- name: old
port: 8181
targetPort: 8181
nodePort: 28181
- name: new
port: 8282
targetPort: 8282
nodePort: 28282
selector:
app: weball
EOF
已完成的镜像
docker pull registry.cn-shenzhen.aliyuncs.com/lolos/loloss:weball-00
测试
[root@dockser tmp]# curl 192.168.8.62:28282
Old Old Old Server<<2222222[root@dockser tmp]# curl 192.168.8.62:28282
Old Old Old Server<<2222222[root@dockser tmp]# curl 192.168.8.62:28282
Old Old Old Server<<2222222[root@dockser tmp]#
[root@dockser tmp]# curl 192.168.8.62:28181
New New New Server>>111111[root@dockser tmp]# curl 192.168.8.62:28181
New New New Server>>111111[root@dockser tmp]# curl 192.168.8.62:28181
New New New Server>>111111[root@dockser tmp]#