搭建 KVM 虚拟机平台
KVM 是虚拟化的一种方案,借助 KVM 可快速创建虚拟机。
开始
- 安装 KVM(
sudo apt list --installed |grep qemu
查看是否安装)sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
virt-manager 附带安装了 qemu-kvm libvirt-clients libvirt-daemon-system,如果不安装 virt-manager 就需要手动安装这些包了
sudo adduser `id -un` libvirt
sudo adduser `id -un` kvm
- 配置
- 修改存储路径为家目录:
virsh pool-edit default
修改里面的<path>
- 删除默认的 defaullt 网络配置文件:
- virsh net-undefine default
- virsh net-destroy default
- 为虚拟机绑定静态 IP(需在 Vgrantfile 中用 libvirt.management_network_mac 给每个虚拟机绑定 MAC 地址 : Vgrantfile 示例
virsh net-edit vagrant-libvirt
打开网络配置文件- 在
<range start='192.168.121.1' end='192.168.121.254'/>
后添加: <host mac='52:54:00:e0:72:28' name='api.viewfilx.com-API' ip='192.168.121.89' /> <host mac='52:54:00:9b:f0:46' name='minio.viewfilx.com-STORAGE' ip='192.168.121.90' /> <host mac='52:54:00:fa:74:93' name='mysql.viewfilx.com-DB' ip='192.168.121.91' /> <host mac='52:54:00:ae:5d:9c' name='nginx.viewfilx.com-NGINX' ip='192.168.121.92' /> <host mac='52:54:00:40:e9:48' name='vue.viewfilx.com-WEB' ip='192.168.121.93' />
- 保存退出
- 启动网卡: virsh net-start vagrant-libvirt
- 重启 libvirtd:sudo systemctl restart libvirtd
- 修改存储路径为家目录:
- 常用命令
- 开机启动 libvirt:
sudo systemctl enable libvirtd
- 查看是否开机启动:sudo systemctl list-unit-files |grep libvirtd
- 启动 libvirt:
sudo systemctl start libvirtd
- 重启 libvirt:sudo systemctl restart libvirtd
- 查看 libvirt 状态:
systemctl status libvirtd || lsmod | grep kvm
- 解除对持久性网络的定义:virsh net-undefine default
- 删除名为 vagrant-libvirt 的网络:virsh net-destroy default
- 列举所有的存储池:virsh pool-list
- 列举所有在 default 存储池中的镜像名称:virsh vol-list default
- 列举所有网络:virsh net-list --all
- 列举所有虚拟机的信息:virsh list --all
- 查看网络配置文件:virsh net-dumpxml vagrant-libvirt
- 创建网络配置文件:sudo virsh net-create /etc/libvirt/qemu/networks/network.xml
- 定义网络配置文件:sudo virsh net-define /etc/libvirt/qemu/networks/network.xml
- 配置网络开机启动:sudo virsh net-autostart network
- 卸载 virt-manager:sudo apt remove virt-manager --purge 或者 sudo dpkg -p virt-manager
- 查看软件包信息:sudo apt show qemu-kvm -a
- 开机启动 libvirt:
- FAQ
- 不用 virt-manager 添加 KVM 虚拟机:
sudo virt-install --name ubuntu-guest --os-variant ubuntu20.04 --vcpus 2 --ram 2048 --location http://ftp.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/ --network bridge=virbr0,model=virtio --graphics none --extra-args='console=ttyS0,115200n8 serial'
/opt/vagrant/embedded/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in require': /usr/lib64/libk5crypto.so.3: undefined symbol:
- 卸载 vagrant-libvirt:
vagrant plugin uninstall vagrant-libvirt
- 卸载原来的 vagrant:
sudo dnf remove vagrant
- 重新安装 vagrant:
sudo dnf install vagrant
- 编译 libssh 库:
sudo yum install gcc-c++ dnf download --source libssh rpm2cpio libssh-*.src.rpm | cpio -imdV tar xf libssh-*.tar.xz mkdir build cd build cmake ../libssh-* -DOPENSSL_ROOT_DIR=/opt/vagrant/embedded/ make sudo cp lib/libssh* /opt/vagrant/embedded/lib64
- 编译 krb5-libs 库:
sudo dnf install byacc dnf download --source krb5-libs rpm2cpio krb5-*.src.rpm | cpio -imdV tar xf krb5-*.tar.gz cd krb5-*/src ./configure make sudo cp -a lib/crypto/libk5crypto.* /opt/vagrant/embedded/lib64/
- 此时不用指定
CONFIGURE_ARGS
参数了,用vagrant plugin install vagrant-libvirt
安装插件,再 用vagrant up
就不会报错了 - 参考:https://github.com/hashicorp/vagrant/issues/11020
- 卸载 vagrant-libvirt:
- 参考:virtualization_deployment_and_administration_guide
- 不用 virt-manager 添加 KVM 虚拟机: