iSCSI: Automated configuration of iSCSI-based block device network storage initiators

write in front


  • Preparing for exams and organizing relevant notes
  • The blog post covers:
    • Manually configure the target to use block storage as backend storage iSCSIand access it through the launcher
    • ansibleConfigure the launcher via automation
  • If you don’t understand enough, please help me correct it.

There is only one true responsibility for everyone: to find themselves. Then stick to it in your heart for the rest of your life, wholeheartedly, and never stop. All other roads are incomplete, human escapes, cowardly returns to popular ideals, drifting, inner fears - Hermann Hesse, "Demian"


Regarding iSCSIwhat it is, you can check out my previous blog. Here we start configuring it directly.

Manual configuration

Provide block-based storage to network clients through an iSCSI target, and then configure the iSCSI initiator to access the device. Configuration steps

  • Configure iSCSI targets.
  • Configure iSCSI initiators and access targets.
  • Format and mount the shared block device.

Configure iSCSI target

Install the targetcli package, a command-line tool for configuring and managing iSCSI targets. Target.service, the service used to support iSCSI targets, is enabled and started. Firewall rules are configured to allow iSCSI target services through the firewall.

[root@serverd ~]# yum -y install  targetcli >> /dev/null
[root@serverd ~]# systemctl enable --now target.service
Created symlink /etc/systemd/system/multi-user.target.wants/target.service → /usr/lib/systemd/system/target.service.
[root@serverd ~]# firewall-cmd --permanent --add-service=iscsi-target
success
[root@serverd ~]# firewall-cmd --reload
success

Enter targetcli configuration

[root@serverd ~]# targetcli
targetcli shell version 2.1.fb49
Copyright 2011-2013 by Datera, Inc and others.
For help on commands, type 'help'.

/> ls
o- / ............................................................................................ [...]
  o- backstores ................................................................................. [...]
  | o- block ..................................................................... [Storage Objects: 0]
  | o- fileio .................................................................... [Storage Objects: 0]
  | o- pscsi ..................................................................... [Storage Objects: 0]
  | o- ramdisk ................................................................... [Storage Objects: 0]
  o- iscsi ............................................................................... [Targets: 0]
  o- loopback ............................................................................ [Targets: 0]
/>

Configuration requirements:

  • Block device: /dev/vdb
  • Target backing store name: store1.disk1
  • Title IQN: iqn.2014-06.com.example:store1
  • Allow access to the target's initiator IQN: iqn.2014-06.com.example:servera
  • Access address: 172.25.250.13, port: 3260
      Block device: /dev/vdb
Backing store name: store1.disk1
        Target IQN: iqn.2014-06.com.example:store1
     Initiator IQN: iqn.2014-06.com.example:servera
    Portal address: 172.25.250.13 (port 3260)

Configure /dev/vdb as block backing store. Use the name store1.disk1 for the backstore object.

/backstores> cd block
/backstores/block> ls
o- block ......................................................................... [Storage Objects: 0]
/backstores/block> create store1.disk1  /dev/vdb
Created block storage object store1.disk1 using /dev/vdb.
/backstores/block> ls
o- block ......................................................................... [Storage Objects: 1]
  o- store1.disk1 .......................................... [/dev/vdb (5.0GiB) write-thru deactivated]
    o- alua .......................................................................... [ALUA Groups: 1]
      o- default_tg_pt_gp .............................................. [ALUA state: Active/optimized]

Configure the IQN for the target. Use the name iqn.2014-06.com.example:store1 as the IQN

/backstores/block> cd /iscsi
/iscsi> create iqn.2014-06.com.example:store1
Created target iqn.2014-06.com.example:store1.
Created TPG 1.
Global pref auto_add_default_portal=true
Created default portal listening on all IPs (0.0.0.0), port 3260.

Create an ACL for the initiator. The initiator IQN is iqn.2014-06.com.example:servera

/iscsi> cd iqn.2014-06.com.example:store1/tpg1/acls
/iscsi/iqn.20...re1/tpg1/acls> create  iqn.2014-06.com.example:servera
Created Node ACL for iqn.2014-06.com.example:servera
/iscsi/iqn.20...re1/tpg1/acls> ls
o- acls ..................................................................................... [ACLs: 1]
  o- iqn.2014-06.com.example:servera ................................................. [Mapped LUNs: 0]

Create a LUN under the target. Use store1.disk1 as the backing store.

/iscsi/iqn.20...re1/tpg1/acls> cd /iscsi/iqn.2014-06.com.example:store1/tpg1/luns
/iscsi/iqn.20...re1/tpg1/luns> create /backstores/block/store1.disk1
Created LUN 0.
Created LUN 0->0 mapping in node ACL iqn.2014-06.com.example:servera
/iscsi/iqn.20...re1/tpg1/luns> ls
o- luns ..................................................................................... [LUNs: 1]
  o- lun0 .......................................... [block/store1.disk1 (/dev/vdb) (default_tg_pt_gp)]

