In an OpenStack Cloud environment, the default IP assignment mechanism is through DHCP. What if you wanted to create an instance with a fixed static IP address in OpenStack?. This can be achieved by using network ports. A port is a connection point for attaching a single device, such as the NIC of a server, to an OpenStack network. A network port also describes the associated network configuration, such as the MAC and IP addresses to be used on that port.
![Create OpenStack Instance With a Fixed / Static IP 1 openstack create server instance fixed ip address](https://neveropen.co.za/wp-content/uploads/2019/09/openstack-create-server-instance-fixed-ip-address.png?ezimgfmt=ng%3Awebp%2Fngcb23%2Frs%3Adevice%2Frscb23-1)
In OpenStack Networking, Virtual routers also use ports to interconnect with subnets. The state of these ports can be reviewed to determine if they’re connecting as expected or not.
Step 1: Create a network port
Use the command openstack port create
to create a new port on specified network.
See all available command arguments.
$ 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 <integer>] [--fit-width]
[--print-empty] --network <network>
[--description <description>]
[--device <device-id>]
[--mac-address <mac-address>]
[--device-owner <device-owner>]
[--vnic-type <vnic-type>] [--host <host-id>]
[--dns-name dns-name]
[--fixed-ip subnet=<subnet>,ip-address=<ip-address> | --no-fixed-ip]
[--binding-profile <binding-profile>]
[--enable | --disable] [--project <project>]
[--project-domain <project-domain>]
[--security-group <security-group> | --no-security-group]
[--qos-policy <qos-policy>]
[--enable-port-security | --disable-port-security]
[--allowed-address ip-address=<ip-address>[,mac-address=<mac-address>]]
[--tag <tag> | --no-tag]
<name>
......
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’ll create a port with a fixed IP address on the private network.
openstack port create --network private --fixed-ip subnet=private_subnet,ip-address=10.10.1.20 server1-port0
Confirm port creation:
openstack port list
openstack port show server1-port0
Step 2: Create a Server with fixed IP in OpenStack
Now that the port is created, we can boot a new instance with preserved IP address.
openstack server create \
--image CentOS-7 \
--flavor m1.small \
--security-group 7fffea2a-b756-473a-a13a-219dd0f1913a \
--port server1-port0 \
server1
Creation 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 server status.
$ openstack server show server1
+-------------------------------------+----------------------------------------------------------+
| Field | Value |
+-------------------------------------+----------------------------------------------------------+
| OS-DCF:diskConfig | MANUAL |
| OS-EXT-AZ:availability_zone | nova |
| OS-EXT-SRV-ATTR:host | dserver.neveropen.co.za |
| OS-EXT-SRV-ATTR:hypervisor_hostname | dserver.neveropen.co.za |
| 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 | |
+-------------------------------------+----------------------------------------------------------+
Your server created with a fixed IP on OpenStack should be reachable.
$ 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':
[centos@server1 ~]$ 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 other articles on OpenStack:
How To Create OpenStack projects, users and roles
How To Migrate OpenStack Instance from one Compute Host to Another
How To Resize OpenStack Instance / Virtual Machine
How To Configure OpenStack Instances / VMs to Autostart after Nova compute reboot