简体中文 繁體中文 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

Rocky Linux集群环境构建实战打造高效稳定的企业服务器架构

3万

主题

317

科技点

3万

积分

大区版主

木柜子打湿

积分
31893

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

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

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

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

x
引言

在当今数字化转型的浪潮中,企业对服务器架构的稳定性、高效性和可靠性要求越来越高。Rocky Linux作为CentOS的替代品,已经成为企业级Linux发行版的首选之一。本文将详细介绍如何构建基于Rocky Linux的高效稳定集群环境,为企业提供强大的服务器架构支持。

1. Rocky Linux概述

Rocky Linux是一个社区支持的企业级操作系统,设计为与Red Hat Enterprise Linux (RHEL) 100%二进制兼容。它由CentOS的创始人Gregory Kurtzer发起,旨在填补CentOS转向CentOS Stream后留下的空白。

1.1 Rocky Linux的优势

• 稳定性:提供长期支持版本,适合企业关键应用
• 安全性:及时的安全更新和补丁
• 兼容性:与RHEL完全兼容,无需担心软件兼容性问题
• 社区支持:活跃的社区提供技术支持和持续开发
• 免费使用:无需许可费用,降低企业IT成本

2. 集群环境的基础概念

2.1 什么是集群

集群是一组相互连接的计算机,它们作为一个统一的计算资源工作,提供高可用性、负载均衡和并行处理能力。

2.2 集群的类型

• 高可用性集群(HA Cluster):确保关键应用持续可用,减少停机时间
• 负载均衡集群:分配工作负载,优化资源使用
• 高性能计算集群(HPC):用于处理复杂的计算任务
• 存储集群:提供集中式、高可用的存储解决方案

2.3 集群架构的关键组件

• 节点(Node):集群中的单个服务器
• 负载均衡器(Load Balancer):分发请求到不同节点
• 集群管理软件:如Pacemaker、Corosync等
• 共享存储:如NFS、iSCSI或分布式存储系统
• 心跳机制:监控节点健康状态
• 故障转移(Failover):在节点故障时自动切换服务

3. Rocky Linux集群环境规划

3.1 需求分析

在构建集群前,需明确以下需求:

• 应用类型:Web服务、数据库、文件服务等
• 性能要求:CPU、内存、存储、网络带宽
• 可用性要求:预期的正常运行时间百分比
• 扩展性需求:未来可能的扩展规模
• 预算限制:硬件、软件和维护成本

3.2 集群规模设计

根据需求确定集群规模:

• 小型集群:2-3个节点,适合小型企业或部门级应用
• 中型集群:4-8个节点,适合中型企业应用
• 大型集群:9个以上节点,适合大型企业或云服务提供商

3.3 拓扑结构设计

常见的集群拓扑结构:

• 主动/被动模式:一个节点提供服务,另一个作为备份
• 主动/主动模式:所有节点同时提供服务
• N层架构:前端Web服务器、中间应用服务器、后端数据库服务器分层设计

4. 硬件和网络准备

4.1 硬件要求

• CPU:64位处理器,建议使用Intel Xeon或AMD EPYC系列
• 内存:至少16GB RAM,根据应用需求调整
• 存储:SSD用于系统盘,HDD或高性能SSD用于数据存储
• 网络接口:至少双网卡,建议使用万兆网卡

• SAN存储:通过光纤通道或iSCSI连接
• NAS存储:通过NFS或SMB协议访问
• 分布式存储:如Ceph、GlusterFS等

4.2 网络配置
  1. [Internet] -> [防火墙] -> [负载均衡器] -> [集群节点]
  2.                      |
  3.                      v
  4.                 [管理网络]
复制代码

为每个节点分配多个IP地址:

• 公共IP:对外提供服务的IP地址
• 私有IP:内部通信的IP地址
• 心跳IP:专用于集群心跳检测的IP地址
• 管理IP:用于系统管理的IP地址
  1. # 编辑网络配置文件
  2. vi /etc/sysconfig/network-scripts/ifcfg-eth0
  3. # 示例配置
  4. TYPE=Ethernet
  5. BOOTPROTO=static
  6. DEFROUTE=yes
  7. NAME=eth0
  8. DEVICE=eth0
  9. ONBOOT=yes
  10. IPADDR=192.168.1.10
  11. PREFIX=24
  12. GATEWAY=192.168.1.1
  13. DNS1=8.8.8.8
  14. DNS2=8.8.4.4
  15. # 重启网络服务
  16. systemctl restart network
复制代码

5. Rocky Linux系统安装和基础配置

5.1 系统安装

从官方网站下载最新的Rocky Linux ISO镜像:
  1. wget https://download.rockylinux.org/pub/rocky/9/isos/x86_64/Rocky-9.1-x86_64-dvd.iso
复制代码

使用dd命令创建USB启动盘:
  1. # 确定USB设备名称
  2. lsblk
  3. # 创建启动盘(假设USB设备为/dev/sdb)
  4. dd if=Rocky-9.1-x86_64-dvd.iso of=/dev/sdb bs=4M status=progress
