OpenStack如何创建固定IP的虚拟机

题记

昨天同事有一个需求,希望我创建一个固定IP的虚拟机,因为他的代码写死IP了,所以不好修改,我的云环境是OpenStack,但是OpenStack的虚拟机都是通过DHCP获得,那么如何

创建一个固定IP的虚拟机呢?


环境

OpenStack icehouse

网络模式:FlatDHCP


步骤

首先需要说明一下,OpenStack不支持,你创建一个其他VM后,然后手动更改配置文件设置IP,这个VM是无法访问的。


1、先查看OpenStack子网的IP范围,由于我使用了扁平DHCP,子网范围135-200,如果设置的IP地址不在该范围内,也无法访问。

[html] view plain copy
  1. [email protected]:~# neutron subnet-list  
  2. +--------------------------------------+------+-----------------+------------------------------------------------------+  
  3. | id                                   | name | cidr            | allocation_pools                                     |  
  4. +--------------------------------------+------+-----------------+------------------------------------------------------+  
  5. | 68e5be93-03a4-42c5-8867-5ea2eb81246f |      | 192.168.12.0/24 | {"start": "192.168.12.135", "end": "192.168.12.200"} |  
  6. +--------------------------------------+------+-----------------+------------------------------------------------------+  


2、创建VM之前,我们需要创建一个端口(该端口需要了解在哪个租户下,哪个网络和子网下,设置什么IP)


查看租户信息,我直接使用admin租户

[html] view plain copy
  1. [email protected]:~# keystone tenant-list  
  2. +----------------------------------+---------+---------+  
  3. |                id                |   name  | enabled |  
  4. +----------------------------------+---------+---------+  
  5. | 98061d7393bb48d1b5f4b4014e04d5dc |  admin  |   True  |  
  6. | 20d00699486c498eac7a81e21e40c7fe |   demo  |   True  |  
  7. | b09e7002151b4053aebcfea714ddc46b | service |   True  |  
  8. +----------------------------------+---------+---------+  

查看网络和内网信息

[html] view plain copy
  1. [email protected]:~# neutron net-list  
  2. +--------------------------------------+------------+------------------------------------------------------+  
  3. | id                                   | name       | subnets                                              |  
  4. +--------------------------------------+------------+------------------------------------------------------+  
  5. | dfb6737c-99ec-4956-b560-71769d1cc595 | sharednet1 | 68e5be93-03a4-42c5-8867-5ea2eb81246f 192.168.12.0/24 |  
  6. +--------------------------------------+------------+------------------------------------------------------+  


通过命令创建固定IP,例如192.168.12.150

neutron port-create --tenant-id 98061d7393bb48d1b5f4b4014e04d5dc(租户ID) --fixed-ip subnet_id=68e5be93-03a4-42c5-8867-5ea2eb81246f(子网ID),ip_address=192.168.12.150(固定IP) dfb6737c-99ec-4956-b560-71769d1cc595(网络ID)

[html] view plain copy
  1. [email protected]:~# neutron port-create --tenant-id 98061d7393bb48d1b5f4b4014e04d5dc --fixed-ip subnet_id=68e5be93-03a4-42c5-8867-5ea2eb81246f,ip_address=192.168.12.150 dfb6737c-99ec-4956-b560-71769d1cc595  
  2. Created a new port:  
  3. +-----------------------+---------------------------------------------------------------------------------------+  
  4. | Field                 | Value                                                                                 |  
  5. +-----------------------+---------------------------------------------------------------------------------------+  
  6. | admin_state_up        | True                                                                                  |  
  7. | allowed_address_pairs |                                                                                       |  
  8. | binding:host_id       |                                                                                       |  
  9. | binding:profile       | {}                                                                                    |  
  10. | binding:vif_details   | {}                                                                                    |  
  11. | binding:vif_type      | unbound                                                                               |  
  12. | binding:vnic_type     | normal                                                                                |  
  13. | device_id             |                                                                                       |  
  14. | device_owner          |                                                                                       |  
  15. | fixed_ips             | {"subnet_id": "68e5be93-03a4-42c5-8867-5ea2eb81246f", "ip_address": "192.168.12.150"} |  
  16. | id                    | 8bbe4913-e82b-4719-aab9-bf856ffb10be                                                  |  
  17. | mac_address           | fa:16:3e:44:79:cf                                                                     |  
  18. | name                  |                                                                                       |  
  19. | network_id            | dfb6737c-99ec-4956-b560-71769d1cc595                                                  |  
  20. | security_groups       | e9943728-0046-40e1-a2be-d97caa68aab9                                                  |  
  21. | status                | DOWN                                                                                  |  
  22. | tenant_id             | 98061d7393bb48d1b5f4b4014e04d5dc                                                      |  
  23. +-----------------------+---------------------------------------------------------------------------------------+  


查看一下创建的端口,这个端口还没有与VM绑定,所以为断开状态

OpenStack如何创建固定IP的虚拟机

接下来我们要创建VM,创建VM,我们需要使用哪个镜像,哪个云主机配置信息,设置哪个端口绑定,所以我们需要查看


镜像列表

[html] view plain copy
  1. [email protected]:~# nova image-list  
  2. +--------------------------------------+------------+--------+--------+  
  3. | ID                                   | Name       | Status | Server |  
  4. +--------------------------------------+------------+--------+--------+  
  5. | 7725f89f-d537-4aa6-8532-1a1f8951532f | cirros     | ACTIVE |        |  
  6. | e3c7c952-db4c-42ae-b429-8b8521b3bde7 | idesktop   | ACTIVE |        |  
  7. | a8e84028-06a7-4d9e-9bb8-ea3152752104 | ubuntu1404 | ACTIVE |        |  
  8. +--------------------------------------+------------+--------+--------+  

云主机配置列表

[html] view plain copy
  1. [email protected]:~# nova flavor-list  
  2. +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+  
  3. | ID                                   | Name      | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |  
  4. +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+  
  5. | 1                                    | m1.tiny   | 512       | 1    | 0         |      | 1     | 1.0         | True      |  
  6. | 2                                    | m1.small  | 2048      | 20   | 0         |      | 1     | 1.0         | True      |  
  7. | 3                                    | m1.medium | 4096      | 40   | 0         |      | 2     | 1.0         | True      |  
  8. | 4                                    | m1.large  | 8192      | 80   | 0         |      | 4     | 1.0         | True      |  
  9. | 5                                    | m1.xlarge | 16384     | 160  | 0         |      | 8     | 1.0         | True      |  
  10. | b40e993f-196a-4854-8383-79a608a1d7dd | MQ        | 4096      | 20   | 0         |      | 2     | 1.0         | True      |  
  11. +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+  

然后通过以下命令创建云主机


 nova boot --flavor MQ(云主机配置) --image a8e84028-06a7-4d9e-9bb8-ea3152752104(镜像) --security_group default (安全组)--nic port-id=8bbe4913-e82b-4719-aab9-bf856ffb10be(端口映射) test150(VM名称)

[html] view plain copy
  1. [email protected]:~# nova boot --flavor MQ --image a8e84028-06a7-4d9e-9bb8-ea3152752104 --security_group default --nic port-id=8bbe4913-e82b-4719-aab9-bf856ffb10be test150  
  2. +--------------------------------------+---------------------------------------------------+  
  3. | Property                             | Value                                             |  
  4. +--------------------------------------+---------------------------------------------------+  
  5. | OS-DCF:diskConfig                    | MANUAL                                            |  
  6. | OS-EXT-AZ:availability_zone          | nova                                              |  
  7. | OS-EXT-SRV-ATTR:host                 | -                                                 |  
  8. | OS-EXT-SRV-ATTR:hypervisor_hostname  | -                                                 |  
  9. | OS-EXT-SRV-ATTR:instance_name        | instance-00000010                                 |  
  10. | OS-EXT-STS:power_state               | 0                                                 |  
  11. | OS-EXT-STS:task_state                | scheduling                                        |  
  12. | OS-EXT-STS:vm_state                  | building                                          |  
  13. | OS-SRV-USG:launched_at               | -                                                 |  
  14. | OS-SRV-USG:terminated_at             | -                                                 |  
  15. | accessIPv4                           |                                                   |  
  16. | accessIPv6                           |                                                   |  
  17. | adminPass                            | Rdk3WD3g5EQx                                      |  
  18. | config_drive                         |                                                   |  
  19. | created                              | 2016-11-02T09:08:59Z                              |  
  20. | flavor                               | MQ (b40e993f-196a-4854-8383-79a608a1d7dd)         |  
  21. | hostId                               |                                                   |  
  22. | id                                   | 2c9efb18-18aa-4fce-bb41-ef8a900ea1fa              |  
  23. | image                                | ubuntu1404 (a8e84028-06a7-4d9e-9bb8-ea3152752104) |  
  24. | key_name                             | -                                                 |  
  25. | metadata                             | {}                                                |  
  26. | name                                 | test150                                           |  
  27. | os-extended-volumes:volumes_attached | []                                                |  
  28. | progress                             | 0                                                 |  
  29. | security_groups                      | default                                           |  
  30. | status                               | BUILD                                             |  
  31. | tenant_id                            | 98061d7393bb48d1b5f4b4014e04d5dc                  |  
  32. | updated                              | 2016-11-02T09:09:00Z                              |  
  33. | user_id                              | f46ec9dacf2741e2a2f8789788785950                  |  
  34. +--------------------------------------+---------------------------------------------------+  

创建之后查看端口信息,已经开启

OpenStack如何创建固定IP的虚拟机


查看虚拟机信息

OpenStack如何创建固定IP的虚拟机


版权声明: 专注于"GIS+"前沿技术的研究与交流,将云计算技术、大数据技术、容器技术、物联网与GIS进行深度融合,探讨"GIS+"技术和行业解决方案;文章允许转载,但必须以链接方式注明源地址,否则追究法律责任! http://blog.csdn.net/chinagissoft/article/details/53009782

题记

昨天同事有一个需求,希望我创建一个固定IP的虚拟机,因为他的代码写死IP了,所以不好修改,我的云环境是OpenStack,但是OpenStack的虚拟机都是通过DHCP获得,那么如何

创建一个固定IP的虚拟机呢?


环境

OpenStack icehouse

网络模式:FlatDHCP


步骤

首先需要说明一下,OpenStack不支持,你创建一个其他VM后,然后手动更改配置文件设置IP,这个VM是无法访问的。


1、先查看OpenStack子网的IP范围,由于我使用了扁平DHCP,子网范围135-200,如果设置的IP地址不在该范围内,也无法访问。

[html] view plain copy
  1. [email protected]:~# neutron subnet-list  
  2. +--------------------------------------+------+-----------------+------------------------------------------------------+  
  3. | id                                   | name | cidr            | allocation_pools                                     |  
  4. +--------------------------------------+------+-----------------+------------------------------------------------------+  
  5. | 68e5be93-03a4-42c5-8867-5ea2eb81246f |      | 192.168.12.0/24 | {"start": "192.168.12.135", "end": "192.168.12.200"} |  
  6. +--------------------------------------+------+-----------------+------------------------------------------------------+  


2、创建VM之前,我们需要创建一个端口(该端口需要了解在哪个租户下,哪个网络和子网下,设置什么IP)


查看租户信息,我直接使用admin租户

[html] view plain copy
  1. [email protected]:~# keystone tenant-list  
  2. +----------------------------------+---------+---------+  
  3. |                id                |   name  | enabled |  
  4. +----------------------------------+---------+---------+  
  5. | 98061d7393bb48d1b5f4b4014e04d5dc |  admin  |   True  |  
  6. | 20d00699486c498eac7a81e21e40c7fe |   demo  |   True  |  
  7. | b09e7002151b4053aebcfea714ddc46b | service |   True  |  
  8. +----------------------------------+---------+---------+  

查看网络和内网信息

[html] view plain copy
  1. [email protected]:~# neutron net-list  
  2. +--------------------------------------+------------+------------------------------------------------------+  
  3. | id                                   | name       | subnets                                              |  
  4. +--------------------------------------+------------+------------------------------------------------------+  
  5. | dfb6737c-99ec-4956-b560-71769d1cc595 | sharednet1 | 68e5be93-03a4-42c5-8867-5ea2eb81246f 192.168.12.0/24 |  
  6. +--------------------------------------+------------+------------------------------------------------------+  


通过命令创建固定IP,例如192.168.12.150

neutron port-create --tenant-id 98061d7393bb48d1b5f4b4014e04d5dc(租户ID) --fixed-ip subnet_id=68e5be93-03a4-42c5-8867-5ea2eb81246f(子网ID),ip_address=192.168.12.150(固定IP) dfb6737c-99ec-4956-b560-71769d1cc595(网络ID)

[html] view plain copy
  1. [email protected]:~# neutron port-create --tenant-id 98061d7393bb48d1b5f4b4014e04d5dc --fixed-ip subnet_id=68e5be93-03a4-42c5-8867-5ea2eb81246f,ip_address=192.168.12.150 dfb6737c-99ec-4956-b560-71769d1cc595  
  2. Created a new port:  
  3. +-----------------------+---------------------------------------------------------------------------------------+  
  4. | Field                 | Value                                                                                 |  
  5. +-----------------------+---------------------------------------------------------------------------------------+  
  6. | admin_state_up        | True                                                                                  |  
  7. | allowed_address_pairs |                                                                                       |  
  8. | binding:host_id       |                                                                                       |  
  9. | binding:profile       | {}                                                                                    |  
  10. | binding:vif_details   | {}                                                                                    |  
  11. | binding:vif_type      | unbound                                                                               |  
  12. | binding:vnic_type     | normal                                                                                |  
  13. | device_id             |                                                                                       |  
  14. | device_owner          |                                                                                       |  
  15. | fixed_ips             | {"subnet_id": "68e5be93-03a4-42c5-8867-5ea2eb81246f", "ip_address": "192.168.12.150"} |  
  16. | id                    | 8bbe4913-e82b-4719-aab9-bf856ffb10be                                                  |  
  17. | mac_address           | fa:16:3e:44:79:cf                                                                     |  
  18. | name                  |                                                                                       |  
  19. | network_id            | dfb6737c-99ec-4956-b560-71769d1cc595                                                  |  
  20. | security_groups       | e9943728-0046-40e1-a2be-d97caa68aab9                                                  |  
  21. | status                | DOWN                                                                                  |  
  22. | tenant_id             | 98061d7393bb48d1b5f4b4014e04d5dc                                                      |  
  23. +-----------------------+---------------------------------------------------------------------------------------+  


查看一下创建的端口,这个端口还没有与VM绑定,所以为断开状态

OpenStack如何创建固定IP的虚拟机

接下来我们要创建VM,创建VM,我们需要使用哪个镜像,哪个云主机配置信息,设置哪个端口绑定,所以我们需要查看


镜像列表

[html] view plain copy
  1. [email protected]:~# nova image-list  
  2. +--------------------------------------+------------+--------+--------+  
  3. | ID                                   | Name       | Status | Server |  
  4. +--------------------------------------+------------+--------+--------+  
  5. | 7725f89f-d537-4aa6-8532-1a1f8951532f | cirros     | ACTIVE |        |  
  6. | e3c7c952-db4c-42ae-b429-8b8521b3bde7 | idesktop   | ACTIVE |        |  
  7. | a8e84028-06a7-4d9e-9bb8-ea3152752104 | ubuntu1404 | ACTIVE |        |  
  8. +--------------------------------------+------------+--------+--------+  

云主机配置列表

