GitOps

0 目标

在本教程中,我们将尝试通过 DevStream 来实现以下目标:

  1. 创建一个 Python Web 应用程序仓库,基于 Flask 框架;
  2. 使用 GitHub Actions 为我们创建的仓库设置基本的 CI 流水线;
  3. 一个已有的 Kubernetes 集群 中安装 Argo CD 以实现 GitOps;
  4. 创建一个 Argo CD 应用程序,用于部署第 1 步中生成的 Web 应用程序。

注意:

在第 3 步中,Argo CD 安装在一个已有的 Kubernetes 集群中。DevStream 不配置基础设施,例如 Kubernetes 集群。

如果你想跟着本教程自己尝试一下,但不知道如何在本地启动和运行 Kubernetes 集群,下面的博客(也来自 DevStream)可能会有所帮助:


1 太长不看版:Demo 演示

如果你想看看 GitOps 的实际运行效果,可以看看下面的视频演示:

这个演示录制于 DevStream 的旧版本,配置文件略有不同,但你还是能从中领略 DevStream GitOps 流程的魅力与要点。我们会尽快更新 DevStream 的最新版本的视频演示,敬请期待~

对于中文读者,可以看看这个:

光看完全不尽兴吧?跟着后面的步骤一起试一试吧!


2 概览

DevStream 将使用下面的插件来实现第 0 节中描述的目标:

  1. repo-scaffolding
  2. github-actions
  3. helm-installer
  4. argocdapp

不过,你不需要担心这些插件,因为 DevStream 会帮你自动管理它们。


3 启程:下载 DevStream (dtm)

为本教程创建一个临时工作目录:

Bash

  1. mkdir test
  2. cd test/

接着,在新创建的目录下,运行下面的命令:

Bash

  1. sh -c "$(curl -fsSL https://download.devstream.io/download.sh)"

这个脚本会根据你的操作系统来下载对应的 dtm 二进制文件。然后,赋予其可执行权限。

如果你执行 ls 命令,你会看到 dtm 二进制文件已经被下载下来了:

Bash

  1. tiexin@mbp ~/work/devstream-io/test $ ls
  2. dtm

然后,出于测试目的,我们可以尝试运行它,你会看到类似下面的输出:

Bash

  1. tiexin@mbp ~/work/devstream-io/test $ ./dtm
  2. DevStream is an open-source DevOps toolchain manager
  3. ###### #####
  4. # # ###### # # # # ##### ##### ###### ## # #
  5. # # # # # # # # # # # # ## ##
  6. # # ##### # # ##### # # # ##### # # # ## #
  7. # # # # # # # ##### # ###### # #
  8. # # # # # # # # # # # # # # #
  9. ###### ###### ## ##### # # # ###### # # # #
  10. Usage:
  11. dtm [command]
  12. Available Commands:
  13. apply Create or update DevOps tools according to DevStream configuration file
  14. completion Generate the autocompletion script for dtm for the specified shell
  15. delete Delete DevOps tools according to DevStream configuration file
  16. destroy Destroy DevOps tools deployment according to DevStream configuration file & state file
  17. develop Develop is used for develop a new plugin
  18. help Help about any command
  19. init Download needed plugins according to the config file
  20. list This command only supports listing plugins now
  21. show Show is used to print plugins' configuration templates or status
  22. upgrade Upgrade dtm to the latest release version
  23. verify Verify DevOps tools according to DevStream config file and state
  24. version Print the version number of DevStream
  25. Flags:
  26. --debug debug level log
  27. -h, --help help for dtm
  28. Use "dtm [command] --help" for more information about a command.

可选:你可以把 dtm 移动到 $PATH 环境变量中的某个目录下。例如:mv dtm /usr/local/bin/。这样,你就可以直接运行 dtm 而不需要再加上 ./ 前缀了。

更多安装方式详见安装 dtm


4 配置文件

运行以下命令来生成 gitops 的模板配置文件 config.yaml

Bash

  1. ./dtm show config -t gitops > config.yaml

按需修改 config.yaml 文件中的 vars 部分。记得修改 githubUserdockerUser 的值为你自己的用户名。

在上面的例子中,我把这些变量设置成了下面的值:

变量例子说明
githubUserIronCore864大小写敏感,请改成你的 GitHub 用户名
dockerUserironcore864大小写敏感,请改成你的 DockerHub 用户名

5 环境变量

我们还需要设置以下环境变量:

Bash

  1. export GITHUB_TOKEN="YOUR_GITHUB_TOKEN_HERE"
  2. export IMAGE_REPO_PASSWORD="YOUR_DOCKERHUB_TOKEN_HERE"

提示: 如果你不知道如何创建这两个 token,可以参考:


6 初始化(Init)

运行以下命令,以根据配置文件自动下载所需插件:

Bash

  1. ./dtm init -f config.yaml

你会看到类似下面的输出:

Bash

  1. 2022-12-05 17:46:01 [INFO] Using dir </Users/tiexin/.devstream/plugins> to store plugins.
  2. 2022-12-05 17:46:01 [INFO] -------------------- [ repo-scaffolding-darwin-arm64_0.10.1 ] --------------------
  3. ... (略)
  4. ... (略)
  5. 2022-12-05 17:46:51 [SUCCESS] Initialize finished.

7 应用(Apply)

运行:

Bash

  1. ./dtm apply -f config.yaml -y

你会看到类似下面的输出:

Text Only

  1. 2022-12-05 17:49:49 [INFO] Apply started.
  2. 2022-12-05 17:49:49 [INFO] Using local backend. State file: devstream.state.
  3. 2022-12-05 17:49:49 [INFO] Tool (repo-scaffolding/myapp) found in config but doesn't exist in the state, will be created.
  4. 2022-12-05 17:49:49 ℹ [INFO] Tool (helm-installer/argocd) found in config but doesn't exist in the state, will be created.
  5. 2022-12-05 17:49:49 [INFO] Tool (github-actions/flask) found in config but doesn't exist in the state, will be created.
  6. 2022-12-05 17:49:49 ℹ [INFO] Tool (argocdapp/default) found in config but doesn't exist in the state, will be created.
  7. 2022-12-05 17:49:49 [INFO] Start executing the plan.
  8. 2022-12-05 17:49:49 [INFO] Changes count: 4.
  9. ... (略)
  10. ... (略)
  11. 2022-12-05 17:51:51 [INFO] -------------------- [ Processing progress: 4/4. ] --------------------
  12. 2022-12-05 17:51:51 [INFO] Processing: (argocdapp/default) -> Create ...
  13. 2022-12-05 17:51:52 [INFO] application.argoproj.io/helloworld created
  14. 2022-12-05 17:51:52 [SUCCESS] Tool (argocdapp/default) Create done.
  15. 2022-12-05 17:51:52 [INFO] -------------------- [ Processing done. ] --------------------
  16. 2022-12-05 17:51:52 [SUCCESS] All plugins applied successfully.
  17. 2022-12-05 17:51:52 [SUCCESS] Apply finished.

8 查看结果

让我们来看看 apply 命令的结果。

8.1 GitHub 仓库

DevStream 已经通过 repo-scaffolding 插件自动创建了一个仓库:

GitOps - 图1

8.2 基于 GitHub Actions 的 CI 流水线

GitHub Actions 流水线已经被创建并运行:

GitOps - 图2

8.3 Argo CD 的安装

Argo CD 已经被安装到了 Kubernetes 集群中:

Bash

  1. tiexin@mbp ~/work/devstream-io/test $ kubectl get namespaces
  2. NAME STATUS AGE
  3. argocd Active 5m42s
  4. default Active 6m28s
  5. kube-node-lease Active 6m29s
  6. kube-public Active 6m29s
  7. kube-system Active 6m29s
  8. local-path-storage Active 6m25s
  9. tiexin@mbp ~/work/devstream-io/test $ kubectl get pods -n argocd
  10. NAME READY STATUS RESTARTS AGE
  11. argocd-application-controller-0 1/1 Running 0 5m43s
  12. argocd-applicationset-controller-66687659f-dsrtd 1/1 Running 0 5m43s
  13. argocd-dex-server-6944757486-clshl 1/1 Running 0 5m43s
  14. argocd-notifications-controller-7944945879-b9878 1/1 Running 0 5m43s
  15. argocd-redis-7887bbdbbb-xzppj 1/1 Running 0 5m43s
  16. argocd-repo-server-d4f5cc7cb-8gj24 1/1 Running 0 5m43s
  17. argocd-server-5bb75c4bd9-g948r 1/1 Running 0 5m43s

8.4 使用 Argo CD 持续部署

CI 流水线已经构建了一个 Docker 镜像并推送到了 Dockerhub,而 DevStream 创建的 Argo CD 应用也部署了这个应用:

Bash

  1. tiexin@mbp ~/work/devstream-io/test $ kubectl get deployment -n default
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. helloworld 1/1 1 1 5m16s
  4. tiexin@mbp ~/work/devstream-io/test $ kubectl get pods -n default
  5. NAME READY STATUS RESTARTS AGE
  6. helloworld-69b5586b94-wjwd9 1/1 Running 0 5m18s
  7. tiexin@mbp ~/work/devstream-io/test $ kubectl get services -n default
  8. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  9. helloworld ClusterIP 10.96.73.97 <none> 80/TCP 5m27s
  10. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8m2s

我们可以通过端口转发来访问这个应用:

Bash

  1. kubectl port-forward -n default svc/helloworld 8080:80

在浏览器中访问 localhost:8080,你可以看到应用返回了一个 “Hello, World!”。大功告成!


9 清理

运行:

Bash

  1. ./dtm delete -f config.yaml -y

你会看到如下的输出:

Bash

  1. 2022-12-05 17:59:25 [INFO] Delete started.
  2. 2022-12-05 17:59:26 [INFO] Using local backend. State file: devstream.state.
  3. 2022-12-05 17:59:26 [INFO] Tool (argocdapp/default) will be deleted.
  4. 2022-12-05 17:59:26 [INFO] Tool (github-actions/flask) will be deleted.
  5. 2022-12-05 17:59:26 [INFO] Tool (repo-scaffolding/myapp) will be deleted.
  6. 2022-12-05 17:59:26 [INFO] Tool (helm-installer/argocd) will be deleted.
  7. 2022-12-05 17:59:26 [INFO] Start executing the plan.
  8. 2022-12-05 17:59:26 [INFO] Changes count: 4.
  9. ... (略)
  10. ... (略)
  11. 2022-12-05 17:59:35 [INFO] -------------------- [ Processing done. ] --------------------
  12. 2022-12-05 17:59:35 [SUCCESS] All plugins deleted successfully.
  13. 2022-12-05 17:59:35 [SUCCESS] Delete finished.

后面我们就能删除创建的所有文件了:

Bash

  1. cd ../
  2. rm -rf test/
  3. rm -rf ~/.devstream/