复制代码

1. 从USB启动盘启动计算机
2. 选择”Install Rocky Linux”
3. 配置语言、键盘和时区
4. 配置网络和主机名
5. 配置磁盘分区(建议使用LVM以便于扩展)
6. 设置root密码和创建用户
7. 开始安装并等待完成

5.2 系统基础配置
  1. # 更新系统软件包
  2. dnf update -y
  3. # 安装常用工具
  4. dnf install -y vim wget curl net-tools telnet
复制代码
  1. # 设置主机名
  2. hostnamectl set-hostname node1.example.com
  3. # 编辑hosts文件
  4. vi /etc/hosts
  5. # 添加以下内容
  6. 192.168.1.10   node1.example.com   node1
  7. 192.168.1.11   node2.example.com   node2
  8. 192.168.1.12   node3.example.com   node3
复制代码
  1. # 安装chrony时间同步服务
  2. dnf install -y chrony
  3. # 启动并设置开机自启
  4. systemctl start chronyd
  5. systemctl enable chronyd
  6. # 检查时间同步状态
  7. chronyc sources
复制代码
  1. # 启动防火墙
  2. systemctl start firewalld
  3. systemctl enable firewalld
  4. # 开放必要端口(以Web服务为例)
  5. firewall-cmd --permanent --add-service=http
  6. firewall-cmd --permanent --add-service=https
  7. firewall-cmd --permanent --add-service=ssh
  8. # 重新加载防火墙配置
  9. firewall-cmd --reload
复制代码
  1. # 检查SELinux状态
  2. sestatus
  3. # 临时禁用SELinux
  4. setenforce 0
  5. # 永久禁用SELinux(编辑配置文件)
  6. vi /etc/selinux/config
  7. # 将SELINUX=enforcing改为SELINUX=disabled
  8. SELINUX=disabled
复制代码

6. 集群软件安装和配置

6.1 高可用性集群软件
  1. # 安装高可用性集群软件包
  2. dnf install -y pcs pacemaker corosync fence-agents-all
  3. # 设置hacluster用户密码
  4. echo "password" | passwd --stdin hacluster
  5. # 启动pcsd服务并设置开机自启
  6. systemctl start pcsd
  7. systemctl enable pcsd
复制代码
  1. # 在所有节点上认证集群节点(只需在一个节点上执行)
  2. pcs host auth node1.example.com node2.example.com node3.example.com -u hacluster -p password
复制代码
  1. # 创建集群(只需在一个节点上执行)
  2. pcs cluster setup --name mycluster node1.example.com node2.example.com node3.example.com
  3. # 启动集群
  4. pcs cluster start --all
  5. # 设置集群开机自启
  6. pcs cluster enable --all
  7. # 检查集群状态
  8. pcs status
复制代码

6.2 负载均衡软件
  1. # 安装HAProxy
  2. dnf install -y haproxy
  3. # 配置HAProxy
  4. vi /etc/haproxy/haproxy.cfg
  5. # 基本配置示例
  6. global
  7.     log         127.0.0.1 local2
  8.     chroot      /var/lib/haproxy
  9.     pidfile     /var/run/haproxy.pid
  10.     maxconn     4000
  11.     user        haproxy
  12.     group       haproxy
  13.     daemon
  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. frontend http-in
  32.     bind *:80
  33.     default_backend servers
  34. backend servers
  35.     balance roundrobin
  36.     server node1 192.168.1.10:80 check
  37.     server node2 192.168.1.11:80 check
  38.     server node3 192.168.1.12:80 check
  39. # 启动HAProxy服务
  40. systemctl start haproxy
  41. systemctl enable haproxy
复制代码
  1. # 安装Nginx
  2. dnf install -y nginx
  3. # 配置Nginx作为负载均衡器
  4. vi /etc/nginx/nginx.conf
  5. # 添加upstream和server配置
  6. http {
  7.     upstream backend {
  8.         server 192.168.1.10:80;
  9.         server 192.168.1.11:80;
  10.         server 192.168.1.12:80;
  11.     }
  12.     server {
  13.         listen 80;
  14.         location / {
  15.             proxy_pass http://backend;
  16.         }
  17.     }
  18. }
  19. # 启动Nginx服务
  20. systemctl start nginx
  21. systemctl enable nginx
复制代码

7. 高可用性配置

7.1 配置浮动IP
  1. # 创建浮动IP资源
  2. pcs resource create ClusterIP ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s
  3. # 确保浮动IP在集群启动时启动
  4. pcs constraint colocation add ClusterIP with cluster
复制代码

7.2 配置Web服务高可用性
  1. # 在所有节点上安装Apache
  2. dnf install -y httpd
  3. # 创建测试页面
  4. echo "<h1>Node $(hostname)</h1>" > /var/www/html/index.html
  5. # 启动Apache服务
  6. systemctl start httpd
  7. systemctl enable httpd
复制代码
  1. # 创建Apache资源
  2. pcs resource create WebServer ocf:heartbeat:apache configfile=/etc/httpd/conf/httpd.conf statusurl="http://localhost/server-status" op monitor interval=30s
  3. # 设置资源约束
  4. pcs constraint colocation add WebServer with ClusterIP INFINITY
  5. pcs constraint order ClusterIP then WebServer
复制代码

7.3 配置STONITH设备

STONITH (Shoot The Other Node In The Head) 是一种确保数据完整性的机制,当节点发生故障时,它会强制重启或关闭故障节点。
  1. # 配置fence_xvm设备(示例)
  2. pcs stonith create vm-fence fence_xvm pcmk_host_map="node1.example.com:node1;node2.example.com:node2;node3.example.com:node3" op monitor interval=60s
  3. # 启用STONITH
  4. pcs property set stonith-enabled=true
复制代码

8. 负载均衡设置

8.1 配置HAProxy高可用性
  1. # 安装HAProxy(如果尚未安装)
  2. dnf install -y haproxy
  3. # 配置HAProxy
  4. vi /etc/haproxy/haproxy.cfg
  5. # 添加以下配置
  6. global
  7.     log         127.0.0.1 local2
  8.     chroot      /var/lib/haproxy
  9.     pidfile     /var/run/haproxy.pid
  10.     maxconn     4000
  11.     user        haproxy
  12.     group       haproxy
  13.     daemon
  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 refresh 30s
  36.     stats show-node
  37.     stats auth admin:password
  38. frontend http-in
  39.     bind *:80
  40.     default_backend servers
  41. backend servers
  42.     balance roundrobin
  43.     cookie SERVERID insert indirect nocache
  44.     server node1 192.168.1.10:80 check cookie node1
  45.     server node2 192.168.1.11:80 check cookie node2
  46.     server node3 192.168.1.12:80 check cookie node3
  47. # 创建HAProxy资源
  48. pcs resource create HAProxy systemd:haproxy op monitor interval=20s
  49. # 设置资源约束
  50. pcs constraint colocation add HAProxy with ClusterIP
  51. pcs constraint order ClusterIP then HAProxy
复制代码

8.2 配置Keepalived实现VIP高可用
  1. # 安装Keepalived
  2. dnf install -y keepalived
  3. # 配置Keepalived
  4. vi /etc/keepalived/keepalived.conf
  5. # 主节点配置示例
  6. vrrp_script chk_haproxy {
  7.     script "killall -0 haproxy"
  8.     interval 2
  9.     weight 2
  10. }
  11. vrrp_instance VI_1 {
  12.     state MASTER
  13.     interface eth0
  14.     virtual_router_id 51
  15.     priority 101
  16.     advert_int 1
  17.     authentication {
  18.         auth_type PASS
  19.         auth_pass password
  20.     }
  21.     virtual_ipaddress {
  22.         192.168.1.100/24 dev eth0
  23.     }
  24.     track_script {
  25.         chk_haproxy
  26.     }
  27. }
  28. # 备节点配置示例(priority值较低)
  29. vrrp_instance VI_1 {
  30.     state BACKUP
  31.     interface eth0
  32.     virtual_router_id 51
  33.     priority 100
  34.     advert_int 1
  35.     authentication {
  36.         auth_type PASS
  37.         auth_pass password
  38.     }
  39.     virtual_ipaddress {
  40.         192.168.1.100/24 dev eth0
  41.     }
  42.     track_script {
  43.         chk_haproxy
  44.     }
  45. }
  46. # 启动Keepalived服务
  47. systemctl start keepalived
  48. systemctl enable keepalived
复制代码

9. 存储解决方案

9.1 配置NFS共享存储
  1. # 安装NFS服务器
  2. dnf install -y nfs-utils
  3. # 创建共享目录
  4. mkdir -p /data/shared
  5. chmod 777 /data/shared
  6. # 配置NFS共享
  7. vi /etc/exports
  8. # 添加以下内容
  9. /data/shared 192.168.1.0/24(rw,sync,no_root_squash)
  10. # 启动NFS服务
  11. systemctl start nfs-server
  12. systemctl enable nfs-server
  13. # 导出共享目录
  14. exportfs -a
复制代码
  1. # 安装NFS客户端
  2. dnf install -y nfs-utils
  3. # 创建挂载点
  4. mkdir -p /mnt/nfs
  5. # 挂载NFS共享
  6. mount 192.168.1.100:/data/shared /mnt/nfs
  7. # 添加到fstab实现开机自动挂载
  8. echo "192.168.1.100:/data/shared /mnt/nfs nfs defaults 0 0" >> /etc/fstab
复制代码

