開發與維運

Podinfo,迷你的 Go 微服務模板

項目介紹


Podinfo 是一個用 Go 製作的小型 web 應用程序,它展示了在 Kubernetes 中運行微服務的最佳實踐。

它已實現的技術指標(截選自官方 README.md ):

微信圖片_20220611121512.png

裡面每一項技術指標的實現方式,其實都可以拿出來單獨講好久,相關理論也有好多。

這裡我只是講針對這個項目,我們該如何使用 Docker 去試玩它。

構建容器調試環境


IDE

VSCode + golang/vscode-go

Go 國內加速鏡像

https://learnku.com/go/wikis/38122

編寫 Dockerfile.dev 文件


FROM golang:1.14
WORKDIR /workspace
# copy modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# 阿里雲
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
# cache modules
RUN go mod download
RUN go get github.com/go-delve/delve/cmd/dlv

構建 Image


docker build -f Dockerfile.dev -t podinfo:dev .

編寫 docker-compose.yaml


version: "3.4"
services:
  golang:
    image: podinfo:dev
    command: >
      bash -c "ls -la
      && dlv debug /workspace/cmd/podinfo --headless --log -l 0.0.0.0:2345 --api-version=2"
    volumes:
    - ./:/workspace
    ports:
      - 9898:9898
      - 2345:2345
    security_opt:
      - "seccomp:unconfined"

配置 .vscodelaunch.json


{
  "version": "0.2.0",
  "configurations": [
      {
          "name": "Remote Docker",
          "type": "go",
          "request": "launch",
          "mode": "remote",
          "remotePath":"/workspace",
          "port": 2345,
          "host": "127.0.0.1",
          "program": "${workspaceFolder}",
          "args": [],
          "trace" : "verbose",
          "env" : {}
      }
  ]
}

開始試玩


docker compose 一鍵啟動


docker-compose up

Run Remote Docker


微信圖片_20220611121545.png

查看首頁

http://localhost:9898

微信圖片_20220611121547.png

查看給 Prometheus 的 metrics API


http://localhost:9898/metrics

微信圖片_20220611121614.png

下斷點,發請求調試


curl http://localhost:9898/api/info

微信圖片_20220611121625.png

Helm Charts 

Podinfo/Charts

因為 Podinfo 是一個雲原生項目,所以它的 Helm Charts 的編寫還是值得借鑑和學習的。

當然這裡需要你有一些 K8S 的經驗。

Helm 安裝 Podinfo


$ helm repo add podinfo https://stefanprodan.github.io/podinfo
$ helm upgrade -i my-release podinfo/podinfo

Helm 卸載 Podinfo


$ helm delete my-release

看配置,瞭解 PodInfo 是如何上雲的?


非常值得借鑑

參數 默認值 描述
replicaCount 1 期望的 K8S Pods(也就是代碼在集群中部署幾個實例)
logLevel info

日誌級別:

 debuginfowarnerrorflat 

or panic

backend None 需要調用的後端或者是第三方的 URL(如 Java 後端)
backends [] 需要調用的後端或者是第三方的 URLs(如 Java 後端)
cache None Redis 地址 <host>:<port>
redis.enabled false 是否開啟 Redis 緩存
ui.color #34577c UI 顏色
ui.message None UI 問候消息
ui.logo None UI logo
faults.delay false 隨機 HTTP 響應延遲 0 到 5 秒
faults.error false 1/3 概率的隨機 HTTP 響應錯誤
faults.unhealthy false 設置後,永遠不會達到健康狀態
faults.unready false 當設置時,永遠不會達到就緒狀態
faults.testFail false 當設置時,helm 測試總是失敗
faults.testTimeout false 當設置時,helm 測試總是包括超時
h2c.enabled false 允許升級到 h2c
image.repository stefanprodan/podinfo 鏡像庫(地址)
image.tag <VERSION> 鏡像 tag
image.pullPolicy IfNotPresent Image 拉取策略
service.enabled true 創建 Kubernetes 服務,使用 Flagger 時應禁用
service.type ClusterIP Kubernetes Service 類型
service.metricsPort 9797 Prometheus 指標端點端口
service.httpPort 9898 Container HTTP 端口
service.externalPort 9898 ClusterIP HTTP 端口
service.grpcPort 9999 ClusterIP gPRC 端口
service.grpcService podinfo gPRC service 名稱
service.nodePort 31198 HTTP 端點的 NodePort
hpa.enabled false

啟用 Kubernetes HPA

(Pod 水平自動伸縮)

hpa.maxReplicas 10 Pods 最大數量
hpa.cpu None 每個 Pod 的目標CPU使用率
hpa.memory None 每個 Pod 的目標內存使用量
hpa.requests None 每個 Pod 每秒目標 HTTP 請求
serviceAccount.enabled false 是否應創建 service account
serviceAccount.name None 要使用的 service account 的名稱,如果未設置且 enabled 為true,則使用 fullname 生成名稱
linkerd.profile.enabled false 創建 Linkerd 服務配置文件
serviceMonitor.enabled false 是否應創建 Prometheus Operator 服務監視器
serviceMonitor.interval 15s Prometheus 抓取間隔
ingress.enabled false 啟用 Ingress
ingress.annotations {} Ingress 註解
ingress.path /* Ingress 路徑
ingress.hosts [] Ingress 接受的 hosts
ingress.tls [] Ingress TLS 配置
resources.requests.cpu 1m Pod CPU 請求
resources.requests.memory 16Mi Pod 內存 請求
resources.limits.cpu None Pod CPU 限制
resources.limits.memory None Pod memory 限制
nodeSelector {} Pod 分配的集群節點標籤(說白了就是固定部署到你指定的機器)
tolerations [] 可容忍的節點汙點列表
affinity None Node/pod 親和力
podAnnotations {} Pod 註解

Leave a Reply

Your email address will not be published. Required fields are marked *