arrow

Kubernetes Continuous Integration Experiment

1. Experiment

1.1 Knowledge points

This document provides a fundamental DevOps best practices guide on Alibaba Cloud. In this guide, you will understand the best practices on how to implement the continuous integration and continuous deployment (CI/CD) by using the cloud services of Alibaba Cloud.

Alibaba Cloud Container Service is compatible with the majority of CI/CD tools. It allows developers to run tests, deploy builds in Kubernetes and update applications with no downtime. While Alibaba Cloud Container Service does work with other open source tools, it comes with CI and CD automation capabilities.

1.2 Experiment process

  • View ECS instances
  • Create an image repository
  • Deploy an application to Container Service
  • Perform continuous integration

1.3 Scene architecture diagram

image desc

1.4 Cloud resources required

  • ECS
  • kubernetes
  • A GitHub account

1.5 Prerequisites

  • 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 instance in order to run the experiment smoothly.
  • Before starting the experiment, please confirm that the previous experiment has been closed normally and exited.

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 Alibaba Cloud console 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 Alibaba Cloud console 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. Create a 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 Cluster。

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

4. View ECS instances

4.1 Add security group rules

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

image desc

We can see one running ECS instance in the US West 1 region. Click it to go to the ECS console as shown in the following figure.

Since the cluster is being created, there may be multiple ECS instances here, but only one is created automatically for the user in the background, and the rest is created by the cluster that was just created. Having an instance of EIP (1 core 2G) is the next step that requires remote login.

image desc

Add a security group rule, as shown in the following figure.

image desc

Click Add Rule.

image desc

In the dialog box that appears, enter 8080/8080 for Port Range and 0.0.0.0/0 for Authorization Objects. Click OK.

image desc

4.2 Install Docker

Copy this ECS instance’s Internet IP address and remotely log on to this ECS instance. For more information about remote logon, see [login] (https://labex.io/questions/150).

image desc

The default account name and password of the ECS instance:

Account name: root

Password: nkYHG890..

Install required packages. yum-utils provides the yum-config-manager utility, and device-mapper-persistent-data and lvm2 are required by the devicemapper storage driver.

yum install -y yum-utils device-mapper-persistent-data lvm2

image desc

Run the following command to set up the stable repository. You always need the stable repository, even if you want to install builds from the edge or test repositories as well.

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

image desc

Install the latest version of Docker CE, or go to the next step to install a specific version.

yum install docker-ce -y

image desc

Docker is installed but not started. The Docker group is created, but no users are added to the group.

Start Docker.

systemctl start docker

image desc

Verify that Docker is installed correctly by running the hello-world image.

docker run hello-world

image desc

4.3 Create a GitHub code repository

If you do not have a GitHub account, go to www.github.com and sign up for a new account. Fill in the username, email, and password. Then, after verification, choose the Free account.

image desc

After registration is completed, it shall bring you the main landing page.

image desc

In this lab, we are using GitHub as the source code repository. First, you would need to fork the source codes from existing Git repository: https://github.com/xdstone1/java-webapp-docker. To do this, log on to your own GitHub and navigate to this repository https://github.com/xdstone1/java-webapp-docker. Click Fork on the top right hand corner on the screen.

image desc

After completing the Fork operation, you can view the project in your repository.

image desc

4.4 Create an image

Install the latest version of Git.

yum install git -y

image desc

Next, you would need to clone the codes to the local computer. Replace the repository address in the following command with the Fork project address of your account from section 3.3.

git clone <you own repository name>

ls

image desc

To build the Docker image, first switch to the directory of the source codes that have cloned locally.

cd java-webapp-docker

image desc

Run the following command to build the Docker image:

docker build -t simplewebapp .

image desc

Verify if the Docker image is built successfully.

docker images

image desc

Before Docker is being pushed to Kubernetes, let’s try to run it locally to make sure everything is running properly.

docker run -p 8080:8080 simplewebapp

image desc

Open your browser and enter the URL of the web application, for example, if the CI server IP address is 47.254.33.1: http://47.254.33.1:8080/simplewebapp/

The response should be as below:

image desc

Press Ctrl + C to stop Container Service.

image desc

5. Create an image repository

5.1 Bind the GitHub account to Container 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) and click OK.

image desc

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

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

image desc

Go to Code Source and click Bind Account.

image desc

On the dialog that appears, click on the right arrow. It will open a new link to sign in to the GitHub account.

image desc