9.2 配置iSCSI共享存储
  1. # 安装iSCSI目标软件
  2. dnf install -y targetcli
  3. # 启动并设置开机自启
  4. systemctl start target
  5. systemctl enable target
  6. # 配置iSCSI目标
  7. targetcli
  8. # 创建后端存储
  9. /backstores/block create disk1 /dev/sdb1
  10. # 创建iSCSI目标
  11. /iscsi create iqn.2023-01.com.example:storage.disk1
  12. # 创建LUN
  13. /iscsi/iqn.2023-01.com.example:storage.disk1/tpg1/luns create /backstores/block/disk1
  14. # 设置ACL
  15. /iscsi/iqn.2023-01.com.example:storage.disk1/tpg1/acls create iqn.2023-01.com.example:client
  16. # 保存配置
  17. saveconfig
  18. exit
复制代码
  1. # 安装iSCSI发起端软件
  2. dnf install -y iscsi-initiator-utils
  3. # 配置发起端名称
  4. vi /etc/iscsi/initiatorname.iscsi
  5. # 设置为与目标服务器ACL匹配的名称
  6. InitiatorName=iqn.2023-01.com.example:client
  7. # 启动并设置开机自启
  8. systemctl start iscsid
  9. systemctl enable iscsid
  10. # 发现目标
  11. iscsiadm -m discovery -t st -p 192.168.1.100
  12. # 登录目标
  13. iscsiadm -m node -l
  14. # 查看新发现的磁盘
  15. lsblk
  16. # 分区并格式化新磁盘
  17. fdisk /dev/sdb
  18. mkfs.ext4 /dev/sdb1
  19. # 挂载新磁盘
  20. mkdir -p /mnt/iscsi
  21. mount /dev/sdb1 /mnt/iscsi
  22. # 添加到fstab实现开机自动挂载
  23. echo "/dev/sdb1 /mnt/iscsi ext4 defaults,_netdev 0 0" >> /etc/fstab
复制代码

9.3 配置Ceph分布式存储
  1. # 安装Ceph部署工具
  2. dnf install -y cephadm
  3. # 配置Ceph仓库
  4. cephadm add-repo --release pacific
  5. # 安装Ceph Common
  6. dnf install -y ceph-common
复制代码
  1. # 引导Ceph集群
  2. cephadm bootstrap --mon-ip 192.168.1.10
  3. # 安装Ceph CLI工具
  4. cephadm install ceph-common
  5. # 添加其他节点到集群
  6. ceph orch host add node2 192.168.1.11
  7. ceph orch host add node3 192.168.1.12
  8. # 部署OSD(假设使用/dev/sdb作为OSD磁盘)
  9. ceph orch daemon add osd node1:/dev/sdb
  10. ceph orch daemon add osd node2:/dev/sdb
  11. ceph orch daemon add osd node3:/dev/sdb
  12. # 创建Ceph池
  13. ceph osd pool create mypool 64 64
  14. # 创建Ceph文件系统
  15. ceph fs new myfs myfs_metadata myfs_data
  16. # 挂载Ceph文件系统
  17. mkdir -p /mnt/cephfs
  18. mount -t ceph 192.168.1.10:6789:/ /mnt/cephfs
  19. # 添加到fstab实现开机自动挂载
  20. echo "192.168.1.10:6789:/ /mnt/cephfs ceph name=admin,secretfile=/etc/ceph/secret.key,noatime,_netdev 0 0" >> /etc/fstab
复制代码

10. 监控和维护

10.1 安装和配置Zabbix监控系统
  1. # 安装Zabbix仓库
  2. rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/9/x86_64/zabbix-release-5.0-1.el9.noarch.rpm
  3. dnf clean all
  4. # 安装Zabbix服务器、前端和代理
  5. dnf install -y zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-sql-scripts zabbix-agent
  6. # 安装MariaDB数据库
  7. dnf install -y mariadb-server mariadb
  8. # 启动MariaDB并设置开机自启
  9. systemctl start mariadb
  10. systemctl enable mariadb
  11. # 配置MariaDB
  12. mysql_secure_installation
  13. # 创建Zabbix数据库和用户
  14. mysql -u root -p
  15. create database zabbix character set utf8 collate utf8_bin;
  16. create user zabbix@localhost identified by 'password';
  17. grant all privileges on zabbix.* to zabbix@localhost;
  18. quit;
  19. # 导入Zabbix数据库架构
  20. zcat /usr/share/doc/zabbix-sql-scripts/mysql/create.sql.gz | mysql -uzabbix -p zabbix
  21. # 配置Zabbix服务器
  22. vi /etc/zabbix/zabbix_server.conf
  23. # 设置数据库密码
  24. DBPassword=password
  25. # 启动Zabbix服务器和代理
  26. systemctl restart zabbix-server zabbix-agent httpd php-fpm
  27. systemctl enable zabbix-server zabbix-agent httpd php-fpm
复制代码

1. 访问http://zabbix-server-ip/zabbix
2. 按照安装向导完成前端配置
3. 默认用户名:Admin,密码:zabbix
  1. # 在所有集群节点上安装Zabbix代理
  2. rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/9/x86_64/zabbix-release-5.0-1.el9.noarch.rpm
  3. dnf clean all
  4. dnf install -y zabbix-agent
  5. # 配置Zabbix代理
  6. vi /etc/zabbix/zabbix_agentd.conf
  7. # 设置服务器IP
  8. Server=192.168.1.100
  9. ServerActive=192.168.1.100
  10. Hostname=node1.example.com
  11. # 启动Zabbix代理
  12. systemctl start zabbix-agent
  13. systemctl enable zabbix-agent
