Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

12.5. 基于 iSCSI 的存储池

本节介绍使用基于 iSCSI 的设备来存储客户机虚拟机。
iSCSI(互联网小型计算机系统接口)是用于共享存储设备的网络协议。iSCSI 通过 IP 层使用 SCSI 指令连接到目标(存储服务器)。

12.5.1. 配置软件 iSCSI 目标

scsi-target-utils 软件包提供了用于创建软件支持的 iSCSI 目标的工具。

过程 12.4. 创建 iSCSI 目标

  1. 安装所需软件包

    安装 scsi-target-utils 软件包及所有依赖项
    # yum install scsi-target-utils
  2. 启动 tgtd 服务

    tgtd 服务托管物理机器 SCSI 目标,并使用 iSCSI 协议托管物理机器目标。启动 tgtd 服务,并使用 chkconfig 命令重新启动后使服务持久。
    # service tgtd start
    # chkconfig tgtd on
  3. 可选:创建 LVM 卷

    LVM 卷对于 iSCSI 后备镜像很有用。对于 guest 虚拟机,LVM 快照和大小大小会很有用。这个示例在 RAID5 阵列中创建一个名为 virtimage1 的 LVM 镜像,用于托管 iSCSI 的客户机虚拟机。
    1. 创建 RAID 阵列

      Red Hat Enterprise Linux 部署指南 介绍了如何创建软件 RAID5 阵列。
    2. 创建 LVM 卷组

      使用 vgcreate 命令创建名为 virtstore 的卷组。
      # vgcreate virtstore /dev/md1
    3. 创建 LVM 逻辑卷

      使用 lvcreate 命令,在 virtstore 卷组中创建名为 virtimage1 的逻辑卷组,大小为 20GB。
      # lvcreate --size 20G -n virtimage1 virtstore
      新逻辑卷 virtimage1 已准备好用于 iSCSI。
  4. 可选:创建基于文件的镜像

    基于文件的存储足以进行测试,但不建议用于生产环境或任何重要的 I/O 活动。此可选步骤为 iSCSI 目标创建名为 virtimage2.img 的文件。
    1. 为镜像创建新目录

      创建新目录来存储镜像。目录必须具有正确的 SELinux 上下文。
      # mkdir -p /var/lib/tgtd/virtualization
    2. 创建镜像文件

      创建名为 virtimage2.img、大小为 10GB 的镜像。
      # dd if=/dev/zero of=/var/lib/tgtd/virtualization/virtimage2.img bs=1M seek=10000 count=0
    3. 配置 SELinux 文件上下文

      为新镜像和目录配置正确的 SELinux 上下文。
      # restorecon -R /var/lib/tgtd
      基于文件的新映像 virtimage2.img 已准备好用于 iSCSI。
  5. 创建目标

    通过在 /etc/tgt/targets.conf 文件中添加 XML 条目,即可创建目标。target 属性需要 iSCSI 限定名称(IQN)。IQN 采用以下格式:
    iqn.yyyy-mm.reversed domain name:optional identifier text
    其中:
    • YYYY-mm 表示设备已启动的年和月(例如 :2010-05);
    • reversed 域名 是反向主机物理机器域名(例如 IQN 中的 server1.example.com )是 com.example.server1;以及
    • 可选标识符文本 是任何文本字符串,没有空格,可协助管理员识别设备或硬件。
    本例为在 server1.example.com 上可选步骤创建的两类镜像创建 iSCSI 目标(可选标识符 test )。将以下内容添加到 /etc/tgt/targets.conf 文件中。
    <target iqn.2010-05.com.example.server1:iscsirhel6guest>
       backing-store /dev/virtstore/virtimage1  #LUN 1
       backing-store /var/lib/tgtd/virtualization/virtimage2.img  #LUN 2
       write-cache off
    </target>
    
    确保 /etc/tgt/targets.conf 文件包含 default-driver iscsi 行,以将驱动程序类型设置为 iSCSI。驱动程序默认使用 iSCSI。
    重要
    这个示例创建了一个全局可访问的目标,且无访问权限控制。有关实现安全访问的信息,请参阅 scsi-target-utils。
  6. 重启 tgtd 服务

    重启 tgtd 服务以重新载入配置更改。
    # service tgtd restart
  7. iptables 配置

    通过 iptables 打开端口 3260,以进行 iSCSI 访问。
    # iptables -I INPUT -p tcp -m tcp --dport 3260 -j ACCEPT
    # service iptables save
    # service iptables restart
  8. 验证新目标

    查看新目标,以确保设置成功,使用 tgt-admin --show 命令。
    # tgt-admin --show
    Target 1: iqn.2010-05.com.example.server1:iscsirhel6guest
    System information:
    Driver: iscsi
    State: ready
    I_T nexus information:
    LUN information:
    LUN: 0
        Type: controller
        SCSI ID: IET     00010000
        SCSI SN: beaf10
        Size: 0 MB
        Online: Yes
        Removable media: No
        Backing store type: rdwr
        Backing store path: None
    LUN: 1
        Type: disk
        SCSI ID: IET     00010001
        SCSI SN: beaf11
        Size: 20000 MB
        Online: Yes
        Removable media: No
        Backing store type: rdwr
        Backing store path: /dev/virtstore/virtimage1
    LUN: 2
        Type: disk
        SCSI ID: IET     00010002
        SCSI SN: beaf12
        Size: 10000 MB
        Online: Yes
        Removable media: No
        Backing store type: rdwr
        Backing store path: /var/lib/tgtd/virtualization/virtimage2.img
    Account information:
    ACL information:
    ALL
    
    警告
    ACL 列表设置为 all。这允许本地网络上的所有系统访问这个设备。建议在生产环境中设置主机物理机器访问 ACL。
  9. 可选:测试发现

    测试新 iSCSI 设备是否可以发现。
    # iscsiadm --mode discovery --type sendtargets --portal server1.example.com
    127.0.0.1:3260,1 iqn.2010-05.com.example.server1:iscsirhel6guest
  10. 可选:测试附加该设备

    附加新设备(iqn.2010-05.com.example.server1:iscsirhel6guest),以确定是否可以附加该设备。
    # iscsiadm -d2 -m node --login
    scsiadm: Max file limits 1024 1024
    
    Logging in to [iface: default, target: iqn.2010-05.com.example.server1:iscsirhel6guest, portal: 10.0.0.1,3260]
    Login to [iface: default, target: iqn.2010-05.com.example.server1:iscsirhel6guest, portal: 10.0.0.1,3260] successful.
    分离该设备。
    # iscsiadm -d2 -m node --logout
    scsiadm: Max file limits 1024 1024
    
    Logging out of session [sid: 2, target: iqn.2010-05.com.example.server1:iscsirhel6guest, portal: 10.0.0.1,3260
    Logout of [sid: 2, target: iqn.2010-05.com.example.server1:iscsirhel6guest, portal: 10.0.0.1,3260] successful.
iSCSI 设备现在可以用于虚拟化。