Using ACK and NAS to quickly build elastic nginx website

Time:2021-7-22

Scene introduction

This paper introduces how to build a simple elastic and highly available nginx website within half an hour through Alibaba cloud container ack service and File Storage NAS service. After completing all the operations in this article, you will get a single page website. Users’ requests will be scattered to multiple container nodes, and the capacity will be automatically expanded and reduced according to the business load. Even if a container node goes down, users’ access will not be affected. In addition, you can quickly update the web page edited locally to the website.

background knowledge

The cloud products used in this tutorial are as follows:

ECS

Cloud server (ECS) is an IAAs (infrastructure as a service) level cloud computing service with excellent performance, stability, reliability and elastic expansion provided by Alibaba cloud. ECS eliminates the preparation for purchasing it hardware, enables you to use the server as conveniently and efficiently as water, electricity, natural gas and other public resources, and realizes the on-demand and elastic scaling of computing resources. Alibaba cloud ECS continues to provide innovative servers to meet a variety of business needs and help your business development.

File Storage NAS

Alicloud network attached storage (NAS) is a file storage service for computing nodes such as alicloud ECS instance, e-hpc and container service. NAS provides a simple scalable file storage for use with ECS. Multiple ECS instances can access the NAS file system at the same time, and the storage capacity will automatically grow and shrink flexibly as you add and delete files. It provides a common data source for workload and applications running on multiple instances or servers.

Container services kubernetes

Alibaba cloud container service for kubernetes (ACK) is the first service platform in the world that has passed kubernetes consistency certification. It provides high-performance container application management services, supports enterprise kubernetes container application lifecycle management, and enables you to easily and efficiently run kubernetes container applications in the cloud.

There are seven steps in this tutorial. After completing the first five steps, the elastic and highly available nginx website can be realized. The last two steps verify the elastic and highly available properties of the website.

Step 1: create resources
Step 2: mount the file system NAS to the ECS server
Step 3: upload files to NAS
Step 4: configure NAS mount information
Step 5: create nginx application
Step 6: visit the test website
Step 7: verify high availability of service
Step 8: verify the elastic expansion and contraction capacity
Using ACK and NAS to quickly build elastic nginx website

Step 1: open the experience

Cloud product resource experience address:https://developer.aliyun.com/…

Open Cloud product resources

Step 2: mount the file system NAS to the ECS server

Alicloud File Storage NAS is a distributed file system with shared access, elastic expansion, high reliability and high performance. It can provide persistent storage services for containers. In the next operation, your web files will be saved in the NAS file system. When the pod is created, you can directly call the files in the NAS, and after the pod is destroyed, the files in the NAS will continue to be saved.
1. Log in to the NAS file system console by using the sub account provided by the resource (you can use the traceless mode of the browser).
2. Click the file system list in the left guide bar, and then click the file system ID to enter the file system details page.
3. Click mount to view the mount point information and copy the mount command.
Using ACK and NAS to quickly build elastic nginx website
4. Open the terminal tool and enter the connection command SSH [user name] @ [IPAddress] in the terminal. You need to replace the user name and IPAddress with the public IP of the ECS server provided by the resource.
For example:

ssh [email protected]

Using ACK and NAS to quickly build elastic nginx website
The result of the command is as follows:
Using ACK and NAS to quickly build elastic nginx website
5. Execute the following command in the terminal to mount the NAS to the ECS server.
Install client for NFS.

sudo yum install nfs-utils

Execute the mount command copied in step 3 to mount the NAS to the / MNT directory of ECs.

sudo mount -t nfs -o vers=3,nolock,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport 3******7.cn-shanghai.nas.aliyuncs.com:/ /mnt

Step 3: upload files to NAS

After using the ACK cluster to build the nginx service, when you open the homepage of the website, the container will read the web page file uploaded in this step from the NAS file system and return it to the browser. After the website is built, you can update the files in NAS in the same way.
1. Create index.html file locally.

  • Windows system: open the text editor and enter test index page for
    Nginx NAS demo, and then save the file type as HTML file.
  • MacOS or Linux: open the command line tool and execute the following command.
