preface

It’s 2020, and I don’t know what other programmers are still packing, testing, uploading, deploying, and other mechanical things every time a release goes live. The idea of DevOps was born to automate these scenarios and avoid repetitive work.

As for how best to implement the concept of DevOps, there are already mature solutions in the market based on the size of the company and the size of the project. But in keeping with the principle of “no end to the grind” and as a research hobby, here’s how I built my own DevOps system at the lowest cost, without relying on any third party platforms!

I put the resource files and one-click deployment scripts needed for this article on Github open source, fork these are yours ~

Github:github.com/otokaze/Dev…

Quick start

Let me briefly explain the basic behavior that a Devops system itself should have:

Coding >> Commit >> Hooks >> Building >> Unit Testing >> Image Pushing >> Deploying

Obviously, Devops is made up of multiple systems. Each component carried on the division of labor and cooperation, pipeline-type operation, and finally achieved the result we want. Therefore, I selected a few lightweight and low-resource open source projects in the open source community to form part of our system.

Gitea

As a private GIT repository, Gitea is lightweight enough to hold all the functionality in a single binary file of less than 60MB. It is also due to GO’s high performance compiler that it is born with extremely low memory and CPU usage at runtime, which is the best reason for me to choose it.

The deployment of
docker-compose -f ./Gitea/docker-compose.yaml up -d
Copy the code

Registry

Registry is currently the only choice as a private Docker Registry, which is also Docker’s official open source private Registry solution. Besides, you have no choice. : (

The deployment of
docker-compose -f ./Registry/docker-compose.yaml up -d
Copy the code

Drone

Now, we also need a CI/CD platform to get things moving!

Also thanks to the Go language, Drone is not only easy to deploy, but more importantly: it takes up less than 20MB of memory on the server for steady operation. Love love ~

The deployment of
docker-compose -f ./Drone/docker-compose.yaml up -d
Copy the code
Example

In addition, in order for Drone to work in coordination with our own Gitea and tell it what needs to be done after the code is submitted, we also need a YAML description of its “work items.”

Here is another of my open source tests to view Docker container load balancing and IP&hostname information as a small tool for Demo.

Github:github.com/otokaze/you…

---
kind: pipeline
type: docker
name: default

platform:
  os: linux
  arch: amd64

steps:
- name: linter
  image: alpine/git
  commands:
  - change=$(git diff origin/master $DRONE_COMMIT ./CHANGELOG.md 2> /dev/null) && code=0 || code=$? - if [ ! $code -eq 0 ]; then echo 'CHANGELOG.md not fount.'; exit $code; fi - if [ -z "$change" ]; then echo 'CHANGELOG.md no change.'; exit 1; fi- name: builder
  image: Golang: 1.13.8
  environment:
    GOOS: linux
    GOARCH: amd64
    CGO_ENABLED: 0
  commands:
  - go build -o yourip
  - go test

- name: tagger
  image: alpine
  commands:
  - tags=$(grep -E -o  v[0-9]+\.[0-9]+\.[0-9]+ CHANGELOG.md | head -1 | sed s/v/latest,/g) - if [ -z $tags ]; then echo 'No version found in CHANGELOG.md'; exit 1; else echo $tags > .tags; fi  when:
    event:
    - push

- name: pushing
  image: plugins/docker
  settings:
    username:
      from_secret: DOCKER_REGISTRY_USERNAME
    password:
      from_secret: DOCKER_REGISTRY_PASSWORD
    repo: registry.otokaze.cn/yourip
    registry: registry.otokaze.cn
  when:
    event:
    - push

- name: deploying
  image: curlimages/curl
  environment:
    DEPLOY_API: https://swarm.otokaze.cn/api/services/yourip/redeploy
    TOKEN:
      from_secret: DOCKER_SWARMPIT_TOKEN
  commands:
  - code=$(curl -XPOST -s -w %{http_code} "$DEPLOY_API? tag=latest" -H 'Content-Type:application/json' -H "authorization:$TOKEN")
  - if [[ $code = = "" || $code -lt 200 ]]. then echo "redeploy failed. HTTP_CODE=${code}"; exit 1; fi
  when:
    event:
    - push

trigger:
  branch:
  - master
  event:
  - pull_request
  - push

.
Copy the code

Swarmpit

Finally, I need a container orchestration engine to manage my services, smooth upgrades, horizontal scaling, rollback publishing, and so on.

Docker swarm was not a mainstream choice for container orcheography, but as a small user, I didn’t need the complex features of K8S, so Docker swarm was the best choice for me because it was simple enough and very easy to use.

The deployment of
docker stack deploy -c ./Swarmpit/docker-compose.yml swarmpit
Copy the code

You’re not gonna have one?

  • Gitea: git.otokaze.cn
  • Drone: drone.otokaze.cn
  • Registry: registry.otokaze.cn
  • Swarmpit: swarm.otokaze.cn