一、安装我们所需的包
[root@kvm01-server ~]# yum install -y libvirt virt-install qemu-kvm
####################################################
libvirt #虚拟机管理工具
virt-install #安装虚拟机和克隆
qemu-kvm #管理磁盘
二、开启自启服务
[root@kvm01-server ~]# systemctl start libvirtd
[root@kvm01-server ~]# systemctl enable libvirtd
三、准备安装虚拟机
1、安装命令
[root@kvm01-server ~]# virt-install --virt-type kvm --os-type=linux --os-variant rhel6 --name web01 --memory 1024 --vcpus 1 --disk /opt/web01.raw,format=raw,size=10 --cdrom /opt/CentOS-6.10-x86_64-bin-DVD1.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
2、内置命令介绍
3、在windows页面下载vnc软件
软件下载地址为https://www.tightvnc.com/
在这时候需要找到我们下载后的VNC软件
然后进入输入我们的主机ip(注意不是kvm中创建的ip地址)------由于当前只创建一个kvm主机,所以不需要特殊指定
确定后进入正常的创建页面(镜像创建主机,与正常搭建centos6的虚拟机几乎一致,所以就不在过多说明)给个进入页面表示与前面几乎一致
需要注意的是在kvm创建新机的时候几乎不需要做什么改动,我们在创建的时候只需要修改他的ip为自动获取即可,其他可不做改动
查看kvm主机安装完成后。
4、在linux中可以看到端口开启(5900)
[root@kvm01-server /opt]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN 9768/qemu-kvm
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 8676/rpcbind
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 9373/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 8938/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 9045/master
tcp6 0 0 :::111 :::* LISTEN 8676/rpcbind
tcp6 0 0 :::22 :::* LISTEN 8938/sshd
tcp6 0 0 ::1:25 :::* LISTEN 9045/master
5、虚拟机相关文件介绍查看
[root@kvm01-server /opt]# ll -h
total 5.5G
-rw-r--r-- 1 qemu qemu 4.3G Jul 28 16:29 CentOS-7-x86_64-DVD-1810.iso
-rw------- 1 qemu qemu 10G Jul 28 16:46 web01.raw #磁盘
[root@kvm01-server /opt]# ll /etc/libvirt/qemu
total 4
drwx------ 3 root root 42 Jul 28 16:09 networks
-rw------- 1 root root 4071 Jul 28 16:38 web01.xml #配置文件
五、虚拟机基础的管理操作
1、查看正在运行的主机
[root@kvm01-server /opt]# virsh list
Id Name State
----------------------------------------------------
2、查看所有状态下的主机
[root@kvm01-server /opt]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
3、启动主机
3.1启动主机
[root@kvm01-server /opt]# virsh start web01
Domain web01 started
3.2查看主机状态
[root@kvm01-server /opt]# virsh list --all
Id Name State
----------------------------------------------------
3 web01 running
4、xshell尝试连接kvm虚拟机
[root@kvm01-server /opt]# ssh root@192.168.122.158
The authenticity of host '192.168.122.158 (192.168.122.158)' can't be established.
ECDSA key fingerprint is SHA256:jbzuGVfGYqAF46wfuoomdxTKnIb0kBt7C3s1d9Luk9w.
ECDSA key fingerprint is MD5:75:56:da:4c:44:ec:1c:e0:89:52:c8:69:5c:72:8c:4a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.122.158' (ECDSA) to the list of known hosts.
root@192.168.122.158's password:
Last login: Tue Jul 28 17:34:58 2020
[root@kvm ~]#
5、尝试能否ping通外网
[root@kvm ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.122.1
[root@kvm ~]# ping 192.168.122.1
PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data.
64 bytes from 192.168.122.1: icmp_seq=1 ttl=64 time=0.217 ms
64 bytes from 192.168.122.1: icmp_seq=2 ttl=64 time=0.259 ms
^C
--- 192.168.122.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.217/0.238/0.259/0.021 ms
[root@kvm ~]# ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=1 ttl=127 time=31.3 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=2 ttl=127 time=30.6 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=3 ttl=127 time=30.7 ms
64 bytes from 39.156.69.79 (39.156.69.79): icmp_seq=4 ttl=127 time=31.2 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 30.644/30.980/31.302/0.302 ms
6、关机----kvm虚拟机
6.1、正常关机
[root@kvm01-server /opt]# virsh shutdown web01
Domain web01 is being shutdown
[root@kvm01-server /opt]# virsh list --all
Id Name State
----------------------------------------------------
3 web01 in shutdown
[root@kvm01-server /opt]# virsh list --all
Id Name State
----------------------------------------------------
- web01 shut off
6.2、强制关机
[root@kvm01-server /opt]# virsh destroy web01
大致也差不多就这样算是kvm虚拟机创建完成了