设为首页 收藏本站
查看: 1858|回复: 0

[经验分享] 1. kvm 安装

[复制链接]
发表于 2017-6-25 11:45:31 | 显示全部楼层 |阅读模式
2.  安装kvm前的准备工作
a. 清除iptables规则
service iptables stop; service iptables save
b. 关闭selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0


3. 开始安装kvm
a. 检查你的系统是否支持虚拟化
grep -Ei 'vmx|svm' /proc/cpuinfo
如果有输出内容,则支持,其中intel cpu支持会有vmx,amd cpu支持会有svm

b. 通过yum安装虚拟化的软件包
yum install -y kvm virt-*  libvirt  bridge-utils qemu-img
说明:
kvm:软件包中含有KVM内核模块,它在默认linux内核中提供kvm管理程序
libvirts:安装虚拟机管理工具,使用virsh等命令来管理和控制虚拟机。
bridge-utils:设置网络网卡桥接。
virt-*:创建、克隆虚拟机命令,以及图形化管理工具virt-manager
qemu-img:安装qemu组件,使用qemu命令来创建磁盘等。




c. 检查kvm模块是否加载
lsmod |grep kvm
正常应该是:
kvm_intel              55496  3
kvm                   337772  1 kvm_intel

如果没有,需要执行
modprobe kvm-intel
还没有就重启一下试试

d. 配置网卡
cd /etc/sysconfig/network-scripts/
cp ifcfg-eth0 ifcfg-br0
分别编辑eth0和br0
ifcfg-eth0改成如下:
DEVICE=eth0
HWADDR=00:0C:29:55:A7:0A
TYPE=Ethernet
UUID=2be47d79-2a68-4b65-a9ce-6a2df93759c6
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
BRIDGE=br0


ifcfg-br0改成如下:
DEVICE=br0
#HWADDR=00:0C:29:55:A7:0A
TYPE=Bridge
#UUID=2be47d79-2a68-4b65-a9ce-6a2df93759c6
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.1.124
NETMASK=255.255.255.0
GATEWAY=192.168.11.1
DNS1=202.106.0.20

说明: 我的虚拟机是桥接模式,所以设置br0的ip和我的真机同样的网段,包括网关也是我路由器的ip,
可以根据自己的环境去配置,目的是为了让虚拟机可以上网。

/etc/init.d/network restart
查看网卡如下:
br0       Link encap:Ethernet  HWaddr 00:0C:29:55:A7:0A
          inet addr:192.168.1.124  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe55:a70a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:141326 errors:0 dropped:0 overruns:0 frame:0
          TX packets:90931 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:456024940 (434.8 MiB)  TX bytes:10933593 (10.4 MiB)

eth0      Link encap:Ethernet  HWaddr 00:0C:29:55:A7:0A
          inet6 addr: fe80::20c:29ff:fe55:a70a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:341978 errors:0 dropped:0 overruns:0 frame:0
          TX packets:90946 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:468848861 (447.1 MiB)  TX bytes:10934699 (10.4 MiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:14:EF:D5
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)



e. 启动或重启libvirtd服务和messagebus 服务
/etc/init.d/libvirtd start
/etc/init.d/messagebus restart

此时可以查看网络接口列表
brctl show 结果如下:
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c2955a70a       no              eth0
virbr0          8000.52540014efd5       yes             virbr0-nic
把刚才添加的硬盘分区格式话
fdisk -l
fdisk /dev/sdb
mkfs.ext4 /dev/sdb1
mount /dev/sdb1 /data/


4. 创建虚拟机
mkdir /data/kvm   //创建一个存储虚拟机虚拟磁盘的目录,该目录所在分区必须足够大

virt-install \
--name  aming1 \
--ram 512 \
--disk path=/data/aming1.img,size=30 \
--vcpus 1 \
--os-type linux \
--os-variant rhel6 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirrors.163.com/centos/6.7/os/i386/' \
--extra-args 'console=ttyS0,115200n8 serial'


virt-install \
--name  aming1 \
--ram 512 \
--disk path=/data/aming1.img,size=30 \
--vcpus 1 \
--os-type linux \
--os-variant rhel6 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirrors.163.com/centos/6.7/os/x86_64/' \
--extra-args 'console=ttyS0,115200n8 serial'
安装
virt-install --name  aming2 --ram 512 --disk path=/data/kvm/aming2.img,format=qcow2,size=10,bus=virtio
--vcpus 1 --os-type linux --os-variant rhel6 --network bridge=br0 --graphics none --console pty,target_type=serial
--location '/mnt/' --extra-args 'console=ttyS0,115200n8 serial'


192.168.1.125给虚拟机设置的ip

