How to create an OpenStack instance with a fixed / static IP address

In the OpenStack Cloud environment, the default IP allocation mechanism is through DHCP. What if you want to create an instance with a fixed static IP address in OpenStack? This can be done by using network port. A kind port A connection point used to connect a single device (such as a server’s NIC) to an OpenStack network. The network port also describes the relevant network configuration, such as the MAC and IP addresses to be used on the port.

In OpenStack networks, virtual routers also use ports to interconnect with subnets. You can check the status of these ports to determine if they are connected as expected.

Step 1: Create a network port

Use command openstack port create Creates a new port on the specified network.

View all available command parameters.

$ openstack port create --help
.....
openstack port create --help
usage: openstack port create [-h] [-f {json,shell,table,value,yaml}]
                             [-c COLUMN] [--noindent] [--prefix PREFIX]
                             [--max-width ] [--fit-width]
                             [--print-empty] --network 
                             [--description ]
                             [--device ]
                             [--mac-address ]
                             [--device-owner ]
                             [--vnic-type ] [--host ]
                             [--dns-name dns-name]
                             [--fixed-ip subnet=,ip-address= | --no-fixed-ip]
                             [--binding-profile ]
                             [--enable | --disable] [--project ]
                             [--project-domain ]
                             [--security-group  | --no-security-group]
                             [--qos-policy ]
                             [--enable-port-security | --disable-port-security]
                             [--allowed-address ip-address=[,mac-address=]]
                             [--tag  | --no-tag]
                             
......

List available networks and subnets:

$ openstack network list
+--------------------------------------+---------+--------------------------------------+
| ID                                   | Name    | Subnets                              |
+--------------------------------------+---------+--------------------------------------+
| b94431cb-08cf-42ea-be61-55f5cf459276 | private | 57601b99-ea64-41a8-a927-fbd591ae3f2b |
| f7ccac3b-73eb-49bf-a4ec-af750216b819 | public  | 7536e4a8-6aa8-45dc-aed6-1a98afcf416d |
+--------------------------------------+---------+--------------------------------------+

$ openstack subnet list --network private
+--------------------------------------+----------------+--------------------------------------+--------------+
| ID                                   | Name           | Network                              | Subnet       |
+--------------------------------------+----------------+--------------------------------------+--------------+
| 57601b99-ea64-41a8-a927-fbd591ae3f2b | private_subnet | b94431cb-08cf-42ea-be61-55f5cf459276 | 10.10.1.0/24 |
+--------------------------------------+----------------+--------------------------------------+--------------+

I will create a port with a fixed IP address on the port private The internet.

openstack port create --network private --fixed-ip 
subnet=private_subnet,ip-address=10.10.1.20 server1-port0

Confirm the port creation:

$ openstack port list
$ openstack port show server1-port0

Step 2: Create a server with a fixed IP in OpenStack

Now that the port has been created, we can use the reserved IP address to boot the new instance.

openstack server create 
 --image CentOS-7 
 --flavor m1.small 
 --security-group  7fffea2a-b756-473a-a13a-219dd0f1913a  
 --port server1-port0 
 server1

Create output:

+-------------------------------------+-------------------------------------------------+
| Field                               | Value                                           |
+-------------------------------------+-------------------------------------------------+
| OS-DCF:diskConfig                   | MANUAL                                          |
| OS-EXT-AZ:availability_zone         |                                                 |
| OS-EXT-SRV-ATTR:host                | None                                            |
| OS-EXT-SRV-ATTR:hypervisor_hostname | None                                            |
| OS-EXT-SRV-ATTR:instance_name       |                                                 |
| OS-EXT-STS:power_state              | NOSTATE                                         |
| OS-EXT-STS:task_state               | scheduling                                      |
| OS-EXT-STS:vm_state                 | building                                        |
| OS-SRV-USG:launched_at              | None                                            |
| OS-SRV-USG:terminated_at            | None                                            |
| accessIPv4                          |                                                 |
| accessIPv6                          |                                                 |
| addresses                           |                                                 |
| adminPass                           | wsDLy8PG6q9C                                    |
| config_drive                        |                                                 |
| created                             | 2019-09-19T10:23:07Z                            |
| flavor                              | m1.small (1)                                    |
| hostId                              |                                                 |
| id                                  | 9c74c4e9-e996-4397-be9b-4fe81aa51881            |
| image                               | CentOS-7 (b8d75f31-fb6b-428b-8b83-f3904b0bd114) |
| key_name                            | jmutai                                          |
| name                                | server1                                         |
| progress                            | 0                                               |
| project_id                          | 06bcc3c56ab1489282b65681e782d7f6                |
| properties                          |                                                 |
| security_groups                     | name='7fffea2a-b756-473a-a13a-219dd0f1913a'     |
| status                              | BUILD                                           |
| updated                             | 2019-09-19T10:23:07Z                            |
| user_id                             | 336acbb7421f47f8be4891eabf0c9cc8                |
| volumes_attached                    |                                                 |
+-------------------------------------+-------------------------------------------------+

After creation, check the server status.

$ openstack server show server1
+-------------------------------------+----------------------------------------------------------+
| Field                               | Value                                                    |
+-------------------------------------+----------------------------------------------------------+
| OS-DCF:diskConfig                   | MANUAL                                                   |
| OS-EXT-AZ:availability_zone         | nova                                                     |
| OS-EXT-SRV-ATTR:host                | dserver.computingforgeeks.com                            |
| OS-EXT-SRV-ATTR:hypervisor_hostname | dserver.computingforgeeks.com                            |
| OS-EXT-SRV-ATTR:instance_name       | instance-00000028                                        |
| OS-EXT-STS:power_state              | Running                                                  |
| OS-EXT-STS:task_state               | None                                                     |
| OS-EXT-STS:vm_state                 | active                                                   |
| OS-SRV-USG:launched_at              | 2019-09-19T10:23:11.000000                               |
| OS-SRV-USG:terminated_at            | None                                                     |
| accessIPv4                          |                                                          |
| accessIPv6                          |                                                          |
| addresses                           | private=10.10.1.20                                       |
| config_drive                        |                                                          |
| created                             | 2019-09-19T10:23:07Z                                     |
| flavor                              | m1.small (1)                                             |
| hostId                              | da0a05ef20a03e97f301563551bab9b669ef04adbce232d941519946 |
| id                                  | 9c74c4e9-e996-4397-be9b-4fe81aa51881                     |
| image                               | CentOS-7 (b8d75f31-fb6b-428b-8b83-f3904b0bd114)          |
| key_name                            | jmutai                                                   |
| name                                | server1                                                  |
| progress                            | 0                                                        |
| project_id                          | 06bcc3c56ab1489282b65681e782d7f6                         |
| properties                          |                                                          |
| security_groups                     | name='default'                                           |
| status                              | ACTIVE                                                   |
| updated                             | 2019-09-19T10:23:11Z                                     |
| user_id                             | 336acbb7421f47f8be4891eabf0c9cc8                         |
| volumes_attached                    |                                                          |
+-------------------------------------+----------------------------------------------------------+

Servers created on OpenStack with a fixed IP should be accessible.

$  ping -c 2 10.10.1.20
 PING 10.10.1.20 (10.10.1.20) 56(84) bytes of data.
 64 bytes from 10.10.1.20: icmp_seq=1 ttl=64 time=0.768 ms
 64 bytes from 10.10.1.20: icmp_seq=2 ttl=64 time=0.491 ms
 --- 10.10.1.20 ping statistics ---

$ ssh [email protected]
 Warning: Permanently added '10.10.1.20' (ECDSA) to the list of known hosts.
 Enter passphrase for key '/home/centos/.ssh/id_rsa': 
 [[email protected] ~]$ ip ad
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
     inet6 ::1/128 scope host 
        valid_lft forever preferred_lft forever
 2: eth0:  mtu 1450 qdisc pfifo_fast state UP group default qlen 1000
     link/ether fa:16:3e:a3:fd:ce brd ff:ff:ff:ff:ff:ff
     inet 10.10.1.20/24 brd 10.10.1.255 scope global dynamic eth0
        valid_lft 86035sec preferred_lft 86035sec
     inet6 fe80::f816:3eff:fea3:fdce/64 scope link 
        valid_lft forever preferred_lft forever

Check out other articles about OpenStack:

How to create OpenStack projects, users and roles

How to migrate OpenStack instances from one compute host to another

How to resize an OpenStack instance / virtual machine

How to configure NovaStack to automatically start after restarting NovaStack

Sidebar