====本文旨在记录实验过程,原理相关请参见其他相关资料====

实验目的:模拟在物理机中安装kvm虚拟机并搭建cobbler服务器, 实现自动化(个性化)安装。

                真实环境,可以在IDC机房中将物理机连接到同一二层交换机接可以,在一台物理机中安装kvm虚拟机(搭建cobbler服务器)

环境介绍:localhost模拟物理机使用的NAT模式,在localhost中创建cobbler_server 虚拟机;

        cobbler_server 桥接到 localhost创建的虚拟网桥 br0上;在其中搭建cobbler tftp dhcp rsync服务;

        cobbler_client 自动化安装后的主机,使用的与localhost 相同的NAT模式。以便与开机后可以获取IP。


实验准备工作

#取消vmw的NAT模式下的DHCP功能,防止安装cobbler_server 中dhcp功能受影响。

kvm虚拟机如何联网 kvm虚拟机安装教程_kvm虚拟机如何联网


#配置vnet8 为静态IP

kvm虚拟机如何联网 kvm虚拟机安装教程_数据结构与算法_02


[root@localhost ~]# uname -a 
Linux node5 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux


#################################################################################

一、centos7.5安装kvm虚拟机

#下载阿里云epel源文件
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo       http://mirrors.aliyun.com/repo/epel-7.repo
#临时关闭防火墙和selinux
[root@localhost ~]# iptables -F && setenforce 
#使用KVM(基于内核的虚拟机)+ QEMU的虚拟化。 需要具有Intel VT或AMD-V功能的CPU。要有vmx|svm才支持虚拟化
#本机已开启vmw的cpu虚拟化功能
[root@localhost ~]# egrep '(vmx|svm)'  /proc/cpuinfo   
#yum安装KVM模块以及 libvirt   ##qemu-kvm\libvirt 在updates 源中
[root@localhost ~]# yum install qemu-kvm libvirt virt-install  bridge-utils                 virt-manager (图形管理工具可选)

kvm虚拟机如何联网 kvm虚拟机安装教程_操作系统_03

#安装完毕检测是否加载kvm相关模块

[root@node5 ~]# lsmod | grep kvm
kvm_intel             174841  0 
kvm                   578518  1 kvm_intel
irqbypass              13503  1 kvm
#创建虚拟网桥,修改配置文件 
修改前
[root@localhost /etc/sysconfig/network-scripts]# cat ifcfg-ens33   ;   cp ifcfg-ens33   ifcfg-br0
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="9e87410a-63b9-43a2-b6e6-f20031261ea9"
DEVICE="ens33"
IPV6_PRIVACY="no"
ONBOOT="yes
IPADDR="192.168.188.15"
PREFIX="24"
GATEWAY="192.168.188.2"
DNS1="192.168.188.2"

修改后
[root@localhost /etc/sysconfig/network-scripts]# cat ifcfg-ens33 
DEVICE="ens33"
TYPE="Ethernet"
BOOTPROTO="none"
ONBOOT="yes"
BRIDGE=br0
NM_CONTROLLED=no       ###建议配置
[root@localhost /etc/sysconfig/network-scripts]# cat ifcfg-br0
DEVICE="br0"
TYPE="Bridge"
BOOTPROTO="none"
ONBOOT="yes"
NM_CONTROLLED=no      ###建议配置
IPADDR="192.168.188.15"
PREFIX="24"
GATEWAY="192.168.188.2"
DNS1="192.168.188.2"

#关闭NetworkManager服务
[root@localhost /etc/sysconfig/network-scripts]# systemctl stop  NetworkManager
[root@localhost /etc/sysconfig/network-scripts]# systemctl disable  NetworkManager
#重启网络服务,可以看到IP已经配置到br0上
[root@localhost /etc/sysconfig/network-scripts]# systemctl restart network

kvm虚拟机如何联网 kvm虚拟机安装教程_操作系统_04

#开启libvirtd 服务,可以看到默认virbr0 网桥

[root@localhost ~]# systemctl start  libvirtd 
[root@localhost ~]# systemctl enable libvirtd   #使开机自动启动
[root@localhost ~]# systemctl status   libvirtd  #查看服务状态

#接下来创建用于存放ISO文件,以及磁盘映像文件的目录;
[root@localhost ~]# mkdir  -v  /home/{iso,img}  
#上传ISO文件至目录
[root@localhost /home/iso]# ll  -h
total 906M
-rw-r--r--. 1 root root 906M Aug 29 23:58 CentOS-7-x86_64-Minimal-1804.iso

#创建磁盘映像文件,格式指定为qcow2格式 (稀疏),不同的映像文件应用场景不同,涉及kvm优化 本人不太懂,就不误导了
[root@localhost /home/img]# qemu-img create -f qcow2  centos75.qcow2 40G
[root@localhost /home/img]# qemu-img info centos75.qcow2 
image: centos75.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false


#指定虚拟机的主机名,cpu,内存,使用的磁盘文件,启动方式,以及vnc连接的地址、端口、密码 ,桥接网络,开机自启

