慎重!慎重!照这个方法删除cell后,会导致已存在的虚拟机无法获取详情!
此文后续 https://www.jianshu.com/p/48e1451cf7dc
自从为了配置高可用集群,给OpenStack装了双控制节点后,就遇到了一个奇怪的现象:
所有计算节点都出现了两次!
当时想排查错误,但一来没头绪,二来任务紧急,浅浅尝试一下就放弃排查了。好在也不影响使用,就这么用了下去,直到有一天!
直到有一天,我运行了如下命令:
nova-manage cell_v2 discover_hosts --verbose
看到如下输出时,突然产生疑惑:
为什么出现了两次cell?
好像隐隐预感到了什么!
连忙输入如下命令查看一下:
nova-manage cell_v2 list_cells
见下图:
果然,有一个兄弟跟 “正统”的cell1
长得很像!
它应该就是罪魁祸首了!
那么这个Name是None
的兄弟,是怎么混进来的?
我一看就明白了,原来它曾经也是一个正统的cell1
,毕竟它的数据库配置中还@了曾经的controller
,可是改朝换代后,原来的单控制节点 controller
由虚拟IP对应的hostname controllerv
代替。在配置第二个控制节点时,我又创了一个cell1
,导致原来的cell1
变成了无名无姓的None
。
既然你没有用了,就把你删掉吧,于是我:
nova-manage cell_v2 delete_cell --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad
可是:
意思是你根深蒂固,我除不了你了呗?
不行,我又:
nova-manage cell_v2 delete_cell --force --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad
结果还是:
好了,原来这个force
是假的 https://bugs.launchpad.net/nova/+bug/1721179
不行,我要把你连根拔起:
nova-manage cell_v2 delete_host --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad --host compute1
结果:
好了好了,惹不起!只能祭出终极武器了,改一下源码。
来到 /usr/lib/python2.7/dist-packages/nova/cmd/manage.py
找到 delete_cell
方法:
@args('--force', action='store_true', default=False,
help=_('Delete hosts that belong to the cell as well.'))
@args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
required=True, help=_('The uuid of the cell to delete.'))
def delete_cell(self, cell_uuid, force=False):
"""Delete an empty cell by the given uuid.
This command will return a non-zero exit code in the following cases.
* The cell is not found by uuid.
* It has hosts and force is False.
* It has instance mappings.
If force is True and the cell has host, hosts are deleted as well.
Returns 0 in the following cases.
* The empty cell is found and deleted successfully.
* The cell has hosts and force is True and the cell and the hosts are
deleted successfully.
"""
ctxt = context.get_admin_context()
# Find the CellMapping given the uuid.
try:
cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
except exception.CellMappingNotFound:
print(_('Cell with uuid %s was not found.') % cell_uuid)
return 1
# Check to see if there are any HostMappings for this cell.
host_mappings = objects.HostMappingList.get_by_cell_id(
ctxt, cell_mapping.id)
nodes = []
if host_mappings:
if not force:
print(_('There are existing hosts mapped to cell with uuid '
'%s.') % cell_uuid)
return 2
# We query for the compute nodes in the cell,
# so that they can be unmapped.
with context.target_cell(ctxt, cell_mapping) as cctxt:
nodes = objects.ComputeNodeList.get_all(cctxt)
# Check to see if there are any InstanceMappings for this cell.
instance_mappings = objects.InstanceMappingList.get_by_cell_id(
ctxt, cell_mapping.id)
if instance_mappings:
print(_('There are existing instances mapped to cell with '
'uuid %s.') % cell_uuid)
return 3
# Unmap the compute nodes so that they can be discovered
# again in future, if needed.
for node in nodes:
node.mapped = 0
node.save()
# Delete hosts mapped to the cell.
for host_mapping in host_mappings:
host_mapping.destroy()
# There are no hosts or instances mapped to the cell so delete it.
cell_mapping.destroy()
return 0
把 return 2
和 return 3
的部分注释掉!
直奔主题,即最后一句cell_mapping.destroy()
!
好了,再删一下:
root@controller:~# nova-manage cell_v2 delete_cell --force --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad
An error has occurred:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nova/cmd/manage.py", line 1868, in main
ret = fn(*fn_args, **fn_kwargs)
File "/usr/lib/python2.7/dist-packages/nova/cmd/manage.py", line 1713, in delete_cell
cell_mapping.destroy()
File "/usr/lib/python2.7/dist-packages/oslo_versionedobjects/base.py", line 226, in wrapper
return fn(self, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/nova/objects/cell_mapping.py", line 114, in destroy
self._destroy_in_db(self._context, self.uuid)
File "/usr/lib/python2.7/dist-packages/oslo_db/sqlalchemy/enginefacade.py", line 979, in wrapper
return fn(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/nova/objects/cell_mapping.py", line 108, in _destroy_in_db
uuid=uuid).delete()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 3212, in delete
delete_op.exec_()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 1179, in exec_
self._do_exec()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 1363, in _do_exec
mapper=self.mapper)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1107, in execute
bind, close_with_result=True).execute(clause, params or {})
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 945, in execute
return meth(self, multiparams, params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1398, in _handle_dbapi_exception
util.raise_from_cause(newraise, exc_info)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/lib/python2.7/dist-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/lib/python2.7/dist-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/lib/python2.7/dist-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
DBReferenceError: (pymysql.err.IntegrityError) (1451, u'Cannot delete or update a parent row: a foreign key constraint fails (`nova_api`.`instance_mappings`, CONSTRAINT `instance_mappings_ibfk_1` FOREIGN KEY (`cell_id`) REFERENCES `cell_mappings` (`id`))') [SQL: u'DELETE FROM cell_mappings WHERE cell_mappings.uuid = %(uuid_1)s'] [parameters: {u'uuid_1': u'88d1334b-8794-4481-9207-aa12dae132ad'}]
虽然出错了!但看到最后一段,我知道故事已经要走到结局了。
破局的关键就在于这张图:
简单的说,就是我想在 nova_api
数据库的 cell_mappings
表中删除 uuid
为 88d1334b-8794-4481-9207-aa12dae132ad
的这位仁兄。
但这位仁兄在cell_mappings
表中的 id
和 instance_mappings
表中的 cell_id
有“裙带关系” !删不掉!
哦原来是上头有人,那就简单了!
mysql -uroot -p
use nova_api;
找一下这位仁兄:
select * from cell_mappings where cell_mappings.uuid = '88d1334b-8794-4481-9207-aa12dae132ad';
找到了,id
是 2 !
然后,删!
delete from instance_mappings where cell_id = '2';
再删!
delete from cell_mappings where cell_mappings.uuid = '88d1334b-8794-4481-9207-aa12dae132ad';
好了,赶紧看一下:
nova-manage cell_v2 list_cells
同步下数据库,重启下nova服务:
nova-manage api_db sync
service nova-api restart
service nova-consoleauth restart
service nova-scheduler restart
service nova-conductor restart
service nova-novncproxy restart
又回到最初的起点:
openstack compute service list
干净了!
虽然好像并没什么用,但也是时候学习一下nova中关于cell的知识了……