KVM 虚拟化-centos8

目录

一、安装软件

二、开启服务(保证qemu正常运行)

三、使用命令调出图形化工具

四、命令行管理

1.查看默认网桥

2.删除默认网桥

3.通过nmcli创建虚拟bridge

4.使用cloud镜像拉取虚拟机

1)下载

2)创建目录存放qcow2文件

3)自定义qcow2文件

4)利用修改后的qcow2文件拉取虚拟机

5)查看虚拟机

6)进去虚拟机控制台

 7)配置虚拟机的ip地址

 5.扩展虚拟机的根分区

1)干掉虚拟机

2)使用virt-filesystems命令查看qcow2文件的分区情况

 3)重新创建更大的qcow2文件

 4)查看重新创建qcow2文件

 5)让重新生成的qcow2文件替换原先的qcow2文件 

 6)再次查看qcow2文件分区

 7)再次拉取虚拟机

 8)ssh进入虚拟机进行测试

五、为虚拟机配置桥接网络(选做)


一、安装软件

[root@KVM ~]# yum -y install virt*

二、开启服务(保证qemu正常运行)

[root@KVM ~]# systemctl enable libvirtd --now

三、使用命令调出图形化工具

[root@KVM ~]# virt-manager 

本文不在详细说明图形化管理,侧重于命令行。利用现有qocw2文件拉取虚拟机

四、命令行管理

1.查看默认网桥

[root@KVM ~]# virsh net-list --all
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

2.删除默认网桥

[root@KVM ~]# virsh net-destroy default 
Network default destroyed

[root@KVM ~]# virsh net-undefine default 
Network default has been undefined

[root@KVM ~]# virsh net-list --all
 Name   State   Autostart   Persistent
----------------------------------------


3.通过nmcli创建虚拟bridge

[root@KVM ~]# nmcli con add con-name vm-1 ifname vm-1 type bridge ipv4.address 100.100.100.100/24 ipv4.method manual
Connection 'vm-1' (2c4531f3-7658-45a5-b320-acfab8898100) successfully added.


4.使用cloud镜像拉取虚拟机

1)下载

网址: CentOS Cloud images

[root@KVM ~]wget https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-StreamGenericCloud-8-20220125.1.x86_64.qcow2

2)创建目录存放qcow2文件

[root@KVM ~]# mkdir /usr/images
[root@KVM ~]# mv CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2 /usr/images/

#一般不会将qcow2文件放到/tmp下,因为默认情况下/tmp目录会周期性的自我清理,容易干掉qcow2文件, 所以你可以将qcow2文件放到别的目录,但是这个目录必须要对qemu有rwx权限才可以拉起虚拟机。

3)自定义qcow2文件

当我们使用qcow2文件拉取虚拟机时,不知道root用户密码,无法正常进入虚拟机,在进行批量部署时,我们不可能每次都进入系统破解root密码,所以通过修改现有的qocw2文件,设置密码,便于我们正常进入系统。

[root@KVM ~]# virt-customize -a /usr/images/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2 --root-password password:1 --selinux-relabel
[   0.0] Examining the guest ...
[  33.4] Setting a random seed
[  33.4] Setting the machine ID in /etc/machine-id
[  33.4] Setting passwords
[  35.5] SELinux relabelling
[  50.3] Finishing off

4)利用修改后的qcow2文件拉取虚拟机

[root@KVM ~]# virt-install --name centos-stream8-1 --memory 1024 --vcpus 1 --import --os-variant centos-stream8 --disk /usr/images/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2,bus=virtio --network bridge=vm-1,model=virtio --noautoconsole


WARNING  Requested memory 1024 MiB is less than the recommended 1536 MiB for OS centos-stream8

Starting install...
Domain creation completed.

5)查看虚拟机

[root@KVM ~]# virsh list --all 
 Id   Name               State
----------------------------------
 1    centos-stream8-1   running

6)进去虚拟机控制台

[root@KVM ~]# virsh console centos-stream8-1
Connected to domain 'centos-stream8-1'
Escape character is ^] (Ctrl + ])

CentOS Stream 8
Kernel 4.18.0-358.el8.x86_64 on an x86_64

Activate the web console with: systemctl enable --now cockpit.socket

localhost login: root
Password: 
[root@localhost ~]# 

 7)配置虚拟机的ip地址

nmcli con add con-name node1 ifname eth0 type ethernet ipv4.address 100.100.100.2/24 ipv4.method man
nmcli con up node1

此时通过ssh即可连接虚拟机

[root@KVM ~]# ssh 100.100.100.2
The authenticity of host '100.100.100.2 (100.100.100.2)' can't be established.
ECDSA key fingerprint is SHA256:xzzxEjrvLCi131Ou5M0N059a0SPXoJpTUX4Ai7aGgi8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '100.100.100.2' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Tue May 10 14:43:42 2022

 5.扩展虚拟机的根分区