mkdir -p ~/Documents/nginx-nas-demo
echo "test index page for nginx-nas-demo" > ~/Documents/nginx-nas-demo/index.html

2. Download and install SFTP Client, such as FileZilla.
3. Upload index.html file to NAS.

  • Double click to run filezila.
  • According to the following information, connect to the server. Host: ECS public IP provided by resource. User name: root password: the ECS password provided by the resource. Port: 22.
  • Find the locally created index.html file in the left directory tree, and enter / MNT in the right directory tree to enter the NAS directory.
  • Drag index.html in the left area to the right area to upload local files to NAS.

Using ACK and NAS to quickly build elastic nginx website
4. You can use step 2 to remotely connect to ECs and view the newly uploaded index.html file in the / MNT directory.
Using ACK and NAS to quickly build elastic nginx website

Step 4: configure NAS mount information

To mount and use NAS with ack service, you need to configure container storage volume PV and storage statement PVC information, which will be used when you deploy nginx application.
1. Configure storage volume.
Enter the ACK cluster list, click the ACK cluster name to enter the cluster details page.
Click the storage volume in the left boot bar.
Click the storage volumes tab, and then click create.
Select the NAS storage volume type, fill in the storage volume name, select the NAS mount point, and finally click create.
The operation process is shown in the figure below.
Figure 1
Using ACK and NAS to quickly build elastic nginx website
Figure 2
Using ACK and NAS to quickly build elastic nginx website
2. Configure the storage statement.

1. Click storage volume in the left navigation bar.
2. Click store declaration, and then click create.
3. Configure the storage claim according to the following information.

Storage claim type: select NAS.
Name: fill in the name of the storage statement, for example: NAS PVC.
Allocation mode: select an existing storage volume.
Existing storage volume: select the storage volume created in the previous step.
Total amount: this column has no practical significance for NAS, just keep the default value.
Using ACK and NAS to quickly build elastic nginx website

Step 5: create nginx application

1. On the cluster details page, click the workload in the left guide bar, and in the default stateless tab, click create with image.

2. In the application basic information page, fill in the application name, and then click next.
Using ACK and NAS to quickly build elastic nginx website
3. Container configuration.

a. On the container configuration page, click Select mirror.

Using ACK and NAS to quickly build elastic nginx website

b. In the pop-up dialog box, click search, select docker image, fill in nginx, and then click search.
c. Select the nginx official image in the search results, and then click OK.

Using ACK and NAS to quickly build elastic nginx website

d. Pull the container configuration page to the bottom, click Add persistent volume claim in the data volume column, select the storage claim created in step 2 as the mount source, and fill in / usr / share / nginx / HTML / in the container path.

Explain that this configuration will mount NAS to the / usr / share / nginx / HTML / path of the container, that is, the directory where nginx stores index.html by default.

Using ACK and NAS to quickly build elastic nginx website

e. Other configurations on this page can use the default values. Click the next button to continue.
  • Advanced configuration.
    a. On the advanced configuration page, click create on the right side of service.

    Set the following information in the port mapping of the pop-up create service dialog box, and then click create.

Name: http (HTTP must be all lowercase, otherwise the creation will fail).
Service port: 80.
Container port: 80.
Protocol: TCP.
Using ACK and NAS to quickly build elastic nginx website
Note that other options can be left at the default value.

b. Click create on the right side of the route.
    Configure the following information in the rule item of the pop-up create dialog box, and then click create.
  • Domain name: the domain name of your website, such as foo.bar.com. If you don’t have a domain name, you can fill in a test domain name at will.
  • Service: select the nginx NAS SVC service created in the previous step.

Using ACK and NAS to quickly build elastic nginx website
Note that other options can be left at the default value.
At this time, the routing column should display the domain name and the corresponding service information. The routing function will scatter the user requests to multiple container nodes for processing through simple fan out method.

