{"id":359,"date":"2015-10-15T23:05:30","date_gmt":"2015-10-15T15:05:30","guid":{"rendered":"https:\/\/drho.club\/?p=359"},"modified":"2015-10-15T23:05:30","modified_gmt":"2015-10-15T15:05:30","slug":"python-api-live-migrate-all-vm","status":"publish","type":"post","link":"https:\/\/104.es\/index.php\/2015\/10\/15\/python-api-live-migrate-all-vm\/","title":{"rendered":"\u4f7f\u7528Python API\u4e00\u500b\u6307\u4ee4migrate\u6240\u6709\u7684VM"},"content":{"rendered":"<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u5169\u53f0\u4e0d\u540c\u7684\u4e3b\u6a5f\u9593\u5982\u679c\u90fd\u57f7\u884c\u4e86libvirtd\uff0c\u5c31\u53ef\u4ee5\u900f\u904evirsh\u7684\u6307\u4ee4\u96c6\u4f86\u5c0d\u65bc\u865b\u64ec\u6a5fVM\u505a\u8a31\u591a\u7684\u64cd\u4f5c\uff0c\u9019\u500b\u6211\u5011\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u6709\u63d0\u5230\u904e\u3002\u73fe\u5728\u6211\u5011\u6709\u4e86Python API\uff0c\u7576\u7136\u53ef\u4ee5\u66f4\u9032\u4e00\u6b65\u5730\u505a\u81ea\u52d5\u5316\u6279\u6b21\u4f5c\u696d\u3002\u9664\u4e86\u524d\u4e00\u7bc7\u6587\u7ae0\u4e2d\u4ecb\u7d39\u5230\u7684\u628a\u6240\u6709\u7684VM\u95dc\u9589\u6216\u662f\u6253\u958b\u4e4b\u5916\uff0c\u9019\u4e00\u7bc7\u6587\u7ae0\u6211\u5011\u5c31\u4f86\u505aLive Migration\u7684\u81ea\u52d5\u5316\u64cd\u4f5c\uff0c\u4e00\u500b\u7a0b\u5f0f\u628a\u6240\u6709\u6b63\u5728\u57f7\u884c\u4e2d\u7684VM\u5728\u4e0d\u505c\u6a5f\u7684\u60c5\u6cc1\u4e0b\u5168\u90e8\u642c\u5230\u53e6\u5916\u4e00\u53f0\u4e3b\u6a5fNode\u4e0a\u3002\u7a0b\u5f0f\u5982\u4e0b(Python 2)\uff1a<\/p>\n<p>[code lang=&#8221;python&#8221;]<br \/>\n#! \/usr\/bin\/python<br \/>\nimport libvirt<br \/>\nimport sys<\/p>\n<p>conn = libvirt.open(&quot;qemu:\/\/\/system&quot;)<br \/>\ndest_conn = libvirt.open(&quot;qemu+ssh:\/\/192.168.1.50\/system&quot;)<\/p>\n<p>alldom = conn.listAllDomains()<br \/>\nfor dom in alldom:<br \/>\n  if dom.isActive():<br \/>\n    print dom.name() + &quot; is migrating&#8230;&quot;<br \/>\n    new_dom = dom.migrate(dest_conn, 0, None, None, 0)<br \/>\n    if new_dom == None:<br \/>\n      print dom.name() + &quot; is fail to migrate!&quot;<br \/>\n    else:<br \/>\n      print dom.name() + &quot; was migrated successfully!&quot;<br \/>\ndest_conn.close()<br \/>\nconn.close()<\/p>\n<p>[\/code]<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u7a0b\u5f0f\u5176\u5be6\u4e5f\u5f88\u7c21\u55ae\uff0c\u5c31\u662f\u628a\u81ea\u5df1\u672c\u5730\u7aef\u7684\u4e3b\u6a5f\u9023\u63a5\u6253\u958b\u653e\u5728conn\u4e2d\uff0c\u540c\u6642\u4e5f\u628a\u5c0d\u65b9\u9023\u7dda\u7684\u4e3b\u6a5f\u9023\u7d50\u6253\u958b\u653e\u5728dest_conn\u4e2d\u3002\u63a5\u4e0b\u4f86\u5728\u672c\u5730\u7aef\u900f\u904elistAllDomains()\u53d6\u51fa\u6240\u6709\u5b9a\u7fa9\u7684domain\uff08\u4e0d\u7ba1\u662f\u6709\u6c92\u5728\u57f7\u884c\u7684\uff09\uff0c\u900f\u904efor\u8ff4\u5708\u4e00\u500b\u4e00\u500b\u6aa2\u8996\uff0c\u53ea\u6709isActive()\uff08\u6b63\u5728\u57f7\u884c\u4e2d\u7684\uff09\u865b\u64ec\u6a5fdoman\u624d\u505alive migration\u7684\u52d5\u4f5c\u3002\u57f7\u884clive migration\u7684\u51fd\u6578\u5c31\u662fdom.migrate(dest_conn&#8230;)\u9019\u500b\u51fd\u6578\u800c\u5df2\u3002<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u7531\u65bc\u9023\u7dda\u9700\u4f7f\u7528SSH\u901a\u9053\uff0c\u6240\u4ee5\u5982\u679c\u4f60\u6c92\u6709\u505aSSH\u514d\u5bc6\u78bc\u8a2d\u5b9a\u7684\u8a71\uff0c\u5728\u57f7\u884c\u7684\u904e\u7a0b\u4e2d\u6703\u8ddf\u4f60\u8981\u6c42\u8f38\u5165\u5bc6\u78bc\u3002<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u53e6\u5916\uff0cLive Migration\u6642\uff0c\u5169\u53f0\u96fb\u8166\u7684\u6307\u4ee4\u96c6\u9700\u8981\u76f8\u5bb9\u3002\u5982\u679c\u4e0d\u76f8\u5bb9\u7684\u8a71\uff0c\u662f\u6c92\u6709\u8fa6\u6cd5\u9806\u5229\u9077\u79fb\u904e\u53bb\u4e86\u3002\u4f8b\u5982\u7ad9\u9577\u4f7f\u7528\u7684\u5169\u53f0\u96fb\u8166\uff0c\u4e00\u53f0\u7684CPU\u662fIntel i3-4130 (Node A)\uff0c\u53e6\u5916\u4e00\u53f0\u5247\u662fIntel i5 M460(Node B)\uff0c\u7d50\u679c\u90a3\u500bi5\u7684CPU\u5c45\u7136\u4e0d\u652f\u63f4AES\u6307\u4ee4\u96c6\uff0c\u5728\u4e0d\u505a\u4efb\u4f55\u8abf\u6574\u7684\u60c5\u6cc1\u4e0b\uff0c\u5728Node A\u5b89\u88dd\u7684\u865b\u64ec\u6a5f\u6c92\u6709\u8fa6\u6cd5migrate\u5230Node B\uff0c\u4f46\u662f\u5728Node B\u5efa\u7acb\u5b89\u88dd\u7684\u865b\u64ec\u6a5f\u5247\u53ef\u4ee5\u81ea\u7531\u5730\u5728\u5169\u53f0Node\u4e4b\u9593migrate\u3002\u9019\u662f\u8981\u7279\u5225\u7559\u610f\u7684\u5730\u65b9\u3002<\/p>\n<p style=\"font-family: \u5fae\u8edf\u6b63\u9ed1\u9ad4; font-size: 12pt; line-height: 200%; letter-spacing: 2pt;\">\u6240\u4ee5\uff0c\u6211\u5011\u5982\u679c\u628a\u7a0b\u5f0f\u547d\u540d\u70bamigrateall.py\uff0c\u90a3\u9ebc\u53ea\u8981\u57f7\u884c.\/migrateall.py\uff0c\u672c\u5730\u7aefNode\u4e2d\u6240\u6709\u7684\u865b\u64ec\u6a5f\u5c31\u5168\u90e8\u88ab\u642c\u5230\u53e6\u5916\u4e00\u53f0Node\u4e0a\u4e86\u3002\u90a3\u9ebc\u5982\u4f55\u642c\u56de\u4f86\u5462\uff1f\u5c31\u628a\u4f86\u6e90\u548c\u76ee\u6a19\u63db\u4e00\u4e0b\uff0c\u53e6\u5916\u518d\u5beb\u4e00\u500b\u53eb\u505a.\/migratebackall.py\u56c9\uff1a<\/p>\n<p>[code lang=&#8221;python&#8221;]<br \/>\n#! \/usr\/bin\/python<br \/>\nimport libvirt<br \/>\nimport sys<\/p>\n<p>conn = libvirt.open(&quot;qemu+ssh:\/\/192.168.1.50\/system&quot;)<br \/>\ndest_conn = libvirt.open(&quot;qemu:\/\/\/system&quot;)<\/p>\n<p>alldom = conn.listAllDomains()<br \/>\nfor dom in alldom:<br \/>\n  if dom.isActive():<br \/>\n    print dom.name() + &quot; is migrating&#8230;&quot;<br \/>\n    new_dom = dom.migrate(dest_conn, 0, None, None, 0)<br \/>\n    if new_dom == None:<br \/>\n      print dom.name() + &quot; is fail to migrate!&quot;<br \/>\n    else:<br \/>\n      print dom.name() + &quot; was migrated successfully!&quot;<br \/>\ndest_conn.close()<br \/>\nconn.close()<\/p>\n<p>[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5169\u53f0\u4e0d\u540c\u7684\u4e3b\u6a5f\u9593\u5982\u679c\u90fd\u57f7\u884c\u4e86libvirtd\uff0c\u5c31\u53ef\u4ee5\u900f\u904evirsh\u7684\u6307\u4ee4\u96c6\u4f86\u5c0d\u65bc\u865b\u64ec\u6a5fVM\u505a\u8a31\u591a\u7684\u64cd\u4f5c\uff0c\u9019\u500b\u6211\u5011\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u6709\u63d0\u5230\u904e\u3002\u73fe\u5728\u6211\u5011\u6709\u4e86Python API\uff0c\u7576\u7136\u53ef\u4ee5\u66f4\u9032\u4e00\u6b65\u5730\u505a\u81ea\u52d5\u5316\u6279\u6b21\u4f5c\u696d\u3002\u9664\u4e86\u524d\u4e00\u7bc7\u6587\u7ae0\u4e2d\u4ecb\u7d39\u5230\u7684\u628a\u6240\u6709\u7684VM\u95dc\u9589\u6216\u662f\u6253\u958b\u4e4b\u5916\uff0c\u9019\u4e00\u7bc7\u6587\u7ae0\u6211\u5011\u5c31\u4f86\u505aLive Migration\u7684\u81ea\u52d5\u5316\u64cd\u4f5c\uff0c\u4e00\u500b\u7a0b\u5f0f\u628a\u6240\u6709\u6b63\u5728\u57f7\u884c\u4e2d\u7684VM\u5728\u4e0d\u505c\u6a5f\u7684\u60c5\u6cc1\u4e0b\u5168\u90e8\u642c\u5230\u53e6\u5916\u4e00\u53f0\u4e3b\u6a5fNode\u4e0a\u3002\u7a0b\u5f0f\u5982\u4e0b(Python 2)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[97,15,100],"tags":[160,161],"class_list":["post-359","post","type-post","status-publish","format-standard","hentry","category-kvm-libvirt","category-python","category-vm","tag-python-api-migrateion","tag-python-kvm-live-migration"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":0,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"wp:attachment":[{"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/104.es\/index.php\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}