Delete the default destination port and create a destination port that listens only to port 3260 on the 172.25.250.13 address

/iscsi/iqn.20...re1/tpg1/luns> cd /iscsi/iqn.2014-06.com.example:store1/tpg1/portals/
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................... [Portals: 1]
  o- 0.0.0.0:3260 ................................................................................ [OK]
/iscsi/iqn.20.../tpg1/portals> delete 0.0.0.0 3260
Deleted network portal 0.0.0.0:3260
/iscsi/iqn.20.../tpg1/portals> create 172.25.250.13 3260
Using default IP port 3260
Created network portal 172.25.250.13:3260.
/iscsi/iqn.20.../tpg1/portals> ls
o- portals ............................................................................... [Portals: 1]
  o- 172.25.250.13:3260 .......................................................................... [OK]

List the target configuration to verify the configuration

/iscsi/iqn.20.../tpg1/portals> ls /
o- / ............................................................................................ [...]
  o- backstores ................................................................................. [...]
  | o- block ..................................................................... [Storage Objects: 1]
  | | o- store1.disk1 ........................................ [/dev/vdb (5.0GiB) write-thru activated]
  | |   o- alua ...................................................................... [ALUA Groups: 1]
  | |     o- default_tg_pt_gp .......................................... [ALUA state: Active/optimized]
  | o- fileio .................................................................... [Storage Objects: 0]
  | o- pscsi ..................................................................... [Storage Objects: 0]
  | o- ramdisk ................................................................... [Storage Objects: 0]
  o- iscsi ............................................................................... [Targets: 1]
  | o- iqn.2014-06.com.example:store1 ....................................................... [TPGs: 1]
  |   o- tpg1 .................................................................. [no-gen-acls, no-auth]
  |     o- acls ............................................................................. [ACLs: 1]
  |     | o- iqn.2014-06.com.example:servera ......................................... [Mapped LUNs: 1]
  |     |   o- mapped_lun0 ............................................. [lun0 block/store1.disk1 (rw)]
  |     o- luns ............................................................................. [LUNs: 1]
  |     | o- lun0 .................................. [block/store1.disk1 (/dev/vdb) (default_tg_pt_gp)]
  |     o- portals ....................................................................... [Portals: 1]
  |       o- 172.25.250.13:3260 .................................................................. [OK]
  o- loopback ............................................................................ [Targets: 0]
/iscsi/iqn.20.../tpg1/portals>

iSCSI initiator configuration

Configure an iSCSI initiator on servera to access targets provided by serverd

Install the iscsi-initiator-utils software package, which provides iSCSI-related tools and services. The system's iSCSI hostname was changed to iqn.2014-06.com.example:servera. The iSCSI service was restarted for the changes to take effect.

[root@servera ~]# yum search iscsi
==================================== Name & Summary Matched: iscsi ====================================
libiscsi.x86_64 : iSCSI client library
udisks2-iscsi.x86_64 : Module for iSCSI
libiscsi-utils.x86_64 : iSCSI Client Utilities
qemu-kvm-block-iscsi.x86_64 : QEMU iSCSI block driver
libiscsi-devel.x86_64 : iSCSI client development libraries
iscsi-initiator-utils.i686 : iSCSI daemon and utility programs
iscsi-initiator-utils.x86_64 : iSCSI daemon and utility programs
libvirt-daemon-driver-storage-iscsi.x86_64 : Storage driver plugin for iscsi
python3-iscsi-initiator-utils.x86_64 : Python 3.6 bindings to iscsi-initiator-utils
iscsi-initiator-utils-iscsiuio.x86_64 : Userspace configuration daemon required for some iSCSI hardware
[root@servera ~]# yum -y install iscsi-initiator-utils >> /dev/null
[root@servera ~]# cat /etc/iscsi/initiatorname.iscsi
InitiatorName=iqn.1994-05.com.redhat:1ba9ed303a28
[root@servera ~]# echo "InitiatorName=iqn.2014-06.com.example:servera" > /etc/iscsi/initiatorname.iscsi
[root@servera ~]# systemctl restart iscsid

iSCSI target discovered and logged in successfully. Mounted the iSCSI disk to the /iscsidisk directory. Configure the system to automatically mount this disk when booting.

[root@servera ~]# iscsiadm -m discovery  -t st -p  172.25.250.13
172.25.250.13:3260,1 iqn.2014-06.com.example:store1
[root@servera ~]#  iscsiadm -m node -T iqn.2014-06.com.example:store1 -p 172.25.250.13 -l
Logging in to [iface: default, target: iqn.2014-06.com.example:store1, portal: 172.25.250.13,3260]
Login to [iface: default, target: iqn.2014-06.com.example:store1, portal: 172.25.250.13,3260] successful.
[root@servera ~]# iscsiadm -m session -P 3 | grep disk
                        Attached scsi disk sda          State: running
