Create a Network in OpenStack
Creating an internal network in OpenStack involves several steps: creating the network itself, creating a subnet within the network, and configuring a router if necessary.
Step 1: Source Your OpenStack RC File
First, source your OpenStack RC file to set the necessary environment variables for authentication.
sudo -i -u stack
cd ~/devstack
source openrc <USERNAME> <PROJECT_NAME>
Step 2: Create a Network
Use the openstack network create
command to create a new network.
openstack network create <NETWORK_NAME>
Step 3: Create a Subnet
Create a subnet within the network using the openstack subnet create
command. You need to specify the network ID or name, the subnet name, the subnet range (CIDR), and the DNS nameservers.
openstack subnet create --network <NETWORK_NAME> --subnet-range <SUBNET_CIDR> --dns-nameserver <DNS_NAMESERVER> <SUBNET_NAME>
For example,
openstack subnet create --network my-network --subnet-range 192.168.10.0/24 --dns-nameserver 8.8.8.8 my-subnet
Step 4 (Optional): Create an External Network
Use the openstack network create
command with the --external
flag to mark the network as external. You have to log in as admin to create an external network.
openstack network create --external <EXTERNAL_NETWORK_NAME>
Next, create a subnet for the external network using the openstack subnet create
command. You'll need to provide the network ID or name, subnet range, gateway, and other relevant details.
openstack subnet create --network <NETWORK_NAME> --subnet-range <SUBNET_CIDR> --gateway <GATEWAY_IP> --allocation-pool start=<START_IP_ADDRESS>,end=<END_IP_ADDRESS> --no-dhcp <SUBNET_NAME>
For example,
openstack subnet create --network new-public --subnet-range 192.168.2.0/24 --gateway 192.168.2.1 --allocation-pool start=192.168.2.2,end=192.168.2.254 --no-dhcp new-public-subnet
Step 5: Attach the Network to a Router
If you need instances in this internal network to access external networks (e.g., the Internet), you need to attach the network to a router. This step involves creating a router and adding the internal subnet to it.
First, create a router.
openstack router create <ROUTER_NAME>
Find an external network.
openstack network list --external
Set the external gateway for the router to enable outbound traffic. The router gateway will be used to translate (route) traffic from internal OpenStack networks to external networks.
openstack router set --external-gateway <EXTERNAL_NETWORK> <ROUTER_NAME>
Finally, add the internal subnet to the router.
openstack router add subnet <ROUTER_NAME> <SUBNET_NAME>