Nestedな仮想化の設定（ホストOS:CentOS（KVM） ゲストOS:Ubuntu（VirtualBox））

##概要
自宅のCentOSのKVMの上のUbuntuでDockerためそうとしていたところ、途中でVituralboxによるVMの作成に失敗。調べたところゲストOSの上で入れ子の仮想化するための対策あった。実施したところ解決した。

##環境
ホストOS: CentOS 7.5.1804
ゲストOS: Ubuntu 18.04.3 LTS

##記録
DockerのGet StartedでDockerのお勉強してました。Part 4: Swarms（https://docs.docker.com/get-started/part4/ ）のところでVMを作成しようとしたら、エラーでてVMが作成できない様子。

[myoden@ubuntu002 Part3]$ docker-machine create --driver virtualbox myvm1
Creating CA: /home/myoden/.docker/machine/certs/ca.pem
Creating client certificate: /home/myoden/.docker/machine/certs/cert.pem
Running pre-create checks...
Error with pre-create check: "This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory"
[myoden@ubuntu002 Part3]$

「VMのなかでさらにVMは作れないのかなぁ」とおもっていたら、サイト（https://docs.fedoraproject.org/en-US/quick-docs/using-nested-virtualization-in-kvm/ ）にNested Virtualizationの方法が説明してあったので実施してみた。

[root@nuc2 ~]# cat /sys/module/kvm_intel/parameters/nested
Y
[root@nuc2 ~]# 
[root@nuc2 ~]# modprobe -r kvm_intel
[root@nuc2 ~]# modprobe kvm_intel nested=1       
[root@nuc2 ~]# vi /etc/modprobe.d/kvm.conf
[root@nuc2 ~]# cat /etc/modprobe.d/kvm.conf
options kvm_intel nested=1
[root@nuc2 ~]#

「Configuring nested virtualization in virt-manager」の部分
![Screenshot from 2019-08-25 16-18-02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/487301/242a7c3c-7ac4-9501-b90a-7386ce751145.png)
![Screenshot from 2019-08-25 16-18-56.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/487301/099b3ffd-c346-6326-73bf-01cac01ed78d.png)


設定後、VMを再起動。結果、ちゃんと作れるようになりました。

myoden@ubuntu002:~/SandBox/Docker/Part3$ docker-machine create --driver virtualbox myvm1
Running pre-create checks...
(myvm1) Image cache directory does not exist, creating it at /home/myoden/.docker/machine/cache...
(myvm1) No default Boot2Docker ISO found locally, downloading the latest release...
(myvm1) Latest release for github.com/boot2docker/boot2docker is v19.03.1
(myvm1) Downloading /home/myoden/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v19.03.1/boot2docker.iso...
(myvm1) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100%
Creating machine...
(myvm1) Copying /home/myoden/.docker/machine/cache/boot2docker.iso to /home/myoden/.docker/machine/machines/myvm1/boot2docker.iso...
(myvm1) Creating VirtualBox VM...
(myvm1) Creating SSH key...
(myvm1) Starting the VM...
(myvm1) Check network to re-create if needed...
(myvm1) Found a new host-only adapter: "vboxnet0"
(myvm1) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env myvm1
myoden@ubuntu002:~/SandBox/Docker/Part3$

myoden@ubuntu002:~/SandBox/Docker/Part3$ docker-machine create --driver virtualbox myvm2
Running pre-create checks...
Creating machine...
(myvm2) Copying /home/myoden/.docker/machine/cache/boot2docker.iso to /home/myoden/.docker/machine/machines/myvm2/boot2docker.iso...
(myvm2) Creating VirtualBox VM...
(myvm2) Creating SSH key...
(myvm2) Starting the VM...
(myvm2) Check network to re-create if needed...
(myvm2) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env myvm2
myoden@ubuntu002:~/SandBox/Docker/Part3$

myoden@ubuntu002:~/SandBox/Docker/Part3$ docker-machine ls
NAME    ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER     ERRORS
myvm1   -        virtualbox   Running   tcp://192.168.99.100:2376           v19.03.1   
myvm2   -        virtualbox   Running   tcp://192.168.99.101:2376           v19.03.1   
myoden@ubuntu002:~/SandBox/Docker/Part3$