Go-Zero 短链项目 DevOps 实战,利用 Drone CI/CD 打通上云(Kubernetes)迭代流程

mb600becf249518 · · 441 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。


准备工作

我这里直接在 K8S 开发集群中部署相关实例。

生产求稳,建议大家还是买云数据库服务。

部署 Mysql、Redis、Etcd。

部署 Drone、Drone-Runner-Kube

开始探索

准备 DevOps 部署相关配置

Dockerfile.alpine.base

FROM alpine:3.12RUN addgroup -S app \
    && adduser -S -g app app \
    && apk --no-cache add \
    ca-certificates curl netcat-openbsd复制代码

Dockerfile.base

FROM golang:1.15-alpineRUN go env -w GO111MODULE=onRUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,directRUN mkdir -p /shorturl/WORKDIR /shorturlCOPY go.mod go.modRUN go mod download复制代码

Dockerfile.prod.rpc

### shorturl:baseFROM hub.your-domain.com/library/shorturl:base as builderWORKDIR /shorturl-rpcCOPY . .RUN CGO_ENABLED=0 go build -a -o bin/shorturl-rpc rpc/transform/*.go### shorturl-alpine:baseFROM hub.your-domain.com/library/shorturl-alpine:baseLABEL maintainer="为少"WORKDIR /home/appCOPY --from=builder /shorturl-rpc/bin/shorturl-rpc .COPY ./rpc/transform/etc ./rpc/transform/etcRUN chown -R app:app ./RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeRUN echo 'Asia/Shanghai' >/etc/timezoneUSER appCMD ["./shorturl-rpc", "-f", "rpc/transform/etc/transform.yaml"]复制代码

Dockerfile.prod.api

### shorturl:baseFROM hub.your-domain.com/library/shorturl:base as builderWORKDIR /shorturl-apiCOPY . .RUN CGO_ENABLED=0 go build -a -o bin/shorturl-api api/*.go### shorturl-alpine:baseFROM hub.your-domain.com/library/shorturl-alpine:baseLABEL maintainer="为少"WORKDIR /home/appCOPY --from=builder /shorturl-api/bin/shorturl-api .COPY ./api/etc ./api/etcRUN chown -R app:app ./RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeRUN echo 'Asia/Shanghai' >/etc/timezoneUSER appCMD ["./shorturl-api", "-f", "api/etc/shorturl-api.yaml"]复制代码

shorturl-api-configmap.yaml

apiVersion: v1kind: ConfigMapmetadata:
  name: shorturl-apidata:
  shorturl-api.yaml: |-
    Name: shorturl-api
    Host: 0.0.0.0
    Port: 8888
    Transform:
      Etcd:
        Hosts:
          - your-ip:2379
        Key: transform.rpc复制代码

shorturl-rpc-configmap.yaml

apiVersion: v1kind: ConfigMapmetadata:
  name: shorturl-transform-rpcdata:
  transform.yaml: |-
    Name: transform.rpc
    Log:
      Mode: console
    ListenOn: 0.0.0.0:8081
    Etcd:
      Hosts:
      - your-ip:2379
      Key: transform.rpc
    DataSource: root:123456@tcp(your-ip:3306)/shorturl?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
    Table: shorturl
    Cache:
      - Host: your-ip:6379复制代码

.drone.yml

kind: pipelinetype: kubernetesname: ShortUrl(transform.rpc)steps:

  - name: 更新 Charts(transform.rpc)image: busyboxcommands:  - echo $DRONE_COMMIT  - '[ -n "$DRONE_COMMIT" ] && (
          sed -i "s/APP_VERSION/${DRONE_COMMIT}/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
          sed -i "s/SHORTURL/shorturl-transform-rpc/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
          sed -i "s/IMAGE/shorturl-transform-rpc/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/APICONFIGMAP//g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/RPCCONFIGMAP/shorturl-transform-rpc/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/CONTAINERPORT/8081/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/INGRESS/false/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/8080/8081/g" k8s-devops/helm-shorturl/shorturl/templates/NOTES.txt;
        )'  - cat k8s-devops/helm-shorturl/shorturl/Chart.yaml  - cat k8s-devops/helm-shorturl/shorturl/values.yaml

  - name: 构建 transform.rpc Imageimage: plugins/dockersettings:  debug: true  dockerfile: Dockerfile.prod.rpc  repo: hub.your-domain.com/library/shorturl-transform-rpc  tags: ${DRONE_COMMIT}  registry: hub.your-domain.com  username:from_secret: docker_user  password:from_secret: docker_pass

  - name: 上云 HelmV3(transform.rpc) -> K8Simage: pelotech/drone-helm3settings:  helm_command: upgrade  chart: ./k8s-devops/helm-shorturl/shorturl  release: shorturl-transform-rpc  namespace: shorturl  api_server:from_secret: api_server  kubernetes_token:from_secret: k8s_token  skip_tls_verify: truetrigger:
  branch:- main---kind: pipelinetype: kubernetesname: ShortUrl(shorturl-api)steps:

  - name: 更新 Charts(shorturl-api)image: busyboxcommands:  - echo $DRONE_COMMIT  - '[ -n "$DRONE_COMMIT" ] && (
          sed -i "s/APP_VERSION/${DRONE_COMMIT}/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
          sed -i "s/SHORTURL/shorturl-api/g" k8s-devops/helm-shorturl/shorturl/Chart.yaml;
          sed -i "s/IMAGE/shorturl-api/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/APICONFIGMAP/shorturl-api/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/RPCCONFIGMAP//g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/CONTAINERPORT/8888/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/INGRESS/true/g" k8s-devops/helm-shorturl/shorturl/values.yaml;
          sed -i "s/8080/8888/g" k8s-devops/helm-shorturl/shorturl/templates/NOTES.txt;
        )'  - cat k8s-devops/helm-shorturl/shorturl/Chart.yaml  - cat k8s-devops/helm-shorturl/shorturl/values.yaml

  - name: 构建 shorturl-api Imageimage: plugins/dockersettings:  debug: true  dockerfile: Dockerfile.prod.api  repo: hub.your-domain.com/library/shorturl-api  tags: ${DRONE_COMMIT}  registry: hub.your-domain.com  username:from_secret: docker_user  password:from_secret: docker_pass

  - name: 上云 HelmV3(shorturl-api) -> K8Simage: pelotech/drone-helm3settings:  helm_command: upgrade  chart: ./k8s-devops/helm-shorturl/shorturl  release: shorturl-api  namespace: shorturl  api_server:from_secret: api_server  kubernetes_token:from_secret: k8s_token  skip_tls_verify: truetrigger:
  branch:- main复制代码

验证

我这里已经部署好了一个开发测试的 Pod 实例,大家可以试用。

shorten api 调用

# curl -i "https://shorturl.your-domain.com/shorten?url=https://www.your-domain.com"curl -i "https://shorturl.hacker-linner.com/shorten?url=https://www.hacker-linner.com"复制代码

expand api 调用

# curl -i "https://shorturl.your-domain.com/expand?shorten=6d11a1"curl -i "https://shorturl.hacker-linner.com/expand?shorten=6d11a1"复制代码

利用工具查看 Redis 与 Etcd 中的键值对



有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

441 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传