On the GitHub sign-in page, enter the logon details and click Sign In.

image desc

On the Authorization page, click “Authorize Aliyun Developer”.

image desc

Once it is authorized, you should receive a notification email. Go back to the Container Registry page.

By now, it should show “Bound” on the GitHub code source section.

image desc

5.2 Create a namespace

A namespace is a collection of repositories.

Create a namespace according to the following figure. The new namespace cannot be the same as an existing one. If the namespace you entered already exists, try another one.

image desc

The following figure shows that the namespace has been created.

image desc

5.3 Create a repository

Create a repository according to the following figure. Select the region US (Silicon Valley).

image desc

Set parameters according to the following figure and click Next. Select the namespace you created earlier.Repository Type please select Public.

image desc

Select GitHub, enter your account username and project. Click Create Repository.

image desc

The following figure shows that the repository has been created.

image desc

5.4 Add a build rule

Click Manage to open the repository.

image desc

Add a build rule as shown in the following figures.

image desc

image desc

image desc

Return to the CLI of the ECS instance.

Run “vim /root/java-webapp-docker/src/main/webapp/index.jsp” to open the project’s index.jsp file, as shown in the following figure.

image desc

Run the following command to configure Git before submitting the code.

git config --global user.email "labex@aliyun.com"
git config --global user.name  "labex"

image desc

Run the following commands to submit the modification to the repository.

git add .

git commit -m "first commit"

image desc

Before uploading the code to Github, you also need to create an authentication token on Github.

Refer to the figure below, on the Github page, click Settings.

image desc

Select Developer settings.

image desc

image desc

Refer to the following figure to set up.

image desc

image desc

Remember to copy the user’s own Token value and save it to the local notebook. For more detailed tokens function, users can refer to Github official documentation

image desc

Go back to the ECS command line and enter the following command to upload the code to the github repository.

git push origin master

image desc

After you submit the code, automatic build will be automatically triggered in Alibaba Cloud Container Registry.It will take about 6 minutes.

image desc

As you can see, the image has been built successfully.

image desc

6. Deploy an application to Container Service

6.1 Perform initial application deployment in a Kubernetes cluster

Click “Deployment Platform”.

image desc

On the page that appears, click the Deploy button.

image desc

On the Container Service page, retain all default settings and click Next.

image desc

Select “latest” as the image version, check “Always”, and click Next.

image desc

Click Create.

image desc

The next page should show Create Success. Verify if the deployment is successful.

image desc

6.2 Create a service

In the left-side navigation pane, choose Discovery and Load Balancing > Service. Click create to create a new Server Load Balancer instance to expose the port of the application.

image desc

Configure the parameters and click Create, as shown in the following figure.

image desc

After the service is created, the IP address and port of the service appear.

image desc

After accessing this service, you can see that the modified image has been successfully deployed.

image desc

Enter the created deployment.

image desc

Create a trigger.

image desc

A trigger link is generated after you create a trigger. Note it down for subsequent use.

image desc

7. Perform continuous integration

7.1 Create a trigger

Return to the Container Registry console and create a trigger as shown in the following figure. Enter the trigger link in the Trigger URL field.

image desc

image desc

<font color='red'>The user can cut off the above result picture when doing the experiment and send it to the teacher, indicating that the part of the current chapter has been completed.</font>

7.2 Execute automatic deployment

Return to the CLI of the ECS instance.

Run “vim /root/java-webapp-docker/src/main/webapp/index.jsp” to open the project’s index.jsp file again, as shown in the following figure.

image desc

Run the following commands to submit the modification to the repository:

git add .

git commit -m "second commit"

git push origin master

image desc

Wait About 6 minutes. After the code is submitted, image building is triggered automatically and the image is deployed to Deployment in Container Service.

Access the previously created service link again to check that the modification has been applied. This indicates that deployment was successful.

image desc

<font color='red'>Users can cut off the above result picture when they are doing the experiment and send it to the teacher, indicating that the current experiment has been completed.</font>

Reminder:
Before you leave this lab, 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

8. Experiment summary

This document describes the practical approach of implementing the lifecycle of CI/CD for a real-world scenario. The software industry is rapidly seeing the value of using containers as a way to facilitate development, deployment, and environment orchestration for application developers. That is because containers effectively manage environmental differences, allow for improved scalability, and provide predictability that supports Continuous Delivery (CD) of new features. In addition to the technical advantages, containers have been shown to dramatically reduce the cost model of complex environments.