Using a Public NAT Gateway to Enable Servers to Share an EIP to Access the Internet
Scenarios
When there is a large number of ECSs running in a VPC, assigning an EIP to each of them reduces management efficiency and raises exposure to potential scans and attacks.
To address these issues, you can use a public NAT gateway to centrally manage the Internet ingress and egress:
- Add an SNAT rule and configure an EIP to allow all ECSs in a VPC to share this EIP to access the Internet, and keep all resources in the VPC running properly.
- Unbind the EIP from each ECS and add a DNAT rule to minimize port exposure risks and keep access mode unchanged.
Solution Architecture
In enterprise applications, backend services are usually centrally managed to improve service availability and management efficiency. However, directly exposing backend services to the Internet brings security risks. If your servers need to access the Internet, use the following solution to centrally manage the Internet ingress and egress:
- Use a load balancer as the unified ingress to distribute external requests across backend servers.
- Use a public NAT gateway as the unified egress, as shown in Figure 1. This solution helps you centrally manage backend services, reduce security risks, and improve O&M efficiency.
Resource Planning
In this practice, you need to create a VPC, three ECSs, a public NAT gateway, and a load balancer. For details about the resource planning, see Table 1.
| Resource | Quantity | Description |
|---|---|---|
| VPC and subnet | One VPC with a subnet |
|
| Public NAT gateway | 1 | In this practice, the NAT gateway is named nat_public. You can add an SNAT rule to this NAT gateway to allow ECSs to share an EIP to access the Internet. |
| Load balancer | 1 | This load balancer distributes external traffic across ECSs. |
| ECS | 3 | ECS-A, ECS-B, and ECS-C are deployed in subnet-A01. |
| EIP | 3 |
|
Preparations
- Buy three EIPs (EIP-ELB, EIP-SNAT, and EIP-DNAT), as shown in Table 1.
- Buy a load balancer and three ECSs, and deploy applications on these ECSs. Add two ECSs as backend servers of the load balancer. For details, see Using ELB to Distribute Traffic to a Web Application Across ECSs.
Step 1: Create a Public NAT Gateway
- Go to the Buy Public NAT Gateway page.
- Configure the required parameters. For details, see Buying a Public NAT Gateway. Figure 2 Buying a public NAT gateway
Step 2: Add an SNAT Rule
ECSs in subnet-A01 can share the EIP configured for the SNAT rule to access the Internet.
- Go to the public NAT gateway list page.
- On the Public NAT Gateways page, click the public NAT gateway purchased in Step 1.
The public NAT gateway details page is displayed.
- On the SNAT Rules tab, click Add SNAT Rule.
- Add an SNAT rule as prompted. Set Public IP Address Type to EIP and select EIP-SNAT. Figure 3 Adding an SNAT rule
- Click OK.
Step 3: Add a DNAT Rule
ECSs in subnet-A01 can share the EIP configured for the DNAT rule to provide services accessible from the Internet.
- Go to the public NAT gateway list page.
- On the Public NAT Gateways page, click the public NAT gateway purchased in Step 1.
The public NAT gateway details page is displayed.
- On the DNAT Rules tab, click Add DNAT Rule.
- Add a DNAT rule as prompted.
- Click OK.
Step 4: Verify Network Connectivity
- Remotely log in to the ECS where the backend service is deployed.
Multiple methods are available for logging in to an ECS. For details, see Logging In to an ECS.
- Ping the three EIPs on the service ECS to verify that the ECSs can be accessed by the Internet clients. Figure 5 Successful Internet access to the server
- Run the following command on each of the three ECSs to obtain the IP address used by the ECSs to access the Internet:
As shown in Figure 6, EIP-SNAT is returned for each of the ECS.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot


