如何使用固定/静态IP地址创建OpenStack实例

在OpenStack Cloud环境中,默认IP分配机制是通过DHCP。如果要在OpenStack中创建具有固定静态IP地址的实例怎么办?这可以通过使用 网络端口。 港口 用于将单个设备(例如服务器NIC)连接到OpenStack网络的连接点。网络端口还描述了相关的网络配置,例如该端口使用的MAC地址或IP地址。

在OpenStack网络中,虚拟路由器还使用端口与子网互连。您可以检查这些端口的状态,以确定它们是否按预期连接。

步骤1:创建一个网络端口

使用命令 openstack port create 在指定的网络上创建一个新端口。

查看所有可用的命令参数。

$ 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]
                             
......

列出可用的网络和子网。

$ 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 |
+--------------------------------------+----------------+--------------------------------------+--------------+

创建具有固定IP地址的端口 私人的 通讯网络。

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

确认端口创建。

$ openstack port list
$ openstack port show server1-port0

步骤2:在OpenStack中使用静态IP创建服务器

现在已经创建了端口,您可以使用保存的IP地址启动新实例。

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

创建输出:

+-------------------------------------+-------------------------------------------------+
| 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                    |                                                 |
+-------------------------------------+-------------------------------------------------+

创建后,检查服务器的状态。

$ 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                    |                                                          |
+-------------------------------------+----------------------------------------------------------+

OpenStack需要能够访问使用固定IP创建的服务器。

$  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

查看其他OpenStack文章。

如何创建OpenStack项目,用户和角色

如何将OpenStack实例从一台计算主机迁移到另一台计算主机

如何调整OpenStack实例/虚拟机的大小

如何将OpenStack实例/ VM配置为在Nova计算重启后自动启动

Sidebar