GitOps
- 创建一个 Python Web 应用程序仓库,基于 Flask 框架;
- 使用 GitHub Actions 为我们创建的仓库设置基本的 CI 流水线;
- 在 一个已有的 Kubernetes 集群 中安装 以实现 GitOps;
- 创建一个 Argo CD 应用程序,用于部署第 1 步中生成的 Web 应用程序。
1 太长不看版:Demo 演示
如果你想看看 GitOps 的实际运行效果,可以看看下面的视频演示:
这个演示录制于 DevStream 的旧版本,配置文件略有不同,但你还是能从中领略 DevStream GitOps 流程的魅力与要点。我们会尽快更新 DevStream 的最新版本的视频演示,敬请期待~
对于中文读者,可以看看这个:
光看完全不尽兴吧?跟着后面的步骤一起试一试吧!
2 概览
DevStream 将使用下面的插件来实现第 0 节中描述的目标:
不过,你不需要担心这些插件,因为 DevStream 会帮你自动管理它们。
为本教程创建一个临时工作目录:
Bash
接着,在新创建的目录下,运行下面的命令:
Bash
sh -c "$(curl -fsSL https://download.devstream.io/download.sh)"
这个脚本会根据你的操作系统来下载对应的 dtm
二进制文件。然后,赋予其可执行权限。
如果你执行 ls
命令,你会看到 dtm
二进制文件已经被下载下来了:
Bash
tiexin@mbp ~/work/devstream-io/test $ ls
dtm
然后,出于测试目的,我们可以尝试运行它,你会看到类似下面的输出:
tiexin@mbp ~/work/devstream-io/test $ ./dtm
DevStream is an open-source DevOps toolchain manager
###### #####
# # ###### # # # # ##### ##### ###### ## # #
# # # # # # # # # # # # ## ##
# # ##### # # ##### # # # ##### # # # ## #
# # # # # # # ##### # ###### # #
# # # # # # # # # # # # # # #
Usage:
dtm [command]
Available Commands:
apply Create or update DevOps tools according to DevStream configuration file
completion Generate the autocompletion script for dtm for the specified shell
delete Delete DevOps tools according to DevStream configuration file
destroy Destroy DevOps tools deployment according to DevStream configuration file & state file
develop Develop is used for develop a new plugin
help Help about any command
init Download needed plugins according to the config file
list This command only supports listing plugins now
show Show is used to print plugins' configuration templates or status
upgrade Upgrade dtm to the latest release version
verify Verify DevOps tools according to DevStream config file and state
Flags:
--debug debug level log
-h, --help help for dtm
Use "dtm [command] --help" for more information about a command.
4 配置文件
运行以下命令来生成 gitops 的模板配置文件 config.yaml
。
Bash
./dtm show config -t gitops > config.yaml
按需修改 config.yaml
文件中的 vars
部分。记得修改 githubUser
和 dockerUser
的值为你自己的用户名。
在上面的例子中,我把这些变量设置成了下面的值:
5 环境变量
我们还需要设置以下环境变量:
Bash
运行以下命令,以根据配置文件自动下载所需插件:
Bash
./dtm init -f config.yaml
你会看到类似下面的输出:
Bash
2022-12-05 17:46:01 ℹ [INFO] Using dir </Users/tiexin/.devstream/plugins> to store plugins.
2022-12-05 17:46:01 ℹ [INFO] -------------------- [ repo-scaffolding-darwin-arm64_0.10.1 ] --------------------
... (略)
... (略)
2022-12-05 17:46:51 ✔ [SUCCESS] Initialize finished.
7 应用(Apply)
运行:
Bash
./dtm apply -f config.yaml -y
你会看到类似下面的输出:
Text Only
2022-12-05 17:49:49 ℹ [INFO] Apply started.
2022-12-05 17:49:49 ℹ [INFO] Using local backend. State file: devstream.state.
2022-12-05 17:49:49 ℹ [INFO] Tool (repo-scaffolding/myapp) found in config but doesn't exist in the state, will be created.
2022-12-05 17:49:49 ℹ [INFO] Tool (helm-installer/argocd) found in config but doesn't exist in the state, will be created.
2022-12-05 17:49:49 ℹ [INFO] Tool (argocdapp/default) found in config but doesn't exist in the state, will be created.
2022-12-05 17:49:49 ℹ [INFO] Start executing the plan.
2022-12-05 17:49:49 ℹ [INFO] Changes count: 4.
... (略)
... (略)
2022-12-05 17:51:51 ℹ [INFO] Processing: (argocdapp/default) -> Create ...
2022-12-05 17:51:52 ℹ [INFO] application.argoproj.io/helloworld created
2022-12-05 17:51:52 ✔ [SUCCESS] Tool (argocdapp/default) Create done.
2022-12-05 17:51:52 ℹ [INFO] -------------------- [ Processing done. ] --------------------
2022-12-05 17:51:52 ✔ [SUCCESS] All plugins applied successfully.
2022-12-05 17:51:52 ✔ [SUCCESS] Apply finished.
8 查看结果
DevStream 已经通过 repo-scaffolding
插件自动创建了一个仓库:
GitHub Actions 流水线已经被创建并运行:
Argo CD 已经被安装到了 Kubernetes 集群中:
Bash
CI 流水线已经构建了一个 Docker 镜像并推送到了 Dockerhub,而 DevStream 创建的 Argo CD 应用也部署了这个应用:
Bash
tiexin@mbp ~/work/devstream-io/test $ kubectl get deployment -n default
NAME READY UP-TO-DATE AVAILABLE AGE
helloworld 1/1 1 1 5m16s
tiexin@mbp ~/work/devstream-io/test $ kubectl get pods -n default
NAME READY STATUS RESTARTS AGE
helloworld-69b5586b94-wjwd9 1/1 Running 0 5m18s
tiexin@mbp ~/work/devstream-io/test $ kubectl get services -n default
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
helloworld ClusterIP 10.96.73.97 <none> 80/TCP 5m27s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8m2s
我们可以通过端口转发来访问这个应用:
Bash
kubectl port-forward -n default svc/helloworld 8080:80
在浏览器中访问 localhost:8080
,你可以看到应用返回了一个 “Hello, World!”。大功告成!
运行:
Bash
./dtm delete -f config.yaml -y
你会看到如下的输出:
Bash
2022-12-05 17:59:25 ℹ [INFO] Delete started.
2022-12-05 17:59:26 ℹ [INFO] Using local backend. State file: devstream.state.
2022-12-05 17:59:26 ℹ [INFO] Tool (argocdapp/default) will be deleted.
2022-12-05 17:59:26 ℹ [INFO] Tool (github-actions/flask) will be deleted.
2022-12-05 17:59:26 ℹ [INFO] Tool (repo-scaffolding/myapp) will be deleted.
2022-12-05 17:59:26 ℹ [INFO] Tool (helm-installer/argocd) will be deleted.
2022-12-05 17:59:26 ℹ [INFO] Start executing the plan.
2022-12-05 17:59:26 ℹ [INFO] Changes count: 4.
... (略)
... (略)
2022-12-05 17:59:35 ℹ [INFO] -------------------- [ Processing done. ] --------------------
2022-12-05 17:59:35 ✔ [SUCCESS] All plugins deleted successfully.
后面我们就能删除创建的所有文件了:
Bash