Linux SSSD同步大量AD用户缓慢
1、背景
在使用AD + sssd (ad作为ldap)同步用户,其中AD中存在10000+ 用户,同步时,用户信息获取缓慢,导致cdh的namenode 的rpc 队列打高,服务不正常。id 用户达到9s左右需要进行优化

2、解决方案
1、缩小拉取用户范围,进行定点多ou同步,相关配置请参考前面的文章sssd 多ou用户同步
2、优化sssd.conf中的参数配置
根据相关参考文档添加以下参数
subdomain_inherit = ignore_group_members, ldap_purge_cache_timeout ignore_group_members = True ldap_purge_cache_timeout = 0
在tmpfs中挂载缓存
缺点: 在重启服务器后缓存丢失,需要重新进行缓存,缓存速度可能较慢
优点:缓存在内存中,io速度会很快
在/etc/fstab中添加
tmpfs /var/lib/sss/db/ tmpfs size=300M,mode=0700,rootcontext=system_u:object_r:sssd_var_lib_t:s0 0 0
mount /var/lib/sss/db/ systemctl restart sssd
参考文档:https://jhrozek.wordpress.com/2015/08/19/performance-tuning-sssd-for-large-ipa-ad-trust-deployments/
3、测试
1、新建10000+用户
数据清单 :sample_data.csv
AD用户添加脚本
# 导入Active Directory模块
Import-Module ActiveDirectory
# 从CSV文件中读取用户信息
$users = Import-Csv -Path 'C:\sample_data.csv'
# 迭代遍历每个用户信息
foreach ($user in $users) {
# 解析用户信息
$firstname = $user.FirstName
$lastname = $user.LastName
$username = $user.UserName
$password = $user.Password
$ou = $user.OU
# 创建用户
$newUserParams = @{
SamAccountName = $username
UserPrincipalName = "$username@fzcdh.com"
Name = "$firstname"
GivenName = $firstname
Surname = $lastname
Path = $ou
AccountPassword = (ConvertTo-SecureString -String $password -AsPlainText -Force)
Enabled = $true
PasswordNeverExpires = $true
}
New-ADUser @newUserParams
}2、使用并发脚本进行性能验证(依赖python3,pandas)
import pandas as pd
import threading
import subprocess
# 读取CSV文件
data = pd.read_csv('sample_data.csv',nrows=0)
# 定义一个函数,生成随机命令并执行
def execute_random_command(row):
id = row['ID']
username = row['UserName']
# 生成随机命令(示例中为打印ID和用户名)
command = f"echo 'ID: {id}, Username: {username}'"
# 执行命令
result = subprocess.getoutput(command)
print(result)
# 创建多线程来执行随机命令
threads = []
for _, row in data.iterrows():
thread = threading.Thread(target=execute_random_command, args=(row,))
threads.append(thread)
thread.start()
# 等待所有线程完成
for thread in threads:
thread.join()
print("所有命令执行完成")查看用户获取时间是否正常
python3 cc.py > cc.log





