OpenStack入门篇(四)之KVM虚拟机介绍和管理

时间:2023-12-21 18:37:04
  • 1.查看虚拟机,启动虚拟机

[root@linux-node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
- CentOS--x86_64 shut off [root@linux-node1 ~]# virsh start CentOS--x86_64

通过TightVNC连接,修改网络vi /etc/sysconfig/network-scripts/ifcfg-eth0

OpenStack入门篇(四)之KVM虚拟机介绍和管理

tips:
KVM虚拟机在宿主机上的体现是一个进程,可以通过ps axu |grep kvm进行查看。对KVM的管理,就是在宿主机上对KVM的进程进行管理。

  • 2.libvirt介绍

libvirt组成部分:libvirt daemon后台进程,libvirt API库,User-space management tools

OpenStack入门篇(四)之KVM虚拟机介绍和管理

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
-rw-------. root root Nov : CentOS--x86_64.xml
drwx------. root root Nov : networks

(2)CentOS-7-x86_64.xml是自动生成的,无法进行修改。如果需要修改,需要通过virsh edit进行编辑。但是在openstack上是无法进行修改的。

[root@linux-node1 qemu]# virsh edit CentOS--x86_64

(3)KVM修改热添加:virsh edit CentOS-7-x86_64,热添加会影响系统的性能,不适合在生产环境使用
tips:KVM支持热添加,公有云是不支持热添加的。需要宿主机和虚拟机的系统保持一致性。

①cpu的热添加,将cpu核数改为2,openstack不支持

OpenStack入门篇(四)之KVM虚拟机介绍和管理

修改为:

OpenStack入门篇(四)之KVM虚拟机介绍和管理

[root@linux-node1 qemu]# virsh setvcpus CentOS--x86_64  --live

②内存热添加

a.查看KVM内存:

[root@linux-node1 ~]# virsh qemu-monitor-command CentOS--x86_64 --hmp --cmd info balloon
balloon: actual=

b.修改,将内存调成512M:

[root@linux-node1 ~]# virsh qemu-monitor-command CentOS--x86_64 --hmp --cmd  balloon 

c.增加内存:修改xml文件,修改完xml文件需要重启虚拟机

<memory unit='KiB'></memory>
修改为:
<memory unit='KiB'></memory>
<currentMemory unit='KiB'></currentMemory> [root@linux-node1 ~]# virsh shutdown CentOS--x86_64
[root@linux-node1 ~]# virsh start CentOS--x86_64

(4)网络管理,配置KVM的桥接网络

[root@linux-node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
virbr0 .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/
ifconfig br0 192.168.56.11/ up
route add default gw 192.168.56.2
[root@linux-node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 .000c2903f3fe no eth0
virbr0 .52540067fb26 yes virbr0-nic
vnet0

②修改xml

[root@linux-node1 ~]# virsh edit CentOS--x86_64
interface type='network' 修改为 interface type='bridge'
source network='default'/ 修改为 source bridge='br0'/

③重启KVM虚拟机

[root@linux-node1 ~]# virsh shutdown CentOS--x86_64
Domain CentOS--x86_64 is being shutdown
[root@linux-node1 ~]# virsh start CentOS--x86_64
Domain CentOS--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) () bytes of data.
bytes from 192.168.56.111: icmp_seq= ttl= time=0.618 ms
bytes from 192.168.56.111: icmp_seq= ttl= time=0.387 ms
bytes from 192.168.56.111: icmp_seq= ttl= time=0.264 ms
bytes from 192.168.56.111: icmp_seq= ttl= time=0.328 ms
^C
--- 192.168.56.111 ping statistics ---
packets transmitted, received, % 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::9f:::b8:b2:a3:::f9:3b:8c:dd:c0:e7:.
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 ::