[root@servera ~]# lsblk --fs
NAME   FSTYPE LABEL UUID                                 MOUNTPOINT
sda    ext4         55966983-f99f-4123-b7ce-7bdf1f491563
vda
└─vda1 xfs          f7614c41-2835-4125-bb13-50772dc2f30c /
vdb
[root@servera ~]# mkdir  /iscsidisk
[root@servera ~]# echo "UUID=55966983-f99f-4123-b7ce-7bdf1f491563 /iscsidisk  ext4 _netdev  0  0" >> /e
tc/fstab
[root@servera ~]# mount /iscsidisk/
[root@servera ~]# df /iscsidisk/
Filesystem     1K-blocks  Used Available Use% Mounted on
/dev/sda         5095040 20596   4795916   1% /iscsidisk
[root@servera ~]#

Target automated configuration

For the automatic configuration of the target end, one method can directly copy saveconfig.jsonthe file, and the second method uses the method of directly passing the shell command, but the shell method does not meet the idempotence, so the priority is given to copying saveconfig.json, time relationship, and no organization.^_^

Launcher automation configuration

Documents involved

[student@workstation iscsi-review]$ tree .
.
├── ansible.cfg
├── inventory
├── playbook.yml
└── templates
    └── initiatorname.iscsi.j2

1 directory, 4 files
[student@workstation iscsi-review]$ ls
ansible.cfg  inventory  playbook.yml  templates

The specific configuration is relatively simple and I won’t go into details here.

[student@workstation iscsi-review]$ cat ansible.cfg
[defaults]
inventory=inventory
remote_user=devops
[student@workstation iscsi-review]$ cat inventory
[initiators]
servera.lab.example.com
[student@workstation iscsi-review]$ cat templates/initiatorname.iscsi.j2
InitiatorName=iqn.2014-06.com.example:{
    
    {
    
     ansible_facts['hostname'] }}
[student@workstation iscsi-review]$ cat playbook.yml

Mainly read the main script, you need to increase the rights.

[student@workstation iscsi-review]$ cat playbook.yml
---
- name: Ensure /iscsidisk is mounted from serverd iSCSI target
  hosts: initiators
  # 提升权限为超级用户(root)
  become: true

  tasks:
    # 启动器相关的配置 
    - name: the iSCSI initiator software installed
      # 安装所需软件包
      yum:
        name: iscsi-initiator-utils
        state: present
    - name: the IQN is set for the initiator
      # 设置启动器 将启动器 IQN 设置为 iqn.2014-06.com.example:servera
      copy:
        dest: /etc/iscsi/initiatorname.iscsi
        content: "InitiatorName=iqn.2014-06.com.example:{
    
    { ansible_facts['hostname'] }}\n"
        mode: '644'
        owner: root
        group: root
      notify: restart iscsid

    # Forces the handler to run so that the iscsid service is restarted
    # and is aware of the new initiator IQN
    - meta: flush_handlers
      # - meta: flush_handlers时,它会导致处理程序队列中的所有处理程序立即执行,而不需要等待到Playbook
的结尾
    #  
    - name: the iSCSI target is discovered and available
      # open_iscsi 模块发现并登录到指定的 iSCSI 目标
      open_iscsi:
        portal: 172.25.250.13
        port: '3260'
        target: iqn.2014-06.com.example:store1
        discover: yes
        login: yes
      # 将结果注册到 target 变量中
      register: target

    - name: display the discovered devices
      debug:
        msg: The new device is {
    
    {
    
     target['devicenodes'][0] }}
    # 使用角色  rhel-system-roles.storage 来完成 格式化和挂载设备
    - name: the new device is formatted and mounted under /iscsidisk
      include_role:
        name: rhel-system-roles.storage
      vars:
        #FIXME: mount target['devicenodes'][0] into /iscsidisk
        #       If the device is not yet formatted in ext4, format it.
        #       Use the proper mount option for an iSCSI disk.
        storage_volumes:
          - name: devdata
            state: present
            type: disk
            disks:
              - "{
    
    { target['devicenodes'][0] }}"
            mount_point: /iscsidisk
            fs_type: ext4
            mount_options: '_netdev'

  handlers:
    - name: restart iscsid
      service:
        name: iscsid
        state: restarted

Reference to part of the blog post

© The copyright of the reference links in this article belongs to the original author. Please inform us if there is any infringement.


红帽服务管理与自动化(RH358)Lecture Notes


© 2018-2023 [email protected] , All rights reserved. Keep Attribution-NonCommercial-ShareAlike (CC BY-NC-SA 4.0)

Guess you like

Origin blog.csdn.net/sanhewuyang/article/details/132824160