Skip to content

Commit 5b0960d

Browse files
authored
Merge pull request #1392 from aFlyBird0/docs-bestpractice-gitlabci-argocd-python
docs: use case gitops-python-flask-gitlab-apps
2 parents a036687 + 8189712 commit 5b0960d

File tree

5 files changed

+314
-2
lines changed

5 files changed

+314
-2
lines changed

docs/use-cases/gitops-python-flask/4-gitlab-dtm-apps.zh.md

Lines changed: 314 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,315 @@
1-
# GitLab + GitLab CI with DTM Apps
1+
# 用 DevStream 搭建 Gitlab CI + Argo CD 工具链,管理 Python Flask 项目(GitLab + GitLab CI with DTM Apps)
2+
3+
## 0 目标
4+
5+
在本教程中,我们将尝试通过 DevStream 来实现以下目标:
6+
7+
1. 使用 Docker 安装 GitLab,作为代码仓库(如果你的服务器上已经安装了 GitLab,可以跳过这一步);
8+
2. 在 GitLab 上创建一个 Python Web 应用程序仓库,基于 [Flask](https://flask.palletsprojects.com/en/2.2.x/) 框架;
9+
3. 使用 GitHub CI 为我们创建的仓库设置基本的 CI 流水线;
10+
4._一个已有的 Kubernetes 集群_ 中安装 [Argo CD](https://argo-cd.readthedocs.io/en/stable/) 以实现 GitOps;
11+
5. 创建一个 Argo CD 应用程序,用于部署第 1 步中生成的 Web 应用程序。
12+
13+
> 先决条件:
14+
>
15+
> - [Docker](https://www.docker.com/) (GitLab 使用 Docker 来安装)
16+
> - Kubernetes 集群(Argo CD 安装在 Kubernetes 集群中)
17+
18+
> 如果你想跟着本教程自己尝试一下,但不知道如何在本地启动和运行 Kubernetes 集群,下面的博客(也来自 DevStream)可能会有所帮助:
19+
>
20+
> - [用 Kind 从零开始快速搭建本地 Kubernetes 测试环节](https://blog.devstream.io/posts/%E7%94%A8kind%E9%83%A8%E7%BD%B2k8s%E7%8E%AF%E5%A2%83/)
21+
> - [minikube结合阿里云镜像搭建本地开发测试环境](https://blog.devstream.io/posts/%E4%BD%BF%E7%94%A8minikube%E5%92%8C%E9%98%BF%E9%87%8C%E4%BA%91%E9%95%9C%E5%83%8F%E5%AE%89%E8%A3%85k8s/)
22+
23+
---
24+
25+
## 1 概览
26+
27+
DevStream 将使用下面的插件来实现[第 0 节](#)中描述的目标:
28+
29+
1. [gitlab-ce-docker](../plugins/gitlab-ce-docker.md):用于在 Docker 中安装 GitLab;
30+
2. [repo-scaffolding](../plugins/repo-scaffolding.md): 用于在 GitLab 上创建一个 Python Web 应用程序仓库;
31+
3. [gitlab-ci](../plugins/gitlab-ci.md):用于为我们创建的仓库设置基本的 CI 流水线;
32+
4. [helm-installer](../plugins/helm-installer/helm-installer.md): 用于在 Kubernetes 集群中安装 Argo CD;
33+
5. [argocdapp](../plugins/argocdapp.md): 用于创建一个 Argo CD 应用程序,来部署第 1 步中生成的 Web 应用程序。
34+
35+
我们将分成两个步骤来完成这些目标:
36+
37+
1. 编写一个配置文件,完成工具的安装,GitLab 和 Argo CD;
38+
2. 编写一个配置文件,完成后续流水线的创建、代码仓库的创建,并将其部署到 Argo CD 中。
39+
40+
> 说明:实际上,DevOps 工具的安装和配置可以在同一个配置文件中完成,但 GitLab 较为特殊,需要在安装完成之后由用户手动创建 token,因此我们将工具的安装单独拆分出来了。
41+
42+
## 2 启程:下载 DevStream (`dtm`)
43+
44+
为本教程创建一个临时工作目录:
45+
46+
```bash
47+
mkdir test
48+
cd test/
49+
```
50+
51+
接着,在新创建的目录下,运行下面的命令:
52+
53+
```shell
54+
sh -c "$(curl -fsSL https://download.devstream.io/download.sh)
55+
```
56+
57+
这个脚本会根据你的操作系统来下载对应的 `dtm` 二进制文件,保存到当前目录。然后,赋予其可执行权限。
58+
59+
> 可选:你可以把 `dtm` 移动到 $PATH 环境变量中的某个目录下。例如:`mv dtm /usr/local/bin/`。这样,你就可以直接运行 `dtm` 而不需要再加上 `./` 前缀了。
60+
>
61+
> 更多安装方式详见[安装 dtm](../install.zh.md)。
62+
63+
## 2 安装 GitLab 和 Argo CD
64+
65+
### 2.1 配置准备
66+
67+
创建 `config-tools.yaml` 文件,你可以修改 `vars` 中的值来适应你的环境:
68+
69+
```yaml title="config-tools.yaml"
70+
config:
71+
state:
72+
backend: local
73+
options:
74+
stateFile: devstream-1.state
75+
vars:
76+
gitlabHostname: gitlab.example.com
77+
gitlabSSHPort: 30022
78+
gitlabHttpPort: 80
79+
gitlabHttpsPort: 30443
80+
tools:
81+
- name: gitlab-ce-docker
82+
instanceID: default
83+
dependsOn: []
84+
options:
85+
hostname: [[ gitlabHostname ]]
86+
gitlabHome: /srv/gitlab
87+
sshPort: [[ gitlabSSHPort ]]
88+
httpPort: [[ gitlabHttpPort ]]
89+
httpsPort: [[ gitlabHttpsPort ]]
90+
rmDataAfterDelete: false
91+
imageTag: "rc"
92+
- name: helm-installer
93+
instanceID: argocd
94+
```
95+
96+
并修改服务器的 `/etc/hosts` 文件,添加 `gitlab.example.com` 的域名解析。如果你的服务器 ip 是 44.33.22.11,就可以这样配置:
97+
98+
```text title="/etc/hosts"
99+
44.33.22.11 gitlab.example.com
100+
```
101+
102+
### 2.2 初始化(Init)
103+
104+
运行下面的命令来下载安装 GitLab 和 Argo CD 所需的插件:
105+
106+
```shell
107+
dtm init -f config-tools.yaml -y
108+
```
109+
110+
### 2.3 应用(Apply)
111+
112+
运行下面的命令来通过配置文件来安装 GitLab 和 Argo CD:
113+
114+
```shell
115+
dtm apply -f config-tools.yaml -y
116+
```
117+
118+
你会看到类似于下面的输出:
119+
120+
<script id="asciicast-wxdTxqwycg12UurbRqZpMvSym" src="https://asciinema.org/a/wxdTxqwycg12UurbRqZpMvSym.js" async autoplay=true></script>
121+
122+
### 2.4 检查安装结果
123+
124+
#### 2.4.1 访问 GitLab
125+
126+
你可以在自己的 PC 里配置 `44.33.22.11 gitlab.example.com` 静态域名解析记录,然后在浏览器里通过 `http://gitlab.example.com` 访问到 GitLab(如果浏览器报了:
127+
128+
<figure markdown>
129+
![GitLab 登录界面](./gitlab-apps/gitlab-login.png){ width="1000" }
130+
<figcaption>GitLab 登录界面</figcaption>
131+
</figure>
132+
133+
通过执行如下命令,你可以设置 GitLab 的 root 密码:
134+
135+
```shell title="get GitLab root Password"
136+
docker exec -it gitlab bash # 进入容器
137+
gitlab-rake "gitlab:password:reset" # 执行后按照提示输入用户名 root,回车后输入密码
138+
```
139+
140+
<script id="asciicast-547097" src="https://asciinema.org/a/547097.js" async></script>
141+
142+
拿到 root 密码后,你可以尝试用 root/YOUR_PASSWORD 来登录 GitLab。因为后面你还需要用到 GitLab 的 token,所以这时候你可以顺手先创建一个 token:
143+
144+
<figure markdown>
145+
![GitLab token](./gitlab-apps/gitlab-token.png){ width="1000" }
146+
<figcaption>Generate GitLab token</figcaption>
147+
</figure>
148+
149+
#### 2.4.2 查看 Argo CD
150+
151+
可以看到 Argo CD 已经被安装到了 Kubernetes 的 `argocd` 命名空间中:
152+
153+
```bash
154+
[root@ip-10-18-13-200 devstream]# kubectl get ns
155+
NAME STATUS AGE
156+
argocd Active 36s
157+
default Active 6d4h
158+
kube-node-lease Active 6d4h
159+
kube-public Active 6d4h
160+
kube-system Active 6d4h
161+
[root@ip-10-18-13-200 devstream]# kubectl get pods -n argocd
162+
NAME READY STATUS RESTARTS AGE
163+
argocd-application-controller-0 1/1 Running 0 49s
164+
argocd-applicationset-controller-7f4577c5fd-8z926 1/1 Running 0 49s
165+
argocd-dex-server-7cdb45c7c9-nspgz 1/1 Running 0 49s
166+
argocd-notifications-controller-65b77fb646-phdwh 1/1 Running 0 49s
167+
argocd-redis-577c6c8f5c-nf5xm 1/1 Running 0 49s
168+
argocd-repo-server-7bd9fd899c-7f6cp 1/1 Running 0 49s
169+
argocd-server-6686bbcf68-fms5w 1/1 Running 0 49s
170+
```
171+
172+
## 3 创建、部署应用程序
173+
174+
### 3.1 配置准备
175+
176+
创建 `config-apps.yaml` 文件,你可以修改 `vars` 中的值来适应你的环境(尤其是`dockerhubUser`这个配置):
177+
178+
```yaml title="config-apps.yaml"
179+
config:
180+
state:
181+
backend: local
182+
options:
183+
stateFile: devstream-2.state
184+
vars:
185+
appName: myapp
186+
gitlabURL: http://gitlab.example.com
187+
defaultBranch: main
188+
dockerhubUser: DOCKERHUB_USER
189+
apps:
190+
- name: [[ appName ]]
191+
spec:
192+
language: python
193+
framework: flask
194+
repo:
195+
url: [[ gitlabURL ]]/root/[[ appName ]].git
196+
branch: [[ defaultBranch ]]
197+
token: [[ env GITLAB_TOKEN ]] # use "GITLAB_TOKEN" env var
198+
repoTemplate:
199+
url: https://github.com/devstream-io/dtm-repo-scaffolding-python-flask.git
200+
ci:
201+
- type: template
202+
templateName: ci-pipeline
203+
cd:
204+
- type: argocdapp
205+
pipelineTemplates:
206+
- name: ci-pipeline
207+
type: gitlab-ci
208+
options:
209+
imageRepo:
210+
user: [[ dockerhubUser ]]
211+
password: [[ env DOCKERHUB_TOKEN ]] # use "DOCKERHUB_TOKEN" env var
212+
```
213+
214+
你可能已经注意到了,上面的配置中有形如 `[[ env XXX ]]` 的内容,这表示我们引用了 "XXX" 环境变量来填充配置。所以我们还需要设置如下两个环境变量:
215+
216+
```bash
217+
export GITLAB_TOKEN="YOUR_GITLAB_TOKEN_HERE"
218+
export DOCKERHUB_TOKEN="YOUR_DOCKERHUB_TOKEN_HERE"
219+
```
220+
221+
> 提示:
222+
>
223+
> 如果你不知道如何创建 DockerHub 的 token,可以参考:[Manage access tokens](https://docs.docker.com/docker-hub/access-tokens/)
224+
225+
### 3.2 初始化(Init)
226+
227+
同样地,我们需要下载第二个配置文件中所需的插件,运行:
228+
229+
```bash
230+
dtm init -f config-apps.yaml
231+
```
232+
233+
### 3.3 应用(Apply)
234+
235+
运行:
236+
237+
```bash
238+
dtm apply -f config-apps.yaml -y
239+
```
240+
241+
你会看到类似下面的输出:
242+
243+
<script id="asciicast-547096" src="https://asciinema.org/a/547096.js" async></script>
244+
245+
### 3.4 查看结果
246+
247+
#### 3.4.1 查看 在 GitLab 上创建的 Flask 仓库
248+
249+
<figure markdown>
250+
![Flask 仓库](./gitlab-apps/repo-scaffolding.png){ width="1000" }
251+
<figcaption>Flask 仓库</figcaption>
252+
</figure>
253+
254+
#### 3.4.2 基于 GitLab CI 的 CI 流水线
255+
256+
通过浏览器访问 `http://gitlab.example.com`,依次点击 `CI/CD``Pipelines`
257+
258+
<figure markdown>
259+
![GitLab CI 概览](./gitlab-apps/gitlabci-1.png){ width="1000" }
260+
<figcaption>GitLab CI 概览</figcaption>
261+
</figure>
262+
263+
#### 3.4.3 基于 Argo CD 的持续交付/部署
264+
265+
CI 流水线已经构建了一个 Docker 镜像并推送到了 Dockerhub,而 DevStream 创建的 Argo CD 应用也部署了这个应用:
266+
267+
```bash
268+
[root@ip-10-18-13-200 devstream]# kubectl get deployment -n default
269+
NAME READY UP-TO-DATE AVAILABLE AGE
270+
myapp 1/1 1 1 101s
271+
[root@ip-10-18-13-200 devstream]# kubectl get pods -n default
272+
NAME READY STATUS RESTARTS AGE
273+
myapp-b65774f56-8cmjc 1/1 Running 0 106s
274+
[root@ip-10-18-13-200 devstream]# kubectl get services -n default
275+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
276+
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d
277+
myapp ClusterIP 10.101.148.66 <none> 8080/TCP 110s
278+
```
279+
280+
我们可以通过端口转发来访问这个应用:
281+
282+
```bash
283+
kubectl port-forward -n default svc/myapp 8080:8080
284+
```
285+
286+
在浏览器中访问 `localhost:8080`,你可以看到应用返回了一个 "Hello, World!"。大功告成!
287+
288+
## 4 清理
289+
290+
### 4.1 删除 Web 应用
291+
292+
运行:
293+
294+
```bash
295+
dtm delete -f config-apps.yaml -y
296+
```
297+
298+
### 4.2 删除 GitLab 和 Argo CD
299+
300+
运行:
301+
302+
```bash
303+
dtm delete -f config-tools.yaml -y
304+
```
305+
306+
### 4.3 删除其他文件
307+
308+
```bash
309+
cd ../
310+
rm -rf test/
311+
rm -rf ~/.devstream/
312+
```
313+
314+
2315
3-
TODO
265 KB
Loading
485 KB
Loading
121 KB
Loading
588 KB
Loading

0 commit comments

Comments
 (0)