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

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

作者:互联网

Go-Zero 官方短链项目教程:快速构建高并发微服务

关于 go-zero,大家可以看文档。为少认为它是中国目前最好用的 golang 微服务框架。

完整的 Go-Zero ShortUrl DevOps Demo

准备工作

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

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

部署 Mysql、Redis、Etcd。

部署 Drone、Drone-Runner-Kube

开始探索

准备 DevOps 部署相关配置

Dockerfile.alpine.base

FROM alpine:3.12

RUN addgroup -S app \
    && adduser -S -g app app \
    && apk --no-cache add \
    ca-certificates curl netcat-openbsd

Dockerfile.base

FROM golang:1.15-alpine

RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct
RUN mkdir -p /shorturl/

WORKDIR /shorturl
COPY go.mod go.mod
RUN go mod download

Dockerfile.prod.rpc

### shorturl:base
FROM hub.your-domain.com/library/shorturl:base as builder

WORKDIR /shorturl-rpc
COPY . .
RUN CGO_ENABLED=0 go build -a -o bin/shorturl-rpc rpc/transform/*.go

### shorturl-alpine:base
FROM hub.your-domain.com/library/shorturl-alpine:base

LABEL maintainer="为少"
WORKDIR /home/app
COPY --from=builder /shorturl-rpc/bin/shorturl-rpc .
COPY ./rpc/transform/etc ./rpc/transform/etc
RUN chown -R app:app ./
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
USER app
CMD ["./shorturl-rpc", "-f", "rpc/transform/etc/transform.yaml"]

Dockerfile.prod.api

### shorturl:base
FROM hub.your-domain.com/library/shorturl:base as builder

WORKDIR /shorturl-api
COPY . .
RUN CGO_ENABLED=0 go build -a -o bin/shorturl-api api/*.go

### shorturl-alpine:base
FROM hub.your-domain.com/library/shorturl-alpine:base

LABEL maintainer="为少"
WORKDIR /home/app
COPY --from=builder /shorturl-api/bin/shorturl-api .
COPY ./api/etc ./api/etc
RUN chown -R app:app ./
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
USER app
CMD ["./shorturl-api", "-f", "api/etc/shorturl-api.yaml"]

shorturl-api-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: shorturl-api
data:
  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: v1
kind: ConfigMap
metadata:
  name: shorturl-transform-rpc
data:
  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: pipeline
type: kubernetes
name: ShortUrl(transform.rpc)

steps:

  - name: 更新 Charts(transform.rpc)
    image: busybox
    commands:
      - 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 Image
    image: plugins/docker
    settings:
      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) -> K8S
    image: pelotech/drone-helm3
    settings:
      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: true

trigger:
  branch:
    - main
---
kind: pipeline
type: kubernetes
name: ShortUrl(shorturl-api)

steps:

  - name: 更新 Charts(shorturl-api)
    image: busybox
    commands:
      - 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 Image
    image: plugins/docker
    settings:
      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) -> K8S
    image: pelotech/drone-helm3
    settings:
      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: true

trigger:
  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 中的键值对

我是为少。

微信:uuhells123。

公众号:黑客下午茶。

谢谢点赞支持

标签:CI,Kubernetes,yaml,上云,shorturl,rpc,api,helm,k8s
来源: https://www.cnblogs.com/hacker-linner/p/14219662.html