The scene is introduced

This article describes how to build a simple, resilient and highly available NGINX website in half an hour through the Ali Cloud container ACK service and file storage NAS service. At the end of this article, you will have a single-page web site, where user requests will be scattered across multiple container nodes and automatically scaled up based on the service load, so that users will not be affected even if one container node goes down. In addition, you can quickly update locally edited pages to the website.

Background knowledge

The following cloud products are used in this tutorial:

Cloud server ECS

The Elastic Compute Service (ECS) is an Infrastructure as a Service (IaaS) cloud computing Service that aliyun provides with excellent performance, reliability, and Elastic scalability. Cloud server ECS eliminates the need for you to purchase IT hardware. IT allows you to use the server as easily and efficiently as you use public resources such as water, electricity, and natural gas, realizing the out-of-the-hole and elastic scaling of computing resources. Aliyun ECS continues to provide innovative servers to solve various business needs and help your business development.

File Storage NAS

Ali Cloud Network Attached Storage (NAS) is a file Storage service for computing nodes such as Ali Cloud ECS instances, E-HPC, and container services. NAS provides simple, scalable file storage for use with ECS, multiple ECS instances can access the NAS file system simultaneously, and storage capacity automatically grows and shrinks flexibly as you add and remove files, providing a common data source for workloads and applications running on multiple instances or servers.

Container service Kubernetes edition

Alibaba Cloud Container Service for Kubernetes (ACK) is the world’s first Service platform approved by Kubernetes consistency certification, providing high-performance Container application management services. Support for enterprise-class Kubernetes container application lifecycle management, allowing you to easily and efficiently run Kubernetes container applications in the cloud.

Complete the first five steps of this seven-step tutorial to achieve a resilient and highly available NGINX web site, and the last two steps to verify the resilient and highly available properties of the web site.

Step 1: Create resources Step 2: Mount the file system NAS to the ECS server Step 3: upload the file to the NAS Step 4: Configure the NAS mount information Step 5: create the NGINX application Step 6: Access the test website Step 7: Verify the high availability of the service Step 8: Verify the elastic capacity expansion

Step 1: Start the experience

Cloud resources product experience address: developer.aliyun.com/adc/scenari…

Start the cloud product resource

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

Ali Cloud file storage NAS is a shareable, scalable, highly reliable and high-performance distributed file system. It provides a persistent storage service for the container. In the following operation, your web page files will be saved in the NAS file system. When the pod is created, you can directly call the FILES in the NAS file system. After the POD is destroyed, the FILES in the NAS file system will remain. 1. Log in to the NAS file system console using the subaccount provided by the resource (you can use the non-trace mode of the browser). 2. Click the file system list on the left boot bar and click the file system ID to go to the file system details page. 3. Click Mount Use, view the mount point information, and copy the mount command.4. Open the terminal tool and run the SSH [username]@[ipaddress] command. You need to replace the username and ipAddress with the public IP address of the ECS server provided by the resource. Such as:

SSH [email protected]Copy the code

The command output is as follows:5. Run the following command on the terminal to mount the NAS to the ECS server. Install an NFS client.

sudo yum install nfs-utils
Copy the code

Run the mount command replicated in Step 3 to mount the NAS to the/MNT directory of the 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
Copy the code

Step 3: Upload files to the NAS

After you set up the NGINX service using the ACK cluster, when you open the homepage of the website, the container will read the uploaded webpage files from the NAS file system and return them to the browser. After the website is set up, you can use the same method to update files in the NAS. 1. Create the index. HTML file locally.

  • Windows: Open a text editor, enter Test Index page for nginx-nas-demo, and save the file as AN HTML file.
  • MacOS or Linux: Open the command line tool and run the following command.
mkdir -p ~/Documents/nginx-nas-demo
echo "test index page for nginx-nas-demo" > ~/Documents/nginx-nas-demo/index.html
Copy the code

2. Download and install the SFTP client, for example, FileZilla. 3. Upload the index.html file to the NAS.

  • Double-click FileZila to run it.
  • Connect to the server based on the following information. Host: ECS public IP address provided by the resource. User name: root Password: ECS password provided by the resource. Port: 22.
  • In the left directory tree, locate the locally created index.html file. In the right directory tree, enter/MNT to go to the NAS directory.
  • To upload the local file to the NAS, drag and drop the index. HTML from the left to the right.

4. You can use step 2 to remotely connect to the ECS and view the newly uploaded index.html file in the/MNT directory.

Step 4: Configure the NAS mount information

