GitOps

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

    1 太长不看版:Demo 演示

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

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

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

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


    2 概览

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

    1. github-actions
    2. argocdapp

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


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

    Bash

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

    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

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

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

    4 配置文件

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

    Bash

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

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

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

    5 环境变量

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

    Bash


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

    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 (argocdapp/default) found in config but doesn't exist in the state, will be created.
    6. 2022-12-05 17:49:49 [INFO] Start executing the plan.
    7. 2022-12-05 17:49:49 [INFO] Changes count: 4.
    8. ... (略)
    9. ... (略)
    10. 2022-12-05 17:51:51 [INFO] Processing: (argocdapp/default) -> Create ...
    11. 2022-12-05 17:51:52 [INFO] application.argoproj.io/helloworld created
    12. 2022-12-05 17:51:52 [SUCCESS] Tool (argocdapp/default) Create done.
    13. 2022-12-05 17:51:52 [INFO] -------------------- [ Processing done. ] --------------------
    14. 2022-12-05 17:51:52 [SUCCESS] All plugins applied successfully.
    15. 2022-12-05 17:51:52 [SUCCESS] Apply finished.

    8 查看结果

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

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

    GitOps - 图2

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

    Bash

    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!”。大功告成!


    运行:

    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.

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

    Bash