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
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.
.
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.
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:
Go to the logon page of Alibaba Cloud console.
Fill in the sub-user account and click Next.
Fill in the sub-user password and click Log on.
After you successfully log on to the console, the following page is displayed.
3. Create a cluster
Refer to the following figure and select Container Service to enter the container service console.
Refer to the figure below to create a cluster first.
Select Delicated Cluster。
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.
Set to obtain the public network access address of the cluster. Click Next.
Configure the Master node,
Refer to the figure below to set the master node instance type. Click Next.
Configure the Worker node.
Set the cluster access password and click Next.
The component configuration can default here, click Next.
Click Create Cluster。
It takes about 15 minutes to create a cluster. Please wait patiently.
4. View ECS instances
4.1 Add security group rules
Click Elastic Computer Service, as shown in the following figure.
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.
Add a security group rule, as shown in the following figure.
Click Add Rule.
In the dialog box that appears, enter 8080/8080 for Port Range and 0.0.0.0/0 for Authorization Objects. Click OK.
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).
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
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
Install the latest version of Docker CE, or go to the next step to install a specific version.
yum install docker-ce -y
Docker is installed but not started. The Docker group is created, but no users are added to the group.
Start Docker.
systemctl start docker
Verify that Docker is installed correctly by running the hello-world image.
docker run hello-world
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.
After registration is completed, it shall bring you the main landing page.
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.
After completing the Fork operation, you can view the project in your repository.
4.4 Create an image
Install the latest version of Git.
yum install git -y
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
To build the Docker image, first switch to the directory of the source codes that have cloned locally.
cd java-webapp-docker
Run the following command to build the Docker image:
docker build -t simplewebapp .
Verify if the Docker image is built successfully.
docker images
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
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:
Press Ctrl + C to stop Container Service.
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.
The prompt shown in the following figure appears upon your first logon. Select US(Silicon Valley) and click OK.
The prompt shown in the following figure appears upon your first logon. Select US(Silicon Valley) in the upper left corner and click OK.
Set the Docker logon password to “Aliyun-test”.
Go to Code Source and click Bind Account.
On the dialog that appears, click on the right arrow. It will open a new link to sign in to the GitHub account.
On the GitHub sign-in page, enter the logon details and click Sign In.
On the Authorization page, click “Authorize Aliyun Developer”.
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.
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.
The following figure shows that the namespace has been created.
5.3 Create a repository
Create a repository according to the following figure. Select the region US (Silicon Valley).
Set parameters according to the following figure and click Next. Select the namespace you created earlier.Repository Type please select Public.
Select GitHub, enter your account username and project. Click Create Repository.
The following figure shows that the repository has been created.
5.4 Add a build rule
Click Manage to open the repository.
Add a build rule as shown in the following figures.
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.
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"
Run the following commands to submit the modification to the repository.
git add .
git commit -m "first commit"
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.
Select Developer settings.
Refer to the following figure to set up.
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
Go back to the ECS command line and enter the following command to upload the code to the github repository.
git push origin master
After you submit the code, automatic build will be automatically triggered in Alibaba Cloud Container Registry.It will take about 6 minutes.
As you can see, the image has been built successfully.
6. Deploy an application to Container Service
Click “Deployment Platform”.
On the page that appears, click the Deploy button.
On the Container Service page, retain all default settings and click Next.
Select “latest” as the image version, check “Always”, and click Next.
Click Create.
The next page should show Create Success. Verify if the deployment is successful.
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.
Configure the parameters and click Create, as shown in the following figure.
After the service is created, the IP address and port of the service appear.
After accessing this service, you can see that the modified image has been successfully deployed.
6.3 Create a trigger link for deployment
Enter the created deployment.
Create a trigger.
A trigger link is generated after you create a trigger. Note it down for subsequent use.
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.
<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.
Run the following commands to submit the modification to the repository:
git add .
git commit -m "second commit"
git push origin master
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.
<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:
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.