1. 前言

    因为虚拟机业务的撤销,compute上面的instance越来越少,随着虚拟机的迁移和下线,compute节点可能处于资源空闲的状态,为了进一步利用系统资源,节约成本,需要将空闲的计算节点从openstack中删除。在删除compute节点之前,首先需要确保该计算节点上没有业务,可以通过virsh list --all查看是否还有虚拟机业务,同时保守起见,建议将compute节点的服务关闭关闭,通过service openstack-nova-compute stop && chkconfig openstack-nova-compute off。

2. 删除nova计算节点

    openstack建议在删除nova节点的时候,建议通过操作nova.instances中的deleted字段来实现,而不是直接将compute节点的条目从数据库中删除,一来是为了安全考虑,而是能够实现快速的回滚操作,具体的操作如下:

  1. 校验nova-compute的状态

[root@controller ~]# nova service-list
+------------------+-----------------------+---------------+---------+-------+----------------------------+-----------------+
| Binary           | Host                  | Zone          | Status  | State | Updated_at                 | Disabled Reason |
+------------------+-----------------------+---------------+---------+-------+----------------------------+-----------------+
| nova-scheduler   | controller            | internal      | enabled | up    | 2016-01-20T04:12:33.000000 | -               |
| nova-cert        | controller            | internal      | enabled | up    | 2016-01-20T04:12:31.000000 | -               |
| nova-conductor   | controller            | internal      | enabled | up    | 2016-01-20T04:12:27.000000 | -               |
| nova-consoleauth | controller            | internal      | enabled | up    | 2016-01-20T04:12:33.000000 | -               |
| nova-compute     | YiZhuang_10_1_112_105 | YiZhuangZone2 | enabled | down  | 2016-01-16T09:11:57.000000 | -               |    #已经处于down的状态
| nova-compute     | YiZhuang_10_1_112_106 | YiZhuangZone2 | enabled | up    | 2016-01-20T04:12:24.000000 | -               |

2. 删除compute service节点

mysql> select * from nova.services where host='YiZhuang_10_1_112_105';  
+---------------------+---------------------+------------+----+-----------------------+--------------+---------+--------------+----------+---------+-----------------+
| created_at          | updated_at          | deleted_at | id | host                  | binary       | topic   | report_count | disabled | deleted | disabled_reason |
+---------------------+---------------------+------------+----+-----------------------+--------------+---------+--------------+----------+---------+-----------------+
| 2015-09-21 03:21:40 | 2016-01-16 09:11:57 | NULL       | 37 | YiZhuang_10_1_112_105 | nova-compute | compute |      1011705 |        0 |       0 | NULL            |
+---------------------+---------------------+------------+----+-----------------------+--------------+---------+--------------+----------+---------+-----------------+
1 row in set (0.00 sec)
mysql> update nova.services set deleted=1 where host='YiZhuang_10_1_112_105';             #设置deleted字段,即标志位            
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> select * from nova.services where host='YiZhuang_10_1_112_105';                   #检查deleted字段设置情况
+---------------------+---------------------+------------+----+-----------------------+--------------+---------+--------------+----------+---------+-----------------+
| created_at          | updated_at          | deleted_at | id | host                  | binary       | topic   | report_count | disabled | deleted | disabled_reason |
+---------------------+---------------------+------------+----+-----------------------+--------------+---------+--------------+----------+---------+-----------------+
| 2015-09-21 03:21:40 | 2016-01-16 09:11:57 | NULL       | 37 | YiZhuang_10_1_112_105 | nova-compute | compute |      1011705 |        0 |       1 | NULL            |   
+---------------------+---------------------+------------+----+-----------------------+--------------+---------+--------------+----------+---------+-----------------+
1 row in set (0.00 sec)

3. 确认节点的service状态

[root@controller ~]# nova service-list
+------------------+-----------------------+---------------+---------+-------+----------------------------+-----------------+
| Binary           | Host                  | Zone          | Status  | State | Updated_at                 | Disabled Reason |
+------------------+-----------------------+---------------+---------+-------+----------------------------+-----------------+
| nova-scheduler   | controller            | internal      | enabled | up    | 2016-01-20T04:12:33.000000 | -               |
| nova-cert        | controller            | internal      | enabled | up    | 2016-01-20T04:12:31.000000 | -               |
| nova-conductor   | controller            | internal      | enabled | up    | 2016-01-20T04:12:27.000000 | -               |
| nova-consoleauth | controller            | internal      | enabled | up    | 2016-01-20T04:12:33.000000 | -               |
| nova-compute     | YiZhuang_10_1_112_106 | YiZhuangZone2 | enabled | up    | 2016-01-20T04:12:24.000000 | -               |    #service已经删除

4. 查看hypervisor情况

[root@controller ~]# nova hypervisor-list
+----+-----------------------+
| ID | Hypervisor hostname   |
+----+-----------------------+
| 25 | YiZhuang_10_1_112_105 |          #services已经删除,但hypervisor依旧在,类似的方法,将nova.compute_nodes中的deleted字段修改,查看步骤5    
| 27 | YiZhuang_10_1_112_106 |

5. 删除compute_nodes节点

mysql> update nova.compute_nodes set deleted=1 where hypervisor_hostname='YiZhuang_10_1_112_105'\G;       #设置deleted标志位           
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from nova.compute_nodes where hypervisor_hostname='YiZhuang_10_1_112_105'\G;               #确认设置情况    
*************************** 1. row ***************************
          created_at: 2015-09-21 03:21:40
          updated_at: 2016-01-16 09:11:36
          deleted_at: NULL
                  id: 25
          service_id: 37
               vcpus: 24
           memory_mb: 64396
            local_gb: 3062
          vcpus_used: 0
      memory_mb_used: 1024
       local_gb_used: 20
     hypervisor_type: QEMU
  hypervisor_version: 12001
            cpu_info: {"vendor": "Intel", "model": "SandyBridge", "arch": "x86_64", "features": ["vme", "dtes64", "vmx", "erms", "xtpr", "smep", "pcid", "est", "monitor", "smx", "tm", "acpi", "osxsave", "ht", "dca", "pdcm", "pdpe1gb", "fsgsbase", "f16c", "ds", "tm2", "ss", "pbe", "ds_cpl", "rdrand"], "topology": {"cores": 6, "threads": 2, "sockets": 1}}
disk_available_least: 2671
         free_ram_mb: 63372
        free_disk_gb: 3042
    current_workload: 0
         running_vms: 0
 hypervisor_hostname: YiZhuang_10_1_112_105
             deleted: 1                        #设置成功
             host_ip: 0.0.0.0
 supported_instances: [["i686", "qemu", "hvm"], ["i686", "kvm", "hvm"], ["x86_64", "qemu", "hvm"], ["x86_64", "kvm", "hvm"]]
           pci_stats: []
             metrics: []
     extra_resources: NULL
               stats: {"num_task_None": 2, "io_workload": 0, "num_instances": 2, "num_vm_stopped": 2, "num_proj_a49b16d5324a4d20bde2217b17200485": 2, "num_vcpus_used": 8, "num_os_type_None": 2}
1 row in set (0.00 sec)

6. 确认hypervisor node是否删除

[root@controller ~]# nova hypervisor-list        #YiZhuang_10_1_112_105已被删除
+----+-----------------------+
| ID | Hypervisor hostname   |
+----+-----------------------+  
| 27 | YiZhuang_10_1_112_106 |

3. 删除neutron agent服务

    以上将nova的service和hypervisor从数据库中删除,但是compute节点在安装过程中,neutron-openvswitch-agent会建立一个OVS的agent,该agent也记录在数据库中,通过neutron agent-list可以查看到agent的情况,当compute node删除之后,neutron也应该删除,方法和上面相类似,具体如下:

  1. 确认agent的情况

[root@controller ~]# neutron agent-list
+--------------------------------------+--------------------+-----------------------+-------+----------------+
| id                                   | agent_type         | host                  | alive | admin_state_up |
+--------------------------------------+--------------------+-----------------------+-------+----------------+
| 0160e0b7-22fb-42d9-8e43-0d13d98db594 | L3 agent           | LuGu_10_1_81_209      | :-)   | True           |
| 4e1c9957-98e5-4516-af2d-6c67a00ecd77 | Open vSwitch agent | YiZhuang_10_1_112_105 | xxx   | True           |    #计算节点已经关闭,所以agent状态变为xxx,即不可用,:-)可用
| 6a9e2647-6eed-459d-8f60-e9ea40ae04df | Open vSwitch agent | YiZhuang_10_1_112_106 | :-)   | True           |

2. 删除OVS agent

mysql> select * from neutron.agents where host='YiZhuang_10_1_112_105'\G;        #没有deleted标志位,所以只能从数据库中删除
*************************** 1. row ***************************
                 id: 4e1c9957-98e5-4516-af2d-6c67a00ecd77
         agent_type: Open vSwitch agent
             binary: neutron-openvswitch-agent
              topic: N/A
               host: YiZhuang_10_1_112_105
     admin_state_up: 1
         created_at: 2015-09-21 03:22:01
         started_at: 2016-01-19 09:15:45
heartbeat_timestamp: 2016-01-19 09:15:45
        description: NULL
     configurations: {"tunnel_types": [], "tunneling_ip": "", "bridge_mappings": {"physnet1": "br-eth1", "physnet0": "br-eth0"}, "l2_population": false, "devices": 0}
1 row in set (0.00 sec)


mysql> delete from neutron.agents where host='YiZhuang_10_1_112_105'\G;         #删除         
Query OK, 1 row affected (0.00 sec)

校验:
[root@controller ~]# neutron agent-list
+--------------------------------------+--------------------+-----------------------+-------+----------------+
| id                                   | agent_type         | host                  | alive | admin_state_up |
+--------------------------------------+--------------------+-----------------------+-------+----------------+
| 0160e0b7-22fb-42d9-8e43-0d13d98db594 | L3 agent           | LuGu_10_1_81_209      | :-)   | True           |
| 6a9e2647-6eed-459d-8f60-e9ea40ae04df | Open vSwitch agent | YiZhuang_10_1_112_106 | :-)   | True           |

4. 总结

   以上是将compute_nodes从opentack中删除的方法,在Juno以上的版本,可以通过nova service-delete 的方式将compute nodes删除,在Icehouse以下的版本,暂时未能提供,解决的方法可以通过调用nova db的api,将其删除,后续再补充。或者通过编写的脚本的方式,也可以实现,建议通过调用API的方式,直接修改数据库,而不建议直接修改数据库的方法。