Add a router to connect instances to external subnets

Add a router to enable instance communication with external subnets.

Before you begin

  • The administrator has created a project for you and has provided you with a clouds.yaml file for you to access the cloud.
  • The python-openstackclient package is on your workstation.
    $ dnf list installed python-openstackclient

About this task

Instances need routers to communicate with external subnets, including those in the physical network. Routers and subnets connect using interfaces, with each subnet requiring its own interface to the router.

The default gateway of a router defines the next hop for any traffic received by the router. Its network is typically configured to route traffic to the external physical network by using a virtual bridge.

Procedure

  1. Confirm that the system OS_CLOUD variable is set for your cloud:
    $ echo $OS_CLOUD
    my_cloud

    Reset the variable if necessary:

    $ export OS_CLOUD=my_other_cloud

    As an alternative, you can specify the cloud name by adding the --os-cloud <cloud_name> option each time you run an openstack command.

  2. Create a router.
    $ openstack router create <router-name> --external-gateway <network> \
    --project <name>
    • Replace <router-name> with the name of the router you are creating.
    • Replace <network> with the name of an external network.
    • Replace <name> with the name of your project. Tip

      If you want to create a router in an availability zone (AZ) add the --availability-zone-hint <availability-zone> option. Repeat the option to set multiple AZs.

Results

  • Confirm that the router was created:
    $ openstack router list -c Name -c Status -c State -c Project
    Sample output
    +---------+--------+-------+----------------------------------+
    | Name    | Status | State | Project                          |
    +---------+--------+-------+----------------------------------+
    | router1 | ACTIVE | UP    | 24089d2fe1a94dd29ca2f665794fbe92 |
    | router2 | ACTIVE | UP    | ecf285621c509223ade3358691bbde59 |
    +---------+--------+-------+----------------------------------+

What to do next

Add a subnet to a router

Add a subnet interface to a router to enable traffic routing.

Before you begin

  • The administrator has created a project for you and has provided you with a clouds.yaml file for you to access the cloud.
  • The python-openstackclient package is on your workstation.
    $ dnf list installed python-openstackclient
  • You have access to a virtual router.

Procedure

  1. Confirm that the system OS_CLOUD variable is set for your cloud:
    $ echo $OS_CLOUD
    my_cloud

    Reset the variable if necessary:

    $ export OS_CLOUD=my_other_cloud

    As an alternative, you can specify the cloud name by adding the --os-cloud <cloud_name> option each time you run an openstack command.

  2. Obtain the virtual router name that you are using to route traffic to and from your network.

    Retain this name, because you will need it later:

    $ openstack router list
    Sample output
    +---------+--------+-------+----------------------------------+
    | Name    | Status | State | Project                          |
    +---------+--------+-------+----------------------------------+
    | router1 | ACTIVE | UP    | 24089d2fe1a94dd29ca2f665794fbe92 |
    | router2 | ACTIVE | UP    | ecf285621c509223ade3358691bbde59 |
    +---------+--------+-------+----------------------------------+
  3. Obtain the name of the subnet for which you want to add an interface for your router.

    Retain this name, because you will need it later:

    $ openstack subnet list -c Name -c Network -c Subnet --max-width=72
    Sample output
    +----------------------+-------------------------+---------------------+
    | Name                 | Network                 | Subnet              |
    +----------------------+-------------------------+---------------------+
    | private_subnet24     | 317be3d3-5265-43f7-b52b | 10.0.24.0/24        |
    |                      | -930e3fd19b8b           |                     |
    | lb-mgmt-subnet       | c4588d49-9151-414b-8832 | 172.24.0.0/16       |
    |                      | -37313e3b4c57           |                     |
    | external_subnet      | bcdb3cc0-8c0b-4d2d-813c | 10.0.0.0/24         |
    |                      | -e141bb97aa8f           |                     |
    | external_ipv6_subnet | bcdb3cc0-8c0b-4d2d-813c | 2620:52:0:13b8::/64 |
    |                      | -e141bb97aa8f           |                     |
    | private_subnet       | 317be3d3-5265-43f7-b52b | 10.0.1.0/24         |
    |                      | -930e3fd19b8b           |                     |
    +----------------------+-------------------------+---------------------+
  4. Using the router and subnet names, add an interface to your router for the subnet.
    Example

    In this example, private_subnet is added as an interface on router2:

    $ openstack router add subnet router2 private_subnet

