|
1 | 1 | # GitLab + GitLab CI with DTM Apps |
2 | 2 |
|
3 | | -TODO |
| 3 | +## 0 Goal |
| 4 | + |
| 5 | +In this tutorial, we will try to achieve the following goals with DevStream: |
| 6 | + |
| 7 | +1. Use Docker to install GitLab as a code warehouse (if GitLab is already installed on your server, you can skip this step); |
| 8 | +2. Create a Python web application repository on GitLab, based on the [Flask](https://flask.palletsprojects.com/en/2.2.x/) framework; |
| 9 | +3. Use GitHub CI to set up a basic CI pipeline for the warehouse we created; |
| 10 | +4. Install [Argo CD](https://argo-cd.readthedocs.io/en/stable/) in _an existing Kubernetes cluster_ to implement GitOps; |
| 11 | +5. Create an Argo CD application to deploy the web application generated in step 1. |
| 12 | + |
| 13 | +> prerequisites: |
| 14 | +> |
| 15 | +> - [Docker](https://www.docker.com/) (GitLab uses Docker for installation) |
| 16 | +> - Kubernetes cluster (Argo CD is installed in the Kubernetes cluster) |
| 17 | +
|
| 18 | +> If you want to follow along with this tutorial and try it yourself, but don't know how to get a Kubernetes cluster up and running locally, the following blog (also from DevStream) might help: |
| 19 | +> |
| 20 | +> - [Creating a Local Kubernetes Cluster from the Groud Up - a Tutorial of "Kind"](https://blog.devstream.io/posts/creating-a-local-k8s-cluster-with-kind/) |
| 21 | +> - [Getting Started with minikube](https://blog.devstream.io/posts/getting-started-with-minikube/) |
| 22 | +
|
| 23 | +--- |
| 24 | + |
| 25 | +## 1 Overview |
| 26 | + |
| 27 | +DevStream will use the following plugins to achieve the goals described in [Section 0](#): |
| 28 | + |
| 29 | +1. [gitlab-ce-docker](../../plugins/gitlab-ce-docker.md): used to install GitLab in Docker; |
| 30 | +2. [repo-scaffolding](../../plugins/repo-scaffolding.md): used to create a Python web application repository on GitLab; |
| 31 | +3. [gitlab-ci](../../plugins/gitlab-ci.md): used to set up the basic CI pipeline for the warehouse we created; |
| 32 | +4. [helm-installer](../../plugins/helm-installer/helm-installer.md): used to install Argo CD in a Kubernetes cluster; |
| 33 | +5. [argocdapp](../../plugins/argocdapp.md): Used to create an Argo CD application to deploy the web application generated in step 1. |
| 34 | + |
| 35 | +We will accomplish these goals in two steps: |
| 36 | + |
| 37 | +1. Write a configuration file to complete the installation of tools, GitLab and Argo CD; |
| 38 | +2. Write a configuration file to complete the creation of subsequent pipelines and code warehouses, and deploy them to Argo CD. |
| 39 | + |
| 40 | +> Note: In fact, the installation and configuration of DevOps tools can be completed in the same configuration file, but GitLab is special, requiring the user to manually create a token after the installation is complete, so we split the tool installation separately. |
| 41 | +
|
| 42 | +--- |
| 43 | + |
| 44 | +## 2 Getting Started: Download DevStream (`dtm`) |
| 45 | + |
| 46 | +Create a test directory for this tutorial: |
| 47 | + |
| 48 | +```bash |
| 49 | +mkdir test |
| 50 | +cd test/ |
| 51 | +``` |
| 52 | + |
| 53 | +In the newly created directory, execute: |
| 54 | + |
| 55 | +```shell |
| 56 | +sh -c "$(curl -fsSL https://download.devstream.io/download.sh) |
| 57 | +``` |
| 58 | +
|
| 59 | +This script will download the corresponding `dtm` binary file according to your operating system and save it to the current directory. Then, give it executable permissions. |
| 60 | +
|
| 61 | +> Optional: You can move `dtm` to a directory in your $PATH environment variable. For example: `mv dtm /usr/local/bin/`. In this way, you can run `dtm` directly without prefixing it with `./`. |
| 62 | +> |
| 63 | +> For more installation methods, see [Install dtm](../../install.md). |
| 64 | +
|
| 65 | +## 2 Install GitLab And Argo CD |
| 66 | +
|
| 67 | +### 2.1 Prepare Config |
| 68 | +
|
| 69 | +Create file `config-tools.yaml`. Modify `vars` to your values: |
| 70 | +
|
| 71 | +```yaml title="config-tools.yaml" |
| 72 | +config: |
| 73 | + state: |
| 74 | + backend: local |
| 75 | + options: |
| 76 | + stateFile: devstream-1.state |
| 77 | +vars: |
| 78 | + gitlabHostname: gitlab.example.com |
| 79 | + gitlabSSHPort: 30022 |
| 80 | + gitlabHttpPort: 80 |
| 81 | + gitlabHttpsPort: 30443 |
| 82 | +tools: |
| 83 | + - name: gitlab-ce-docker |
| 84 | + instanceID: default |
| 85 | + dependsOn: [] |
| 86 | + options: |
| 87 | + hostname: [[ gitlabHostname ]] |
| 88 | + gitlabHome: /srv/gitlab |
| 89 | + sshPort: [[ gitlabSSHPort ]] |
| 90 | + httpPort: [[ gitlabHttpPort ]] |
| 91 | + httpsPort: [[ gitlabHttpsPort ]] |
| 92 | + rmDataAfterDelete: false |
| 93 | + imageTag: "rc" |
| 94 | + - name: helm-installer |
| 95 | + instanceID: argocd |
| 96 | +``` |
| 97 | +
|
| 98 | +And modify the `/etc/hosts` file of the server to add the domain name resolution of `gitlab.example.com`. If your server ip is 44.33.22.11, you can configure it like this: |
| 99 | +
|
| 100 | +```text title="/etc/hosts" |
| 101 | +44.33.22.11 gitlab.example.com |
| 102 | +``` |
| 103 | +
|
| 104 | +### 2.2 Init |
| 105 | +
|
| 106 | +Run the following command to download the plugins required to install GitLab and Argo CD: |
| 107 | +
|
| 108 | +```shell |
| 109 | +dtm init -f config-tools.yaml -y |
| 110 | +``` |
| 111 | +
|
| 112 | +### 2.3 Apply |
| 113 | +
|
| 114 | +Run the following command to install GitLab and Argo CD: |
| 115 | +
|
| 116 | +```shell |
| 117 | +dtm apply -f config-tools.yaml -y |
| 118 | +``` |
| 119 | +
|
| 120 | +You'll see similar outputs to: |
| 121 | +
|
| 122 | +<script id="asciicast-wxdTxqwycg12UurbRqZpMvSym" src="https://asciinema.org/a/wxdTxqwycg12UurbRqZpMvSym.js" async autoplay=true></script> |
| 123 | +
|
| 124 | +### 2.4 Check Installation Results |
| 125 | +
|
| 126 | +#### 2.4.1 Access GitLab |
| 127 | +
|
| 128 | +You can configure the `44.33.22.11 gitlab.example.com` static domain name resolution record in your PC, and then access GitLab through `http://gitlab.example.com` in the browser (if the browser reports: |
| 129 | +
|
| 130 | +<figure markdown> |
| 131 | + { width="1000" } |
| 132 | + <figcaption>GitLab Login</figcaption> |
| 133 | +</figure> |
| 134 | +
|
| 135 | +Run the following command to get GitLab's root password: |
| 136 | +
|
| 137 | +```shell title="get GitLab root Password" |
| 138 | +docker exec -it gitlab bash |
| 139 | +gitlab-rake "gitlab:password:reset" |
| 140 | +``` |
| 141 | +
|
| 142 | +<script id="asciicast-547097" src="https://asciinema.org/a/547097.js" async></script> |
| 143 | +
|
| 144 | +Login with root/YOUR_PASSWORD. We will use GitLab token later, so let's create one now: |
| 145 | +
|
| 146 | +<figure markdown> |
| 147 | + { width="1000" } |
| 148 | + <figcaption>Generate GitLab token</figcaption> |
| 149 | +</figure> |
| 150 | +
|
| 151 | +#### 2.4.2 Check Argo CD |
| 152 | +
|
| 153 | +We can see that Argo CD is installed into the `argocd` namespace of the Kubernetes cluster: |
| 154 | +
|
| 155 | +```bash |
| 156 | +[root@ip-10-18-13-200 devstream]# kubectl get ns |
| 157 | +NAME STATUS AGE |
| 158 | +argocd Active 36s |
| 159 | +default Active 6d4h |
| 160 | +kube-node-lease Active 6d4h |
| 161 | +kube-public Active 6d4h |
| 162 | +kube-system Active 6d4h |
| 163 | +[root@ip-10-18-13-200 devstream]# kubectl get pods -n argocd |
| 164 | +NAME READY STATUS RESTARTS AGE |
| 165 | +argocd-application-controller-0 1/1 Running 0 49s |
| 166 | +argocd-applicationset-controller-7f4577c5fd-8z926 1/1 Running 0 49s |
| 167 | +argocd-dex-server-7cdb45c7c9-nspgz 1/1 Running 0 49s |
| 168 | +argocd-notifications-controller-65b77fb646-phdwh 1/1 Running 0 49s |
| 169 | +argocd-redis-577c6c8f5c-nf5xm 1/1 Running 0 49s |
| 170 | +argocd-repo-server-7bd9fd899c-7f6cp 1/1 Running 0 49s |
| 171 | +argocd-server-6686bbcf68-fms5w 1/1 Running 0 49s |
| 172 | +``` |
| 173 | +
|
| 174 | +--- |
| 175 | +
|
| 176 | +## 3 Create and Deploy the App |
| 177 | +
|
| 178 | +### 3.1 Prepare Config |
| 179 | +
|
| 180 | +Create file `config-apps.yaml`. Modify `vars` to your values (pay extra attention to `dockerhubUser`): |
| 181 | +
|
| 182 | +```yaml title="config-apps.yaml" |
| 183 | +config: |
| 184 | + state: |
| 185 | + backend: local |
| 186 | + options: |
| 187 | + stateFile: devstream-2.state |
| 188 | +vars: |
| 189 | + appName: myapp |
| 190 | + gitlabURL: http://gitlab.example.com |
| 191 | + defaultBranch: main |
| 192 | + dockerhubUser: DOCKERHUB_USER |
| 193 | +apps: |
| 194 | + - name: [[ appName ]] |
| 195 | + spec: |
| 196 | + language: python |
| 197 | + framework: flask |
| 198 | + repo: |
| 199 | + url: [[ gitlabURL ]]/root/[[ appName ]].git |
| 200 | + branch: [[ defaultBranch ]] |
| 201 | + token: [[ env GITLAB_TOKEN ]] # use "GITLAB_TOKEN" env var |
| 202 | + repoTemplate: |
| 203 | + url: https://github.com/devstream-io/dtm-repo-scaffolding-python-flask.git |
| 204 | + ci: |
| 205 | + - type: template |
| 206 | + templateName: ci-pipeline |
| 207 | + cd: |
| 208 | + - type: argocdapp |
| 209 | +pipelineTemplates: |
| 210 | + - name: ci-pipeline |
| 211 | + type: gitlab-ci |
| 212 | + options: |
| 213 | + imageRepo: |
| 214 | + user: [[ dockerhubUser ]] |
| 215 | + password: [[ env DOCKERHUB_TOKEN ]] # use "DOCKERHUB_TOKEN" env var |
| 216 | +``` |
| 217 | +
|
| 218 | +You may have noticed that the above configuration has something like `[[ env XXX ]]`, which means that we reference the "XXX" environment variable to fill the configuration. So we also need to set the following two environment variables: |
| 219 | +
|
| 220 | +```bash |
| 221 | +export GITLAB_TOKEN="YOUR_GITLAB_TOKEN_HERE" |
| 222 | +export DOCKERHUB_TOKEN="YOUR_DOCKERHUB_TOKEN_HERE" |
| 223 | +``` |
| 224 | +
|
| 225 | +> Note: |
| 226 | +> |
| 227 | +> If you don't know how to create a DockerHub token, you can refer to: [Manage access tokens](https://docs.docker.com/docker-hub/access-tokens/) |
| 228 | +
|
| 229 | +### 3.2 Init |
| 230 | +
|
| 231 | +Similarly, we need to download the required plugins from the config file. Run: |
| 232 | +
|
| 233 | +```bash |
| 234 | +dtm init -f config-apps.yaml |
| 235 | +``` |
| 236 | +
|
| 237 | +### 3.3 Apply |
| 238 | +
|
| 239 | +Run: |
| 240 | +
|
| 241 | +```bash |
| 242 | +dtm apply -f config-apps.yaml -y |
| 243 | +``` |
| 244 | +
|
| 245 | +And you'll get similar outputs to: |
| 246 | +
|
| 247 | +<script id="asciicast-547096" src="https://asciinema.org/a/547096.js" async></script> |
| 248 | +
|
| 249 | +### 3.4 Check Results |
| 250 | +
|
| 251 | +#### 3.4.1 Check the Repo Created in GitLab |
| 252 | +
|
| 253 | +<figure markdown> |
| 254 | + { width="1000" } |
| 255 | + <figcaption>Flask repo</figcaption> |
| 256 | +</figure> |
| 257 | +
|
| 258 | +#### 3.4.2 GitLab CI Workflow |
| 259 | +
|
| 260 | +Access `http://gitlab.example.com` in your browser, and click `CI/CD`, then `Pipelines`: |
| 261 | +
|
| 262 | +<figure markdown> |
| 263 | + { width="1000" } |
| 264 | + <figcaption>GitLab CI Overview</figcaption> |
| 265 | +</figure> |
| 266 | +
|
| 267 | +#### 3.4.3 ArgoCD-Based Continuous Deployment |
| 268 | +
|
| 269 | +The CI workflow has already built a Docker image and pushed it to Dockerhub and Argo CD created by DevStream has deployed it: |
| 270 | +
|
| 271 | +```bash |
| 272 | +[root@ip-10-18-13-200 devstream]# kubectl get deployment -n default |
| 273 | +NAME READY UP-TO-DATE AVAILABLE AGE |
| 274 | +myapp 1/1 1 1 101s |
| 275 | +[root@ip-10-18-13-200 devstream]# kubectl get pods -n default |
| 276 | +NAME READY STATUS RESTARTS AGE |
| 277 | +myapp-b65774f56-8cmjc 1/1 Running 0 106s |
| 278 | +[root@ip-10-18-13-200 devstream]# kubectl get services -n default |
| 279 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 280 | +kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d |
| 281 | +myapp ClusterIP 10.101.148.66 <none> 8080/TCP 110s |
| 282 | +``` |
| 283 | +
|
| 284 | +We can access this application through port forwarding: |
| 285 | +
|
| 286 | +```bash |
| 287 | +kubectl port-forward -n default svc/myapp 8080:8080 |
| 288 | +``` |
| 289 | +
|
| 290 | +Visit `localhost:8080` in your browser, and you can see that the application returns a "Hello, World!". You're done! |
| 291 | +
|
| 292 | +--- |
| 293 | +
|
| 294 | +## 4 Clean-Up |
| 295 | +
|
| 296 | +### 4.1 Delete the Web App |
| 297 | +
|
| 298 | +Run |
| 299 | +
|
| 300 | +```bash |
| 301 | +dtm delete -f config-apps.yaml -y |
| 302 | +``` |
| 303 | +
|
| 304 | +### 4.2 Delete GitLab and Argo CD |
| 305 | +
|
| 306 | +Run |
| 307 | +
|
| 308 | +```bash |
| 309 | +dtm delete -f config-tools.yaml -y |
| 310 | +``` |
| 311 | +
|
| 312 | +### 4.3 Delete Other Files |
| 313 | +
|
| 314 | +```bash |
| 315 | +cd ../ |
| 316 | +rm -rf test/ |
| 317 | +rm -rf ~/.devstream/ |
| 318 | +``` |
0 commit comments