复制代码

10.2 安装和配置Prometheus和Grafana
  1. # 创建Prometheus用户
  2. useradd --no-create-home --shell /bin/false prometheus
  3. # 下载Prometheus
  4. wget https://github.com/prometheus/prometheus/releases/download/v2.36.2/prometheus-2.36.2.linux-amd64.tar.gz
  5. tar -xvzf prometheus-2.36.2.linux-amd64.tar.gz
  6. # 移动文件到合适位置
  7. mkdir -p /etc/prometheus /var/lib/prometheus
  8. cp prometheus-2.36.2.linux-amd64/prometheus /usr/local/bin/
  9. cp prometheus-2.36.2.linux-amd64/promtool /usr/local/bin/
  10. cp -r prometheus-2.36.2.linux-amd64/console* /etc/prometheus/
  11. cp -r prometheus-2.36.2.linux-amd64/prometheus.yml /etc/prometheus/
  12. # 设置权限
  13. chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
  14. chown prometheus:prometheus /usr/local/bin/prometheus
  15. chown prometheus:prometheus /usr/local/bin/promtool
  16. # 创建systemd服务文件
  17. vi /etc/systemd/system/prometheus.service
  18. # 添加以下内容
  19. [Unit]
  20. Description=Prometheus
  21. Wants=network-online.target
  22. After=network-online.target
  23. [Service]
  24. User=prometheus
  25. Group=prometheus
  26. Type=simple
  27. ExecStart=/usr/local/bin/prometheus \
  28.     --config.file /etc/prometheus/prometheus.yml \
  29.     --storage.tsdb.path /var/lib/prometheus/ \
  30.     --web.console.templates=/etc/prometheus/consoles \
  31.     --web.console.libraries=/etc/prometheus/console_libraries
  32. [Install]
  33. WantedBy=multi-user.target
  34. # 启动Prometheus服务
  35. systemctl start prometheus
  36. systemctl enable prometheus
复制代码
  1. # 在所有集群节点上安装Node Exporter
  2. useradd --no-create-home --shell /bin/false node_exporter
  3. wget https://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gz
  4. tar -xvzf node_exporter-1.3.1.linux-amd64.tar.gz
  5. cp node_exporter-1.3.1.linux-amd64/node_exporter /usr/local/bin/
  6. chown -R node_exporter:node_exporter /usr/local/bin/node_exporter
  7. # 创建systemd服务文件
  8. vi /etc/systemd/system/node_exporter.service
  9. # 添加以下内容
  10. [Unit]
  11. Description=Node Exporter
  12. After=network.target
  13. [Service]
  14. User=node_exporter
  15. Group=node_exporter
  16. Type=simple
  17. ExecStart=/usr/local/bin/node_exporter
  18. [Install]
  19. WantedBy=multi-user.target
  20. # 启动Node Exporter服务
  21. systemctl start node_exporter
  22. systemctl enable node_exporter
复制代码
  1. # 编辑Prometheus配置文件
  2. vi /etc/prometheus/prometheus.yml
  3. # 添加以下内容到scrape_configs部分
  4.   - job_name: 'cluster_nodes'
  5.     static_configs:
  6.       - targets: ['node1.example.com:9100']
  7.       - targets: ['node2.example.com:9100']
  8.       - targets: ['node3.example.com:9100']
  9. # 重启Prometheus服务
  10. systemctl restart prometheus
复制代码
  1. # 安装Grafana仓库
  2. dnf install -y grafana
  3. # 启动Grafana服务
  4. systemctl start grafana-server
  5. systemctl enable grafana-server
  6. # 配置防火墙
  7. firewall-cmd --permanent --add-port=3000/tcp
  8. firewall-cmd --reload
  9. # 访问Grafana Web界面(http://grafana-server-ip:3000)
  10. # 默认用户名:admin,密码:admin
  11. # 添加Prometheus数据源
  12. 1. 登录Grafana
  13. 2. 进入Configuration > Data Sources
  14. 3. 点击Add data source
  15. 4. 选择Prometheus
  16. 5. 设置URL为http://prometheus-server-ip:9090
  17. 6. 点击Save & Test
  18. # 导入Node Exporter仪表板
  19. 1. 进入Dashboards > Import
  20. 2. 输入仪表板ID:1860
  21. 3. 点击Load
  22. 4. 选择Prometheus数据源
  23. 5. 点击Import
复制代码

11. 安全性考虑

11.1 系统安全加固
  1. # 编辑SSH配置文件
  2. vi /etc/ssh/sshd_config
  3. # 修改以下配置
  4. PermitRootLogin no
  5. PasswordAuthentication no
  6. Port 2222
  7. AllowUsers adminuser
  8. # 重启SSH服务
  9. systemctl restart sshd
复制代码
  1. # 配置防火墙规则
  2. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="ssh" accept'
  3. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="http" accept'
  4. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" service name="https" accept'
  5. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="5405" accept'
  6. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="udp" port="5404" accept'
  7. firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="21064" accept'
  8. firewall-cmd --reload
复制代码
  1. # 安装Fail2ban
  2. dnf install -y fail2ban
  3. # 创建配置文件
  4. cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  5. # 编辑配置文件
  6. vi /etc/fail2ban/jail.local
  7. # 添加以下内容
  8. [sshd]
  9. enabled = true
  10. port = 2222
  11. filter = sshd
  12. logpath = /var/log/secure
  13. maxretry = 3
  14. bantime = 3600
  15. # 启动Fail2ban服务
  16. systemctl start fail2ban
  17. systemctl enable fail2ban
复制代码

11.2 集群安全配置
  1. # 生成Corosync密钥
  2. corosync-keygen
  3. # 复制密钥到所有节点
  4. scp /etc/corosync/authkey node2.example.com:/etc/corosync/
  5. scp /etc/corosync/authkey node3.example.com:/etc/corosync/
  6. # 设置正确的权限
  7. chmod 400 /etc/corosync/authkey
复制代码
  1. # 设置Pacemaker属性
  2. pcs property set stonith-enabled=true
  3. pcs property set no-quorum-policy=stop
  4. pcs property set symmetric-cluster=true
  5. pcs property set default-resource-stickiness=100
复制代码

12. 实战案例

12.1 Web服务器集群案例

构建一个高可用的Web服务器集群,包含3个节点,使用Apache作为Web服务器,HAProxy作为负载均衡器,NFS作为共享存储。
  1. [Internet] -> [防火墙] -> [HAProxy (VIP: 192.168.1.100)] -> [Web服务器集群]
  2.                                                     |
  3.                                                     v
  4.                                                [NFS共享存储]
复制代码

1. 系统准备
  1. # 在所有节点上更新系统
  2. dnf update -y
  3. # 安装必要软件包
  4. dnf install -y vim wget curl net-tools telnet
  5. # 配置主机名和hosts文件
  6. hostnamectl set-hostname node1.example.com
  7. echo "192.168.1.10   node1.example.com   node1" >> /etc/hosts
  8. echo "192.168.1.11   node2.example.com   node2" >> /etc/hosts
  9. echo "192.168.1.12   node3.example.com   node3" >> /etc/hosts
  10. # 配置时间同步
  11. dnf install -y chrony
  12. systemctl start chronyd
  13. systemctl enable chronyd
复制代码

1. 安装和配置集群软件
  1. # 在所有节点上安装集群软件
  2. dnf install -y pcs pacemaker corosync fence-agents-all
  3. # 设置hacluster用户密码
  4. echo "password" | passwd --stdin hacluster
  5. # 启动pcsd服务
  6. systemctl start pcsd
  7. systemctl enable pcsd
  8. # 在node1上认证集群节点
  9. pcs host auth node1.example.com node2.example.com node3.example.com -u hacluster -p password
  10. # 创建集群
  11. pcs cluster setup --name webcluster node1.example.com node2.example.com node3.example.com
  12. # 启动集群
  13. pcs cluster start --all
  14. pcs cluster enable --all
复制代码

1. 配置浮动IP
  1. # 创建浮动IP资源
  2. pcs resource create WebVIP ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s
复制代码

1. 安装和配置Web服务器
  1. # 在所有节点上安装Apache
  2. dnf install -y httpd
  3. # 创建测试页面
  4. mkdir -p /var/www/html
  5. echo "<h1>Web Server Cluster</h1>" > /var/www/html/index.html
  6. # 启动Apache服务
  7. systemctl start httpd
  8. systemctl enable httpd
复制代码

1. 配置Apache为集群资源
  1. # 创建Apache资源
  2. pcs resource create WebServer ocf:heartbeat:apache configfile=/etc/httpd/conf/httpd.conf statusurl="http://localhost/server-status" op monitor interval=30s
  3. # 设置资源约束
  4. pcs constraint colocation add WebServer with WebVIP INFINITY
  5. pcs constraint order WebVIP then WebServer
复制代码

1. 安装和配置NFS共享存储
  1. # 在专用存储服务器上安装NFS
  2. dnf install -y nfs-utils
  3. # 创建共享目录
  4. mkdir -p /data/web
  5. chmod 777 /data/web
  6. # 配置NFS共享
  7. echo "/data/web 192.168.1.0/24(rw,sync,no_root_squash)" >> /etc/exports
  8. # 启动NFS服务
  9. systemctl start nfs-server
  10. systemctl enable nfs-server
  11. exportfs -a
复制代码

1. 在Web服务器节点上挂载NFS共享
  1. # 在所有Web服务器节点上安装NFS客户端
  2. dnf install -y nfs-utils
  3. # 创建挂载点
  4. mkdir -p /var/www/html
  5. # 挂载NFS共享
  6. mount 192.168.1.100:/data/web /var/www/html
  7. # 添加到fstab
  8. echo "192.168.1.100:/data/web /var/www/html nfs defaults,_netdev 0 0" >> /etc/fstab
复制代码

1. 安装和配置HAProxy
  1. # 在专用负载均衡器节点上安装HAProxy
  2. dnf install -y haproxy
  3. # 配置HAProxy
  4. vi /etc/haproxy/haproxy.cfg
  5. # 添加以下配置
  6. global
  7.     log         127.0.0.1 local2
  8.     chroot      /var/lib/haproxy
  9.     pidfile     /var/run/haproxy.pid
  10.     maxconn     4000
  11.     user        haproxy
  12.     group       haproxy
  13.     daemon
  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 refresh 30s
  36.     stats show-node
  37.     stats auth admin:password
  38. frontend http-in
  39.     bind *:80
  40.     default_backend servers
  41. backend servers
  42.     balance roundrobin
  43.     cookie SERVERID insert indirect nocache
  44.     server node1 192.168.1.10:80 check cookie node1
  45.     server node2 192.168.1.11:80 check cookie node2
  46.     server node3 192.168.1.12:80 check cookie node3
  47. # 启动HAProxy服务
  48. systemctl start haproxy
  49. systemctl enable haproxy
复制代码

1. 测试集群
  1. # 检查集群状态
  2. pcs status
  3. # 测试Web服务
  4. curl http://192.168.1.100
  5. # 模拟节点故障
  6. pcs node standby node1.example.com
  7. # 再次测试Web服务
  8. curl http://192.168.1.100
  9. # 恢复节点
  10. pcs node unstandby node1.example.com
复制代码

12.2 数据库集群案例

构建一个高可用的MySQL数据库集群,包含3个节点,使用Galera Cluster进行多主复制,HAProxy作为负载均衡器。
  1. [应用服务器] -> [HAProxy (VIP: 192.168.1.100)] -> [MySQL Galera集群]
  2.                                                 (node1, node2, node3)
复制代码

1. 系统准备
  1. # 在所有节点上更新系统
  2. dnf update -y
  3. # 安装必要软件包
  4. dnf install -y vim wget curl net-tools telnet
  5. # 配置主机名和hosts文件
  6. hostnamectl set-hostname dbnode1.example.com
  7. echo "192.168.1.10   dbnode1.example.com   dbnode1" >> /etc/hosts
  8. echo "192.168.1.11   dbnode2.example.com   dbnode2" >> /etc/hosts
  9. echo "192.168.1.12   dbnode3.example.com   dbnode3" >> /etc/hosts
  10. # 配置时间同步
  11. dnf install -y chrony
  12. systemctl start chronyd
  13. systemctl enable chronyd
复制代码

1. 安装MariaDB和Galera
  1. # 在所有节点上安装MariaDB和Galera
  2. dnf install -y mariadb-server mariadb-client galera
  3. # 启动MariaDB服务
  4. systemctl start mariadb
  5. systemctl enable mariadb
  6. # 运行安全安装脚本
  7. mysql_secure_installation
复制代码

1. 配置Galera集群
  1. # 在所有节点上创建Galera配置文件
  2. vi /etc/my.cnf.d/galera.cnf
  3. # 添加以下内容
  4. [mysqld]
  5. binlog_format=ROW
  6. default-storage-engine=innodb
  7. innodb_autoinc_lock_mode=2
  8. bind-address=0.0.0.0
  9. # Galera Provider Configuration
  10. wsrep_on=ON
  11. wsrep_provider=/usr/lib64/galera-4/libgalera_smm.so
  12. # Galera Cluster Configuration
  13. wsrep_cluster_name="my_galera_cluster"
  14. wsrep_cluster_address="gcomm://dbnode1.example.com,dbnode2.example.com,dbnode3.example.com"
  15. # Galera Synchronization Configuration
  16. wsrep_sst_method=rsync
  17. # Galera Node Configuration
  18. wsrep_node_address="dbnode1.example.com"  # 在每个节点上使用对应的主机名
  19. wsrep_node_name="dbnode1"  # 在每个节点上使用对应的节点名
复制代码

1. 启动集群
  1. # 在第一个节点上启动集群
  2. systemctl stop mariadb
  3. galera_new_cluster
  4. # 在其他节点上启动MariaDB
  5. systemctl start mariadb
  6. # 检查集群状态
  7. mysql -u root -p -e "SHOW STATUS LIKE 'wsrep_cluster_size'"
复制代码

