NFS高可用性構築:lsyncdによる3ノード三方向同期実装

概要

NFS(Network File System)の高可用性を実現するため、3台のサーバー間でlsyncdを使用したリアルタイム三方向同期構成を構築します。これにより、単一障害点を排除し、サービス継続性を確保します。

技術要素

  • rsync: 差分同期を実現する基本的なツール
  • lsyncd: inotifyとrsyncを組み合わせたリアルタイム同期ツール
  • Keepalived: 仮想IPを管理する高可用性ソリューション
  • NFS: ネットワークファイルシステムサービス

環境構成

仮想IPホスト名IPアドレス同期ディレクトリ認証情報役割
192.168.56.120svr01192.168.56.100/data/nfsadmin/pass100NFSノード1
svr02192.168.56.101/data/nfsadmin/pass101NFSノード2
svr03192.168.56.102/data/nfsadmin/pass102NFSノード3
client01192.168.56.103/mnt/nfsテストクライアント

1. 基本環境設定

# 全ノードで実行
hostnamectl set-hostname svr01 && bash
# SELinux無効化
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
setenforce 0

# ファイアウォール設定
firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.56.0/24' accept"
firewall-cmd --reload

2. lsyncdとrsyncの導入

2.1 パッケージインストール

# 全NFSノードで実行
yum install -y epel-release
yum install -y rsync lsyncd

# サービス有効化
systemctl enable rsyncd lsyncd

2.2 rsync設定

# 共通ディレクトリ作成
mkdir -p /data/nfs

# svr01の設定
cat > /etc/rsyncd.conf << 'EOF'
log file = /var/log/rsyncd.log
pidfile = /run/rsyncd.pid
lock file = /run/rsync.lock

[nfs_share]
path = /data/nfs
comment = NFS Data Synchronization
uid = root
gid = root
read only = no
list = yes
max connections = 200
timeout = 600
auth users = admin
secrets file = /etc/rsync.auth
hosts allow = 192.168.56.0/24
EOF

# 認証ファイル作成
echo 'admin:pass100' > /etc/rsync.auth
chmod 600 /etc/rsync.auth

2.3 lsyncd設定

# svr01のlsyncd設定
cat > /etc/lsyncd.conf << 'EOF'
settings {
    logfile = "/var/log/lsyncd/lsyncd.log",
    statusFile = "/var/log/lsyncd/lsyncd.status",
    inotifyMode = "CloseWrite",
    maxProcesses = 7,
    nodaemon = false,
}

sync {
    default.rsync,
    source = "/data/nfs",
    target = "admin@192.168.56.101::nfs_share",
    delete = true,
    delay = 3,
    rsync = {
        binary = "/usr/bin/rsync",
        password_file = "/etc/rsync.pass101",
        archive = true,
        compress = false,
        _extra = {"--bwlimit=5000"}
    }
}

sync {
    default.rsync,
    source = "/data/nfs",
    target = "admin@192.168.56.102::nfs_share",
    delete = true,
    delay = 3,
    rsync = {
        binary = "/usr/bin/rsync",
        password_file = "/etc/rsync.pass102",
        archive = true,
        compress = false,
        _extra = {"--bwlimit=5000"}
    }
}
EOF

# パスワードファイル作成
echo 'pass101' > /etc/rsync.pass101
echo 'pass102' > /etc/rsync.pass102
chmod 600 /etc/rsync.pass*

2.4 サービス起動

systemctl restart rsyncd
systemctl restart lsyncd
systemctl status rsyncd lsyncd

3. NFSサーバー構成

# 全NFSノードで実行
yum install -y nfs-utils

# エクスポート設定
cat > /etc/exports << 'EOF'
/data/nfs 192.168.56.0/24(rw,sync,all_squash,anonuid=0,anongid=0)
EOF

# サービス起動
systemctl start nfs-server
systemctl enable nfs-server
exportfs -a

4. KeepalivedによるVIP管理

4.1 ヘルスチェックスクリプト

mkdir -p /opt/keepalived
cat > /opt/keepalived/check_nfs.sh << 'EOF'
#!/bin/bash
# NFSサービス状態確認
if systemctl is-active nfs-server > /dev/null 2>&1; then
    # rsyncポート監視
    if netstat -ln | grep -q ':873 '; then
        exit 0
    fi
fi
exit 1
EOF

chmod +x /opt/keepalived/check_nfs.sh

4.2 Keepalived設定

# svr01 (MASTER)
cat > /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
    router_id NFS_NODE01
}

vrrp_script chk_nfs {
    script "/opt/keepalived/check_nfs.sh"
    interval 3
    weight -20
    fall 3
    rise 2
}

vrrp_instance VI_NFS {
    state MASTER
    interface eth0
    virtual_router_id 100
    priority 110
    advert_int 1
    nopreempt
    
    authentication {
        auth_type PASS
        auth_pass securePass123
    }
    
    virtual_ipaddress {
        192.168.56.120
    }
    
    track_script {
        chk_nfs
    }
}
EOF

# svr02 (BACKUP)
cat > /etc/keepalived/keepalived.conf << 'EOF'
global_defs {
    router_id NFS_NODE02
}

vrrp_script chk_nfs {
    script "/opt/keepalived/check_nfs.sh"
    interval 3
    weight -20
    fall 3
    rise 2
}

vrrp_instance VI_NFS {
    state BACKUP
    interface eth0
    virtual_router_id 100
    priority 100
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass securePass123
    }
    
    virtual_ipaddress {
        192.168.56.120
    }
    
    track_script {
        chk_nfs
    }
}
EOF

4.3 サービス起動

systemctl start keepalived
systemctl enable keepalived
ip addr show | grep 192.168.56.120

5. 動作検証

5.1 クライアント設定

# client01で実行
yum install -y nfs-utils

# VIPへの接続確認
showmount -e 192.168.56.120

# マウント
mount -t nfs 192.168.56.120:/data/nfs /mnt/nfs

5.2 検証シナリオ

  1. 同期確認: client01でファイル作成後、全NFSノードで確認
  2. フェイルオーバー: svr01停止後、VIPの移行とサービス継続確認
  3. 復旧: svr01再起動後、データ同期確認
  4. 負荷テスト: 同時書き込みでの一貫性確認

6. トラブルシューティング

6.1 同期遅延対策

# lsyncd.confで帯域幅調整
rsync = {
    _extra = {"--bwlimit=10000", "--inplace"}
}

6.2 ログ監視

# 主要ログ場所
tail -f /var/log/lsyncd/lsyncd.log
tail -f /var/log/messages | grep keepalived
tail -f /var/log/rsyncd.log

7. パフォーマンス最適化

7.1 NFSチューニング

# /etc/sysctl.confに追加
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# 反映
sysctl -p

7.2 同期パラメータ調整

# 頻繁な変更の場合
settings {
    maxProcesses = 15,
    maxDelays = 10,
    delay = 1
}

タグ: NFS lsyncd Keepalived 高可用性 同期

5月17日 12:33 投稿