[root@localhost ~]# virt-install  --name centos75 --memory 512 --vcpu=4  -f /home/img/centos75.qcow2 --cdrom /home/iso/CentOS-7-x86_64-Minimal-1804.iso --graphics vnc,listen=0.0.0.0,port=5910,password='test.com@123',keymap='en-us'  --network bridge=br0  --force --autostart

#使用客户端vncview 连接libvirt服务端的地址和端口  进如kvm虚拟机安装界面

kvm虚拟机如何联网 kvm虚拟机安装教程_centos_05


kvm虚拟机如何联网 kvm虚拟机安装教程_centos_06


#修改虚拟机网卡配置文件配置跟localhost 同网段IP,测试网络的连通性。

kvm虚拟机如何联网 kvm虚拟机安装教程_centos_07


#使用virsh 查看管理虚拟机,更多高级用法请自行查阅或请man

[root@localhost ~]# virsh list  --all
 Id    Name                           State
----------------------------------------------------
 2     centos75                       running
#开机 virsh start centos75   
#关机 virsh shutdown centos75
#断电 virsh destroy centos75

#虚拟机配置文件/etc/libvirt/qemu/*.xml  

  (*)可以根据此文件为模板,编写循环脚本更改部分内容,,也可对virt-install 进行循环  都可以批量生成kvm虚拟机。

kvm虚拟机如何联网 kvm虚拟机安装教程_centos_08


[root@localhost /etc/libvirt/qemu]# yum install qemu-kvm-tools 
[root@localhost /etc/libvirt/qemu]# rpm -ql qemu-kvm-tools
/usr/bin/kvm_stat
[root@localhost /etc/libvirt/qemu]# kvm_stat          (虽然现在看不懂输出结果但是记录一下 )
kvm statistics

 kvm_fpu                                       2596     108
 kvm_exit                                      2068 90
 kvm_entry                                     2068 90
 kvm_pio                                        910 38
 kvm_userspace_exit                             910 38
 kvm_emulate_insn                               798 33
 kvm_msr                                        649 29
 kvm_hv_timer_state                             618 27
 kvm_inj_virq                                   470 20
 kvm_apic_accept_irq                            470 20
 kvm_eoi                                        470 20
 kvm_pv_eoi                                     465 20
 kvm_wait_lapic_expire                          403 17
 kvm_vcpu_wakeup                                388 16
 kvm_ioapic_set_irq                             126  6
 kvm_set_irq                                    126  6
 kvm_pic_set_irq                                126  6
 kvm_halt_poll_ns                                74  3
 kvm_apic                                        57  3
 kvm_apic_ipi                                    26  1
 kvm_msi_set_irq                                  7  0
 kvm_exit(MSR_WRITE)                              3  0
 kvm_exit(HLT)                                    2       0

#实现无密码登陆cobbler_server 

kvm虚拟机如何联网 kvm虚拟机安装教程_数据结构与算法_09

#免密登陆成功

kvm虚拟机如何联网 kvm虚拟机安装教程_数据结构与算法_10

##################################################################

二、安装配置 cobbler服务(cobbler 可以看成PXE的进一步封装)                    详细更多内容移步cobbler官网

会pxe  cobbler肯定没有问题,cobbler可以管理 dhcp ,rsync ,tftp  以及添加其他额外的yum 源,实现精细化安装

kvm虚拟机如何联网 kvm虚拟机安装教程_开发工具_11


#修改主机名,安装wget ,下载阿里云epel源文件( 以便安装cobbler)

[root@cobbler_server ~]# yum -y install wget ; wget -O /etc/yum.repos.d/epel.repo       http://mirrors.aliyun.com/repo/epel-7.repo 
#安装cobbler服务涉及的组件
[root@cobbler_server ~]# yum install  dhcp  tftp-server   syslinux   cobbler  cobbler-web  mod_wsgi  httpd  pykickstart rsync 
#

kvm虚拟机如何联网 kvm虚拟机安装教程_数据结构与算法_12



#临时关闭防火墙和 selinux ,启动cobbler httpd

kvm虚拟机如何联网 kvm虚拟机安装教程_centos_13

#vim /etc/cobbler/setttings

kvm虚拟机如何联网 kvm虚拟机安装教程_数据结构与算法_14


kvm虚拟机如何联网 kvm虚拟机安装教程_centos_15


kvm虚拟机如何联网 kvm虚拟机安装教程_centos_16

#systemctl  start  tftp       (UDP  69)

#cobbler get-loaders
#systemctl  start  rsyncd  (TCP 873)

#cobbler get-loaders
#yum install debmirror (根据提示修改注释配置文件部分内容)
#vim /etc/selinux/config  修改策略为disabled
#同步一下,切记cobbler sync   ##然后chbbler check 一下,所有修改完最好重启一下设备

#一切就绪可以开干了
[root@cobbler_server ~]# cobbler check 
No configuration problems found.  All systems go.

#访问web界面

kvm虚拟机如何联网 kvm虚拟机安装教程_开发工具_17


kvm虚拟机如何联网 kvm虚拟机安装教程_centos_18

至此cobbler,ryncd,tftp,httpd 服务安装完成,差一个DHCP服务

vim /etc/cobbler/setttings

kvm虚拟机如何联网 kvm虚拟机安装教程_数据结构与算法_19

修改dhcp模板文件配置文件  


[root@cobbler_server ~]# vim /etc/cobbler/dhcp.template
subnet 192.168.188.0 netmask 255.255.255.0 {
     option routers             192.168.188.2;
     option domain-name-servers 192.168.188.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.188.50 192.168.188.60;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
     filename                   "pxelinux.0";
}
#systemctl restart cobblerd && cobbler sync
#systemctl start dhcpd
#systemctl enable dhcpd

#重启cobbler服务同步一下配置, 查看dhcp 服务状态(UDP   67)

  测试vmw 桥接模式下主机能不能获取IP ,,dhcp也OK了

kvm虚拟机如何联网 kvm虚拟机安装教程_开发工具_20


#挂载镜像文件(坑! 不知道怎么回事,直接指定iso文件位置,不挂载会有报错)

[root@cobbler_server ~]# mount -o loop -t iso9660 /iso/CentOS-7-x86_64-Minimal-1804.iso   /mnt
mount: /dev/loop0 is write-protected, mounting read-only
#导入ISO 文件( !path指定的是iso文件的挂载目录)
[root@cobbler_server ~]# cobbler import  --path=/mnt   --name=centos75  --arch=x86_64
task started: 2018-09-01_213323_import
task started (id=Media import, time=Sat Sep  1 21:33:23 2018)
Found a candidate signature: breed=redhat, version=rhel6
Found a candidate signature: breed=redhat, version=rhel7
Found a matching signature: breed=redhat, version=rhel7
Adding distros from path /var/www/cobbler/ks_mirror/centos75-x86_64:
creating new distro: centos75-x86_64
trying symlink: /var/www/cobbler/ks_mirror/centos75-x86_64 -> /var/www/cobbler/links/centos75-x86_64
creating new profile: centos75-x86_64
associating repos
checking for rsync repo(s)
checking for rhn repo(s)
checking for yum repo(s)
starting descent into /var/www/cobbler/ks_mirror/centos75-x86_64 for centos75-x86_64
processing repo at : /var/www/cobbler/ks_mirror/centos75-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/centos75-x86_64
looking for /var/www/cobbler/ks_mirror/centos75-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/centos75-x86_64/repodata
*** TASK COMPLETE ***

#导入成功发有相关发行版以及profiles生成,还会生成一份默认的ks文件(命令查看与web界面一致)

kvm虚拟机如何联网 kvm虚拟机安装教程_开发工具_21

kvm虚拟机如何联网 kvm虚拟机安装教程_开发工具_22


#如上tftp目录多了 发行版的内核文件以及 initrd.img 。

kvm虚拟机如何联网 kvm虚拟机安装教程_kvm虚拟机如何联网_23

#启动菜单与下图相符,可以自定义图片以及相关提示信息。

kvm虚拟机如何联网 kvm虚拟机安装教程_操作系统_24

kvm虚拟机如何联网 kvm虚拟机安装教程_数据结构与算法_25


kvm虚拟机如何联网 kvm虚拟机安装教程_centos_26

实验成功!!!

################################################################

三、 简单介绍cobbler   web界面的简单操作

该博客详细介绍了web界面的操作,我的师傅牛x 老大了,望成莫及                  #主要关注 网络,运维,云计算方向openstack

https://blog.51cto.com/zhanguo1110/1671137       


我主要看的是绑定mac地址,指定额外的yum源,实现定制安装,     配置IDAC 地址\用户名密码\以便通过cobbler 实现开关机;

老司机的世界我看不懂,他主要用于搭建openstack 的基础环境。 (snippets  、ks文件中可以实现安后脚本) 



[root@localhost qemu]# cat  /etc/libvirt/qemu/centos72.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh edit centos72
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>centos72</name>
  <uuid>4b781760-8b8a-4084-8341-bcd324d93a2b</uuid>
  <memory unit='KiB'>524288</memory>
  <currentMemory unit='KiB'>524288</currentMemory>
  <vcpu placement='static'>4</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
  </features>
  <cpu mode='custom' match='exact' check='partial'>
    <model fallback='allow'>Broadwell-noTSX</model>
  </cpu>
  <clock offset='utc'>
    <timer name='rtc' tickpolicy='catchup'/>
    <timer name='pit' tickpolicy='delay'/>
    <timer name='hpet' present='no'/>
  </clock>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/libexec/qemu-kvm</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/home/img/centos72.qcow2'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'/>
    <controller type='ide' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:15:59:3e'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <input type='tablet' bus='usb'>
      <address type='usb' bus='0' port='1'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='5910' autoport='no' listen='0.0.0.0' keymap='en-us' passwd='test.com@123'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
    </memballoon>
  </devices>
</domain>


转载于:https://blog.51cto.com/mengyao/2168947