To use the ACK service to mount the NAS, you need to first configure the container storage volume PV and storage declaration PVC information, which will be used when you deploy the NGINX application. 1. Configure storage volumes. The ACK cluster list is displayed. Click the NAME of the ACK cluster to go to the details page. Click the volume in the left boot bar. Click the Storage Volume TAB, and then click Create. Select the TYPE of the NAS storage volume, enter the name of the storage volume, select the NAS mount point, and click Create. The following figure shows the operation process. Figure 1:Figure 2:2. Configure the storage declaration. 1. On the navigation bar on the left, click the storage volume. 2. Click the storage declaration and click Create. 3. Configure a storage statement based on the following information: Storage declaration type: SELECT NAS. Name: Enter the storage declaration name, for example, NAS-PVC. Allocation mode: Select an existing storage volume. Existing volume: Select the storage volume created in the previous step. Total: This column has no actual limit for NAS. Keep the default value.

Step 5: Create an NGINX application

1. On the cluster details page, click workload in the left boot bar. On the default stateless TAB page, click Create with Mirror.

2. On the basic application information page, enter an application name and click Next.3. Configure containers. A. On the container configuration page, click to select a mirror.B. In the dialog box that is displayed, click Search, select the Docker image, enter nginx, and click Search. C. Select the nGINx official image from the search results and click OK.D. Scroll down to the bottom of the container configuration page, click PersistentVolumeClaim in the data volume column, select the storage declaration created in Step 2 as the mount source, and enter /usr/share/nginx/html/ in the container path.Note This configuration will mount the NAS to the /usr/share/nginx/ html-/ directory of the container, which is the default directory where nginx stores index.html.

E. Use the default Settings for other parameters on the page and click Next.

  • Advanced configuration. A. On the advanced configuration page, click on the right of Service. Set the following information in the Port mapping that pops up in the Create Service dialog, and then click Create. Name: HTTP (HTTP must be all lowercase, otherwise the creation will fail).

Service port: 80. Container port: 80. Protocol: TCP.Note Retain the default values for other options.

B. Click to create the Ingress route. In the create dialog box that is displayed, configure the following information in the rule item, and then click Create.Copy the code
  • Domain name: your site domain name, for example: foo.bar.com. Note If you do not have a domain name, you can fill in a test domain name.

  • Service: Select the nginx-nas-svc service created in the previous step.

Note Retain the default values for other options. In this case, the route column should display the domain name and the corresponding service information. The routing function breaks up user requests for processing across multiple container nodes by simply fanning them out. C. Select Open on the right of container group horizontal scaling and select the minimum pairWait until the creation is successful.

Step 6: Visit the test site

Once the NAS and containers are configured, you can use your local computer to access the test site. 1. Configure the hosts file on the office computer.Note: If you are in the NGINX application configuration, using the official website domain name, and has been registered, you can skip this step, directly through the browser to open the website. If you are using a test domain name (such as foo.bar.com), you will need to configure the hosts file on your local computer and have the browser redirect requests 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 name of the NGINX service created in Step 3.C. Click the Access Mode TAB and copy the IP address of the endpoint of the route to the stickboard. The IP address is used to access the website from the public network.D. Open the local C:\Windows\System32\drivers\etc\hosts file with the administrator permission, add the endpoint IP address and the domain name set in step three to the hosts file, save the file and exit, as shown in the following figure.Note On Linux and Mac, modify the /etc/hosts file.

2. Open the website using a browser.Note: If access is not normal, you can try to clear the browser cache, or use the non-trace browsing mode access.

Step 7: Verify that the service is highly available

When NGINX applications are deployed, the minimum number of copies of the container group is 2. Therefore, when one container host node is down, the website can still be provided by another container host node, ensuring the high availability of services. 1. The simulation node is faulty. A. On the cluster details page, click Node Management in the left navigation bar. The node management page is displayed. B. Select two nodes and click Node Maintenance. In the dialog box that is displayed, select the nodes to be emptied and click OK.At this point, you can see that the node status is unschedulable.C. Open the website using a browser and find that the website is still accessible.2. Restore the node. A. Click in the operation column. In the dialog box that is displayed, click OK. B. Open the website using a browser and find that the website is still accessible.

Step 8: Verify elastic capacity expansion

1. View the initial status of the container group. A. On the workload page of the ACK console, click the name of the NGINX service created in Step 3. B. Click the container group Horizontal scaler to view the current number of copies and the current CPU usage of the container group.2. Simulate high container load. A. On the cluster details page, click to manage the cluster through CloudShell in the upper right corner and go to the CloudShell command line interface (CLI).B. On the command line interface (CLI), run the following command to view the nodes.

kubectl get pod

Copy the code

C. Run the following command to go to the container.

kubectl exec -it lalala-***pod-name****-kxjbg bash

Copy the code
D. Run the following command to simulate a high container load.Copy the code
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; readCopy the code

3. Confirm the automatic expansion. A. On the workload page of the ACK console, click the name of the NGINX service created in Step 3. B. Click the container group Horizontal scaler to view the number of copies and the CPU usage of the container group. Wait a little while and you can see that the current CPU usage is significantly higher.Wait a few minutes and check again. The current number of copies is 10.C. Open the website using a browser and find that the website is still accessible.4. Remove the high load from the container. A. Go to the CloudShell command line interface. B. On the CLI, run the following command: