arrow

Automatically Build Java Projects With GitLab And Kubernetes

1. Experiment

1.1 Knowledge points

The following experiment primarily uses Alibaba Cloud Container Service and open-source GitLab.GitLab and Container Service for Kubernetes are automatically installed before the experiment. During the experiment, configure the mapping between GitLab Server and GitLab Runner, and create a Java project and submit it to GitLab. GitLab Server calls GitLab Runner to automatically build code and publish it to a Kubernetes container cluster.

1.2 Experiment process

  • Configure GitLab
  • Upload the Java project
  • Verify the build

1.3 Scene architecture diagram

image desc

1.4 Cloud resources required

  • ECS
  • Container service
  • Container Registry

1.5 Prerequisites

  • You have a basic understanding of Elastic Compute Service (ECS) and Server Load Balancer (SLB).
  • If you’re using your own Alibaba Cloud account instead of the account provided by this lab to operate the experiment, please note that you’ll need to choose the same Ubuntu 16.04 operating system for your ECS in order to run the experiment smoothly.

2. Start the experiment environment

Click Start Lab in the upper right corner of the page to start the experiment.

image desc.

After the experiment environment is successfully started, the system has deployed resources required by this experiment in the background, including the ECS instance, RDS instance, Server Load Balancer instance, and OSS bucket. An account consisting of the username and password for logging on to the Web console of Alibaba Cloud is also provided.

image desc

After the experiment environment is started and related resources are properly deployed, the experiment starts a countdown. You have two hours to perform experimental operations. After the countdown ends, the experiment stops, and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Next, use the username and password provided by the system to log on to the Web console of Alibaba Cloud and view related resources:

openCole

Go to the logon page of Alibaba Cloud console.

image desc

Fill in the sub-user account and click Next.

image desc

Fill in the sub-user password and click Log on.

image desc

After you successfully log on to the console, the following page is displayed.

image desc

3. Configure GitLab

3.1 Log on to GitLab

Click Elastic Computer Service, as shown in the following picture.

image desc

Select US (Silicon Valley). Two automatically created Ubuntu ECS instances are displayed. If CentOS ECS instances automatically created by Container Service are also displayed, ignore them.One ECS instance is a 2-core 4 GB instance, and the other ECS instance is a 1-core 2 GB instance.Rename the 2-core 4 GB ECS instance “gitlab-server” and rename the 1-core 2 GB ECS instance “gitlab-runner”, as shown in the following figure.

image desc

The “gitlab-server” ECS instance has GitLab Server installed. The “gitlab-runner” ECS instance has GitLab Runner installed for running the pipeline tasks assigned by GitLab Server.

Copy the public IP address of the “gitlab-server” ECS instance and log on to the instance remotely.For more information about remote logon, see Logon.

The default account name and password of the ECS instance:

Account name: root

Password: nkYHG890..

After successful logon, run vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml to open the “gitlab.yml” configuration file. In the file, replace the host address (shown in the following figure) with the public IP address of the “gitlab-server” ECS instance. Save the settings and exit.

image desc

Run vim /etc/gitlab/gitlab.rb to open the “gitlab.rb” configuration file. In the file, replace the IP address with the public IP address of the “gitlab-server” ECS instance.Save the settings and exit.

image desc

The original IP addresses in the two configuration files are the IP address of the ECS instance used to create an image. Therefore, they must be replaced here.

Run the following command to restart GitLab:

systemctl restart gitlab-runsvdir.service

image desc

Copy the public IP address of the “gitlab-server” ECS instance to the address bar of your web browser. When the GitLab logon page is displayed, enter the following username and password to log on to GitLab:

Username: root

Password: Aliyun-test123

image desc

After successful logon, an empty project without any files is displayed. It will be used in subsequent operations.

image desc

3.2 Register GitLab Runner

Obtain the URL and token of GitLab, as shown in the following figure.

image desc

Copy the public IP address of the “gitlab-runner” ECS instance and log on to the instance remotely.For more information about remote logon, see Logon.

The default account name and password of the ECS instance:

Account name: root

Password: nkYHG890..

Run the following command after successful logon:

l

Perform configuration as shown in the following figure. Replace the URL and token shown in the figure with your own.

image desc

Return to the web browser and refresh the page. The registered GitLab Runner is displayed.

image desc

3.3 Upload an SSH key

Back to the gitlab-runner command line, Run the following command to generate an SSH key:

ssh-keygen -t rsa

image desc

Run the following command to retrieve the created SSH key.The command output shows the complete SSH key.

cat ~/.ssh/id_rsa.pub

image desc

Go to the SSH key management page of GitLab, as shown in the following figure.

image desc

Copy the SSH key to the page and click Add key.

image desc

The SSH key has been added successfully.

image desc

3.4 Create a registry

Go to the Alibaba Cloud console, click Home in the upper left corner of the page, and select Container Registry.

image desc

The prompt shown in the following figure appears upon your first logon. Select US(Silicon Valley) in the upper left corner and click OK.