c. Check the opening behind the horizontal expansion of the container group, and then select the opening after the minimum expansion[ Fill in 2 in this number, and finally click the Create button at the bottom( https://img-blog.csdnimg.cn/20201103152725698.png?x -oss-process=image/watermark,type_ ZmFuZ3poZW5naGVpdGk,shadow_ 10,text_ aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2dhbmd5aWtlamk=,size_ 16,color_ FFFFFF,t_ 70#pic_ center)

Wait for the creation to succeed.
Using ACK and NAS to quickly build elastic nginx website

Step 6: visit the test website

After the NAS and container are configured, you can use your local computer to access the test website.
1. Configure the hosts file of the office computer.
Note: if you use the official website domain name in nginx application configuration and have passed the record, you can skip this step and open the website directly through the browser. If you are using a test domain name (for example, foo. Bar. Com), you need to configure the hosts file on the local computer to let the browser transfer the request for the domain name to the IP address of the ACK cluster.

a. Go to the workload page of the ACK console.
b. Click the nginx service name created in step 3.

Using ACK and NAS to quickly build elastic nginx website

c. Click the access mode tab to copy the IP address of the routing endpoint to the pasteboard, which is the IP address used by the public network to access the website.

Using ACK and NAS to quickly build elastic nginx website

d. Use the administrator's permission to open the local C::? Windows? System32? Drivers? Etc? Hosts file, add the IP address of the endpoint and the domain name set in the three steps to the hosts file, and then save and exit, as shown in the figure below.

Note: for Linux and MAC systems, please modify the / etc / hosts file.

Using ACK and NAS to quickly build elastic nginx website
2. Use the browser to open the website.
Using ACK and NAS to quickly build elastic nginx website
Note: if you can’t access it normally, you can try to clear the browser cache or use traceless browsing mode.

Step 7: verify high availability of service

Because when deploying nginx application, the minimum number of copies of container group is 2, so when one container host node is down, the website can still be provided by another container host node, which ensures the high availability of the service.
1. Simulate node failure.

a. On the cluster details page, click node management in the left guide bar to enter the node management page.
b. Select two of the nodes, and then click the operation bar node maintenance below. Check the empty node in the pop-up dialog box, and then click OK.

Using ACK and NAS to quickly build elastic nginx website
At this point, you can see that the node state is not schedulable.
Using ACK and NAS to quickly build elastic nginx website

c. Use the browser to open the website and find that it can still be accessed normally.

Using ACK and NAS to quickly build elastic nginx website

  1. Recover the node.
    a. Click the operation bar node below and click OK in the pop-up dialog box.
    b. Use the browser to open the website and find that it can still be accessed normally.

Using ACK and NAS to quickly build elastic nginx website

Step 8: verify elastic expansion

1. View the initial state of container group.

a. Enter the workload page of the ACK console and click the nginx service name created in step 3.
b. Click the container group horizontal extender to view the current number of copies and the current CPU utilization of the container group.

Using ACK and NAS to quickly build elastic nginx website

  1. Simulate high container load.
    a. On the cluster details page, click the top right to manage the cluster through cloudshell and enter the cloudshell command line.

Using ACK and NAS to quickly build elastic nginx website

b. On the command line, execute the following command to view the node.
kubectl get pod

Using ACK and NAS to quickly build elastic nginx website

c. Execute the following command to enter the container.
kubectl exec -it lalala-***pod-name****-kxjbg bash
d. Execute the following command to simulate high container load.
fulload() { dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null | dd if=/dev/zero of=/dev/null & }; fulload; read

3. Confirm automatic capacity expansion.

a. Enter the workload page of the ACK console and click the nginx service name created in step 3.
b. Click the container group horizontal retractor to view the current number of copies and the current CPU utilization of the container group.

Wait for a moment, you can see that the current CPU utilization rate has increased significantly.
Using ACK and NAS to quickly build elastic nginx website
Wait a few minutes and check again. It is found that the current number of copies has changed to 10.
Using ACK and NAS to quickly build elastic nginx website

c. At this time, use the browser to open the website and find that it can still be accessed normally.

Using ACK and NAS to quickly build elastic nginx website

  1. Release the container from high load.
    a. Enter cloudshell command line.
    b. Execute the following command on the command line.