- KVM嵌套
服务器centos7开通Nested KVM
cat << EOF > /etc/modprobe.d/kvm-nested.conf
options kvm-intel nested=1
options kvm-intel enable_shadow_vmcs=1
options kvm-intel enable_apicv=1
options kvm-intel ept=1
EOF
服务器重启
reboot
查看是否支持kvm嵌套(支持输出Y)
cat /sys/module/kvm_intel/parameters/nested
- 原始服务器创建支持vms的虚拟机
创建普通vm后,虚机的xml:
原始:
<cpu mode='host-model' check='partial'>
<model fallback='allow'/>
<topology sockets='24' cores='1' threads='1'/>
<numa>
<cell id='0' cpus='0-23' memory='4194304' unit='KiB'/>
</numa>
</cpu>
更改为:
<cpu mode='custom' match='exact' check='partial'>
<model fallback='allow'>Nehalem</model>
<feature policy='require' name='vmx'/>
<numa>
<cell id='0' cpus='0-23' memory='4194304' unit='KiB'/>
</numa>
</cpu>
这种方式为虚拟机定义需要模拟的CPU类型 “Nehalem”,并且为 CPU 添加 “vmx” 特性,需要在以上基础上增加原始配置里的numa
- 虚拟机内部安装kvm环境
查看是否嵌套成功:lscpu | grep vmx 查看cpu、内存数 cat /proc/cpuinfo | grep “cpu cores” | uniq cpu cores : 1 cat /proc/meminfo | grep MemTotal MemTotal: 3876804 kB 查看centos版本 cat /etc/centos-release CentOS Linux release 7.9.2009 (Core)
systemctl stop firewalld.service #关闭防火墙
systemctl disable firewalld.service #关闭开机自启
安装一些最基本的工具
yum install epel-release net-tools vim unzip zip wget ftp -y
安装kvm
yum install -y virt-manager* virt-install libvirt* *vnc*
yum install -y kvm virt-* libvirts bridge-utils qemu-img
开启kvm服务,并设置为开机自启动
systemctl start libvirtd
systemctl enable libvirtd
创建磁盘镜像
qemu-img create -f qcow2 testimage01.qcow2 30G
上传iso镜像
查看端口是否被占用
netstat -anp | grep 5907
虚拟机xml:
<domain type='kvm'>
<name>vm-test02</name>
<memory unit='KiB'>8388608</memory>
<currentMemory unit='KiB'>8388608</currentMemory>
<vcpu placement='static'>2</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='cdrom'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact'>
<model fallback='allow'>Nehalem</model>
</cpu>
<clock offset='localtime'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/var/lib/libvirt/images/testimage01.qcow2'/>
<target dev='hda' bus='ide'/>
</disk>
<disk type='file' device='cdrom'>
<source file='/home/mnt/CentOS-7-x86_64-DVD-1511.iso'/>
<target dev='hdb' bus='ide'/>
</disk>
<interface type='network'>
<source network='default' bridge='virbr0'/>
</interface>
<input type='tablet' bus='usb'/>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='5905' autopart='no' listen='0.0.0.0' keymap='en-us'/>
</devices>
</domain>
define一下,创建成功
查看vnc端口后进入刚创建的虚拟机:
配置最新vm的网络
ONBOOT=yes
NAMESERVER=223.5.5.5
systemctl restart network网络就好了
- 最内层虚拟机安装docker
防火墙记得关:
systemctl stop firewalld
systemctl disable firewalld
卸载
yum remove docker-ce
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
设置存储库
安装yum-utils包(提供yum-config-manager 实用程序)并设置稳定存储库。
yum install -y yum-utils
yum-config-manager \
--add-repo \http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装 Docker 引擎
安装最新版本的 Docker Engine 和 containerd,或者进入下一步安装特定版本:
yum install docker-ce docker-ce-cli containerd.io
启动 Docker。
$ sudo systemctl start docker
大功告成
|