虚拟化技术Ovirt@IT·互联网

Ovirt实现虚拟机通过NAT上网

2015-08-05  本文已影响1774人  strongz

环境说明

1. 安装Centos7-mini并update(省略)

2. 安装和配置ovirt

3. 创建虚拟机(通过NAT连接外网)

1.导入iso镜像文件

    [root@ovirthost01 ~]# engine-iso-uploader -i iso_stor upload  /data/iso/*.iso

    Please provide the REST API password for the admin@internal oVirt Engine user (CTRL+D to abort): 
    Uploading, please wait...
    INFO: Start uploading /data/iso/CentOS-6.5-x86_64-minimal.iso 
    INFO: /data/iso/CentOS-6.5-x86_64-minimal.iso uploaded successfully
    INFO: Start uploading /data/iso/CentOS-7.0-1406-x86_64-Minimal.iso 
    INFO: /data/iso/CentOS-7.0-1406-x86_64-Minimal.iso uploaded successfully
    INFO: Start uploading /data/iso/virtio-win-0.1.96.iso 
    INFO: /data/iso/virtio-win-0.1.96.iso uploaded successfully

2.创建NAT网络配置文件/etc/libvirt/qemu/networks/nat.xml,内容如下

    <network>
        <name>nat</name>
        <uuid>b09d09a8-ebbd-476d-9045-e66012c9e83d</uuid>
        <forward mode='nat'/>
        <bridge name='natbr0' stp='on' delay='0' />
        <mac address='52:54:00:9D:82:DE'/>
        <ip address='192.168.1.1' netmask='255.255.255.0'>
            <dhcp>
                <range start='192.168.1.2' end='192.168.1.250' />
            </dhcp>
        </ip>
    </network>

3.通过libvirt/virsh创建NAT网络

    [root@ovirthost01 ~]# cat /etc/pki/vdsm/keys/libvirt_password 
    shibboleth
    
    [root@ovirthost01 ~]# virsh

    Welcome to virsh, the virtualization interactive terminal.
    
    Type:  'help' for help with commands
           'quit' to quit
    
    virsh # connect qemu:///system
    Please enter your authentication name: vdsm@ovirt
    Please enter your password: shibboleth
    
    virsh # net-list
     Name                 State      Autostart     Persistent
    ----------------------------------------------------------
     ;vdsmdummy;          active     no            no
     vdsm-ovirtmgmt       active     yes           yes
    
    virsh # net-define /etc/libvirt/qemu/networks/nat.xml
    Network nat defined from /etc/libvirt/qemu/networks/nat.xml
    
    virsh # net-autostart nat
    Network nat marked as autostarted
    
    virsh # net-start nat
    Network nat started
    
    virsh # net-list --all
     Name                 State      Autostart     Persistent
    ----------------------------------------------------------
     ;vdsmdummy;          active     no            no
     nat                  active     yes           yes
     vdsm-ovirtmgmt       active     yes           yes

    以上操作将创建nat功能的网桥,如下
    [root@ovirthost01 ~]# brctl show
    bridge name     bridge id               STP enabled     interfaces
    ;vdsmdummy;             8000.000000000000       no
    natbr0          8000.5254009d82de       yes             natbr0-nic
    ovirtmgmt               8000.b083fea27fed       no              p4p1

4.安装vdsm-hook-extnet

    [root@ovirthost01 ~]# yum install -y vdsm-hook-extnet

注:此处将下载extnet的hooks文件并存放到以下两目录

    [root@ovirthost01 ~]# ll /usr/libexec/vdsm/hooks/before_device_create
    total 4
    -rwxr-xr-x. 1 root root 1925 Jun  5 01:47 50_extnet
    [root@ovirthost01 ~]# ll /usr/libexec/vdsm/hooks/before_nic_hotplug
    total 4
    -rwxr-xr-x. 1 root root 1925 Jun  5 01:47 50_extnet

5.添加自定义设备属性extnet

    [root@ovirthost01 ~]# engine-config -s CustomDeviceProperties='{type=interface;prop={extnet=^[a-zA-Z0-9_ ---]+$}}'
    Please select a version:
    1. 3.0
    2. 3.1
    3. 3.2
    4. 3.3
    5. 3.4
    6. 3.5
    6

    [root@ovirthost01 ~]# engine-config -g CustomDeviceProperties
    CustomDeviceProperties:  version: 3.0
    CustomDeviceProperties:  version: 3.1
    CustomDeviceProperties:  version: 3.2
    CustomDeviceProperties:  version: 3.3
    CustomDeviceProperties: {type=interface;prop={SecurityGroups=^(?:(?:[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}, *)*[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}|)$}} version: 3.4
    CustomDeviceProperties: {type=interface;prop={extnet=^[a-zA-Z0-9_ ---]+$}} version: 3.5

    [root@ovirthost01 ~]# systemctl restart ovirt-engine

6.通过webadmin-portal创建虚拟机,并通过ISO安装GuestOS(省略)

7.添加nat端口配置集

图1-ovirt-nat0.JPG

8.添加vnic到虚拟机,并关联nat端口配置集

ovirt-nat1.JPG

9.进入到虚拟机验证(成功)

检查网卡是否添加

    [root@VM01-CentOS6 ~]# ifconfig -a
图3-ovirt-nat2.JPG

从vdsm主机的dhcp服务器处获取IP

    [root@VM01-CentOS6 ~]# dhclient eth0
图4-ovirt-nat3.JPG

通过ping外网来检查NAT是否成功

    [root@VM01-CentOS6 ~]# ping www.ovirt.org
图5-ovirt-nat4.JPG

10.参考资料

http://www.ovirt.org/VDSM-Hooks/network-nat
http://blog.lofyer.org/add-nat-ovirt-vdsm-hooks/
http://users.ovirt.narkive.com/WVp1moNk/ovirt-users-ovirt-3-5-nat
https://access.redhat.com/documentation/zh-CN/Red_Hat_Enterprise_Virtualization/3.5/html-single/Installation_Guide/index.html

上一篇 下一篇

猜你喜欢

热点阅读