6

Podinfo,迷你的 Go 微服务模板

 3 years ago
source link: http://www.cnblogs.com/hacker-linner/p/14318010.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

FbY3Qj.png!mobile

​项目介绍

Podinfo 是一个用 Go 制作的小型 web 应用程序,它展示了在 Kubernetes 中运行微服务的最佳实践。

它已实现的技术指标(截选自官方 README.md ):

7f2IraJ.png!mobile

里面每一项技术指标的实现方式,其实都可以拿出来单独讲好久,相关理论也有好多。

这里我只是讲针对这个项目,我们该如何使用 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"

配置  .vscode  的  launch.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

aIFrMz7.png!mobile

查看首页

http://localhost:9898

NvIjYrR.png!mobile

查看给 Prometheus 的  metrics  API

http://localhost:9898/metrics

BrUvqyM.png!mobile

下断点,发请求调试

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

vUNb2qR.png!mobile

Helm Charts

Podinfo/Charts

  • https://github.com/stefanprodan/podinfo/tree/master/charts/podinfo

因为 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 注解

Refs

笔者修改过的 Podinfo 项目地址

  • https://github.com/Hacker-Linner/podinfo

官方 Podinfo

  • https://github.com/stefanprodan/podinfo


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK