1. KVM安装(推荐用桌面系统创建创建虚拟机客户机,方便用vnc进入客户机否则命令行会很难受)
1.1 配置确认
首先需要确认服务器的硬件是否支持虚拟化,执行如下命令确认
egrep -c '(vmx|svm)' /proc/cpuinfo
如果输出结果大于0,意味着服务器硬件是支持虚拟化的。否则,重启进入BIOS设置中启用VT技术。
执行如下命令安装kvm-ok程序,来确定服务器是否能够运行硬件加速的KVM虚拟机
sudo kvm-ok
1.2 安装KVM
安装KVM及依赖项
sudo apt update
sudo apt install qemu qemu-kvm libvirt-bin bridge-utils virt-manager
sudo systemctl start libvirtd.service
sudo systemctl enable libvirtd.service
1.3 桥接网络配置
一般虚拟机网络配置有Bridge、NAT等几种模式。NAT模式下,虚拟机不需要配置自己的IP,通过宿主机来访问外部网络;Bridge模式下, 虚拟机需要配置自己的IP,然后虚拟出一个网卡, 与宿主机的网卡一起挂到一个虚拟网桥上(类似于交换机)来访问外部网络,这种模式下,虚拟机拥有独立的IP,局域网其它主机能直接通过IP与其通信。简单理解,就是NAT模式下,虚机隐藏在宿主机后面了,虚机能通过宿主机访问外网,但局域网其它主机访问不到它,Bridge模式下,虚机跟宿主机一样平等地存在,局域网其它主机可直接通过IP与其通信。一般我们创建虚机是用来部署服务供使用的, 所以都是用Bridge模式。
ubuntu 18中,网络配置通过netplan来实现了,如下,更改配置文件 /etc/netplan/50-cloud-init.yaml
devuser@cserver_01:~$ sudo vim /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by # the datasource. Changes to it will not persist across an instance. # To disable cloud-init's network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: ethernets: enp6s0: dhcp4: true enp7s0: dhcp4: no dhcp6: no version: 2 bridges: br0: interfaces: [enp7s0] dhcp4: no addresses: [192.168.40.241/24] gateway4: 192.168.40.1 nameservers: addresses: [114.114.114.114,8.8.8.8]
将宿主机原有网卡enp7s0挂到网桥br0上,并指定IP地址为192.168.40.241,nameservers指定DNS服务器。修改完后,通过sudo netplan apply
重启网络服务生效,然后通过ifconfig
查看,
原来挂在enp7s0网卡下的IP现在挂到了br0上,宿主机及所有其它虚拟机都通过该网桥来与外部通讯。我们也可以通过brctl show
来直观地查看,
devuser@server_01:~$ brctl show bridge name bridge id STP enabled interfaces br0 8000.2a5be3ec2698 no enp7s0 docker0 8000.02424524dcce no veth580af8e veth74119f3 vethe7a2b0f vethfe89039