192.168.1.1 网关
────────────────┤ Manual TCP/IP Configuration ├─────────────────┐
       │                                                                │
       │ Enter the IPv4 and/or the IPv6 address and prefix (address /   │
       │ prefix).  For IPv4, the dotted-quad netmask or the CIDR-style  │
       │ prefix are acceptable. The gateway and name server fields must │
       │ be valid IPv4 or IPv6 addresses.                               │
       │                                                                │
       │ IPv4 address: 192.168.1.125___ / 255.255.255.0___              │
       │ Gateway:      192.168.1.1______________________________        │
       │ Name Server:  8.8.8.8__________________________________        │
       │                                                                │
       │             ┌────┐                        ┌──────┐             │
       │             │ OK │                        │ Back │             │
       │             └────┘                        └──────┘             │
       │                                                                │
       │                                                                │
       └──────────────────────────────────────────────────────
进行下载
下载完后选择 re-initialize all




│                                     │
                    │  Asia/Riyadh                     ↑  │
                    │  Asia/Sakhalin                   ?  │
                    │  Asia/Samarkand                  ?  │
                    │  Asia/Seoul                      ?  │
                    │  Asia/Shanghai                   ↓  │
                    │                                     │
                    │      ┌────┐          ┌──────┐       │
                    │      │ OK │          │ Back │       │
                    │      └────┘          └──────┘       │
                    │


选择上海
输入要给系统设置的密码




    Pick a root password. You must type it    │
                │    twice to ensure you know it and do not    │
                │    make a typing mistake.                    │
                │                                              │
                │ Password:           ******__________________ │
                │ Password (confirm): ******__________________ │
                │                                              │
                │        ┌────┐               ┌──────┐         │
                │        │ OK │               │ Back │         │
                │        └────┘               └──────┘

选择Use Anyway



───────────────────┤ Weak Password ├────────────────────┐
          │                                                         │
          │ You have provided a weak password: it does not contain  │
          │ enough DIFFERENT characters                             │
          │                                                         │
          │        ┌────────┐                ┌────────────┐         │
          │        │ Cancel │                │ Use Anyway │         │
          │        └────────┘                └────────────┘         │
          │


