Ceph Cheatsheet

repository·master·Indexed 43 days ago

https://github.com/thejj/ceph-cheatsheet

A technical guide for operating and managing Ceph distributed storage clusters. Covers architecture, component configuration (OSD, MON, MGR, MDS), data abstractions like Placement Groups (PGs) and the CRUSH algorithm, and operational tasks including BlueStore OSD provisioning, CephFS setup, and cluster balancing.

Tokens
13.3K
Snippets
49
Records
59
Agent score
6%

What's inside Ceph

  1. Manage OSD weights correctly

    master

    Avoid using ceph osd reweight with values other than 0 (out) or 1 (in) unless you have a specific reason.

    The Problem: ceph osd reweight does not affect the bucket (e.g., host) weight. This means the probability of placing a Placement Group (PG) on a host remains the same, causing other OSDs on that same host to receive more PGs than their size would normally dictate.

    The Solution: To artificially shrink the capacity of a device without affecting bucket probabilities, use ceph osd crush reweight instead.

  2. Calculate PG shard sizes for balancing

    master

    When balancing PGs (Placement Groups) across OSDs, it is recommended that a PG shard be around 10-50 GiB. This represents the amount of data that can be moved from one OSD to another during balancing.

    To calculate the shard size manually:

    • For replicated pools: shardsize = pool_size / pg_num
    • For erasure coded (n+m) pools: shardsize = pool_size / (pg_num * n)
  3. Understand Ceph Cluster Components

    master

    Ceph is a distributed storage cluster composed of several key services. Understanding these roles is essential for planning deployment and troubleshooting:

    • Client: Connects to the cluster to access data (e.g., Linux kernel, QEMU, Samba, or CLI tools like ceph, rbd, rados).
    • Cluster (RADOS): The core distributed storage layer.
    • Object Store Device (OSD): The actual storage provider. Each OSD stores data on a single drive using a key-value database.
    • Monitor (MON): The cluster coordinator. It maintains cluster state and requires an odd number of instances (e.g., 3 or 5) to ensure consensus and tolerate outages.
    • Metadata Server (MDS): Specifically for CephFS. It handles filesystem inode transactions and stores its state on OSDs.
    • Manager (MGR): Collects cluster statistics, hosts the WebUI, manages balancing, and collects crash reports.
  4. Restrict CephFS access using Namespaces

    master

    While MDS path restrictions control metadata access, they do not prevent direct OSD access. To truly restrict access, use Namespaces. Namespaces are pool-independent and allow OSDs to check access via namespace restriction.

    Workflow:

    1. Set the namespace on a CephFS directory: setfattr -n ceph.dir.layout.pool_namespace -v $namespacename <path>.
    2. Update client auth caps to include the namespace: osd 'allow rw namespace=$namespacename tag cephfs data=lolfs'.
    # Set namespace on a directory
    sudo setfattr -n ceph.dir.layout.pool_namespace -v $namespacename /mnt/cephfs/directory/name
    
    # Update auth caps for client to use that namespace
    ceph auth caps client.somename mds 'allow rw path=/directory/name' mon 'allow r' osd 'allow rw namespace=$namespacename tag cephfs data=lolfs'
  5. Configure Data Safety with min_size

    master

    To ensure data safety, always set min_size to be at least one more than the minimum required for reachability. This ensures that even when the cluster is at its min_size threshold, every write is still backed by at least one redundant OSD. If another disk fails while at min_size without redundancy, all data is lost.

    Recommended combinations:

    • Replica: For n >= 3, use size = n and min_size = 2.
    • Erasure Coding: For n >= 2, m >= 2, i >= 1, use size = n + m and min_size = n + i.

    Check your current pool settings using: ceph osd pool ls detail

    ceph osd pool ls detail
  6. Understand Ceph Data Abstractions: Pools, Objects, and PGs

    master

    Ceph organizes data using a hierarchy of abstractions:

    • Object: The fundamental unit of storage (typically 4MiB). Files and block devices are split into these chunks.
    • Pool: A logical grouping of objects. Pools allow you to define specific redundancy (e.g., replication vs. erasure coding) and placement rules for a set of data.
    • Namespace: A prefix used to create access realms for objects.
    • Placement Group (PG): A partition of a pool created by hashing object keys. Objects are assigned to PGs, and PGs are distributed across OSDs. This partitioning allows Ceph to scale and manage data distribution efficiently.
  7. How Ceph Data Placement and Scaling Works

    master

    Ceph scales by distributing data across many OSDs using the CRUSH algorithm.

    The Read/Write Workflow:

    1. Hashing: The object's key is hashed.
    2. PG Selection: The last x bits of the hash determine the Placement Group (PG).
    3. CRUSH Algorithm: Used to find the primary OSD ID for that PG, respecting topology constraints (e.g., rack awareness).
    4. OSDMap: Used to resolve the primary OSD's IP address.
    5. Communication: The client connects to the primary OSD. For erasure coding (RAID-like), the primary OSD coordinates with other OSDs in the same PG to reconstruct or write data.

    Key Scaling Behaviors:

    • Recovery: If OSDs fail, Ceph automatically recreates missing data on available OSDs to meet the desired redundancy.
    • Adding OSDs: Ceph performs a "backfill," moving existing PGs to the new OSDs to balance storage.
    • Removing OSDs: Ceph moves PGs off the OSDs marked for removal to other nodes.
  8. Create and configure CephFS pools

    master

    To run CephFS on an erasure coded (EC) pool, you must enable allow_ec_overwrites. Typically, CephFS uses replicated pools for metadata and EC pools for data.

    Pool Setup Example:

    1. Create EC profile.
    2. Create EC data pool with allow_ec_overwrites true.
    3. Create replicated metadata/root pools.
    4. Set size (total replicas) and min_size (minimum OSDs required for a PG to be online).
    # erasure coding pool (for data)
    ceph osd pool create lol_data 32 32 erasure standard_8_2
    ceph osd pool set lol_data allow_ec_overwrites true
    
    # replicated pools (for metadata)
    ceph osd pool create lol_root 32 replicated
    ceph osd pool create lol_metadata 32 replicated
    
    # min_size configuration
    ceph osd pool set lol_root size 3
    ceph osd pool set lol_root min_size 2
    ceph osd pool set lol_metadata size 3
    ceph osd pool set lol_metadata min_size 2
  9. Optimize ext4 on RBD with striping

    master

    Since RBD objects are typically 4 MiB, you can optimize ext4 performance by aligning its stripe width to match the RADOS object size. This ensures allocations are distributed across RADOS objects effectively.

    1. Filesystem Creation: Use mkfs.ext4 with stride and stripe_width set to 1024 (since $1024 \times 4096\text{ bytes} = 4\text{ MiB}$).
    2. Mounting: You can also specify the stripe width during the mount command using the stripe option.

    To verify the current stripe width, use dumpe2fs /dev/rbd/your/rbd and look for RAID stripe width.

    # Create ext4 with optimized striping
    mkfs -E nodiscard,stride=1024,stripe_width=1024 /dev/rbd/your/rbd
    
    # Mount with stripe option
    mount -o defaults,noauto,stripe=1024 /dev/rbd/your/rbd /your/mountpoint
  10. Resolve CephFS Inodes to File Paths

    master

    If you have an inode number (e.g., from ls -li) and need to find its file name and path, you can decode the parent extended attribute (xattr) which contains an inode_backtrace_t structure.

    Note on Pools:

    • For files, use the <cephfs-root-pool>.
    • For directories, use the <cephfs-metadata-pool>.

    Procedure:

    1. Convert the inode number to hex.
    2. Use rados to get the parent xattr.
    3. Pipe the output to ceph-dencoder to decode the inode_backtrace_t JSON.
    # have a inode number of a file with e.g. `ls -li <file>`
    rados -p <cephfs-root-pool> getxattr $(printf %x <inodenumnber>).00000000 parent | ceph-dencoder type inode_backtrace_t import - decode dump_json
  11. Migrate encrypted OSDs to non-encrypted OSDs

    master

    To migrate an encrypted OSD to a non-encrypted one, you must copy the raw block data to a new, same-sized device (HDD or Logical Volume) which effectively performs the decryption during the dd process.

    Workflow:

    1. Backup Tags: Save the existing LVM tags from the encrypted Logical Volume (LV) using lvs. You will need these to make the new device discoverable by ceph-volume.
    2. Prepare New Disk: Create a new Physical Volume (PV), Volume Group (VG), and Logical Volume (LV) on the new disk. Recommended naming convention: ceph-$(uuidgen) for VG and osd-block-$(uuidgen) for LV.
    3. Decrypt via Copy: Use dd to copy data from the encrypted mapper device to the new decrypted LV. This overwrites the encrypted data and the cryptheader.
    4. Restore LVM Tags: Apply the original LVM tags to the new decrypted volume so ceph-volume can recognize it as a valid OSD device.

    Required Tags for Decrypted Volume:

    • ceph.block_device: Path to the decrypted VG
    • ceph.block_uuid: The block UUID (found via blkid)
    • ceph.cephx_lockbox_secret: Leave empty
    • ceph.cluster_fsid: The cluster FSID
    • ceph.cluster_name: ceph
    • ceph.crush_device_class: None
    • ceph.encrypted: 0 (indicates non-encrypted)
    • ceph.osd_fsid: The original OSD FSID
    • ceph.osd_id: The correct OSD ID
    • ceph.type: block
    • ceph.vdo: 0
    # 1. See tags of encrypted volume
    lvs -o lv_tags /dev/path-to-encrypted-lv
    
    # 2. Copy data (this performs decryption)
    dd if=/dev/mapper/encrypteddevice of=/dev/decrypted-lv bs=4096 status=progress
    
    # 3. Set tags on the decrypted volume
    lvchange --addtag "ceph.encrypted=0 ceph.osd_id=$correctosdid ..." /dev/path-to-now-decrypted-vg
  12. Improve RBD performance with local SSD caching

    master

    You can use lvmcache to cache a Ceph RBD on local SSD storage (ensure the SSD is part of a RAID or otherwise secured). This is particularly effective for speeding up HDD-based Ceph pools by:

    1. Drastically reducing on-RBD filesystem commit latency.
    2. Improving read performance by avoiding the ~15ms latency of fetching from HDDs.

    Note: Since Linux 5.7, krbd uses per-cpu queue processing, so grouping multiple RBDs into a single device via LVM/MD may no longer provide significant benefits.