KVM实践02----创建一个虚拟机

前提:参考《KVM实践01》完成环境搭建。

  • 虚拟机网络

网络是通信的基础,也是最复杂的部分。这里介绍的是最基本的网络拓扑,要知道生产环境真实网络是非常复杂的。

《KVM实践01》中安装过依赖包libvirt并启用libvirtd后,libvirt会自动生成网桥virbr0

[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:5b:d7:23 brd ff:ff:ff:ff:ff:ff
    inet 192.168.78.134/24 brd 192.168.78.255 scope global noprefixroute dynamic ens33
       valid_lft 1611sec preferred_lft 1611sec
    inet6 fe80::a7b4:c6d7:ea62:c1ae/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:18:bd:27 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:18:bd:27 brd ff:ff:ff:ff:ff:ff

可以使用该网桥桥接Guest虚机网络,或者重新创建新的网桥。virbr0的默认配置文件为

[root@localhost networks]# cat /usr/share/libvirt/networks/default.xml 
<network>
  <name>default</name>
  <bridge name="virbr0"/>
  <forward/>
  <ip address="192.168.122.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.122.2" end="192.168.122.254"/>
    </dhcp>
  </ip>
</network>

 关于libvirt network xml配置文件定义,请参https://libvirt.org/formatnetwork.html,默认forward模式为nat模式,即虚拟机共享主机网络。本次采用默认的nat模式,还可以采用route、bridge等模式,大同小异。

forward

Inclusion of the forward element indicates that the virtual network is to be connected to the physical LAN.Since 0.3.0. The mode attribute determines the method of forwarding. If there is no forward element, the network will be isolated from any other network (unless a guest connected to that network is acting as a router, of course). The following are valid settings for mode (if there is a forward element but mode is not specified, mode='nat' is assumed):

需要注意的是,是用nat模式无法支持VLAN标签,意味着桥接于同一bridge上的虚拟机无隔离性,想要实现网络隔离和一些高级功能,一般需要借助OVS(Open vSwitch)来实现。OVS基础可以参考http://www.openvswitch.org/https://blog.csdn.net/qq_15437629/article/details/45766235

Network connections that support guest-transparent VLAN tagging include

1) type='bridge' interfaces connected to an Open vSwitch bridge Since 0.10.0,

2) SRIOV Virtual Functions (VF) used via type='hostdev' (direct device assignment) Since 0.10.0, and

3) SRIOV VFs used via type='direct' with mode='passthrough' (macvtap "passthru" mode) Since 1.3.5.

  • 基于libvirt管理虚拟机

前面多次提到libvirt工具,下面简单介绍libvirt。libvirt的定位其实是一个中间库,一座“桥”,北向对接各种第三方管理工具,包括virsh、OpenStack(Nova)等,南向对接KVM、Xen、ESX等虚拟化平台。这样上层API调用无需关心底层HyperVisor。

libvirt is an open-source APIdaemon and management tool for managing platform virtualization.[3] It can be used to manage KVMXenVMware ESXQEMU and other virtualization technologies. These APIs are widely used in the orchestration layer of hypervisors in the development of a cloud-based solution.

  • 通过ISO手动安装虚拟机

(1)使用qemu-img命令创建一个20G的镜像文件

[root@kvm home]# qemu-img create -f qcow2 vm01.qcow2 20G
Formatting 'vm01.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off

(2)使用如下命令创建虚拟机

其中虚拟机规格1u1g,使用(1)中创建的镜像,虚拟网卡使用默认的virbr0桥接,允许使用VNC登陆,OS类型为Linux(rhl7.4),采用ISO镜像安装。

[root@kvm ~]# virt-install --virt-type kvm --name vm01 --vcpus=1 --ram 1024 \
>   --disk /home/vm01.qcow2,format=qcow2 \
>   --network bridge=virbr0  \
>   --graphics vnc,listen=0.0.0.0 --noautoconsole \
>   --os-type=linux --os-variant=rhl7.4  \
>   --location=/home/rhel-server-7.4-x86_64-dvd.iso

Starting install...
Retrieving file .treeinfo...                                                                                            | 1.9 kB  00:00:00
Retrieving file vmlinuz...                                                                                              | 5.6 MB  00:00:00
Retrieving file initrd.img...                                                                                           |  47 MB  00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
[root@kvm ~]# virsh list
 Id    Name                           State
----------------------------------------------------
 6     vm01                           running

(3) 使用如下命令查询vm01的VNC登陆端口


[root@kvm ~]# virsh vncdisplay vm01
:0

(4)使用本地VNC client登陆虚拟机控制台,完成OS安装 

(5)说明:

通过osinfo-query os命令可以查询 --os-variant参数的值;

如果VNC登陆失败,可以检查KVM主机是否开启防火墙,开启防火墙的情况下,VNC端口默认屏蔽

通过virsh undefine vm01取消注册vm01

  • 如果有现成的安装好的qcow2格式镜像,可以直接通过镜像生成虚拟机,省去安装步骤。命令如下:
  cp vm01.qcow2 vm02.qcow2
  
  virt-install --virt-type kvm --name vm02 --vcpus=1 --ram 1024 \
  --disk /home/vm02.qcow2,format=qcow2 \
  --network bridge=virbr0  \
  --graphics vnc,listen=0.0.0.0 --noautoconsole \
  --os-type=linux --os-variant=rhl7.4  \
  --boot hd

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值