1)干掉虚拟机

[root@KVM ~]# virsh destroy centos-stream8-1 
Domain 'centos-stream8-1' destroyed

[root@KVM ~]# virsh undefine centos-stream8-1 
Domain 'centos-stream8-1' has been undefined

2)使用virt-filesystems命令查看qcow2文件的分区情况

[root@KVM ~]# virt-filesystems --long -h --all -a /usr/images/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2
Name       Type        VFS  Label  MBR  Size  Parent
/dev/sda1  filesystem  xfs  -      -    7.8G  -
/dev/sda1  partition   -    -      83   7.8G  /dev/sda
/dev/sda   device      -    -      -    10G   -

 3)重新创建更大的qcow2文件

[root@KVM ~]# qemu-img create -f qcow2 /usr/images/os.qcow2 200G
Formatting '/usr/images/os.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=214748364800 lazy_refcounts=off refcount_bits=16

 4)查看重新创建qcow2文件

[root@KVM ~]# qemu-img info /usr/images/os.qcow2 
image: /usr/images/os.qcow2
file format: qcow2
virtual size: 200 GiB (214748364800 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
    compat: 1.1
    compression type: zlib
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
    extended l2: false

 5)让重新生成的qcow2文件替换原先的qcow2文件 

此步骤可以理解为夺舍,/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2 文件的灵魂去夺舍os.qcow2文件的灵魂。夺舍后新的os.qocw2文件使用新的肉体,老的灵魂。

[root@KVM ~]# virt-resize --expand /dev/sda1 /usr/images/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2  /usr/images/os.qcow2 
[   0.0] Examining /usr/images/CentOS-Stream-GenericCloud-8-20220125.1.x86_64.qcow2
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
**********

Summary of changes:

/dev/sda1: This partition will be resized from 7.8G to 200.0G.  The 
filesystem xfs on /dev/sda1 will be expanded using the ‘xfs_growfs’ 
method.

**********
[  18.8] Setting up initial partition table on /usr/images/os.qcow2
[  19.5] Copying /dev/sda1
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  68.3] Expanding /dev/sda1 using the ‘xfs_growfs’ method

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

 6)再次查看qcow2文件分区

[root@KVM ~]# virt-filesystems -lh --all -a /usr/images/os.qcow2 
Name       Type        VFS  Label  MBR  Size  Parent
/dev/sda1  filesystem  xfs  -      -    200G  -
/dev/sda1  partition   -    -      83   200G  /dev/sda
/dev/sda   device      -    -      -    200G  -

 7)再次拉取虚拟机

[root@KVM ~]# virt-install --name centos-stream8-1 --memory 1024 --vcpus 1 --import --os-variant centos-stream8 --disk /usr/images/os.qcow2,bus=virtio --network bridge=vm-1,model=virtio --noautoconsole
WARNING  Requested memory 1024 MiB is less than the recommended 1536 MiB for OS centos-stream8

Starting install...
Domain creation completed.

 8)ssh进入虚拟机进行测试

[root@KVM ~]# ssh 100.100.100.2
[email protected]'s password: 
Activate the web console with: systemctl enable --now cockpit.socket

Last failed login: Tue May 10 15:11:06 UTC 2022 from 100.100.100.100 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Tue May 10 15:10:40 2022 from 100.100.100.100
[root@localhost ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  368M     0  368M   0% /dev
tmpfs          tmpfs     403M     0  403M   0% /dev/shm
tmpfs          tmpfs     403M   11M  392M   3% /run
tmpfs          tmpfs     403M     0  403M   0% /sys/fs/cgroup
/dev/vda1      xfs       200G  2.8G  198G   2% /
tmpfs          tmpfs      81M     0   81M   0% /run/user/0

 可以发现虚拟机的根分区已经变为我们想要的大小了

五、为虚拟机配置桥接网络(选做)

当我们想要让虚拟机与internet访问,最简单的方法就是给虚拟机配置一个桥接网络

本文通过 network manager 来实现

[root@KVM ~]# nmcli connection modify manager slave-type bridge master vm-1 
[root@KVM ~]# nmcli connection up manager
[root@KVM ~]# bridge link
2: ens33:  mtu 1500 master vm-1 state forwarding priority 32 cost 100 
11: vnet1:  mtu 1500 master vm-1 state forwarding priority 32 cost 100 

此时我们只需要给vm-1配一个网络,并且给虚拟机配置一个与vm-1同一网段的ip,即可实现通信。

你可能感兴趣的:(笔记,centos,linux,运维)