libvirt + qemuを使用して、LinuxでほぼすべてのOSを即座に起動

CoreOSKubernetesに取り組んでいる 、問題を解決するためにユーザー環境を再現する必要がしばしばありました。 コンテナを使用してOSを起動するとき、次のように、完全なOS機能を常に実現できるとは限らないことに同意します。 多くの場合、systemdに関連する問題に対処する必要がありました。







そこで、松葉杖を書くというアイデアが生まれました。これは、ほとんどすべてのオペレーティングシステム( OpenStackのミニチュア)からクラスターを簡単に持ち上げることができます。 現在、次のOSはゲストとして完全にサポートされています。









部分的に:









出版物は、偶然にコードをテストしているのを見た同僚によって宣伝されました。 そして今、 Vagrantに対する利点について。 最初の利点は時間です。 たとえば、わずか20〜30秒で3つの仮想マシンが作成されます。







画像






スクリプトの作業を開始した時点で、Vagrant for LinuxはVirtualBoxのみを使用してイメージを起動できましたが、libvirt / qemuのサポートはまだ粗野でした。 VirtualBoxの場合、特別に準備されたイメージを探す必要があり、仮想マシンからクラスターを再起動するにはかなりの時間がかかりました。







現在、オペレーティングシステムのほとんどの開発者は、OpenStack環境での作業用に準備されたイメージ(「クラウド」イメージ)を提供しています。 これらのイメージは、 cloud-init



が存在するため、設定するのに非常に便利です。 最初の起動段階でcloud-init



を使用すると、公開SSHキーをイメージに簡単に埋め込み、ネットワークを構成できます。







QEMUは、仮想マシンのイメージをレイヤーとして使用できます(ほぼDockerのようなspanshots )。 公式の「クラウド」イメージをダウンロードして、クラスターのすべての仮想マシンのベースイメージとして使用できます。 この場合、ディスクへのすべての変更は、ベースイメージと現在の状態の違いとして記録されます(LVMスナップショットなど)。 この方法では、すべてのマシンを簡単に削除して、数秒以内に再作成することもできます。







3台のdebianマシンの再展開の例

すでに作成されたクラスターをDebianから削除し、ダウンロードした公式イメージdebian-8.5.0-openstack-amd64.qcow2



を使用してゼロから再作成します







 $ virsh list Id Name State ---------------------------------------------------- 12 debian1 running 13 debian2 running 14 debian3 running ./remove_cluster.sh debian Are you sure to remove 'debian1 debian2 debian3 '? (Type 'y' when agree) y Domain debian1 destroyed Domain debian1 has been undefined Vol debian1.qcow2 deleted # Host debian1 found: line 8 /home/user/.ssh/known_hosts.debian updated. Original contents retained as /home/user/.ssh/known_hosts.debian.old Domain debian2 destroyed Domain debian2 has been undefined Vol debian2.qcow2 deleted # Host debian2 found: line 9 /home/user/.ssh/known_hosts.debian updated. Original contents retained as /home/user/.ssh/known_hosts.debian.old Domain debian3 destroyed Domain debian3 has been undefined Vol debian3.qcow2 deleted Host debian3 not found in /home/user/.ssh/known_hosts.debian Pool debian destroyed Pool debian has been undefined $ date && time ./deploy_vms_cluster.sh -o debian -s 3 && date Thu Aug 25 10:26:15 CEST 2016 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Using default 8.5.0 channel for debian Using default current release for debian Will use following path to SSH public key: /home/user/.ssh/id_rsa.pub Pool debian created Pool debian defined from /dev/stdin Formatting '/home/user/libvirt_images/debian/debian1.qcow2', fmt=qcow2 size=2147483648 backing_file=/home/user/libvirt_images/debian/debian-8.5.0-openstack-amd64.qcow2 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16 Pool debian refreshed Total translation table size: 0 Total rockridge attributes bytes: 763 Total directory bytes: 4458 Path table size(bytes): 40 Max brk space used 22000 187 extents written (0 MB) Pool debian refreshed Starting install... Creating domain... | 0 B 00:00:00 Domain creation completed. Pool debian defined from /dev/stdin Formatting '/home/user/libvirt_images/debian/debian2.qcow2', fmt=qcow2 size=2147483648 backing_file=/home/user/libvirt_images/debian/debian-8.5.0-openstack-amd64.qcow2 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16 Pool debian refreshed Total translation table size: 0 Total rockridge attributes bytes: 763 Total directory bytes: 4458 Path table size(bytes): 40 Max brk space used 22000 187 extents written (0 MB) Pool debian refreshed Starting install... Creating domain... | 0 B 00:00:00 Domain creation completed. Pool debian defined from /dev/stdin Formatting '/home/user/libvirt_images/debian/debian3.qcow2', fmt=qcow2 size=2147483648 backing_file=/home/user/libvirt_images/debian/debian-8.5.0-openstack-amd64.qcow2 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16 Pool debian refreshed Total translation table size: 0 Total rockridge attributes bytes: 763 Total directory bytes: 4458 Path table size(bytes): 40 Max brk space used 22000 187 extents written (0 MB) Pool debian refreshed Starting install... Creating domain... | 0 B 00:00:00 Domain creation completed. Use following command to connect to your cluster: 'ssh -i "/home/user/.ssh/id_rsa" debian@debian1' real 0m4.916s user 0m2.112s sys 0m0.268s Thu Aug 25 10:26:20 CEST 2016 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $ ssh debian1 Warning: Permanently added 'debian1,192.168.122.30' (ECDSA) to the list of known hosts. The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. debian@debian1:~$ date Thu Aug 25 08:26:38 UTC 2016 # <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      
      





仮想マシンは、20秒(SSDを搭載したラップトップ)の後、約10秒長くHDDで使用できるようになります。







~/libvirt_images/debian



ディレクトリの内容は次のようになります







 $ find ~/libvirt_images/debian -ls 4456467 4 drwxrwxr-x 5 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian 4853836 4 drwxrwxr-x 3 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian2 4853838 4 -rwxrwxr-x 1 user user 157 Aug 25 11:13 /home/user/libvirt_images/debian/debian2/rebuild_iso.sh 4853837 376 -rw-rw-r-- 1 libvirt-qemu kvm 382976 Aug 25 11:13 /home/user/libvirt_images/debian/debian2/cidata.iso 4985097 4 drwxrwxr-x 3 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian2/openstack 4985103 4 drwxrwxr-x 2 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian2/openstack/latest 4985104 4 -rw-rw-r-- 1 user user 574 Aug 25 11:13 /home/user/libvirt_images/debian/debian2/openstack/latest/user_data 4985105 4 -rw-rw-r-- 1 user user 152 Aug 25 11:13 /home/user/libvirt_images/debian/debian2/openstack/latest/meta_data.json 4456468 7752 -rw-r--r-- 1 libvirt-qemu kvm 7995392 Aug 25 11:13 /home/user/libvirt_images/debian/debian1.qcow2 4456470 7752 -rw-r--r-- 1 libvirt-qemu kvm 7995392 Aug 25 11:13 /home/user/libvirt_images/debian/debian3.qcow2 4722884 4 drwxrwxr-x 3 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian1 4722886 4 -rwxrwxr-x 1 user user 157 Aug 25 11:13 /home/user/libvirt_images/debian/debian1/rebuild_iso.sh 4722885 376 -rw-rw-r-- 1 libvirt-qemu kvm 382976 Aug 25 11:13 /home/user/libvirt_images/debian/debian1/cidata.iso 4853832 4 drwxrwxr-x 3 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian1/openstack 4853833 4 drwxrwxr-x 2 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian1/openstack/latest 4853834 4 -rw-rw-r-- 1 user user 574 Aug 25 11:13 /home/user/libvirt_images/debian/debian1/openstack/latest/user_data 4853835 4 -rw-rw-r-- 1 user user 152 Aug 25 11:13 /home/user/libvirt_images/debian/debian1/openstack/latest/meta_data.json 4985106 4 drwxrwxr-x 3 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian3 4985112 4 -rwxrwxr-x 1 user user 157 Aug 25 11:13 /home/user/libvirt_images/debian/debian3/rebuild_iso.sh 4985111 376 -rw-rw-r-- 1 libvirt-qemu kvm 382976 Aug 25 11:13 /home/user/libvirt_images/debian/debian3/cidata.iso 4985107 4 drwxrwxr-x 3 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian3/openstack 4985108 4 drwxrwxr-x 2 user user 4096 Aug 25 11:13 /home/user/libvirt_images/debian/debian3/openstack/latest 4985109 4 -rw-rw-r-- 1 user user 574 Aug 25 11:13 /home/user/libvirt_images/debian/debian3/openstack/latest/user_data 4985110 4 -rw-rw-r-- 1 user user 152 Aug 25 11:13 /home/user/libvirt_images/debian/debian3/openstack/latest/meta_data.json 4456469 7752 -rw-r--r-- 1 libvirt-qemu kvm 7995392 Aug 25 11:13 /home/user/libvirt_images/debian/debian2.qcow2 4456471 474092 -rw-rw-r-- 1 libvirt-qemu kvm 485465600 Aug 25 10:18 /home/user/libvirt_images/debian/debian-8.5.0-openstack-amd64.qcow2
      
      





ネットワークとDNSはdnsmasqを使用して構成され、 send host-name "<hostname>"



send host-name "<hostname>"



。 ネットワーク構成の段階で、ホスト名を仮想マシンに割り当て、DHCPを介してIPアドレスを要求し、ホスト名をdnsmasqに送信します。dnsmasqはホスト名を内部DNSに登録します。 リゾルバとしてdnsmasqを使用して、新しい仮想マシンのIPを取得します。







また、仮想マシン内で仮想マシンを実行しました。 ネストされたKVM仮想化は正常に機能しますが、追加の構成が必要になる場合があります。







短所:









リポジトリ自体はhttps://github.com/kayrus/scriptsにあります 。 libvirt環境のセットアップに関する情報もあります。







ボーナスとして、リポジトリにはすでにCoreOSの下でKubernetesクラスターの展開スクリプトがあります。







PSそのようなことを思いついたのは私だけではないでしょう。 リンクがこれ以上成熟したソリューションではないことを感謝します。








All Articles