|
1 |
| -# 使用DevStream的新功能Apps来进行GitOps |
| 1 | +# 使用 DevStream 的新特性 Apps 来实现 GitOps |
2 | 2 |
|
3 |
| -TODO |
| 3 | +## 0 目标 |
| 4 | + |
| 5 | +在本教程中,我们会使用 DevStream 的新特性 应用(Apps),来达到与 [GitOps](gitops.zh.md) 相似的效果。但它的配置更短,来展示 应用 的强大能力。如果你还没有读过原始的 GitOps 最佳实践,可以先点击前面的链接。 |
| 6 | + |
| 7 | +我们会创建两个应用程序(一个基于 Python,另一个是 Go 语言),并且创建共用的 CI/CD 流水线,即两个应用程序都会通过 Argo CD 来部署,就像前面的 GitOps 做到的那样。 |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 1 概览 |
| 12 | + |
| 13 | +DevStream 使用 应用 的概念来创建带有代码脚手架和 GitHub Actions 流水线的仓库,并安装 Argo CD,最后使用 Arco CD 部署应用程序。 |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## 2 创建配置文件 |
| 18 | + |
| 19 | +为本教程创建临时工作目录: |
| 20 | + |
| 21 | +```bash |
| 22 | +mkdir test |
| 23 | +cd test/ |
| 24 | +``` |
| 25 | + |
| 26 | +下载 dtm(详见 [GitOps](./gitops.zh.md) 最佳实践,如果你还没有下载过的话) |
| 27 | + |
| 28 | +运行以下命令以生成配置文件: |
| 29 | + |
| 30 | +```bash |
| 31 | +./dtm show config --template=apps > config.yaml |
| 32 | +``` |
| 33 | + |
| 34 | +替换下面命令中的双引号内里面的内容,并运行,以设置环境变量: |
| 35 | + |
| 36 | +```shell |
| 37 | +export GITHUB_USER="<YOUR_GITHUB_PERSONAL_ACCESS_TOKEN_HERE>" |
| 38 | +export DOCKERHUB_USERNAME="<YOUR_DOCKER_HUB_USER_NAME_HERE>" |
| 39 | +``` |
| 40 | + |
| 41 | +现在我们就可以使用前面设置的环境变量来更新配置文件了: |
| 42 | + |
| 43 | +=== "基于 **macOS** 或 **FreeBSD** 的系统" |
| 44 | + |
| 45 | + ```shell |
| 46 | + sed -i.bak "s@YOUR_GITHUB_USER@${GITHUB_USER}@g" config.yaml |
| 47 | + sed -i.bak "s@YOUR_DOCKERHUB_USER@${DOCKERHUB_USERNAME}@g" config.yaml |
| 48 | + ``` |
| 49 | + |
| 50 | +=== "**GNU** Linux 用户" |
| 51 | + |
| 52 | + ```shell |
| 53 | + sed -i "s@YOUR_GITHUB_USERNAME_CASE_SENSITIVE@${GITHUB_USER}@g" config.yaml |
| 54 | + sed -i "s@YOUR_DOCKER_USERNAME@${DOCKERHUB_USERNAME}@g" config.yaml |
| 55 | + ``` |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## 3 初始化(Init)和应用(Apply) |
| 60 | + |
| 61 | +运行下面这个命令,以根据配置文件来自动下载对应的插件: |
| 62 | + |
| 63 | +```bash |
| 64 | +./dtm init -f config.yaml |
| 65 | +``` |
| 66 | + |
| 67 | +再运行 apply 命令: |
| 68 | + |
| 69 | +```bash |
| 70 | +./dtm apply -f config.yaml -y |
| 71 | +``` |
| 72 | + |
| 73 | +你会看到类似下面的输出: |
| 74 | + |
| 75 | +```bash |
| 76 | +tiexin@mbp ~/work/devstream-io/test $ ./dtm apply -f config.yaml -y |
| 77 | +2022-12-16 14:30:27 ℹ [INFO] Apply started. |
| 78 | +2022-12-16 14:30:28 ℹ [INFO] Using local backend. State file: devstream.state. |
| 79 | +2022-12-16 14:30:28 ℹ [INFO] Tool (helm-installer/argocd) found in config but doesn't exist in the state, will be created. |
| 80 | +2022-12-16 14:30:28 ℹ [INFO] Tool (repo-scaffolding/myapp1) found in config but doesn't exist in the state, will be created. |
| 81 | +2022-12-16 14:30:28 ℹ [INFO] Tool (repo-scaffolding/myapp2) found in config but doesn't exist in the state, will be created. |
| 82 | +2022-12-16 14:30:28 ℹ [INFO] Tool (github-actions/myapp1) found in config but doesn't exist in the state, will be created. |
| 83 | +2022-12-16 14:30:28 ℹ [INFO] Tool (argocdapp/myapp1) found in config but doesn't exist in the state, will be created. |
| 84 | +2022-12-16 14:30:28 ℹ [INFO] Tool (github-actions/myapp2) found in config but doesn't exist in the state, will be created. |
| 85 | +2022-12-16 14:30:28 ℹ [INFO] Tool (argocdapp/myapp2) found in config but doesn't exist in the state, will be created. |
| 86 | +2022-12-16 14:30:28 ℹ [INFO] Start executing the plan. |
| 87 | +2022-12-16 14:30:28 ℹ [INFO] Changes count: 7. |
| 88 | +2022-12-16 14:30:28 ℹ [INFO] -------------------- [ Processing progress: 1/7. ] -------------------- |
| 89 | +2022-12-16 14:30:28 ℹ [INFO] Processing: (helm-installer/argocd) -> Create ... |
| 90 | +2022-12-16 14:30:29 ℹ [INFO] Filling default config with instance: argocd. |
| 91 | +2022-12-16 14:30:29 ℹ [INFO] Creating or updating helm chart ... |
| 92 | +... (略) |
| 93 | +... (略) |
| 94 | +2022-12-16 14:32:09 ℹ [INFO] -------------------- [ Processing progress: 7/7. ] -------------------- |
| 95 | +2022-12-16 14:32:09 ℹ [INFO] Processing: (argocdapp/myapp2) -> Create ... |
| 96 | +2022-12-16 14:32:19 ℹ [INFO] application.argoproj.io/myapp2 created |
| 97 | +2022-12-16 14:32:19 ✔ [SUCCESS] Tool (argocdapp/myapp2) Create done. |
| 98 | +2022-12-16 14:32:19 ℹ [INFO] -------------------- [ Processing done. ] -------------------- |
| 99 | +2022-12-16 14:32:19 ✔ [SUCCESS] All plugins applied successfully. |
| 100 | +2022-12-16 14:32:19 ✔ [SUCCESS] Apply finished. |
| 101 | +``` |
| 102 | +
|
| 103 | +--- |
| 104 | +
|
| 105 | +## 4 查看结果 |
| 106 | +
|
| 107 | +让我们来继续看看 `apply` 命令的结果: |
| 108 | +
|
| 109 | +与我们在[GitOps](./gitops.zh.md)最佳实践中所做的类似,我们可以检查 dtm 是否为两个应用程序创建了代码仓库并为其创建了 CI 流水线,同时安装了 Argo CD,而且两个应用程序都使用了 Argo CD 来部署到了 Kubernetes 集群中。 |
| 110 | +
|
| 111 | +--- |
| 112 | +
|
| 113 | +## 5 清理 |
| 114 | +
|
| 115 | +运行: |
| 116 | +
|
| 117 | +```bash |
| 118 | +./dtm delete -f config.yaml -y |
| 119 | +``` |
| 120 | +
|
| 121 | +你会看到类似下面的输出: |
| 122 | +
|
| 123 | +```bash |
| 124 | +2022-12-16 14:34:30 ℹ [INFO] Delete started. |
| 125 | +2022-12-16 14:34:31 ℹ [INFO] Using local backend. State file: devstream.state. |
| 126 | +2022-12-16 14:34:31 ℹ [INFO] Tool (github-actions/myapp1) will be deleted. |
| 127 | +2022-12-16 14:34:31 ℹ [INFO] Tool (argocdapp/myapp1) will be deleted. |
| 128 | +2022-12-16 14:34:31 ℹ [INFO] Tool (github-actions/myapp2) will be deleted. |
| 129 | +2022-12-16 14:34:31 ℹ [INFO] Tool (argocdapp/myapp2) will be deleted. |
| 130 | +2022-12-16 14:34:31 ℹ [INFO] Tool (repo-scaffolding/myapp1) will be deleted. |
| 131 | +2022-12-16 14:34:31 ℹ [INFO] Tool (repo-scaffolding/myapp2) will be deleted. |
| 132 | +2022-12-16 14:34:31 ℹ [INFO] Tool (helm-installer/argocd) will be deleted. |
| 133 | +2022-12-16 14:34:31 ℹ [INFO] Start executing the plan. |
| 134 | +2022-12-16 14:34:31 ℹ [INFO] Changes count: 7. |
| 135 | +2022-12-16 14:34:31 ℹ [INFO] -------------------- [ Processing progress: 1/7. ] -------------------- |
| 136 | +2022-12-16 14:34:31 ℹ [INFO] Processing: (github-actions/myapp1) -> Delete ... |
| 137 | +2022-12-16 14:34:33 ℹ [INFO] Prepare to delete 'github-actions_myapp1' from States. |
| 138 | +2022-12-16 14:34:33 ✔ [SUCCESS] Tool (github-actions/myapp1) delete done. |
| 139 | +... (略) |
| 140 | +... (略) |
| 141 | +2022-12-16 14:34:40 ✔ [SUCCESS] Tool (helm-installer/argocd) delete done. |
| 142 | +2022-12-16 14:34:40 ℹ [INFO] -------------------- [ Processing done. ] -------------------- |
| 143 | +2022-12-16 14:34:40 ✔ [SUCCESS] All plugins deleted successfully. |
| 144 | +2022-12-16 14:34:40 ✔ [SUCCESS] Delete finished. |
| 145 | +``` |
| 146 | +
|
| 147 | +后面我们就能删除创建的所有文件了: |
| 148 | +
|
| 149 | +```bash |
| 150 | +cd ../ |
| 151 | +rm -rf test/ |
| 152 | +rm -rf ~/.devstream/ |
| 153 | +``` |
0 commit comments