使用 DevStream 的新特性 Apps 来实现 GitOps

0 目标

在本教程中,我们会使用 DevStream 的新特性 应用(Apps),来达到与 GitOps 相似的效果。但它的配置更短,来展示 应用 的强大能力。如果你还没有读过原始的 GitOps 最佳实践,可以先点击前面的链接。

我们会创建两个应用程序(一个基于 Python,另一个是 Go 语言),并且创建共用的 CI/CD 流水线,即两个应用程序都会通过 Argo CD 来部署,就像前面的 GitOps 做到的那样。


1 概览

DevStream 使用 应用 的概念来创建带有代码脚手架和 GitHub Actions 流水线的仓库,并安装 Argo CD,最后使用 Arco CD 部署应用程序。


2 创建配置文件

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

Bash

  1. mkdir test
  2. cd test/

下载 dtm(详见 GitOps 最佳实践,如果你还没有下载过的话)

运行以下命令以生成配置文件:

Bash

  1. ./dtm show config --template=apps > config.yaml

替换下面命令中的双引号内里面的内容,并运行,以设置环境变量:

Bash

  1. export GITHUB_USER="<YOUR_GITHUB_PERSONAL_ACCESS_TOKEN_HERE>"
  2. export DOCKERHUB_USERNAME="<YOUR_DOCKER_HUB_USER_NAME_HERE>"

现在我们就可以使用前面设置的环境变量来更新配置文件了:

基于 macOSFreeBSD 的系统GNU Linux 用户

Bash

  1. sed -i.bak "s@YOUR_GITHUB_USER@${GITHUB_USER}@g" config.yaml
  2. sed -i.bak "s@YOUR_DOCKERHUB_USER@${DOCKERHUB_USERNAME}@g" config.yaml

Bash

  1. sed -i "s@YOUR_GITHUB_USERNAME_CASE_SENSITIVE@${GITHUB_USER}@g" config.yaml
  2. sed -i "s@YOUR_DOCKER_USERNAME@${DOCKERHUB_USERNAME}@g" config.yaml

3 初始化(Init)和应用(Apply)

运行下面这个命令,以根据配置文件来自动下载对应的插件:

Bash

  1. ./dtm init -f config.yaml

再运行 apply 命令:

Bash

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

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

Bash

  1. tiexin@mbp ~/work/devstream-io/test $ ./dtm apply -f config.yaml -y
  2. 2022-12-16 14:30:27 [INFO] Apply started.
  3. 2022-12-16 14:30:28 [INFO] Using local backend. State file: devstream.state.
  4. 2022-12-16 14:30:28 [INFO] Tool (helm-installer/argocd) found in config but doesn't exist in the state, will be created.
  5. 2022-12-16 14:30:28 ℹ [INFO] Tool (repo-scaffolding/myapp1) found in config but doesn't exist in the state, will be created.
  6. 2022-12-16 14:30:28 [INFO] Tool (repo-scaffolding/myapp2) found in config but doesn't exist in the state, will be created.
  7. 2022-12-16 14:30:28 ℹ [INFO] Tool (github-actions/myapp1) found in config but doesn't exist in the state, will be created.
  8. 2022-12-16 14:30:28 [INFO] Tool (argocdapp/myapp1) found in config but doesn't exist in the state, will be created.
  9. 2022-12-16 14:30:28 ℹ [INFO] Tool (github-actions/myapp2) found in config but doesn't exist in the state, will be created.
  10. 2022-12-16 14:30:28 [INFO] Tool (argocdapp/myapp2) found in config but doesn't exist in the state, will be created.
  11. 2022-12-16 14:30:28 ℹ [INFO] Start executing the plan.
  12. 2022-12-16 14:30:28 ℹ [INFO] Changes count: 7.
  13. 2022-12-16 14:30:28 ℹ [INFO] -------------------- [ Processing progress: 1/7. ] --------------------
  14. 2022-12-16 14:30:28 ℹ [INFO] Processing: (helm-installer/argocd) -> Create ...
  15. 2022-12-16 14:30:29 ℹ [INFO] Filling default config with instance: argocd.
  16. 2022-12-16 14:30:29 ℹ [INFO] Creating or updating helm chart ...
  17. ... (略)
  18. ... (略)
  19. 2022-12-16 14:32:09 ℹ [INFO] -------------------- [ Processing progress: 7/7. ] --------------------
  20. 2022-12-16 14:32:09 ℹ [INFO] Processing: (argocdapp/myapp2) -> Create ...
  21. 2022-12-16 14:32:19 ℹ [INFO] application.argoproj.io/myapp2 created
  22. 2022-12-16 14:32:19 ✔ [SUCCESS] Tool (argocdapp/myapp2) Create done.
  23. 2022-12-16 14:32:19 ℹ [INFO] -------------------- [ Processing done. ] --------------------
  24. 2022-12-16 14:32:19 ✔ [SUCCESS] All plugins applied successfully.
  25. 2022-12-16 14:32:19 ✔ [SUCCESS] Apply finished.

4 查看结果

让我们来继续看看 apply 命令的结果:

与我们在GitOps最佳实践中所做的类似,我们可以检查 dtm 是否为两个应用程序创建了代码仓库并为其创建了 CI 流水线,同时安装了 Argo CD,而且两个应用程序都使用了 Argo CD 来部署到了 Kubernetes 集群中。


5 清理

运行:

Bash

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

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

Bash

  1. 2022-12-16 14:34:30 [INFO] Delete started.
  2. 2022-12-16 14:34:31 [INFO] Using local backend. State file: devstream.state.
  3. 2022-12-16 14:34:31 [INFO] Tool (github-actions/myapp1) will be deleted.
  4. 2022-12-16 14:34:31 [INFO] Tool (argocdapp/myapp1) will be deleted.
  5. 2022-12-16 14:34:31 [INFO] Tool (github-actions/myapp2) will be deleted.
  6. 2022-12-16 14:34:31 [INFO] Tool (argocdapp/myapp2) will be deleted.
  7. 2022-12-16 14:34:31 [INFO] Tool (repo-scaffolding/myapp1) will be deleted.
  8. 2022-12-16 14:34:31 [INFO] Tool (repo-scaffolding/myapp2) will be deleted.
  9. 2022-12-16 14:34:31 [INFO] Tool (helm-installer/argocd) will be deleted.
  10. 2022-12-16 14:34:31 [INFO] Start executing the plan.
  11. 2022-12-16 14:34:31 [INFO] Changes count: 7.
  12. 2022-12-16 14:34:31 [INFO] -------------------- [ Processing progress: 1/7. ] --------------------
  13. 2022-12-16 14:34:31 [INFO] Processing: (github-actions/myapp1) -> Delete ...
  14. 2022-12-16 14:34:33 [INFO] Prepare to delete 'github-actions_myapp1' from States.
  15. 2022-12-16 14:34:33 [SUCCESS] Tool (github-actions/myapp1) delete done.
  16. ... (略)
  17. ... (略)
  18. 2022-12-16 14:34:40 [SUCCESS] Tool (helm-installer/argocd) delete done.
  19. 2022-12-16 14:34:40 [INFO] -------------------- [ Processing done. ] --------------------
  20. 2022-12-16 14:34:40 [SUCCESS] All plugins deleted successfully.
  21. 2022-12-16 14:34:40 [SUCCESS] Delete finished.

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

Bash

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