1.查看虚拟机,启动虚拟机
[root@linux-node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
- CentOS-7-x86_64 shut off
[root@linux-node1 ~]# virsh start CentOS-7-x86_64
通过TightVNC连接,修改网络vi /etc/sysconfig/network-scripts/ifcfg-eth0
tips:
KVM虚拟机在宿主机上的体现是一个进程,可以通过ps axu |grep kvm进行查看。对KVM的管理,就是在宿主机上对KVM的进程进行管理。
2.libvirt介绍
libvirt组成部分:libvirt daemon后台进程,libvirt API库,User-space management tools
tips:
(1)如果libvirtd服务停用了,会影响命令行管理虚拟机,但不会影响虚拟机本身的运行。
(2)kvm是为openstack进行创建虚拟机,如果openstack服务停用,可以使用libvirtd服务命令行进行管理。
3.KVM常用管理:CPU,内存,网络
(1)libvirt通过对xml文件进行管理
[root@linux-node1 ~]# cd /etc/libvirt/qemu
[root@linux-node1 qemu]# ls -l
total 8
-rw-------. 1 root root 3855 Nov 27 10:00 CentOS-7-x86_64.xml
drwx------. 3 root root 4096 Nov 27 09:33 networks
(2)CentOS-7-x86_64.xml是自动生成的,无法进行修改。如果需要修改,需要通过virsh edit进行编辑。但是在openstack上是无法进行修改的。
[root@linux-node1 qemu]# virsh edit CentOS-7-x86_64
(3)KVM修改热添加:virsh edit CentOS-7-x86_64,热添加会影响系统的性能,不适合在生产环境使用
tips:KVM支持热添加,公有云是不支持热添加的。需要宿主机和虚拟机的系统保持一致性。
①cpu的热添加,将cpu核数改为2,openstack不支持
修改为:
[root@linux-node1 qemu]# virsh setvcpus CentOS-7-x86_64 2 --live
②内存热添加
a.查看KVM内存:
[root@linux-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd info balloon
balloon: actual=1024
b.修改,将内存调成512M:
[root@linux-node1 ~]# virsh qemu-monitor-command CentOS-7-x86_64 --hmp --cmd balloon 512
c.增加内存:修改xml文件,修改完xml文件需要重启虚拟机
<memory unit='KiB'>1048576</memory>
修改为:
<memory unit='KiB'>1548576</memory>
<currentMemory unit='KiB'>1048576</currentMemory>
[root@linux-node1 ~]# virsh shutdown CentOS-7-x86_64
[root@linux-node1 ~]# virsh start CentOS-7-x86_64
(4)网络管理,配置KVM的桥接网络
[root@linux-node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 8000.52540067fb26 yes virbr0-nic
vnet
①创建桥接网卡,写成脚本
[root@linux-node1 ~]# vim bridge.sh
#!/bin/bash
brctl addbr br0
brctl addif br0 ens33
ip addr del dev ens33 192.168.56.11/24
ifconfig br0 192.168.56.11/24 up
route add default gw 192.168.56.2
[root@linux-node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.000c2903f3fe no eth0
virbr0 8000.52540067fb26 yes virbr0-nic
vnet0
②修改xml
[root@linux-node1 ~]# virsh edit CentOS-7-x86_64
interface type='network' 修改为 interface type='bridge'
source network='default'/ 修改为 source bridge='br0'/
③重启KVM虚拟机
[root@linux-node1 ~]# virsh shutdown CentOS-7-x86_64
Domain CentOS-7-x86_64 is being shutdown
[root@linux-node1 ~]# virsh start CentOS-7-x86_64
Domain CentOS-7-x86_64 started
④修改虚拟机网卡配置
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
ONBOOT=yes
IPADDR=192.168.56.111
NETMASK=255.255.255.0
GATEWAY=192.168.56.2
[root@localhost ~]# systemctl restart network
⑤验证宿主机上能否登陆
[root@linux-node1 ~]# ping 192.168.56.111
PING 192.168.56.111 (192.168.56.111) 56(84) bytes of data.
64 bytes from 192.168.56.111: icmp_seq=1 ttl=64 time=0.618 ms
64 bytes from 192.168.56.111: icmp_seq=2 ttl=64 time=0.387 ms
64 bytes from 192.168.56.111: icmp_seq=3 ttl=64 time=0.264 ms
64 bytes from 192.168.56.111: icmp_seq=4 ttl=64 time=0.328 ms
^C
--- 192.168.56.111 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3002ms
rtt min/avg/max/mdev = 0.264/0.399/0.618/0.134 ms
[root@linux-node1 ~]# ssh 192.168.56.111
The authenticity of host '192.168.56.111 (192.168.56.111)' can't be established.
ECDSA key fingerprint is SHA256:k6ODnQ4pdtSMtYrfti2wB8eB5/XfCENW0SWApJrHTgY.
ECDSA key fingerprint is MD5:06:9f:34:32:b8:b2:a3:91:12:f9:3b:8c:dd:c0:e7:63.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.111' (ECDSA) to the list of known hosts.
root@192.168.56.111's password:
Last login: Mon Nov 27 11:44:22 2017