image desc

image desc

image desc

Set the Docker logon password to “Aliyun-test”.

image desc

Create a domain namespace, as shown in the following figure.Because the name of a domain namespace must be unique, we recommend that you name the domain namespace in the format “cicd-k8s-tutorial-“ + digits.

image desc

Create a repository, as shown in the following figure.Set Repository Name to “simple-rest-service” and click Next.

image desc

Repository Type select Public.

image desc

Click Create Repository, as shown in the following figure.

image desc

After the repository is created

The public IP address and username of the registry are displayed. Write them down for future reference.

image desc

3.5 Create Cluster

Refer to the following figure and select Container Service to enter the container service console.

image desc

Refer to the figure below to create a cluster first.

image desc

Select Delicated Kubernetes。

image desc

Refer to the figure below, set the cluster name, select the US (Silicon Valley) area, and check the VPC network and switch to which it belongs.

image desc

Set to obtain the public network access address of the cluster. Click Next.

image desc

Configure the Master node,

Refer to the figure below to set the master node instance type. Click Next.

image desc

Configure the Worker node.

image desc

Set the cluster access password and click Next.

image desc

The component configuration can default here, click Next.

image desc

Click Create Cluster。

image desc

It takes about 15 minutes to create a cluster. Please wait patiently.

image desc

image desc

3.6 Check the IP address of the Kubernetes container cluster

Click on the cluster name.

image desc

Click Manage. The IP address of the master node in the Kubernetes container cluster is displayed.Write down the IP address for future reference.

image desc

3.7 Configure the CI/CD pipeline

Click the GitLab icon in the upper-left corner, as shown in the following figure. On the GitLab homepage, click the “simple-rest-service” project.

image desc

Go to the CI/CD configuration page, as shown in the following figure.

image desc

Set the desired variables and click Save variables, as shown in the following figure.

DOCKER_REGISTRY_IMAGE_URL: Public IP address of the registry
DOCKER_REGISTRY_USERNAME: Username of the registry
DOCKER_REGISTRY_PASSWORD: Password of the registry
K8S_MASTER_PUBLIC_IP: Public IP address of the master node in the Kubernetes container cluster
K8S_PASSWORD: Password of the master node in the Kubernetes container cluster

image desc

4. Upload the Java project

Open the command-line interface (CLI) of the “gitlab-runner” ECS instance.

Run the following command to create a directory named “projects” and go to the directory:

mkdir ~/projects
cd ~/projects

image desc

Run the following command to download the “simple-rest-service” project from GitLab to the local host.Replace YOUR-GITLAB-SERVER-IP with the IP address of the “gitlab-server” ECS instance.

git clone git@YOUR-GITLAB-SERVER-IP:root/simple-rest-service.git

image desc

Run the following command to download the compressed project package:

wget http://labex-ali-data.oss-us-west-1.aliyuncs.com/gitlab-ci/ci-apps.zip

image desc

Run the following command to install the decompression tool:

apt update && apt -y install unzip zip

image desc

Run the following command to decompress the downloaded project package:

unzip ci-apps.zip -d ~/projects/simple-rest-service/

image desc

Run the following command to go to the “simple-rest-service” directory:

cd simple-rest-service

image desc

Run the following command to configure the username and email address of the local Git warehouse:

git add .
git config --global user.name "root"
git config --global user.email "labex@163.com"

image desc

Run the following command to submit the code:

git commit -m "Initial commit"

image desc

Run the following command to upload the Java project to GitLab:

git push origin master

image desc

5. Verify the build

Go to the project page of GitLab.

image desc

The code has already been uploaded, and a pipeline task is being built, wait about 7 minutes.

image desc

The pipeline task has been built successfully.

image desc

Log on to the master node in the Kubernetes container cluster remotely.Use the IP address obtained in Step 3.6

Account name: root

Password: Aliyun-test

Run the following commands after successful logon. Resource objects, such as deployment, pod, and service, have already been created automatically in the Kubernetes container cluster.The resource objects are created through the pipeline task that GitLab automatically runs after the code is submitted.

kubectl get deployments

image desc

kubectl get pods

image desc

kubectl get services

image desc

Users can also go to the container console to view the related created resource objects.

image desc

image desc

Reminder:
Before you leave this lab, remember to log out your Alibaba RAM account before you click the ‘stop’ button of your lab. Otherwise you’ll encounter some issue when opening a new lab session in the same browser:

image descimage desc

6. Experiment summary

This experiment shows how to use GitLab to automatically build and publish a Java project.GitLab is an MIT-licensed Git warehouse management tool that enables access to public or private projects through the web interface. Similar to GitHub, GitLab provides functions for source code browsing, defect and comment management, and support for Concurrent DevOps software development. GitLab provides solutions throughout the DevOps lifecycle, from creating, verifying, and packaging to publishing, configuring, and monitoring. These greatly improve the software cycle efficiency and service upgrade speed.