阿里雲伺服器使用minikube構建開發服務

北辰 Let's Go發表於2020-12-09

服務程式碼
main.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": "pong",
                })
        })
        r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}	

服務程式碼檔案:在/root/hello資料夾下
main.go go.mod go.sum

Dockerfile

FROM golang:latest 
WORKDIR /root/hello
COPY ./hello /root/hello
RUN ls
RUN go env -w GO111MODULE=on && \
go env -w GOPROXY=https://goproxy.cn,direct
RUN CGO_ENABLED=0 GOOS=linux go build -o hello . 
FROM alpine:latest 
WORKDIR /root/
COPY --from=0 /root/hello/hello .
ENTRYPOINT ["./hello"]		

程式碼在Dockerfile並列目錄hello下

Dockerfile  hello(main.go)	

wzzz是我在dockerhub的使用者

docker build -t hellowz:latest .
docker tag hellowz:latest wzzz/hellowz:latest
docker pull wzzz/hellowz"latest

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wzhi
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wzhi
  template:
    metadata:
      labels:
        app: wzhi
    spec:
      containers:
      - name: wzhi
        image: wzzz/hellowz:latest
        ports:
        - containerPort: 8080	
kubectl create -f deployment.yaml
kubectl get pod -o wide
kubectl describe pod xxx 如果pod一致在建立中,執行該命令檢視構建過程

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: wzhi
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
  selector:
    app: wzhi
kubectl create -f service.yaml
kubectl  get service -o wide
minikube service wzhi
//代理k8s內部服務被外部9999埠訪問
nohup  kubectl proxy --port=9999 --address='172.31.185.142' --accept-hosts='^.*' &

並且暴漏外面埠 33567, 代理地址到xxxx(阿里雲內網ip) 自動轉發到我阿里雲外網ip,我就可以通過外網開啟 dashboard了

訪問暴露的服務

curl http://[k8s-master]:9999/api/v1/namespaces/[namespace-name]/services/[service-name]/proxy/[youruri]
//大多數的Kubernetes中的叢集預設會有一個叫default的namespace。
http://阿里雲外網ip:9999/api/v1/namespaces/default/services/wzhi/proxy/ping

相關文章