Results

  • Confirm that the router lists the interface that was added:
    Example
    $ openstack router show router2 --max-width=72
    Sample output
    +-------------------------+--------------------------------------------+
    | Field                   | Value                                      |
    +-------------------------+--------------------------------------------+
    | admin_state_up          | UP                                         |
    | availability_zone_hints |                                            |
    | availability_zones      |                                            |
    | created_at              | 2024-09-09T06:27:48Z                       |
    | description             |                                            |
    | external_gateway_info   | {"network_id":                             |
    |                         | "bcdb3cc0-8c0b-4d2d-813c-e141bb97aa8f",    |
    |                         | "external_fixed_ips": [{"subnet_id":       |
    |                         | "6745edd4-d15f-4971-89bf-70307b0ad2f1",    |
    |                         | "ip_address": "10.0.0.167"}, {"subnet_id": |
    |                         | "cc3f81bb-4d55-4ead-aad4-5362a7ca5b04",    |
    |                         | "ip_address": "2620:52:0:13b8::1000:85"}], |
    |                         | "enable_snat": true}                       |
    | flavor_id               | None                                       |
    | id                      | 9119eade-cf28-42d7-a33d-eb589469bf62       |
    | interfaces_info         | [{"port_id":                               |
    |                         | "5a40b083-27d0-4691-8208-99c507181a33",    |
    |                         | "ip_address": "10.0.24.1", "subnet_id":    |
    |                         | "47d34cf0-0dd2-49bd-a985-67311d80c5c4"},   |
    |                         | {"port_id":                                |
    |                         | "642e522e-2cbd-47b5-8f8b-88c1b5d5e535",    |
    |                         | "ip_address": "10.1.2.1", "subnet_id":     |
    |                         | "7ee56cef-83c0-40d1-b4e7-5287dae1c23c"},   |
    |                         | {"port_id":                                |
    |                         | "9f695259-680c-40a8-bbed-9ca84dd77c33",    |
    |                         | "ip_address": "10.0.1.1", "subnet_id":     |
    |                         | "d7535565-113f-4192-baa6-da21f301f141"},   |
    |                         | {"port_id":                                |
    |                         | "f43a55a7-2e91-4cb7-a6b2-b3b686fc2ebd",    |
    |                         | "ip_address": "10.0.2.1", "subnet_id":     |
    |                         | "317be3d3-5265-43f7-b52b-930e3fd19b8b"}]   |
    | name                    | router2                                    |
    | project_id              | ecf285621c509223ade3358691bbde59           |
    | revision_number         | 7                                          |
    | routes                  |                                            |
    | status                  | ACTIVE                                     |
    | tags                    |                                            |
    | updated_at              | 2024-09-09T09:28:40Z                       |
    +-------------------------+--------------------------------------------+

Remove a subnet from a router

Remove a subnet interface from a router to disconnect that Layer 3 segment from the router..

Before you begin

  • The administrator has created a project for you and has provided you with a clouds.yaml file for you to access the cloud.
  • The python-openstackclient package is on your workstation.
    $ dnf list installed python-openstackclient
  • You have access to a virtual router.

Procedure

  1. Confirm that the system OS_CLOUD variable is set for your cloud:
    $ echo $OS_CLOUD
    my_cloud

    Reset the variable if necessary:

    $ export OS_CLOUD=my_other_cloud

    As an alternative, you can specify the cloud name by adding the --os-cloud <cloud_name> option each time you run an openstack command.

  2. Obtain the virtual router name that contains the interface to a subnet that you want to remove.

    Retain this name, because you will need it later:

    $ openstack router list
    Sample output
    +---------+--------+-------+----------------------------------+
    | Name    | Status | State | Project                          |
    +---------+--------+-------+----------------------------------+
    | router1 | ACTIVE | UP    | 24089d2fe1a94dd29ca2f665794fbe92 |
    | router2 | ACTIVE | UP    | ecf285621c509223ade3358691bbde59 |
    +---------+--------+-------+----------------------------------+
  3. Obtain the name of the subnet that you are using as the interface for your router.

    Retain this name, because you will need it later:

    $ openstack subnet list -c Name -c Network -c Subnet --max-width=72
    Sample output
    +----------------------+-------------------------+---------------------+
    | Name                 | Network                 | Subnet              |
    +----------------------+-------------------------+---------------------+
    | private_subnet24     | 317be3d3-5265-43f7-b52b | 10.0.24.0/24        |
    |                      | -930e3fd19b8b           |                     |
    | lb-mgmt-subnet       | c4588d49-9151-414b-8832 | 172.24.0.0/16       |
    |                      | -37313e3b4c57           |                     |
    | external_subnet      | bcdb3cc0-8c0b-4d2d-813c | 10.0.0.0/24         |
    |                      | -e141bb97aa8f           |                     |
    | external_ipv6_subnet | bcdb3cc0-8c0b-4d2d-813c | 2620:52:0:13b8::/64 |
    |                      | -e141bb97aa8f           |                     |
    | private_subnet       | 317be3d3-5265-43f7-b52b | 10.0.1.0/24         |
    |                      | -930e3fd19b8b           |                     |
    +----------------------+-------------------------+---------------------+
  4. Using the router and subnet names, remove the interface from your router.
    Example

    In this example, private_subnet is removed as an interface on router2:

    $ openstack router remove subnet router2 private_subnet

