简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français

站内搜索

搜索

活动公告

11-02 12:46
10-23 09:32
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,将及时处理!
10-23 09:31
10-23 09:28
通知:签到时间调整为每日4:00(东八区)
10-23 09:26

企业级Red Hat Enterprise Linux实例配置最佳实践详解提升系统性能与可靠性

3万

主题

318

科技点

3万

积分

大区版主

木柜子打湿

积分
31894

财Doro三倍冰淇淋无人之境【一阶】立华奏小樱(小丑装)⑨的冰沙以外的星空【二阶】

发表于 2025-10-3 17:10:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

Red Hat Enterprise Linux (RHEL) 作为企业级操作系统的首选,为关键业务应用提供了稳定、安全和高性能的运行环境。正确配置和优化RHEL实例对于确保系统性能、可靠性和安全性至关重要。本文将详细介绍企业级RHEL实例配置的最佳实践,帮助系统管理员和运维工程师充分发挥RHEL的潜力,提升系统性能与可靠性。

1. RHEL系统基础配置与优化

1.1 系统安装与初始化配置

在安装RHEL时,合理规划分区结构是系统稳定运行的基础。推荐的分区方案如下:
  1. # 典型的企业级RHEL分区方案
  2. /boot     500MB    # 启动分区
  3. swap      内存大小×2 # 交换分区,如果内存大于16GB,则分配16GB即可
  4. /         20GB     # 根分区
  5. /usr      15GB     # 应用程序分区
  6. /var      20GB     # 可变数据分区
  7. /home     剩余空间  # 用户数据分区
  8. /opt      10GB     # 可选软件包分区
  9. /tmp      5GB      # 临时文件分区
复制代码

安装完成后,执行以下基础配置:
  1. # 更新系统到最新版本
  2. yum update -y
  3. # 设置主机名
  4. hostnamectl set-hostname server.example.com
  5. # 配置时区
  6. timedatectl set-timezone Asia/Shanghai
  7. # 配置网络
  8. nmcli con mod eth0 ipv4.addresses 192.168.1.100/24
  9. nmcli con mod eth0 ipv4.gateway 192.168.1.1
  10. nmcli con mod eth0 ipv4.dns "8.8.8.8 8.8.4.4"
  11. nmcli con mod eth0 connection.autoconnect yes
  12. nmcli con up eth0
  13. # 禁用SELinux(仅用于测试环境,生产环境建议启用)
  14. sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
  15. setenforce 0
  16. # 停止并禁用防火墙(仅用于测试环境,生产环境建议配置适当规则)
  17. systemctl stop firewalld
  18. systemctl disable firewalld
复制代码

1.2 内核参数优化

通过调整内核参数,可以显著提升系统性能。编辑/etc/sysctl.conf文件:
  1. # 增加文件描述符限制
  2. fs.file-max = 65535
  3. # 优化网络参数
  4. net.core.rmem_max = 16777216
  5. net.core.wmem_max = 16777216
  6. net.ipv4.tcp_rmem = 4096 87380 16777216
  7. net.ipv4.tcp_wmem = 4096 65536 16777216
  8. net.ipv4.tcp_fin_timeout = 30
  9. net.ipv4.tcp_keepalive_time = 1200
  10. net.ipv4.tcp_max_syn_backlog = 65536
  11. net.core.netdev_max_backlog = 65536
  12. # 优化虚拟内存管理
  13. vm.swappiness = 10
  14. vm.dirty_ratio = 60
  15. vm.dirty_background_ratio = 2
  16. # 避免ARP表溢出
  17. net.ipv4.neigh.default.gc_thresh1 = 1024
  18. net.ipv4.neigh.default.gc_thresh2 = 2048
  19. net.ipv4.neigh.default.gc_thresh3 = 4096
复制代码

应用配置:
  1. sysctl -p
复制代码

1.3 资源限制配置

编辑/etc/security/limits.conf文件,增加用户资源限制:
  1. * soft nofile 65535
  2. * hard nofile 65535
  3. * soft nproc 32768
  4. * hard nproc 32768
  5. * soft core unlimited
  6. * hard core unlimited
复制代码

2. 系统性能调优

2.1 CPU性能优化

RHEL默认使用CFQ(Completely Fair Queuing)调度器,但对于企业级应用,特别是数据库服务器,deadline或noop调度器可能更合适:
  1. # 查看当前调度器
  2. cat /sys/block/sda/queue/scheduler
  3. # 临时更改调度器
  4. echo noop > /sys/block/sda/queue/scheduler
  5. # 永久更改调度器(编辑/etc/rc.local)
  6. echo 'echo noop > /sys/block/sda/queue/scheduler' >> /etc/rc.local
  7. chmod +x /etc/rc.local
复制代码

安装并配置cpupower工具:
  1. yum install -y cpupowerutils
  2. # 查看当前CPU频率策略
  3. cpupower frequency-info
  4. # 设置性能模式
  5. cpupower frequency-set -g performance
  6. # 永久生效(编辑/etc/default/cpupower)
  7. echo 'GOVERNOR="performance"' > /etc/default/cpupower
  8. systemctl enable cpupower
复制代码

2.2 内存优化

对于需要大量内存的应用(如数据库),配置大页内存可以提升性能:
  1. # 查看当前大页设置
  2. cat /proc/meminfo | grep HugePages
  3. # 临时设置大页数量
  4. echo 1024 > /proc/sys/vm/nr_hugepages
  5. # 永久设置(编辑/etc/sysctl.conf)
  6. echo 'vm.nr_hugepages = 1024' >> /etc/sysctl.conf
  7. sysctl -p
  8. # 配置用户组权限访问大页
  9. echo 'vm.hugetlb_shm_group = 1000' >> /etc/sysctl.conf  # 1000是用户组ID
  10. sysctl -p
复制代码

对于某些工作负载,禁用THP可以提高性能:
  1. # 临时禁用THP
  2. echo never > /sys/kernel/mm/transparent_hugepage/enabled
  3. echo never > /sys/kernel/mm/transparent_hugepage/defrag
  4. # 永久禁用(编辑/etc/rc.local)
  5. echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
  6. echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.local
  7. chmod +x /etc/rc.local
复制代码

2.3 I/O性能优化

根据不同的工作负载选择合适的I/O调度器:
  1. # 查看可用的调度器
  2. cat /sys/block/sda/queue/scheduler
  3. # 临时设置调度器
  4. echo deadline > /sys/block/sda/queue/scheduler
  5. # 永久设置(编辑/etc/rc.local)
  6. echo 'echo deadline > /sys/block/sda/queue/scheduler' >> /etc/rc.local
  7. chmod +x /etc/rc.local
复制代码

选择合适的文件系统并优化挂载选项:
  1. # 格式化分区为XFS文件系统
  2. mkfs.xfs /dev/sdb1
  3. # 创建挂载点
  4. mkdir -p /data
  5. # 优化挂载选项(编辑/etc/fstab)
  6. echo '/dev/sdb1 /data xfs defaults,noatime,nodiratime,largeio,inode64,swalloc 0 0' >> /etc/fstab
  7. # 挂载文件系统
  8. mount -a
复制代码

对于ext4文件系统,可以使用以下优化选项:
  1. # 格式化分区为ext4文件系统
  2. mkfs.ext4 /dev/sdb1
  3. # 创建挂载点
  4. mkdir -p /data
  5. # 优化挂载选项(编辑/etc/fstab)
  6. echo '/dev/sdb1 /data ext4 defaults,noatime,nodiratime,data=writeback,barrier=0 0 0' >> /etc/fstab
  7. # 挂载文件系统
  8. mount -a
复制代码

3. 网络配置优化

3.1 网络接口优化

编辑网络接口配置文件(如/etc/sysconfig/network-scripts/ifcfg-eth0):
  1. TYPE=Ethernet
  2. BOOTPROTO=none
  3. DEFROUTE=yes
  4. NAME=eth0
  5. DEVICE=eth0
  6. ONBOOT=yes
  7. IPADDR=192.168.1.100
  8. PREFIX=24
  9. GATEWAY=192.168.1.1
  10. DNS1=8.8.8.8
  11. DNS2=8.8.4.4
  12. ETHTOOL_OPTS="-G eth0 rx 4096 tx 4096"
复制代码

配置网络中断亲和性,提高网络处理性能:
  1. # 安装irqbalance工具
  2. yum install -y irqbalance
  3. # 停止irqbalance服务
  4. systemctl stop irqbalance
  5. systemctl disable irqbalance
  6. # 查看网络中断
  7. cat /proc/interrupts | grep eth
  8. # 设置中断亲和性(示例:将中断45绑定到CPU核心0)
  9. echo 1 > /proc/irq/45/smp_affinity
  10. # 永久生效(编辑/etc/rc.local)
  11. echo 'echo 1 > /proc/irq/45/smp_affinity' >> /etc/rc.local
  12. chmod +x /etc/rc.local
复制代码

3.2 网络协议栈优化

编辑/etc/sysctl.conf文件,添加以下网络协议栈优化参数:
  1. # TCP连接优化
  2. net.ipv4.tcp_syncookies = 1
  3. net.ipv4.tcp_tw_reuse = 1
  4. net.ipv4.tcp_fin_timeout = 30
  5. net.ipv4.tcp_keepalive_time = 1200
  6. net.ipv4.tcp_max_syn_backlog = 65536
  7. net.ipv4.tcp_max_tw_buckets = 5000
  8. net.ipv4.tcp_rfc1337 = 1
  9. net.ipv4.tcp_abort_on_overflow = 0
  10. # 网络连接队列优化
  11. net.core.somaxconn = 65535
  12. net.core.netdev_max_backlog = 65536
  13. net.ipv4.tcp_max_orphans = 3276800
  14. # 网络缓冲区优化
  15. net.core.rmem_max = 16777216
  16. net.core.wmem_max = 16777216
  17. net.ipv4.tcp_rmem = 4096 87380 16777216
  18. net.ipv4.tcp_wmem = 4096 65536 16777216
  19. net.ipv4.tcp_mem = 786432 1048576 1572864
复制代码

应用配置:
  1. sysctl -p
复制代码

配置网络绑定以提高网络可用性和带宽:
  1. # 安装bonding工具
  2. yum install -y bonding-utils
  3. # 创建bonding接口配置文件
  4. cat > /etc/sysconfig/network-scripts/ifcfg-bond0 << EOF
  5. DEVICE=bond0
  6. NAME=bond0
  7. TYPE=Bond
  8. BONDING_MASTER=yes
  9. IPADDR=192.168.1.200
  10. PREFIX=24
  11. GATEWAY=192.168.1.1
  12. DNS1=8.8.8.8
  13. ONBOOT=yes
  14. BOOTPROTO=none
  15. BONDING_OPTS="mode=4 miimon=100 lacp_rate=1 xmit_hash_policy=layer3+4"
  16. EOF
  17. # 配置从接口
  18. cat > /etc/sysconfig/network-scripts/ifcfg-eth1 << EOF
  19. DEVICE=eth1
  20. NAME=eth1
  21. TYPE=Ethernet
  22. ONBOOT=yes
  23. BOOTPROTO=none
  24. MASTER=bond0
  25. SLAVE=yes
  26. EOF
  27. cat > /etc/sysconfig/network-scripts/ifcfg-eth2 << EOF
  28. DEVICE=eth2
  29. NAME=eth2
  30. TYPE=Ethernet
  31. ONBOOT=yes
  32. BOOTPROTO=none
  33. MASTER=bond0
  34. SLAVE=yes
  35. EOF
  36. # 重启网络服务
  37. systemctl restart network
复制代码

4. 存储系统优化

4.1 磁盘I/O优化

根据不同的工作负载选择合适的磁盘调度器:
  1. # 查看当前磁盘调度器
  2. cat /sys/block/sda/queue/scheduler
  3. # 临时设置调度器
  4. echo deadline > /sys/block/sda/queue/scheduler
  5. # 永久设置(编辑/etc/rc.local)
  6. echo 'echo deadline > /sys/block/sda/queue/scheduler' >> /etc/rc.local
  7. chmod +x /etc/rc.local
复制代码

调整磁盘参数以提高I/O性能:
  1. # 查看当前磁盘参数
  2. hdparm -I /dev/sda
  3. # 启用DMA
  4. hdparm -d1 /dev/sda
  5. # 设置多扇区传输
  6. hdparm -m16 /dev/sda
  7. # 设置I/O支持
  8. hdparm -c3 /dev/sda
  9. # 永久生效(编辑/etc/rc.local)
  10. echo 'hdparm -d1 -m16 -c3 /dev/sda' >> /etc/rc.local
  11. chmod +x /etc/rc.local
复制代码

4.2 逻辑卷管理(LVM)优化

创建优化的LVM配置:
  1. # 创建物理卷
  2. pvcreate /dev/sdb1
  3. # 创建卷组,设置物理扩展大小为16MB
  4. vgcreate -s 16M vg_data /dev/sdb1
  5. # 创建逻辑卷,设置条带化
  6. lvcreate -L 100G -i 2 -I 64 -n lv_data vg_data
  7. # 格式化逻辑卷
  8. mkfs.xfs /dev/vg_data/lv_data
  9. # 创建挂载点
  10. mkdir -p /data
  11. # 添加到fstab
  12. echo '/dev/vg_data/lv_data /data xfs defaults,noatime,nodiratime,largeio,inode64,swalloc 0 0' >> /etc/fstab
  13. # 挂载文件系统
  14. mount -a
复制代码

配置LVM快照以支持数据备份:
  1. # 创建快照
  2. lvcreate -L 10G -s -n lv_data_snapshot /dev/vg_data/lv_data
  3. # 挂载快照
  4. mkdir -p /mnt/snapshot
  5. mount /dev/vg_data/lv_data_snapshot /mnt/snapshot
  6. # 备份数据
  7. tar czf /backup/data_backup.tar.gz -C /mnt/snapshot .
  8. # 卸载并删除快照
  9. umount /mnt/snapshot
  10. lvremove /dev/vg_data/lv_data_snapshot
复制代码

4.3 RAID配置优化

使用mdadm配置软件RAID:
  1. # 安装mdadm
  2. yum install -y mdadm
  3. # 创建RAID 0(条带化)用于性能优化
  4. mdadm --create /dev/md0 --level=0 --raid-devices=2 /dev/sdb1 /dev/sdc1
  5. # 创建RAID 1(镜像)用于数据冗余
  6. mdadm --create /dev/md1 --level=1 --raid-devices=2 /dev/sdd1 /dev/sde1
  7. # 创建RAID 10(条带化镜像)平衡性能和冗余
  8. mdadm --create /dev/md10 --level=10 --raid-devices=4 /dev/sdf1 /dev/sdg1 /dev/sdh1 /dev/sdi1
  9. # 创建RAID配置文件
  10. mdadm --detail --scan >> /etc/mdadm.conf
  11. # 格式化RAID设备
  12. mkfs.xfs /dev/md0
  13. mkfs.xfs /dev/md1
  14. mkfs.xfs /dev/md10
  15. # 创建挂载点
  16. mkdir -p /data/raid0 /data/raid1 /data/raid10
  17. # 添加到fstab
  18. echo '/dev/md0 /data/raid0 xfs defaults,noatime,nodiratime,largeio,inode64,swalloc 0 0' >> /etc/fstab
  19. echo '/dev/md1 /data/raid1 xfs defaults,noatime,nodiratime,largeio,inode64,swalloc 0 0' >> /etc/fstab
  20. echo '/dev/md10 /data/raid10 xfs defaults,noatime,nodiratime,largeio,inode64,swalloc 0 0' >> /etc/fstab
  21. # 挂载文件系统
  22. mount -a
复制代码

调整RAID参数以优化性能:
  1. # 设置RAID条带大小(示例:256KB)
  2. echo 256 > /sys/block/md0/md/stripe_cache_size
  3. # 设置RAID读写策略
  4. echo write_through > /sys/block/md0/md/array_state
  5. # 设置最小和最大RAID重建速度
  6. echo 50000 > /proc/sys/dev/raid/speed_limit_min
  7. echo 200000 > /proc/sys/dev/raid/speed_limit_max
  8. # 永久生效(编辑/etc/rc.local)
  9. echo 'echo 256 > /sys/block/md0/md/stripe_cache_size' >> /etc/rc.local
  10. echo 'echo write_through > /sys/block/md0/md/array_state' >> /etc/rc.local
  11. echo 'echo 50000 > /proc/sys/dev/raid/speed_limit_min' >> /etc/rc.local
  12. echo 'echo 200000 > /proc/sys/dev/raid/speed_limit_max' >> /etc/rc.local
  13. chmod +x /etc/rc.local
