官术网_书友最值得收藏!

  • Amazon EC2 Cookbook
  • Sekhar Reddy Aurobindo Sarkar
  • 671字
  • 2021-07-30 10:14:16

Creating an instance with multiple NIC cards and a static private IP address

With multiple NICs, you can better manage your network traffic. Multiple NICs is one of the prerequisite for high availability. The number of NICs attached to the EC2 instance will depend on the type of EC2 instance. ENI's and multiple private IP addresses are only available for instances running in a VPC. In cases of instance failure, we can detach and then re-attach the ENI to a standby instance, where DNS changes are not required for achieving business continuity. We can attach multiple ENIs from different subnets to an instance, but they both should be in the same availability zone. This enables us to separate the public-facing traffic from the management traffic.

We can have one primary address and one or more secondary addresses for an NIC. We can detach and then attach NIC from one instance to another. We can attach one Elastic IP to each private address. When you launch an instance, a public IP address can be autoassigned to the network interface for eth0. This is possible only when you create a network interface for eth0 instead of using an existing network interface. You can detach secondary NIC (ethN) when an instance is running or stopped. However, you can't detach the primary (eth0) interface. In addition, you can attach security groups to NIC. If you set the instance termination policy to delete on termination, then the NIC will automatically be deleted, if you delete the EC2 instance.

How to do it…

Creating an instance with multiple NIC cards requires us to create a network interface, attach it to an instance, and finally associate the EIP to the ENI.

Creating a network interface

Use the following steps to create a network interface:

  1. Run the following command to create the ENI. You will need to provide the subnet ID, security group IDs, and one or more private IP addresses.
    $ aws ec2 create-network-interface 
    --subnet-id [SubnetId] 
    --groups [SecurityGroupIds]
    --private-ip-addresses [PrivateIpAddressList] 
    

    The parameters used in this command are described as follows:

    • [SubnetId]: This gives the ID of the subnet to associate with the network interface
    • [SecurityGroupIds]: This parameter provides IDs of one or more security groups
    • [PrivateIpAddressList]: This is used to show list of private IP addresses

      Syntax:

      PrivateIpAddress=string,Primary=boolean 
      
  2. Next, run the following command to create the ENI with private IP addresses 10.0.0.26 and 10.0.0.27:
    $ aws ec2 create-network-interface 
    --subnet-id subnet-aed11acb 
    --groups sg-ad70b8c8 
    --private-ip-addresses PrivateIpAddress=10.0.0.26,Primary=true PrivateIpAddress=10.0.0.27,Primary=false
    

In the next step, we attach the network interface to the instance.

Attaching the network interface to an instance

By running the following command, we can attach the ENI to an EC2 instance. You will need to provide the ENI ID, EC2 instance ID, and the device index.

$ aws ec2 attach-network-interface 
--network-interface-id [NetworkInterfaceId]
--instance-id [InstanceId]
--device-index [DeviceIndex]

The parameters used in this command are described as follows:

  • [NetworkInterfaceId]: This parameter provides the network interface ID to attach to an EC2 instance
  • [InstanceId]: This one provides an EC2 instance ID
  • [DeviceIndex]: This parameter provides the index of the device for the network interface attachment

Then, run the following command to attach the ENI to the EC2 instance:

$ aws ec2 attach-network-interface 
--network-interface-id eni-5c88f739 
--instance-id i-2e7dace3 
--device-index 1

Associating the EIP to the ENI

By running the following command, we can associate the EIP to the ENI. You have to provide the ENI ID, EIP allocation ID, and the private address.

$ aws ec2 associate-address
--network-interface-id [NetworkInterfaceId]
--allocation-id [AllocationId]
--private-ip-address [PrivateIpAddress]

The parameters used in this command are described as follows:

  • [NetworkInterfaceId]: This parameter provides the network interface ID to attach to an EC2 instance
  • [AllocationId]: This gives the allocation ID of EIP, which is required for EC2-VPC
  • [PrivateIpAddress]: If no private IP address is specified, the Elastic IP address is associated with the primary private IP address

Next, run the following command to associate the EIP to 10.0.0.26 (the private IP address of the ENI):

$ aws ec2 associate-address
--network-interface-id eni-5c88f739
--allocation-id eipalloc-d59f80b7
--private-ip-address 10.0.0.26

See also

  • The Configuring security groups recipe
主站蜘蛛池模板: 汪清县| 乌鲁木齐市| 尖扎县| 湟中县| 忻州市| 军事| 萨嘎县| 乐山市| 富宁县| 巴楚县| 凭祥市| 龙陵县| 万盛区| 辉县市| 昌平区| 定州市| 扬中市| 平利县| 胶南市| 红原县| 张北县| 方城县| 广元市| 洪泽县| 若尔盖县| 朔州市| 莱西市| 镇江市| 遂昌县| 海阳市| 贵港市| 遵义市| 富锦市| 光泽县| 田阳县| 庄浪县| 江陵县| 罗甸县| 大宁县| 资溪县| 若尔盖县|