Results

  • Confirm that the subnet has been removed from the router:
    Example
    $ openstack router show router2 --max-width=72
    Sample output
    +-------------------------+--------------------------------------------+
    | Field                   | Value                                      |
    +-------------------------+--------------------------------------------+
    | admin_state_up          | UP                                         |
    | availability_zone_hints |                                            |
    | availability_zones      |                                            |
    | created_at              | 2024-09-09T06:27:48Z                       |
    | description             |                                            |
    | external_gateway_info   | {"network_id":                             |
    |                         | "bcdb3cc0-8c0b-4d2d-813c-e141bb97aa8f",    |
    |                         | "external_fixed_ips": [{"subnet_id":       |
    |                         | "6745edd4-d15f-4971-89bf-70307b0ad2f1",    |
    |                         | "ip_address": "10.0.0.167"}, {"subnet_id": |
    |                         | "cc3f81bb-4d55-4ead-aad4-5362a7ca5b04",    |
    |                         | "ip_address": "2620:52:0:13b8::1000:85"}], |
    |                         | "enable_snat": true}                       |
    | flavor_id               | None                                       |
    | id                      | 9119eade-cf28-42d7-a33d-eb589469bf62       |
    | interfaces_info         | [{"port_id":                               |
    |                         | "5a40b083-27d0-4691-8208-99c507181a33",    |
    |                         | "ip_address": "10.0.24.1", "subnet_id":    |
    |                         | "47d34cf0-0dd2-49bd-a985-67311d80c5c4"},   |
    |                         | {"port_id":                                |
    |                         | "642e522e-2cbd-47b5-8f8b-88c1b5d5e535",    |
    |                         | "ip_address": "10.1.2.1", "subnet_id":     |
    |                         | "7ee56cef-83c0-40d1-b4e7-5287dae1c23c"},   |
    |                         | {"port_id":                                |
    |                         | "9f695259-680c-40a8-bbed-9ca84dd77c33",    |
    |                         | "ip_address": "10.0.1.1", "subnet_id":     |
    |                         | "d7535565-113f-4192-baa6-da21f301f141"},   |
    | name                    | router2                                    |
    | project_id              | ecf285621c509223ade3358691bbde59           |
    | revision_number         | 7                                          |
    | routes                  |                                            |
    | status                  | ACTIVE                                     |
    | tags                    |                                            |
    | updated_at              | 2024-09-09T11:14:33Z                       |
    +-------------------------+--------------------------------------------+

Remove an unused router

Delete a router that has no connected interfaces. This frees up a router and keeps the environment tidy.

Before you begin

  • The administrator has created a project for you and has provided you with a clouds.yaml file for you to access the cloud.
  • The python-openstackclient package is on your workstation.
    $ dnf list installed python-openstackclient
  • No interfaces are connected to the router that you want to delete.

Procedure

  1. Confirm that the system OS_CLOUD variable is set for your cloud:
    $ echo $OS_CLOUD
    my_cloud

    Reset the variable if necessary:

    $ export OS_CLOUD=my_other_cloud

    As an alternative, you can specify the cloud name by adding the --os-cloud <cloud_name> option each time you run an openstack command.

  2. Obtain the name of the router that you want to delete.

    Retain this name, because you will need it later.

    $ openstack router list -c Name -c Status -c State -c Project
    Sample output
    +---------+--------+-------+----------------------------------+
    | Name    | Status | State | Project                          |
    +---------+--------+-------+----------------------------------+
    | router1 | ACTIVE | UP    | 24089d2fe1a94dd29ca2f665794fbe92 |
    | router2 | ACTIVE | UP    | ecf285621c509223ade3358691bbde59 |
    +---------+--------+-------+----------------------------------+
  3. Using the router name, delete the router.
    Example

    In this example, router2 is deleted:

    $ openstack router delete router2

Results

  • Confirm that the router was deleted:
    $ openstack router list -c Name -c Status -c State -c Project
    Sample output
    +---------+--------+-------+----------------------------------+
    | Name    | Status | State | Project                          |
    +---------+--------+-------+----------------------------------+
    | router1 | ACTIVE | UP    | 24089d2fe1a94dd29ca2f665794fbe92 |
    +---------+--------+-------+----------------------------------+