Deploy a stateless application with an Nginx image

更新时间:
复制 MD 格式

This topic shows how to quickly deploy a stateless application with an Nginx image. You can create an application from the console, pull the Nginx image, and create a service and an Ingress to configure the network. Then, you can access the application through a domain name.

Prerequisites

You need a Container Compute Service (ACS) cluster in which no Ingress is bound to the domain namefoo.bar.com.

Step 1: Create an Nginx application

  1. Log on to the ACS console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Workloads > Deployments.

  3. On the Deployments page, click Create from Image in the upper-right corner. Configure the application by using the parameters in the following table. This table describes only the key parameters; you can keep the default values for all others.

    Wizard step

    Description

    Example value

    Instructions

    Basic Information

    Set the application name and click Next.

    nginxtest

    Set Application Name to nginxtest and click Next.

    Container

    Select an Nginx image:

    1. In the Image Name section, click Select Image.

    2. Click Artifact center.

    3. Search for nginx.

    4. Select the nginx image, click Select Image Version , select a version, and then click OK .

    5. Configure other settings as needed and click Next.

    nginx

    Advanced

    Create a service:

    1. To the right of Services, click Create.

    2. Add a port mapping.

      • Name: http

      • Service Port: 80

      • Container Port: 80

    3. Click Create.

    • Service Name: nginxtest-svc

    • Port Name: http

    • Service Port: 80

    • Container Port: 80

    For Type, select Load Balancer and Public Access. Select New SLB (the specification defaults to slb.s1.small). For External Traffic Policy, select Local.

    Create an Ingress (optional):

    1. To the right of Ingresses, click Create.

    2. Set Domain to foo.bar.com. Under Service, select the service you just created from the Name drop-down list.

    3. Set Ingress Class to the name of the Ingress Class created by the Application Load Balancer (ALB). For more information, see Quick start for ALB Ingresses.

    4. Click Create.

    • Domain: foo.bar.com

    • Name: nginxtest-svc

    In the Create Ingress form, set Name to nginxtest-ingress and select 80 for Port.

    Configure Horizontal Pod Autoscaler (HPA):

    1. Enable HPA.

    2. Set Min. Replicas to 2.

    3. Then, click Create.

    • HPA: Enabled

    • Minimum Replicas: 2

    Set Metric to CPU Usage, the Trigger Condition to 70%, and Maximum Replicas to 10.

The application is created. The deployment, service, Ingress (nginxtest-ingress), and HPA are now active. Click View Application Details to view the application details page.

Step 2: Access the test website

Configure the hosts file

  1. Log on to the ACS console. In the left navigation pane, click Clusters.

  2. On the Clusters page, click the name of the target cluster. In the left navigation pane, choose Workloads > Deployments.

  3. On the Deployments page, click the name of the application that you created in Step 1.

  4. Click the Access Method tab and record the External Endpoint of the Service.

  5. Locate the hosts file on your computer and add a binding entry in the following format: Gateway IP addressDomain name. For example, 39.104.XX.XX foo.bar.com. You can then access the service directly by using the domain name.

    Windows

    1. Open File Explorer and enter C:\WINDOWS\system32\drivers\etc in the address bar to navigate to the etc directory. Locate the hosts file.

    2. Open the hosts file in Notepad, add 39.104.XX.XX foo.bar.com to the last line, and then save the file.

    macOS

    1. From the top menu bar, choose Go > Go to Folder, enter /private/etc, and press Enter.

    2. Find the hosts file and copy it to your desktop to modify it.

    3. Right-click the hosts file on your desktop, select Open With, and then click TextEdit.

    4. Add 39.104.XX.XX foo.bar.com to the last line of the hosts file. After saving your changes, move the edited file from your desktop back to the /private/etc folder, replacing the original file.

  6. Open the website in a browser.

    Open a browser and clear its cache, or open a private browsing window. Enter the domain name http://foo.bar.com to open the website. After you access foo.bar.com, the page displays Welcome to nginx!, which confirms that the hosts entry and service deployment were successful.

ALB DNS resolution

  1. On the Deployments page, click the name of the application that you created in Step 1.

  2. Click the Access Method tab and record the Endpoint of the Ingress.

  3. Next, add a CNAME record.

  4. Log in to the DNS Console.

  5. On the Manage DNS page, click Add Zone.

  6. In the Add Zone dialog box, enter your host domain and click OK.

Important

You must complete TXT record verification for your host domain.

  1. In the Actions column of the target domain, click Settings.

  2. On the Settings page, click Add Record.

  3. In the Add Record panel, configure the following parameters to add the CNAME record, and then click OK.

Parameter

Description

Record Type

From the drop-down list, select CNAME.

Hostname

The prefix for your domain name, such as www.

Query Source

Retain the default value.

Record Value

Enter the CNAME address for the domain name, which is the endpoint you copied.

TTL

TTL stands for Time To Live. This value specifies how long a DNS record is cached on a DNS server. For this tutorial, keep the default value.

  1. Open the website in a browser.

In a new private browsing window, navigate to http://foo.bar.com.

Important

If you receive a message similar to "This website is inaccessible because it has not been filed with the Ministry of Industry and Information Technology (MIIT) as required by law," check if the load balancer (LB) for the Ingress has a public IP address. If it does, you must complete the ICP filing for your domain name with the MIIT. The domain can be accessed only after the filing is approved.