如何将多个公共IP分配给AWS EC2实例?

时间:2017-12-27 08:01:34

标签: amazon-web-services

我有一个m4.4xlarge实例,我最初为其分配了一个弹性IP。此实例的安全组允许SSH访问,还允许访问端口8000上的Web应用程序。

现在我点击EC2实例,选择:Actions>网络>管理IP地址。然后我分配了一个新的私有IP。

然后我创建了一个新的弹性IP地址,并将其映射到网络接口的新分配的私有IP。现在我可以在EC2实例描述中看到弹性IP显示旧的和新的弹性IP。但IPv4公共IP字段仍显示旧的IP地址。

虽然我仍然能够使用旧的弹性IP SSH实例,但我无法使用新的弹性IP。此外,我无法使用新的弹性IP访问端口8000上的Web应用程序。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

这是我编写的脚本,用于使其与其他网络接口一起使用并使更改在RHEL / Centos上持久存在-

#!/bin/bash
# On AWS With multiple network cards with the default route tables the outbound public traffic keeps going out via the default interface
# This can be tested by running tcpdump on default interface and then sending a ping to the 2nd interface
# The second address will try to send return traffic via the 1st interface
# To fix this need to create a rule to direct traffic from second address through the 2nd network interface card
# Also creating a systemd service that will create the rules and routes on boot and also
# adding to the network.service so the script is also called when starting network


    # User inputs
    INTERFACE1="eth0"
    INTERFACE2="eth1"
    IP1=10.0.0.70/32
    IP2=10.0.5.179/32
    ROUTER1=10.0.0.1
    ROUTER2=10.0.5.1
    # End of user inputs

    if [[ $EUID != "0" ]]
    then
        echo "ERROR. You need root privileges to run this script"
        exit 1
    fi


    # Create the file that will be called by the systemd service
    rm -rf /usr/local/src/routes.sh
    cat << EOF > /usr/local/src/routes.sh
    #!/bin/bash
    # Adding the routes for the 2nd network interface to work correctly
    ip route flush tab 1 >/dev/null 2>&1
    ip route flush tab 2 >/dev/null 2>&1
    ip rule del priority 500 >/dev/null 2>&1
    ip rule del priority 600 >/dev/null 2>&1
    ip route add default via $ROUTER1 dev $INTERFACE1 tab 1
    ip route add default via $ROUTER2 dev $INTERFACE2 tab 2
    ip rule add from $IP1 tab 1 priority 500
    ip rule add from $IP2 tab 2 priority 600
    EOF
    chmod a+x /usr/local/src/routes.sh
    # End of file with new routes and rules


    # Create a new systemd service
    rm -rf /etc/systemd/system/multiple-nic.service
    cat << EOF > /etc/systemd/system/multiple-nic.service
    [Unit]
    Description=Configure routing for multiple network interface cards
    After=network-online.target network.service
    [Service]
    ExecStart=/usr/local/src/routes.sh
    [Install]
    WantedBy=network-online.target network.service
    EOF
    # End of new systemd service
    echo "New systemd service - multiple-nic.service created"

    systemctl enable multiple-nic.service

    systemctl restart network
    echo "Network restarted successfully"