3.2. 将 etcd 移动到不同的磁盘


您可以将 etcd 从共享磁盘移到独立磁盘,以防止或解决性能问题。

Machine Config Operator (MCO) 负责为 OpenShift Container Platform 4.19 容器存储挂载辅助磁盘。

注意

这个编码脚本只支持以下设备类型的设备名称:

SCSI 或 SATA
/dev/sd*
虚拟设备
/dev/vd*
NVMe
/dev/nvme*[0-9]*n*

限制:

  • 当新磁盘附加到集群时,etcd 数据库是 root 挂载的一部分。当主节点被重新创建时,它不是二级磁盘的一部分或预期的磁盘。因此,主节点不会创建单独的 /var/lib/etcd 挂载。

先决条件

  • 有集群的 etcd 数据备份。
  • 已安装 OpenShift CLI(oc)。
  • 您可以使用 cluster-admin 权限访问集群。
  • 在上传机器配置前添加额外的磁盘。
  • MachineConfigPool 必须与 metadata.labels[machineconfiguration.openshift.io/role] 匹配。这适用于控制器、worker 或自定义池。
注意

这个过程不会将 root 文件系统的部分内容(如 /var/ )移到已安装节点上的另一个磁盘或分区。

重要

使用 control plane 机器集时不支持这个过程。

流程

  1. 将新磁盘附加到集群,并在 debug shell 中运行 lsblk 命令来验证节点中是否检测到磁盘:

    $ oc debug node/<node_name>
    Copy to Clipboard Toggle word wrap
    # lsblk
    Copy to Clipboard Toggle word wrap

    记录下 lsblk 命令报告的新磁盘的设备名称。

  2. 创建以下脚本,并将其命名为 etcd-find-secondary-device.sh

    #!/bin/bash
    set -uo pipefail
    
    for device in <device_type_glob>; do 
    1
    
    /usr/sbin/blkid "${device}" &> /dev/null
     if [ $? == 2  ]; then
        echo "secondary device found ${device}"
        echo "creating filesystem for etcd mount"
        mkfs.xfs -L var-lib-etcd -f "${device}" &> /dev/null
        udevadm settle
        touch /etc/var-lib-etcd-mount
        exit
     fi
    done
    echo "Couldn't find secondary block device!" >&2
    exit 77
    Copy to Clipboard Toggle word wrap
    1
    <device_type_glob> 替换为您的块设备类型的 shell glob。对于 SCSI 或 SATA 驱动器,使用 /dev/sd*; 对于虚拟驱动器,使用 /dev/vd*; 对于 NVMe 驱动器,使用 /dev/nvme*[0-9]*n*
  3. etcd-find-secondary-device.sh 脚本创建一个 base64 编码的字符串,并记录它的内容:

    $ base64 -w0 etcd-find-secondary-device.sh
    Copy to Clipboard Toggle word wrap
  4. 创建名为 etcd-mc.ymlMachineConfig YAML 文件,其内容如下:

    apiVersion: machineconfiguration.openshift.io/v1
    kind: MachineConfig
    metadata:
      labels:
        machineconfiguration.openshift.io/role: master
      name: 98-var-lib-etcd
    spec:
      config:
        ignition:
          version: 3.5.0
        storage:
          files:
            - path: /etc/find-secondary-device
              mode: 0755
              contents:
                source: data:text/plain;charset=utf-8;base64,<encoded_etcd_find_secondary_device_script> 
    1
    
        systemd:
          units:
            - name: find-secondary-device.service
              enabled: true
              contents: |
                [Unit]
                Description=Find secondary device
                DefaultDependencies=false
                After=systemd-udev-settle.service
                Before=local-fs-pre.target
                ConditionPathExists=!/etc/var-lib-etcd-mount
    
                [Service]
                RemainAfterExit=yes
                ExecStart=/etc/find-secondary-device
    
                RestartForceExitStatus=77
    
                [Install]
                WantedBy=multi-user.target
            - name: var-lib-etcd.mount
              enabled: true
              contents: |
                [Unit]
                Before=local-fs.target
    
                [Mount]
                What=/dev/disk/by-label/var-lib-etcd
                Where=/var/lib/etcd
                Type=xfs
                TimeoutSec=120s
    
                [Install]
                RequiredBy=local-fs.target
            - name: sync-var-lib-etcd-to-etcd.service
              enabled: true
              contents: |
                [Unit]
                Description=Sync etcd data if new mount is empty
                DefaultDependencies=no
                After=var-lib-etcd.mount var.mount
                Before=crio.service
    
                [Service]
                Type=oneshot
                RemainAfterExit=yes
                ExecCondition=/usr/bin/test ! -d /var/lib/etcd/member
                ExecStart=/usr/sbin/setsebool -P rsync_full_access 1
                ExecStart=/bin/rsync -ar /sysroot/ostree/deploy/rhcos/var/lib/etcd/ /var/lib/etcd/
                ExecStart=/usr/sbin/semanage fcontext -a -t container_var_lib_t '/var/lib/etcd(/.*)?'
                ExecStart=/usr/sbin/setsebool -P rsync_full_access 0
                TimeoutSec=0
    
                [Install]
                WantedBy=multi-user.target graphical.target
            - name: restorecon-var-lib-etcd.service
              enabled: true
              contents: |
                [Unit]
                Description=Restore recursive SELinux security contexts
                DefaultDependencies=no
                After=var-lib-etcd.mount
                Before=crio.service
    
                [Service]
                Type=oneshot
                RemainAfterExit=yes
                ExecStart=/sbin/restorecon -R /var/lib/etcd/
                TimeoutSec=0
    
                [Install]
                WantedBy=multi-user.target graphical.target
    Copy to Clipboard Toggle word wrap
    1
    <encoded_etcd_find_secondary_device_script> 替换为您记录的编码脚本内容。
  5. 应用创建的 MachineConfig YAML 文件:

    $ oc create -f etcd-mc.yml
    Copy to Clipboard Toggle word wrap

验证步骤

  • 在节点的 debug shell 中运行 grep /var/lib/etcd /proc/mounts 命令,以确保挂载磁盘:

    $ oc debug node/<node_name>
    Copy to Clipboard Toggle word wrap
    # grep -w "/var/lib/etcd" /proc/mounts
    Copy to Clipboard Toggle word wrap

    输出示例

    /dev/sdb /var/lib/etcd xfs rw,seclabel,relatime,attr2,inode64,logbufs=8,logbsize=32k,noquota 0 0
    Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat