GitLab + GitLab CI with DTM Apps

0 Goal

In this tutorial, we will try to achieve the following goals with DevStream:

  1. Use Docker to install GitLab as a code warehouse (if GitLab is already installed on your server, you can skip this step);
  2. Create a Python web application repository on GitLab, based on the Flask framework;
  3. Use GitHub CI to set up a basic CI pipeline for the warehouse we created;
  4. Install Argo CD in an existing Kubernetes cluster to implement GitOps;
  5. Create an Argo CD application to deploy the web application generated in step 1.

prerequisites:

  • Docker (GitLab uses Docker for installation)
  • Kubernetes cluster (Argo CD is installed in the Kubernetes cluster)

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:


1 Overview

DevStream will use the following plugins to achieve the goals described in Section 0:

  1. gitlab-ce-docker: used to install GitLab in Docker;
  2. repo-scaffolding: used to create a Python web application repository on GitLab;
  3. gitlab-ci: used to set up the basic CI pipeline for the warehouse we created;
  4. helm-installer: used to install Argo CD in a Kubernetes cluster;
  5. argocdapp: Used to create an Argo CD application to deploy the web application generated in step 1.

We will accomplish these goals in two steps:

  1. Write a configuration file to complete the installation of tools, GitLab and Argo CD;
  2. Write a configuration file to complete the creation of subsequent pipelines and code warehouses, and deploy them to Argo CD.

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.


2 Getting Started: Download DevStream (dtm)

Create a test directory for this tutorial:

Bash

  1. mkdir test
  2. cd test/

In the newly created directory, execute:

Bash

  1. sh -c "$(curl -fsSL https://download.devstream.io/download.sh)

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.

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 ./.

For more installation methods, see Install dtm.

2 Install GitLab And Argo CD

2.1 Prepare Config

Create file config-tools.yaml. Modify vars to your values:

config-tools.yaml

  1. config:
  2. state:
  3. backend: local
  4. options:
  5. stateFile: devstream-1.state
  6. vars:
  7. gitlabHostname: gitlab.example.com
  8. gitlabSSHPort: 30022
  9. gitlabHttpPort: 80
  10. gitlabHttpsPort: 30443
  11. tools:
  12. - name: gitlab-ce-docker
  13. instanceID: default
  14. dependsOn: []
  15. options:
  16. hostname: [[ gitlabHostname ]]
  17. gitlabHome: /srv/gitlab
  18. sshPort: [[ gitlabSSHPort ]]
  19. httpPort: [[ gitlabHttpPort ]]
  20. httpsPort: [[ gitlabHttpsPort ]]
  21. rmDataAfterDelete: false
  22. imageTag: "rc"
  23. - name: helm-installer
  24. instanceID: argocd

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:

/etc/hosts

  1. 44.33.22.11 gitlab.example.com

2.2 Init

Run the following command to download the plugins required to install GitLab and Argo CD:

Bash

  1. dtm init -f config-tools.yaml -y

2.3 Apply

Run the following command to install GitLab and Argo CD:

Bash

  1. dtm apply -f config-tools.yaml -y

You’ll see similar outputs to:

2.4 Check Installation Results

2.4.1 Access GitLab

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:

GitLab Login

GitLab Login

Run the following command to get GitLab’s root password:

get GitLab root Password

  1. docker exec -it gitlab bash
  2. gitlab-rake "gitlab:password:reset"

Login with root/YOUR_PASSWORD. We will use GitLab token later, so let’s create one now:

GitLab token

Generate GitLab token

2.4.2 Check Argo CD

We can see that Argo CD is installed into the argocd namespace of the Kubernetes cluster:

Bash

  1. [root@ip-10-18-13-200 devstream]# kubectl get ns
  2. NAME STATUS AGE
  3. argocd Active 36s
  4. default Active 6d4h
  5. kube-node-lease Active 6d4h
  6. kube-public Active 6d4h
  7. kube-system Active 6d4h
  8. [root@ip-10-18-13-200 devstream]# kubectl get pods -n argocd
  9. NAME READY STATUS RESTARTS AGE
  10. argocd-application-controller-0 1/1 Running 0 49s
  11. argocd-applicationset-controller-7f4577c5fd-8z926 1/1 Running 0 49s
  12. argocd-dex-server-7cdb45c7c9-nspgz 1/1 Running 0 49s
  13. argocd-notifications-controller-65b77fb646-phdwh 1/1 Running 0 49s
  14. argocd-redis-577c6c8f5c-nf5xm 1/1 Running 0 49s
  15. argocd-repo-server-7bd9fd899c-7f6cp 1/1 Running 0 49s
  16. argocd-server-6686bbcf68-fms5w 1/1 Running 0 49s

3 Create and Deploy the App

3.1 Prepare Config

Create file config-apps.yaml. Modify vars to your values (pay extra attention to dockerhubUser):

config-apps.yaml

  1. config:
  2. state:
  3. backend: local
  4. options:
  5. stateFile: devstream-2.state
  6. vars:
  7. appName: myapp
  8. gitlabURL: http://gitlab.example.com
  9. defaultBranch: main
  10. dockerhubUser: DOCKERHUB_USER
  11. apps:
  12. - name: [[ appName ]]
  13. spec:
  14. language: python
  15. framework: flask
  16. repo:
  17. url: [[ gitlabURL ]]/root/[[ appName ]].git
  18. branch: [[ defaultBranch ]]
  19. token: [[ env GITLAB_TOKEN ]] # use "GITLAB_TOKEN" env var
  20. repoTemplate:
  21. url: https://github.com/devstream-io/dtm-repo-scaffolding-python-flask.git
  22. ci:
  23. - type: template
  24. templateName: ci-pipeline
  25. cd:
  26. - type: argocdapp
  27. pipelineTemplates:
  28. - name: ci-pipeline
  29. type: gitlab-ci
  30. options:
  31. imageRepo:
  32. user: [[ dockerhubUser ]]
  33. password: [[ env DOCKERHUB_TOKEN ]] # use "DOCKERHUB_TOKEN" env var

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:

Bash

  1. export GITLAB_TOKEN="YOUR_GITLAB_TOKEN_HERE"
  2. export DOCKERHUB_TOKEN="YOUR_DOCKERHUB_TOKEN_HERE"

Note:

If you don’t know how to create a DockerHub token, you can refer to: Manage access tokens

3.2 Init

Similarly, we need to download the required plugins from the config file. Run:

Bash

  1. dtm init -f config-apps.yaml

3.3 Apply

Run:

Bash

  1. dtm apply -f config-apps.yaml -y

And you’ll get similar outputs to:

3.4 Check Results

3.4.1 Check the Repo Created in GitLab

Flask repo

Flask repo

3.4.2 GitLab CI Workflow

Access http://gitlab.example.com in your browser, and click CI/CD, then Pipelines:

GitLab CI Overview

GitLab CI Overview

3.4.3 ArgoCD-Based Continuous Deployment

The CI workflow has already built a Docker image and pushed it to Dockerhub and Argo CD created by DevStream has deployed it:

Bash

  1. [root@ip-10-18-13-200 devstream]# kubectl get deployment -n default
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. myapp 1/1 1 1 101s
  4. [root@ip-10-18-13-200 devstream]# kubectl get pods -n default
  5. NAME READY STATUS RESTARTS AGE
  6. myapp-b65774f56-8cmjc 1/1 Running 0 106s
  7. [root@ip-10-18-13-200 devstream]# kubectl get services -n default
  8. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  9. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d
  10. myapp ClusterIP 10.101.148.66 <none> 8080/TCP 110s

We can access this application through port forwarding:

Bash

  1. kubectl port-forward -n default svc/myapp 8080:8080

Visit localhost:8080 in your browser, and you can see that the application returns a “Hello, World!”. You’re done!


4 Clean-Up

4.1 Delete the Web App

Run

Bash

  1. dtm delete -f config-apps.yaml -y

4.2 Delete GitLab and Argo CD

Run

Bash

  1. dtm delete -f config-tools.yaml -y

4.3 Delete Other Files

Bash

  1. cd ../
  2. rm -rf test/
  3. rm -rf ~/.devstream/