20 Quickly Build Jenkins Gitlab Continuous Integration Environment

20 Quickly Build JenkinsGitlab Continuous Integration Environment #

Introduction to ASK #

1.PNG

First of all, what is ASK? ASK is a serverless version of Kubernetes container service launched by Alibaba Cloud. Compared to traditional Kubernetes services, the biggest feature of ASK is that it accesses the Kubernetes cluster through virtual nodes, and the Master node of Kubernetes is completely managed by Alibaba Cloud Container Service. Therefore, in the entire ASK cluster, users do not need to manage and maintain real nodes, they only need to care about Pod resources. The Pods in ASK are hosted by Alibaba Cloud Elastic Container Instance (ECI).

The advantages of ASK are as follows:

  • Reducing the threshold for using Kubernetes, no need to manage Node nodes;
  • No need to consider the capacity planning of nodes;
  • Billing on-demand based on Pods;
  • Minimal impact in case of node failure, at the Pod level.

At the same time, ASK is mainly suitable for the following scenarios:

  • Online business elasticity (video streaming, online education);
  • Big data computing (Spark);
  • Scheduled tasks;
  • CI/CD continuous integration.

Advantages of GitLab CI on ASK #

When it comes to CI/CD, the two most well-known tools are Jenkins and GitLab CI. With the popularity of the DevOps role, more and more companies are adopting GitLab CI as their continuous integration tool. Now let’s introduce GitLab CI on ASK. The gitlab-runner is registered in the ASK cluster in the form of a Pod, and each CI/CD stage corresponds to a Pod.

2.png

The advantages of doing this are as follows:

  • High availability of services (Deployment+PVC);
  • No need to maintain K8s Master, Node nodes. Only run one Pod (gitlab-runner) when there are no build tasks;
  • Trigger a build task, start a Pod, and pay on-demand;
  • Failure will only affect at the Pod level.

Practical Demonstration #

Next, we will demonstrate how to deploy gitlab-runner on Alibaba Cloud ASK cluster and deploy a Java application to the ASK cluster through gitlab CICD Pipeline.

The knowledge points involved are mainly:

  • Save gitlab runner and executor configurations through configMap;
  • Save access credentials for the ASK cluster and the image repository through secret;
  • Cache runner cache and maven repository through PVC;
  • Cache container images through imageCache.

All the configuration files (yaml) involved in this section have been uploaded to GitHub for download.【Download Link】.

Let’s start the demonstration. For the video version of the course, please click the【Watch Link】.

1. Prepare the ASK Cluster #

image.png

  • After the cluster is created, the API server public link address will be available in the basic information

image.png

  • The ASK cluster access credentials are available in the connection information

image.png

2. Prepare PV/PVC #

Prepare two NAS disks, one for gitlab runner cache and one for the Maven repository. Replace the NAS server address and path with your own.

kubectl apply -f mvn-pv.yaml
kubectl apply -f mvn-pvc.yaml
kubectl apply -f nas-pv.yaml
kubectl apply -f nas-pvc.yaml

3. Prepare Secret #

  • Copy the certificate public and private key from kubeconfig to the secret, secret.yaml

    kubectl apply -f secret.yaml

  • Authentication information for docker-registry, ECI supports password-free pulling, but pushing Docker images still requires it

    kubectl create secret docker-registry registry-auth-secret –docker-server=registry.cn-hangzhou.aliyuncs.com –docker-username=${xxx} –docker-password=${xxx}

  • The generated secret can be viewed with the following command

    kubectl get secret registry-auth-secret –output=yaml

4. Prepare ConfigMap #

Copy the url and token of the gitlab runner and the API server address of the ASK cluster to config.yaml

kubectl apply -f config-map.yaml

5. Prepare imageCache (optional, saving image pulling time) #

At present, AS K is installed with imagecache-crd by default, which can be queried using the following command. If it is not installed, you can install it yourself.

# Check if the image cache CRD is installed
kubectl get crd
# Install image cache CRD
kubectl apply -f imagecache-crd.yaml
# Create image cache
kubectl apply -f imagecache.yaml

6. Deploy gitlab runner #

kubectl apply -f gitlab-runner-deployment.yaml

image.png

7. Perform a simple CI task #

image.png

The .gitlab-ci.yml file in the git repo is similar to Jenkinsfile, defining the workflow of the build task. We modify the src/main/webapp/index.jsp file in the demo project and then commit it with “git commit -m “change index info”. The pipeline task in GitLab is triggered, and the entire process involves compilation, packaging, and deployment.

image.png

image.png

Cost #

Comparison of Using ASK and a Prepaid ECS:

image.png

Based on the above cost calculation, if you have fewer than 126 CI/CD tasks per day, using ASK+ECI is more cost-effective than purchasing a yearly/monthly ECS. While enjoying on-demand billing, it also reduces maintenance costs. More importantly, when the business scale expands and the number of CI/CD tasks increases sharply, there is no longer a need to worry about scaling Node nodes. The ASK+ECI solution can be regarded as the standard configuration for CI/CD continuous integration scenarios.