1. 安装和配置HAProxy
  1. # 在专用负载均衡器节点上安装HAProxy
  2. dnf install -y haproxy
  3. # 配置HAProxy
  4. vi /etc/haproxy/haproxy.cfg
  5. # 添加以下配置
  6. global
  7.     log         127.0.0.1 local2
  8.     chroot      /var/lib/haproxy
  9.     pidfile     /var/run/haproxy.pid
  10.     maxconn     4000
  11.     user        haproxy
  12.     group       haproxy
  13.     daemon
  14. defaults
  15.     mode                    tcp
  16.     log                     global
  17.     option                  httplog
  18.     option                  dontlognull
  19.     option                  redispatch
  20.     retries                 3
  21.     timeout http-request    10s
  22.     timeout queue           1m
  23.     timeout connect         10s
  24.     timeout client          1m
  25.     timeout server          1m
  26.     timeout check           10s
  27.     maxconn                 3000
  28. listen stats
  29.     bind *:9000
  30.     stats enable
  31.     stats uri /stats
  32.     stats refresh 30s
  33.     stats show-node
  34.     stats auth admin:password
  35. listen mysql-cluster
  36.     bind *:3306
  37.     mode tcp
  38.     balance roundrobin
  39.     option mysql-check user haproxy_check
  40.     server dbnode1 192.168.1.10:3306 check
  41.     server dbnode2 192.168.1.11:3306 check
  42.     server dbnode3 192.168.1.12:3306 check
  43. # 启动HAProxy服务
  44. systemctl start haproxy
  45. systemctl enable haproxy
复制代码

1. 创建监控用户
  1. # 在所有MySQL节点上创建监控用户
  2. mysql -u root -p
  3. CREATE USER 'haproxy_check'@'%';
  4. FLUSH PRIVILEGES;
  5. quit;
复制代码

1. 测试集群
  1. # 测试数据库连接
  2. mysql -h 192.168.1.100 -u root -p
  3. # 创建测试数据库和表
  4. CREATE DATABASE testdb;
  5. USE testdb;
  6. CREATE TABLE testtable (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50));
  7. INSERT INTO testtable (name) VALUES ('test');
  8. # 在另一个节点上验证数据复制
  9. mysql -h 192.168.1.11 -u root -p
  10. USE testdb;
  11. SELECT * FROM testtable;
  12. # 模拟节点故障
  13. systemctl stop mariadb
  14. # 测试高可用性
  15. mysql -h 192.168.1.100 -u root -p
  16. USE testdb;
  17. INSERT INTO testtable (name) VALUES ('test2');
  18. # 恢复节点
  19. systemctl start mariadb
  20. # 验证数据同步
  21. mysql -h 192.168.1.10 -u root -p
  22. USE testdb;
  23. SELECT * FROM testtable;
复制代码

13. 总结和最佳实践

13.1 总结

本文详细介绍了如何构建基于Rocky Linux的高效稳定集群环境,涵盖了从系统安装、集群配置、高可用性设置到负载均衡和存储解决方案的各个方面。通过实战案例,我们展示了Web服务器集群和数据库集群的具体实施步骤,为企业构建高效稳定的服务器架构提供了全面指导。

13.2 最佳实践

1. 规划先行:在构建集群前,充分了解需求,进行详细规划,包括硬件、网络、软件和存储等方面。
2. 安全第一:始终将安全性放在首位,包括系统加固、网络安全配置和数据加密等。
3. 监控完备:建立全面的监控系统,实时监控集群状态,及时发现和解决问题。
4. 文档记录:详细记录集群配置、变更和故障处理过程,便于后续维护和问题排查。
5. 定期备份:制定完善的备份策略,定期备份关键数据和配置文件。
6. 测试验证:对集群配置进行充分测试,包括功能测试、性能测试和故障恢复测试。
7. 版本控制:对配置文件使用版本控制系统,便于追踪变更和回滚。
8. 持续优化:根据实际运行情况,持续优化集群配置,提高性能和稳定性。
9. 团队培训:确保团队成员熟悉集群架构和管理流程,提高运维效率。
10. 社区参与:积极参与Rocky Linux和相关软件的社区,获取最新信息和技术支持。

规划先行:在构建集群前,充分了解需求,进行详细规划,包括硬件、网络、软件和存储等方面。

安全第一:始终将安全性放在首位,包括系统加固、网络安全配置和数据加密等。

监控完备:建立全面的监控系统,实时监控集群状态,及时发现和解决问题。

文档记录:详细记录集群配置、变更和故障处理过程,便于后续维护和问题排查。

定期备份:制定完善的备份策略,定期备份关键数据和配置文件。

测试验证:对集群配置进行充分测试,包括功能测试、性能测试和故障恢复测试。

版本控制:对配置文件使用版本控制系统,便于追踪变更和回滚。

持续优化:根据实际运行情况,持续优化集群配置,提高性能和稳定性。

团队培训:确保团队成员熟悉集群架构和管理流程,提高运维效率。

社区参与:积极参与Rocky Linux和相关软件的社区,获取最新信息和技术支持。

通过遵循这些最佳实践,企业可以构建出高效稳定、安全可靠的Rocky Linux集群环境,为业务发展提供强有力的IT基础设施支持。
回复

使用道具 举报

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

本版积分规则

频道订阅

频道订阅

加入社群

加入社群

联系我们|TG频道|RSS

Powered by Pixtech

© 2025 Pixtech Team.