直接ok


                                                         │
       │                 Use entire drive                              │
       │                 Replace existing Linux system                 │
       │                 Use free space                                │
       │                                                               │
       │   Which drive(s) do you want to use for this installation?    │
       │        
  •     vda    30720 MB (Virtio Block Device) ↑         │
           │                                                     ?         │
           │                                                               │
           │                      ┌────┐   ┌──────┐                        │
           │                      │ OK │   │ Back │                        │
           │                      └────┘   └──────┘




    选择 Write changes to disk

         ┌─────────────┤ Writing storage configuration to disk ├──────────────┐
         │                                                                    │
         │ The partitioning options you have selected will now be written to  │
         │ disk.  Any data on deleted or reformatted partitions will be lost. │
         │                                                                    │
         │        ┌─────────┐               ┌───────────────────────┐         │
         │        │ Go back │               │ Write changes to disk │         │
         │        └─────────┘               └───────────────────────┘         │
         │





    用光盘安装

    挂在光盘
    mount /dev/cdrom /mnt
    [iyunv@localhost ~]# qemu-img create -f qcow2 -o preallocation=metadata  /data/kvm/aming2.qcow2 10G

    [iyunv@localhost ~]# ls /data/kvm
    aming1.img  aming2.qcow2

    [iyunv@localhost ~]# virt-install --name  aming2 --ram 512 --disk path=/data/kvm/aming2.qcow2,format=qcow2,size=10,
    bus=virtio  --vcpus 1 --os-type linux --os-variant rhel6 --network bridge=br0 --graphics none --console pty,
    target_type=serial --location '/mnt/' --extra-args 'console=ttyS0,115200n8 serial'



    选择English

    ┌────────┤ Choose a Language ├────────┐
                        │                                     │
                        │ What language would you like to use │
                        │ during the installation process?    │
                        │                                     │
                        │      Catalan                ↑       │
                        │      Chinese(Simplified)    ?       │
                        │      Chinese(Traditional)   ?       │
                        │      Croatian               ?       │
                        │      Czech                  ?       │
                        │      Danish                 ?       │
                        │      Dutch                  ?       │
                        │      English                ↓       │
                        │                                     │
                        │               ┌────┐                │
                        │               │ OK │                │
                        │               └────┘                │
                        │                                     │
                        │

    选择NFS directory


                    ┌───┤ Installation Method ├───┐
                            │                             │
                            │ What type of media contains     │
                            │ the installation image?             │
                            │                                             │
                            │        Local CD/DVD                  │
                            │        Hard drive                      │
                            │        NFS directory                  │
                            │        URL                               │
                            │                                             │
                            │   ┌────┐       ┌──────┐     │
                            │   │ OK    │       │ Back     │     │
                            │   └────┘       └──────┘     │
                            │                                             │
                            │                                             │


    选择Manual configuration


                     ┌────────────┤ Configure TCP/IP ├────────────┐
                     │                                            │
                     │
  • Enable IPv4 support                    │
                     │        ( ) Dynamic IP configuration (DHCP) │
                     │        (*) Manual configuration            │
                     │                                            │
                     │
  • Enable IPv6 support                    │
                     │        (*) Automatic                       │
                     │        ( ) Automatic, DHCP only            │
                     │        ( ) Manual configuration            │
                     │                                            │
                     │        ┌────┐              ┌──────┐        │
                     │        │ OK │              │ Back │        │
                     │        └────┘              └──────┘        │
                     │                                            │
                     │                                            │
                     └────────────────────────────────────────────┘


    192.168.1.116 是要给创建的虚拟机配置ip

    192.168.1.1 (网关)

                                                                   │
           │ Enter the IPv4 and/or the IPv6 address and prefix (address /   │
           │ prefix).  For IPv4, the dotted-quad netmask or the CIDR-style  │
           │ prefix are acceptable. The gateway and name server fields must │
           │ be valid IPv4 or IPv6 addresses.                               │
           │                                                                │
           │ IPv4 address: 192.168.1.116___ / 255.255.255.0___              │
           │ Gateway:      192.168.1.1______________________________        │
           │ Name Server:  8.8.8.8__________________________________        │
           │                                                                │
           │             ┌────┐                        ┌──────┐             │
           │             │ OK │                        │ Back │             │
           │             └────┘                        └──────┘             │
           │

    接下来配置nfs
    yum install -y nfs-utile rpcbind

    vim /etc/exports

    /mnt 192.168.1.0/24

    /etc/init.d/rpcbind start
    /etc/init.d/nfs start

    showmount -e 192.168.1.124

    Export list for 192.168.1.124:
    /mnt 192.168.1.0/24

    192.168.1.124 在那台机器上配置的nfs就写那个ip

    ┌───────────────────────────┤ NFS Setup ├─────────────────────┐
         │                                                                    │
         │ Please enter the server and NFSv3 path to your CentOS installation │
         │ image and optionally additional NFS mount options.                 │
         │                                                                    │
         │       NFS server name:              192.168.1.124_____________       │
         │       CentOS directory:             /mnt/images/install.img_       │
         │       NFS mount options (optional): ro______________________       │
         │                                                                    │
         │             ┌────┐                           ┌──────┐              │
         │             │ OK │                           │ Back │              │
         │             └────┘                           └──────┘              │
         │                                                                    │
         │                                                                    │
         └──────────────────────────────────────────────────────



                                                                             │
    │         Error processing drive:                                 ↑          │
    │                                                                 ?          │
    │         pci-0000:00:05.0-virtio-pci-virtio1                     ?          │
    │         10240MB                                                 ?          │
    │         Virtio Block Device                                     ?          │
    │                                                                 ?          │
    │         This device may need to be reinitialized.               ?          │
    │                                                                 ?          │
    │         REINITIALIZING WILL CAUSE ALL DATA TO BE LOST!          ?          │
    │                                                                 ?          │
    │         This action may also be applied to all other disks      ?          │
    │         needing reinitialization.                               ↓          │
    │                                                                            │
    │  ┌────────┐   ┌────────────┐   ┌───────────────┐   ┌──────┐   │
    │  │ Ignore │   │ Ignore all │   │ Re-initialize │   │ Re-initialize all │   │
    │  └────────┘   └────────────┘   └───────────────┘   └─────┘   │
    │                                                                            │



    --name  指定虚拟机的名字
    --ram 指定内存分配多少
    --disk path 指定虚拟磁盘放到哪里,size=30 指定磁盘大小为30G,这样磁盘文件格式为raw,raw格式不能做快照,
    后面有说明,需要转换为qcow2格式,如果要使用qcow2格式的虚拟磁盘,需要事先创建qcow2格式的虚拟磁盘。
    参考  http://www.361way.com/kvm-qcow2-preallocation-metadata/3354.html
    示例:qemu-img create -f qcow2 -o preallocation=metadata  /data/kvmaming2.qcow2 10G
    --disk path=/data/test02.img,format=qcow2,size=7,bus=virtio

    --vcpus 指定分配cpu几个
    --os-type 指定系统类型为linux
    --os-variant 指定系统版本
    --network  指定网络类型
    --graphics 指定安装通过哪种类型,可以是vnc,也可以没有图形,在这里我们没有使用图形直接使用文本方式
    --console 指定控制台类型
    --location 指定安装介质地址,可以是网络地址,也可以是本地的一个绝对路径,如果是绝对路径,
    那么后面还需要指定一个安装介质,比如NFS,假如虚拟机设置ip后,不能连外网,那么就会提示让我们选择安装途径:

    然后就是我们非常熟悉的OK or  Next 了 ,只不过这个过程是文本模式,如果想使用图形,只能开启vnc啦。

    最后安装完,reboot就进入刚刚创建的虚拟机了。要想退回到宿主机,ctrl  ] 即可。
    virsh list 可以列出当前的子机列表。
    virsh console centos6.6_1  可以进入指定的子机

  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-387933-1-1.html 上篇帖子: KVM分析报告 下篇帖子: virsh KVM管理工具
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表