[html] view plain copy
  1. [email protected]:~# nova flavor-list  
  2. +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+  
  3. | ID                                   | Name      | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |  
  4. +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+  
  5. | 1                                    | m1.tiny   | 512       | 1    | 0         |      | 1     | 1.0         | True      |  
  6. | 2                                    | m1.small  | 2048      | 20   | 0         |      | 1     | 1.0         | True      |  
  7. | 3                                    | m1.medium | 4096      | 40   | 0         |      | 2     | 1.0         | True      |  
  8. | 4                                    | m1.large  | 8192      | 80   | 0         |      | 4     | 1.0         | True      |  
  9. | 5                                    | m1.xlarge | 16384     | 160  | 0         |      | 8     | 1.0         | True      |  
  10. | b40e993f-196a-4854-8383-79a608a1d7dd | MQ        | 4096      | 20   | 0         |      | 2     | 1.0         | True      |  
  11. +--------------------------------------+-----------+-----------+------+-----------+------+-------+-------------+-----------+  

然后通过以下命令创建云主机


 nova boot --flavor MQ(云主机配置) --image a8e84028-06a7-4d9e-9bb8-ea3152752104(镜像) --security_group default (安全组)--nic port-id=8bbe4913-e82b-4719-aab9-bf856ffb10be(端口映射) test150(VM名称)

[html] view plain copy
  1. [email protected]:~# nova boot --flavor MQ --image a8e84028-06a7-4d9e-9bb8-ea3152752104 --security_group default --nic port-id=8bbe4913-e82b-4719-aab9-bf856ffb10be test150  
  2. +--------------------------------------+---------------------------------------------------+  
  3. | Property                             | Value                                             |  
  4. +--------------------------------------+---------------------------------------------------+  
  5. | OS-DCF:diskConfig                    | MANUAL                                            |  
  6. | OS-EXT-AZ:availability_zone          | nova                                              |  
  7. | OS-EXT-SRV-ATTR:host                 | -                                                 |  
  8. | OS-EXT-SRV-ATTR:hypervisor_hostname  | -                                                 |  
  9. | OS-EXT-SRV-ATTR:instance_name        | instance-00000010                                 |  
  10. | OS-EXT-STS:power_state               | 0                                                 |  
  11. | OS-EXT-STS:task_state                | scheduling                                        |  
  12. | OS-EXT-STS:vm_state                  | building                                          |  
  13. | OS-SRV-USG:launched_at               | -                                                 |  
  14. | OS-SRV-USG:terminated_at             | -                                                 |  
  15. | accessIPv4                           |                                                   |  
  16. | accessIPv6                           |                                                   |  
  17. | adminPass                            | Rdk3WD3g5EQx                                      |  
  18. | config_drive                         |                                                   |  
  19. | created                              | 2016-11-02T09:08:59Z                              |  
  20. | flavor                               | MQ (b40e993f-196a-4854-8383-79a608a1d7dd)         |  
  21. | hostId                               |                                                   |  
  22. | id                                   | 2c9efb18-18aa-4fce-bb41-ef8a900ea1fa              |  
  23. | image                                | ubuntu1404 (a8e84028-06a7-4d9e-9bb8-ea3152752104) |  
  24. | key_name                             | -                                                 |  
  25. | metadata                             | {}                                                |  
  26. | name                                 | test150                                           |  
  27. | os-extended-volumes:volumes_attached | []                                                |  
  28. | progress                             | 0                                                 |  
  29. | security_groups                      | default                                           |  
  30. | status                               | BUILD                                             |  
  31. | tenant_id                            | 98061d7393bb48d1b5f4b4014e04d5dc                  |  
  32. | updated                              | 2016-11-02T09:09:00Z                              |  
  33. | user_id                              | f46ec9dacf2741e2a2f8789788785950                  |  
  34. +--------------------------------------+---------------------------------------------------+  

创建之后查看端口信息,已经开启

OpenStack如何创建固定IP的虚拟机


查看虚拟机信息

OpenStack如何创建固定IP的虚拟机