Centos7 practice Kvm under VMware virtualization (plain)

Although the Internet has been a lot of tutorials on kvm installed, but I still see dizziness, a lot of tutorial install some packages, some small, did not explain that the role of the installation package is doing, also command not the same, did not explain what it means command Yes.

 I re-wrote a tutorial, as accessibly point, but also to enhance my memory.

Under this prompt, Hyper-v is a pit! Originally I was using Hyper-v to learn this Kvm virtualization, I created the host virtual machine can be connected to external networks, host created in kvm virtual machine with bridged network, but no matter what the settings are not connected to external networks, kvm the virtual machine can get through the host ping, ping the host can get through kvm virtual machine, the host can get through ping outside the network, but is kvm virtual machines ping nowhere outside the network, but the harm I checked all day behind after the resignation back with VMware, all normal network.

And with Hyper-v, obviously I have a lot of computer memory accounted for, every time you start, we have to wait what the merger checkpoint half a day, not connecting U disk.

 

Preparation: a virtual machine can access the Internet Centos7, VncViewer (which is running in a window, when you can connect to a virtual machine), XShell (remote ssh tool) , xFTP (upload installation package)

VncViewer Download: https://www.realvnc.com/en/connect/download/viewer/

XShell, XFtp Download: https://www.netsarang.com/zh/free-for-home-school/

The main steps:

  1. Open nested virtual function (if you are not using a virtual machine, then this step would not)
  2. Installation Kit kvm
  3. Set the bridge card (if using a Nat, do this step)
  4. Kvm create the first virtual machine (mounting points console installation and remote graphical installation )

 


 

1, open nested virtual function

At first I was installed in the hyper-v virtual machine, vmware hyper-v than the trouble spots, starting the following command operations

# The following commands are not case sensitive 
the Get-VM     # get all the virtual machines 
the Get-VMProcessor -VMName xxx | FL     # view virtual machine properties, ExposeVirtualizationExtensions property on behalf of the nested virtualization is turned on 
the Set-VMProcessor -ExposeVirtualizationExtensions $ to true -VMName xxx     # open nested virtualization, but first virtual machine machine, or be wrong

 

 

VMware behind the switch much easier, as shown below, the red circle in the two check boxes on the hook on the line, you must first shut down the virtual machine can be modified

 

 Open the virtual machine, enter the following command to check whether virtualization, if not above operation is certainly not the

cat /proc/cpuinfo | egrep "vmx|svm"

 


 

2, the installation kit kvm

I see a lot of online tutorials installed packages a lot, but not any descriptions of each package is doing, forgive me a little obsessive-compulsive disorder, I have to understand the role of each package in the end is doing

But after I tested, eventually found only need to install three packages, namely libvirt, qemu-kvm, virt-install (online writing some of the other packages, such as libvirt-python, libvirt-client, qemu-img, in fact, when you install these three packages will be installed incidentally).

我的理解:kvm只是linux一个功能,但linux没有提供任何方便我们去使用它的命令,所以需要安装一些工具,而libvirt、qemu-kvm、virt-install都是工具包,qemu-kvm提供一些qemu开头的命令(这个不安装会提供错误主机不支持任意虚拟化选项),libvirt提供一个服务(libvirtd)和其它virsh管理命令,virt-install提供安装管理虚拟机的命令

yum -y install qemu-kvm libvirt virt-install

因为我的是命令行管理,不在虚拟机上用图形化管理,所以只需这三个包,因为要在虚拟机上图形管理,还得安装个XWindows,我觉得生产环境应该也没人去装个图形管理。

 

还有一个包bridge-utils,这是用来管理桥接网卡的,这个我装libvirt包时也顺带装了,如果你的没包装,就安装一下

yum -y install bridge-untils

 


 

3、设置桥接网卡

 经常玩虚拟机的都知道有两种试,一种是Nat,一种是桥接,

Nat就好比宿主机(就是安装kvm虚拟化的主机)是台路由器,虚拟机是连接路由器下面的电脑,跟宿主机同个网段的电脑是访问不了虚拟机的,要访问的话,就得在宿主机上设置端口映射。

而桥接就是把宿主机的物理网卡当做一个交换机,虚拟机直接连接交换机,那就跟宿主机是同一个网段的了,可以被其它电脑访问到,一般做服务虚拟化都是用桥接模式。

 先切换到网卡目录,然后把原来的网卡文件复制一份,名称为ifcfg-br0,命令如下

cd /etc/sysconfig/network-scripts
cp ifcfg-ens33 ifcfg-br0  #你的不一定是ifcfg-ens33,我开始用hyper-v时,接口是叫eth0

然后修改ifcfg-br0的配置,如下

 

修改ifcfg-ens33,主要是新增加一行,如下所示,如果你的本来是静态IP的话,就把静态IP的配置注释掉,只保留在br0就行了

然后重启下网络服务

systemctl restart NetworkManager
systemctl restart network

 然后查看下网桥接口

brctl show

这个图是我在后面补的,红圈的是在启动虚拟机后才有的,你如果按博客顺序的话,现在应该是看不到这个红圈的vnet0的,等你把虚拟机启动后,再查看就看到这个vnet0,这表示vnet0也挢接在br0下面

 


 

 4、创建第一个kvm虚拟机

 这个就需要用到之前安装的包virt-install了,我先把命令主要参数列出来

#virt-install参数
--name=xxx                            #虚拟机唯一名称
--memory=1024[,maxmemory=2048]        #虚拟机内存,单位为mb --memory=1024,maxmemory=2048
--vcpus=1[,maxvcpus=4]                #虚拟机CPU数量
--cdrom=/xxx/xxx                     #指定安装源文件
--location=/xxx/xxx                    #指定安装源文件,跟--cdrom二选一,如果要用控制台安装得用这个,配合--extra-args参数
--disk path=/xx/xxx[,size=10,format=raw] #存储文件及格式
--graphics vnc,port=xxx,listen=xxx     #图形化参数,不用图形化用--graphics none --extra-args="console=ttyS0"
--network bridge=br0              #网络连接方式
--os-variant=xxx               #对应的系统值,可以osinfo-query os这个查对应值
--virt-type=kvm                   #虚拟机类型
--noautoconsole                #不自动连接,默认是安装时用virt-viewer或者virsh console去连接虚拟机

 把管理虚拟机的命令也写一下

virsh list --all    #查看所有虚拟机,加all列出关机状态的
virsh console xxx    #以控件台连接到指定虚拟机
virsh start xxx        #启动虚拟机
virsh shutdown xxx    #关闭虚拟机,一般关不了
virsh destroy xxx    #强制关闭虚拟机
virsh autostart xxx    #设置虚拟机随机启动
virsh undefine xxx    #删除虚拟机,只会删除对应的xml,硬盘文件不会删除
virsh autostart xxx    #设置虚拟机自动启动

 

4.1、利用VncViewer远程图形化安装的方式

virt-install主要参数--graphics vnc,port=5901,listen=xxx

原理就是用virt-install命令安装时,在宿主机上开启一个vnc端口5901,然后用VncViewer程序连接宿主机的5901端口,跟平时图形化安装一样了。

推荐用Xshell连接,因为可以方便地复制命令,再装个XFtp上传iso文件到/data/iso目录里

 创建所需目录

mkdir -p /data/iso /data/vm

把安装文件上传到/data/iso后,用Xshell连接后,复制以下命令,回车(以下命令的斜杠,只是为了好看分行,不是必要的)

virt-install --name=c7 \
--memory=1024 --vcpus=1 \
--cdrom=/data/iso/CentOS-7-x86_64-Minimal-1810.iso \
--disk path=/data/vm/c7.qcow2,size=10,format=qcow2 \
--network bridge=br0 --virt-type=kvm --os-variant=centos7.0 \
--graphics vnc,listen=0.0.0.0,port=5900 \
--noautoconsole

看到以上提示,就可以用VncViewer来连接了,对了,得先把防火墙服务给关了

systemctl stop firewalld  #关闭防火墙服务
systemctl --zone=public --add-ports=5900/tcp --permanent    #或者用这个开放5900端口

 在vncViewer上输入ip跟端口号

然后就可以图形化安装了,非常方便。

 

 4.2、用控制台安装的方式

virt-install主要参数--location跟--extra-args配合,不用--cdrom参数,复制以下命令,回车

virt-install \
--name=c7-2 \
--memory=1024 \
--vcpus=1 \
--location=/data/iso/CentOS-7-x86_64-Minimal-1810.iso \
--disk path=/data/vm/c7-2.qcow2,size=5,format=qcow2 \
--network bridge=br0 \
--virt-type=kvm \
--os-variant=centos7.0 \
--graphics none \
--extra-args 'console=ttyS0'

 

接着就会进入控制台安装界面,如下图所示

 

可能还有些人没在控制台安装过centos7,那我简单说下

观察这个,这些可以对应我上面的那个图形化安装的图,注意红圈,打X的就是已经设置好了,!的代表还没设置,需要进行配置,输入q退出安装,输入b开始安装,输入r刷新

  1. 语言设置
  2. 时区设置
  3. 安装源
  4. 软件选择
  5. 安装位置
  6. Kdump
  7. 网络配置
  8. Root密码
  9. 用户设置

那我们需要对那些!的配置项进行设置,或者修改原来设置,先修改第1项,语言设置,输入1回车,会显示下图,找到我们的中文,输入68,回车

 

然后输入1,简体中文,回车

 

会回到主界面,你会看到第1项已经变了,变成简体中文了

 

 

再配置时区,输入2,回车,然后输入1(Set timezone),然后输入2(Asia亚洲),然后输入64(Shanhai上海)

 

回到主界面,发现第2项已改变了

 

 

接着再设置安装位置,输入5,回车,如下图,看到硬盘前面已经是X,说明已经选择了,直接输入c继续就好了

 

这里不太懂,我默认,输入c继续

 

还是保持默认LVM,输入c继续

 

会回到主界面,发现安装位置也改变了,那最后来设置下Root密码,输入8,回车

 

重复输入两次root密码

 

 

回到主界面,发现没有!号了,那输入b就可以进行安装了,你会发现其实控制台安装跟图形化安装也是差不多的。

 

安装好后启用下网卡,就可以正常连接外网的,我一开始是用Hyper-v,导致虚拟机一直连不了外网,以为哪里配置不对,用回VMware后就一切正常。

Guess you like

Origin www.cnblogs.com/caijt/p/11174676.html