复制代码

5. 安全配置最佳实践

5.1 系统安全加固

SELinux是RHEL的核心安全组件,正确配置SELinux可以显著提高系统安全性:
  1. # 检查SELinux状态
  2. sestatus
  3. # 设置SELinux为强制模式
  4. sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config
  5. setenforce 1
  6. # 查看SELinux布尔值
  7. getsebool -a
  8. # 设置SELinux布尔值(示例:允许HTTP连接到网络)
  9. setsebool -P httpd_can_network_connect on
  10. # 查看文件SELinux上下文
  11. ls -Z /var/www/html
  12. # 修改文件SELinux上下文
  13. chcon -R -t httpd_sys_content_t /var/www/html
  14. # 恢复默认SELinux上下文
  15. restorecon -R /var/www/html
复制代码

减少运行的服务数量,降低攻击面:
  1. # 查看运行的服务
  2. systemctl list-units --type=service --state=running
  3. # 停止并禁用不必要的服务
  4. systemctl stop postfix
  5. systemctl disable postfix
  6. systemctl stop avahi-daemon
  7. systemctl disable avahi-daemon
  8. systemctl stop cups
  9. systemctl disable cups
  10. # 查看开放端口
  11. ss -tulpn
  12. # 关闭不必要的服务端口
  13. systemctl stop rpcbind
  14. systemctl disable rpcbind
复制代码

5.2 网络安全配置

使用firewalld配置防火墙规则:
  1. # 启动并启用firewalld
  2. systemctl start firewalld
  3. systemctl enable firewalld
  4. # 查看默认区域
  5. firewall-cmd --get-default-zone
  6. # 查看活动区域
  7. firewall-cmd --get-active-zones
  8. # 查看当前规则
  9. firewall-cmd --list-all
  10. # 开放端口(示例:开放80端口)
  11. firewall-cmd --permanent --add-port=80/tcp
  12. firewall-cmd --reload
  13. # 开放服务(示例:开放HTTP服务)
  14. firewall-cmd --permanent --add-service=http
  15. firewall-cmd --reload
  16. # 配置端口转发(示例:将80端口转发到8080端口)
  17. firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080
  18. firewall-cmd --reload
  19. # 配置IP伪装(NAT)
  20. firewall-cmd --permanent --add-masquerade
  21. firewall-cmd --reload
  22. # 配置富规则(示例:允许特定IP访问特定端口)
  23. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="22" accept'
  24. firewall-cmd --reload
复制代码

使用TCP Wrappers限制网络服务访问:
  1. # 编辑/etc/hosts.allow,允许特定IP访问特定服务
  2. echo "sshd: 192.168.1.0/24" >> /etc/hosts.allow
  3. echo "vsftpd: 192.168.1.100" >> /etc/hosts.allow
  4. # 编辑/etc/hosts.deny,拒绝所有其他访问
  5. echo "ALL: ALL" >> /etc/hosts.deny
复制代码

5.3 用户与权限管理

配置用户账户安全策略:
  1. # 设置密码复杂度策略(编辑/etc/security/pwquality.conf)
  2. minlen = 12
  3. minclass = 3
  4. maxrepeat = 3
  5. dcredit = -1
  6. ucredit = -1
  7. lcredit = -1
  8. ocredit = -1
  9. # 设置密码过期策略(编辑/etc/login.defs)
  10. PASS_MAX_DAYS   90
  11. PASS_MIN_DAYS   7
  12. PASS_WARN_AGE   14
  13. # 配置用户登录失败锁定(编辑/etc/pam.d/system-auth)
  14. auth required pam_tally2.so deny=5 unlock_time=900 onerr=fail audit silent
  15. account required pam_tally2.so
  16. # 创建新用户并设置强密码
  17. useradd -m user1
  18. passwd user1
  19. # 限制用户使用su命令
  20. echo "auth required pam_wheel.so use_uid" >> /etc/pam.d/su
  21. usermod -G wheel user1
复制代码

配置文件系统权限:
  1. # 设置关键目录权限
  2. chmod 700 /root
  3. chmod 755 /bin /sbin /usr/bin /usr/sbin
  4. chmod 644 /etc/passwd
  5. chmod 600 /etc/shadow
  6. chmod 644 /etc/group
  7. chmod 600 /etc/gshadow
  8. # 查找并修复权限不正确的文件
  9. rpm -Va | grep '^.M'
  10. # 查找无主文件
  11. find / -nouser -o -nogroup
  12. # 设置默认umask
  13. echo "umask 027" >> /etc/profile
  14. echo "umask 027" >> /etc/csh.cshrc
  15. echo "umask 027" >> /etc/bashrc
复制代码

6. 高可用性与容错配置

6.1 Pacemaker与Corosync高可用集群

安装并配置Pacemaker和Corosync:
  1. # 安装高可用集群软件包
  2. yum install -y pacemaker corosync pcs fence-agents-all
  3. # 设置hacluster用户密码
  4. echo "hacluster:password" | chpasswd
  5. # 启动并启用pcsd服务
  6. systemctl start pcsd
  7. systemctl enable pcsd
  8. # 认证集群节点(在所有节点上执行)
  9. pcs cluster auth node1 node2 node3 -u hacluster -p password
  10. # 创建集群
  11. pcs cluster setup --name mycluster node1 node2 node3
  12. # 启动集群
  13. pcs cluster start --all
  14. # 启用集群自启动
  15. pcs cluster enable --all
  16. # 查看集群状态
  17. pcs status
复制代码

配置集群资源和服务:
  1. # 配置STONITH设备
  2. pcs property set stonith-enabled=true
  3. pcs stonith create fence_xvm fence_xvm pcmk_host_list="node1 node2 node3"
  4. # 配置浮动IP资源
  5. pcs resource create vip ocf:heartbeat:IPaddr2 ip=192.168.1.200 cidr_netmask=24 op monitor interval=30s
  6. # 配置文件系统资源
  7. pcs resource create fs ocf:heartbeat:Filesystem device="/dev/drbd0" directory="/data" fstype="ext4" op monitor interval=20s
  8. # 配置Apache服务资源
  9. pcs resource create httpd systemd:httpd op monitor interval=30s
  10. # 配置资源约束
  11. pcs constraint colocation add fs with vip
  12. pcs constraint order vip then fs
  13. pcs constraint colocation add httpd with fs
  14. pcs constraint order fs then httpd
复制代码

6.2 DRBD数据复制

安装并配置DRBD以实现数据复制:
  1. # 安装ELRepo仓库
  2. yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
  3. # 安装DRBD
  4. yum install -y drbd90-utils kmod-drbd90
  5. # 加载DRBD模块
  6. modprobe drbd
  7. # 配置DRBD资源(编辑/etc/drbd.d/r0.res)
  8. cat > /etc/drbd.d/r0.res << EOF
  9. resource r0 {
  10.     device    /dev/drbd0;
  11.     disk      /dev/sdb1;
  12.     meta-disk internal;
  13.     protocol  C;
  14.    
  15.     on node1 {
  16.         address   192.168.1.101:7788;
  17.     }
  18.    
  19.     on node2 {
  20.         address   192.168.1.102:7788;
  21.     }
  22. }
  23. EOF
  24. # 初始化DRBD资源
  25. drbdadm create-md r0
  26. # 启动DRBD服务
  27. systemctl start drbd
  28. # 设置主节点(在node1上执行)
  29. drbdadm primary r0 --force
  30. # 查看DRBD状态
  31. cat /proc/drbd
复制代码

将DRBD资源集成到Pacemaker集群中:
  1. # 安装DRBD Pacemaker资源代理
  2. yum install -y resource-agents
  3. # 配置DRBD资源
  4. pcs resource create drbd_r0 ocf:linbit:drbd drbd_resource=r0 op monitor interval=60s
  5. # 配置主/从资源
  6. pcs resource master ms_drbd_r0 drbd_r0 master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
  7. # 配置文件系统资源
  8. pcs resource create fs_r0 ocf:heartbeat:Filesystem device="/dev/drbd0" directory="/data" fstype="ext4"
  9. # 配置资源约束
  10. pcs constraint colocation add fs_r0 with ms_drbd_r0 INFINITY with-rsc-role=Master
  11. pcs constraint order promote ms_drbd_r0 then start fs_r0
  12. # 查看资源状态
  13. pcs status
复制代码

6.3 负载均衡配置

安装并配置HAProxy实现负载均衡:
  1. # 安装HAProxy
  2. yum install -y haproxy
  3. # 配置HAProxy(编辑/etc/haproxy/haproxy.cfg)
  4. cat > /etc/haproxy/haproxy.cfg << EOF
  5. global
  6.     log         127.0.0.1 local2
  7.     chroot      /var/lib/haproxy
  8.     pidfile     /var/run/haproxy.pid
  9.     maxconn     4000
  10.     user        haproxy
  11.     group       haproxy
  12.     daemon
  13.     stats socket /var/lib/haproxy/stats
  14. defaults
  15.     mode                    http
  16.     log                     global
  17.     option                  httplog
  18.     option                  dontlognull
  19.     option http-server-close
  20.     option forwardfor       except 127.0.0.0/8
  21.     option                  redispatch
  22.     retries                 3
  23.     timeout http-request    10s
  24.     timeout queue           1m
  25.     timeout connect         10s
  26.     timeout client          1m
  27.     timeout server          1m
  28.     timeout http-keep-alive 10s
  29.     timeout check           10s
  30.     maxconn                 3000
  31. listen stats
  32.     bind *:9000
  33.     stats enable
  34.     stats uri /stats
  35.     stats auth admin:password
  36.     stats refresh 30s
  37. frontend http-in
  38.     bind *:80
  39.     default_backend servers
  40. backend servers
  41.     balance roundrobin
  42.     server server1 192.168.1.101:80 check
  43.     server server2 192.168.1.102:80 check
  44.     server server3 192.168.1.103:80 check
  45. EOF
  46. # 启动并启用HAProxy
  47. systemctl start haproxy
  48. systemctl enable haproxy
复制代码

安装并配置Keepalived实现HAProxy的高可用性:
  1. # 安装Keepalived
  2. yum install -y keepalived
  3. # 配置Keepalived(编辑/etc/keepalived/keepalived.conf)
  4. cat > /etc/keepalived/keepalived.conf << EOF
  5. ! Configuration File for keepalived
  6. global_defs {
  7.    router_id LVS_DEVEL
  8. }
  9. vrrp_script chk_haproxy {
  10.     script "killall -0 haproxy"
  11.     interval 2
  12.     weight 2
  13. }
  14. vrrp_instance VI_1 {
  15.     state MASTER
  16.     interface eth0
  17.     virtual_router_id 51
  18.     priority 100
  19.     advert_int 1
  20.     authentication {
  21.         auth_type PASS
  22.         auth_pass password
  23.     }
  24.     virtual_ipaddress {
  25.         192.168.1.200
  26.     }
  27.     track_script {
  28.         chk_haproxy
  29.     }
  30. }
  31. EOF
  32. # 启动并启用Keepalived
  33. systemctl start keepalived
  34. systemctl enable keepalived
复制代码

7. 系统监控与维护

7.1 系统性能监控

使用top和htop命令监控系统资源使用情况:
  1. # 安装htop
  2. yum install -y htop
  3. # 使用top监控系统
  4. top -d 2
  5. # 使用htop监控系统
  6. htop
复制代码

使用vmstat命令监控系统性能:
  1. # 每隔2秒显示一次系统性能数据,共显示5次
  2. vmstat 2 5
  3. # 显示内存统计信息
  4. vmstat -s
  5. # 显示磁盘I/O统计信息
  6. vmstat -d
复制代码

使用iostat命令监控磁盘I/O性能:
  1. # 安装sysstat包
  2. yum install -y sysstat
  3. # 每隔2秒显示一次磁盘I/O数据,共显示5次
  4. iostat 2 5
  5. # 显示扩展的磁盘I/O统计信息
  6. iostat -x 2 5
  7. # 显示特定设备的I/O统计信息
  8. iostat -d /dev/sda 2 5
复制代码

7.2 日志管理

配置rsyslog服务进行日志管理:
  1. # 编辑rsyslog配置文件
  2. cat > /etc/rsyslog.d/server.conf << EOF
  3. # 将所有info级别的日志发送到远程日志服务器
  4. *.info @192.168.1.100:514
  5. # 将所有error级别的日志同时写入本地文件
  6. *.error /var/log/error.log
  7. # 将邮件相关的日志写入特定文件
  8. mail.* /var/log/maillog
  9. # 创建自定义日志模板
  10. $template RemoteLogs,"/var/log/remote/%HOSTNAME%/%PROGRAMNAME%.log"
  11. *.* ?RemoteLogs
  12. EOF
  13. # 重启rsyslog服务
  14. systemctl restart rsyslog
  15. systemctl enable rsyslog
复制代码

配置logrotate进行日志轮转:
  1. # 创建自定义logrotate配置
  2. cat > /etc/logrotate.d/custom-app << EOF
  3. /var/log/custom-app/*.log {
  4.     daily
  5.     missingok
  6.     rotate 30
  7.     compress
  8.     delaycompress
  9.     notifempty
  10.     create 644 root root
  11.     postrotate
  12.         /usr/bin/systemctl reload custom-app.service > /dev/null 2>&1 || true
  13.     endscript
  14. }
  15. EOF
  16. # 测试logrotate配置
  17. logrotate -d /etc/logrotate.d/custom-app
  18. # 强制执行logrotate
  19. logrotate -f /etc/logrotate.d/custom-app
复制代码

7.3 监控工具部署

安装并配置Nagios监控系统:
  1. # 安装EPEL仓库
  2. yum install -y epel-release
  3. # 安装Nagios
  4. yum install -y nagios nagios-plugins-all nagios-plugins-nrpe
  5. # 配置Nagios管理员密码
  6. htpasswd -c /etc/nagios/passwd nagiosadmin
  7. # 启动并启用Nagios服务
  8. systemctl start nagios
  9. systemctl enable nagios
  10. # 安装NRPE代理(在客户端)
  11. yum install -y nagios-plugins-nrpe nagios-plugins-all
  12. # 配置NRPE(编辑/etc/nagios/nrpe.cfg)
  13. echo "allowed_hosts=192.168.1.100" >> /etc/nagios/nrpe.cfg
  14. # 启动并启用NRPE服务
  15. systemctl start nrpe
  16. systemctl enable nrpe
复制代码

安装并配置Zabbix监控系统:
  1. # 安装Zabbix仓库
  2. rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
  3. # 安装Zabbix服务器和前端
  4. yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent
  5. # 安装并配置MariaDB
  6. yum install -y mariadb-server mariadb
  7. systemctl start mariadb
  8. systemctl enable mariadb
  9. # 创建Zabbix数据库
  10. mysql -e "create database zabbix character set utf8 collate utf8_bin;"
  11. mysql -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'password';"
  12. # 导入Zabbix数据库结构
  13. zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -ppassword zabbix
  14. # 配置Zabbix服务器(编辑/etc/zabbix/zabbix_server.conf)
  15. DBHost=localhost
  16. DBName=zabbix
  17. DBUser=zabbix
  18. DBPassword=password
  19. # 配置PHP时区(编辑/etc/httpd/conf.d/zabbix.conf)
  20. php_value date.timezone Asia/Shanghai
  21. # 启动并启用Zabbix服务和Apache服务
  22. systemctl start zabbix-server zabbix-agent httpd
  23. systemctl enable zabbix-server zabbix-agent httpd
  24. # 配置Zabbix客户端
  25. yum install -y zabbix-agent
  26. # 配置Zabbix客户端(编辑/etc/zabbix/zabbix_agentd.conf)
  27. Server=192.168.1.100
  28. ServerActive=192.168.1.100
  29. Hostname=client1
  30. # 启动并启用Zabbix客户端服务
  31. systemctl start zabbix-agent
  32. systemctl enable zabbix-agent
复制代码

8. 实际案例分析

8.1 高性能Web服务器配置

以下是一个高性能Web服务器的配置案例,针对高并发Web访问场景进行了优化:
  1. #!/bin/bash
  2. # 系统基础配置
  3. hostnamectl set-hostname webserver.example.com
  4. timedatectl set-timezone Asia/Shanghai
  5. # 系统更新
  6. yum update -y
  7. # 安装必要的软件包
  8. yum install -y httpd php php-mysql php-gd php-xml php-mbstring php-mcrypt
  9. # 优化内核参数
  10. cat >> /etc/sysctl.conf << EOF
  11. # 网络参数优化
  12. net.ipv4.tcp_syncookies = 1
  13. net.ipv4.tcp_tw_reuse = 1
  14. net.ipv4.tcp_fin_timeout = 30
  15. net.ipv4.tcp_keepalive_time = 1200
  16. net.ipv4.tcp_max_syn_backlog = 65536
  17. net.ipv4.tcp_max_tw_buckets = 5000
  18. net.core.somaxconn = 65535
  19. net.core.netdev_max_backlog = 65536
  20. # 文件系统优化
  21. fs.file-max = 65535
  22. vm.swappiness = 10
  23. vm.dirty_ratio = 60
  24. vm.dirty_background_ratio = 2
  25. EOF
  26. sysctl -p
  27. # 优化系统资源限制
  28. cat >> /etc/security/limits.conf << EOF
  29. * soft nofile 65535
  30. * hard nofile 65535
  31. * soft nproc 32768
  32. * hard nproc 32768
  33. EOF
  34. # 优化Apache配置
  35. cat > /etc/httpd/conf.d/optimized.conf << EOF
  36. KeepAlive On
  37. KeepAliveTimeout 5
  38. MaxKeepAliveRequests 100
  39. Timeout 30
  40. <IfModule mpm_prefork_module>
  41.     StartServers        10
  42.     MinSpareServers     10
  43.     MaxSpareServers     30
  44.     MaxClients          256
  45.     MaxRequestsPerChild 4000
  46. </IfModule>
  47. <IfModule mod_php7.c>
  48.     php_value max_execution_time 30
  49.     php_value memory_limit 256M
  50.     php_value post_max_size 32M
  51.     php_value upload_max_filesize 32M
  52. </IfModule>
  53. EOF
  54. # 优化PHP配置
  55. cat > /etc/php.d/optimized.ini << EOF
  56. memory_limit = 256M
  57. max_execution_time = 30
  58. max_input_time = 60
  59. post_max_size = 32M
  60. upload_max_filesize = 32M
  61. max_file_uploads = 20
  62. display_errors = Off
  63. log_errors = On
  64. error_log = /var/log/php/error.log
  65. EOF
  66. # 创建错误日志目录
  67. mkdir -p /var/log/php
  68. touch /var/log/php/error.log
  69. chown -R apache:apache /var/log/php
  70. # 配置防火墙
  71. firewall-cmd --permanent --add-service=http
  72. firewall-cmd --permanent --add-service=https
  73. firewall-cmd --reload
  74. # 启动并启用Apache服务
  75. systemctl start httpd
  76. systemctl enable httpd
  77. # 配置网站目录
  78. mkdir -p /var/www/html/example
  79. chown -R apache:apache /var/www/html/example
  80. chmod 755 /var/www/html/example
  81. # 创建测试页面
  82. echo "<?php phpinfo(); ?>" > /var/www/html/example/index.php
  83. # 配置虚拟主机
  84. cat > /etc/httpd/conf.d/example.conf << EOF
  85. <VirtualHost *:80>
  86.     ServerName example.com
  87.     ServerAlias www.example.com
  88.     DocumentRoot /var/www/html/example
  89.     ErrorLog /var/log/httpd/example_error.log
  90.     CustomLog /var/log/httpd/example_access.log combined
  91. </VirtualHost>
  92. EOF
  93. # 重启Apache服务
  94. systemctl restart httpd
复制代码

8.2 高性能数据库服务器配置

以下是一个高性能数据库服务器的配置案例,针对MySQL数据库进行了优化:
  1. #!/bin/bash
  2. # 系统基础配置
  3. hostnamectl set-hostname dbserver.example.com
  4. timedatectl set-timezone Asia/Shanghai
  5. # 系统更新
  6. yum update -y
  7. # 安装MySQL
  8. yum install -y mariadb-server mariadb
  9. # 优化内核参数
  10. cat >> /etc/sysctl.conf << EOF
  11. # 网络参数优化
  12. net.ipv4.tcp_syncookies = 1
  13. net.ipv4.tcp_tw_reuse = 1
  14. net.ipv4.tcp_fin_timeout = 30
  15. net.ipv4.tcp_keepalive_time = 1200
  16. net.ipv4.tcp_max_syn_backlog = 65536
  17. net.ipv4.tcp_max_tw_buckets = 5000
  18. net.core.somaxconn = 65535
  19. net.core.netdev_max_backlog = 65536
  20. # 文件系统优化
  21. fs.file-max = 65535
  22. vm.swappiness = 10
  23. vm.dirty_ratio = 60
  24. vm.dirty_background_ratio = 2
  25. # 虚拟内存优化
  26. vm.nr_hugepages = 1024
  27. EOF
  28. sysctl -p
  29. # 配置大页内存
  30. groupadd mysql
  31. usermod -a -G mysql mysql
  32. echo "vm.hugetlb_shm_group = $(grep mysql /etc/group | cut -d: -f3)" >> /etc/sysctl.conf
  33. sysctl -p
  34. # 优化系统资源限制
  35. cat >> /etc/security/limits.conf << EOF
  36. mysql soft nofile 65535
  37. mysql hard nofile 65535
  38. mysql soft nproc 32768
  39. mysql hard nproc 32768
  40. * soft memlock unlimited
  41. * hard memlock unlimited
  42. EOF
  43. # 配置磁盘I/O调度器
  44. echo 'echo deadline > /sys/block/sda/queue/scheduler' >> /etc/rc.local
  45. chmod +x /etc/rc.local
  46. echo 'echo deadline > /sys/block/sda/queue/scheduler' > /etc/rc.local
  47. # 配置文件系统
  48. mkdir -p /data/mysql
  49. mkfs.xfs /dev/sdb1
  50. echo '/dev/sdb1 /data/mysql xfs defaults,noatime,nodiratime,largeio,inode64,swalloc 0 0' >> /etc/fstab
  51. mount -a
  52. # 创建MySQL数据目录
  53. mkdir -p /data/mysql/data
  54. chown -R mysql:mysql /data/mysql
  55. chmod 755 /data/mysql
  56. # 优化MySQL配置
  57. cat > /etc/my.cnf << EOF
  58. [mysqld]
  59. # 基本配置
  60. datadir=/data/mysql/data
  61. socket=/var/lib/mysql/mysql.sock
  62. user=mysql
  63. symbolic-links=0
  64. # InnoDB优化
  65. innodb_buffer_pool_size = 8G
  66. innodb_buffer_pool_instances = 8
  67. innodb_log_file_size = 1G
  68. innodb_log_buffer_size = 64M
  69. innodb_flush_log_at_trx_commit = 2
  70. innodb_flush_method = O_DIRECT
  71. innodb_file_per_table = 1
  72. innodb_read_io_threads = 8
  73. innodb_write_io_threads = 8
  74. innodb_thread_concurrency = 0
  75. innodb_doublewrite = 1
  76. innodb_old_blocks_time = 1000
  77. innodb_open_files = 2000
  78. innodb_stats_on_metadata = 0
  79. # MyISAM优化
  80. key_buffer_size = 256M
  81. myisam_sort_buffer_size = 64M
  82. myisam_max_sort_file_size = 10G
  83. myisam_repair_threads = 1
  84. # 连接优化
  85. max_connections = 500
  86. max_connect_errors = 100000
  87. back_log = 512
  88. max_allowed_packet = 64M
  89. interactive_timeout = 28800
  90. wait_timeout = 28800
  91. # 查询优化
  92. query_cache_type = 1
  93. query_cache_size = 256M
  94. query_cache_limit = 4M
  95. sort_buffer_size = 4M
  96. read_buffer_size = 2M
  97. read_rnd_buffer_size = 4M
  98. join_buffer_size = 4M
  99. thread_cache_size = 128
  100. table_open_cache = 2000
  101. table_definition_cache = 2000
  102. # 日志配置
  103. slow_query_log = 1
  104. slow_query_log_file = /var/log/mysql/slow.log
  105. long_query_time = 2
  106. log_queries_not_using_indexes = 1
  107. log_error = /var/log/mysql/error.log
  108. # 其他配置
  109. skip-name-resolve
  110. skip-external-locking
  111. default-storage-engine = InnoDB
  112. character-set-server = utf8mb4
  113. collation-server = utf8mb4_unicode_ci
  114. max_allowed_packet = 256M
  115. [mysqld_safe]
  116. log-error=/var/log/mariadb/mariadb.log
  117. pid-file=/var/run/mariadb/mariadb.pid
  118. [mysql]
  119. default-character-set = utf8mb4
  120. [client]
  121. default-character-set = utf8mb4
  122. EOF
  123. # 创建日志目录
  124. mkdir -p /var/log/mysql
  125. touch /var/log/mysql/slow.log
  126. touch /var/log/mysql/error.log
  127. chown -R mysql:mysql /var/log/mysql
  128. # 初始化MySQL数据库
  129. mysql_install_db --datadir=/data/mysql/data --user=mysql
  130. # 启动并启用MySQL服务
  131. systemctl start mariadb
  132. systemctl enable mariadb
  133. # 安全初始化
  134. mysql_secure_installation
  135. # 配置防火墙
  136. firewall-cmd --permanent --add-service=mysql
  137. firewall-cmd --reload
复制代码

结论

企业级Red Hat Enterprise Linux实例的配置是一个复杂而细致的过程,涉及系统基础配置、性能调优、网络优化、存储管理、安全加固、高可用性配置以及系统监控等多个方面。通过本文介绍的最佳实践,系统管理员和运维工程师可以充分发挥RHEL的潜力,构建高性能、高可靠性的企业级系统环境。

在实际应用中,需要根据具体的工作负载和业务需求,灵活调整各项配置参数,并持续监控系统性能,及时进行优化调整。同时,保持系统的更新和安全加固也是确保系统长期稳定运行的重要措施。

通过遵循本文提供的最佳实践,企业可以显著提高RHEL实例的性能和可靠性,为关键业务应用提供稳定、高效的运行环境,从而提升整体业务运营效率和竞争力。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

频道订阅

频道订阅

加入社群

加入社群

联系我们|TG频道|RSS

Powered by Pixtech

© 2025 Pixtech Team.