CentOS环境下3台服务器集群搭建与Python编程实现高可用性配置

在现代IT架构中,高可用性(High Availability, HA)是确保服务连续性和数据完整性的关键因素。本文将详细介绍如何在CentOS环境下搭建一个由3台服务器组成的集群,并通过Python编程实现高可用性配置。我们将涵盖从环境准备到集群搭建,再到高可用性实现的完整过程。

一、环境准备

1. 硬件与软件要求
  • 硬件:3台服务器(物理机或虚拟机)
  • 操作系统:CentOS 7.9
  • 软件:Python 3.x、Nginx、Keepalived、HAProxy
2. 服务器配置

每台服务器需要进行以下基础配置:

    安装CentOS 7.9:确保每台服务器都安装了CentOS 7.9操作系统。

    网络配置:为每台服务器分配静态IP地址,并确保网络连通性。

    主机名设置:分别为三台服务器设置主机名,例如node1node2node3

    关闭防火墙和SELinux

    systemctl stop firewalld
    systemctl disable firewalld
    setenforce 0
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    

    更新系统

    yum update -y
    

二、集群搭建

1. 安装Nginx

Nginx将作为负载均衡器,分发请求到后端服务器。

yum install nginx -y
systemctl start nginx
systemctl enable nginx
2. 安装Keepalived

Keepalived用于实现Nginx的高可用性。

yum install keepalived -y
3. 配置HAProxy

HAProxy作为另一层负载均衡,增加系统的冗余性。

yum install haproxy -y
4. 配置Nginx负载均衡

编辑Nginx配置文件/etc/nginx/nginx.conf,添加以下内容:

http {
    upstream backend {
        server node1:80;
        server node2:80;
        server node3:80;
    }

    server {
        listen 80;
        location / {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}
5. 配置Keepalived

node1node2上分别配置Keepalived。编辑/etc/keepalived/keepalived.conf

node1配置

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

node2配置

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.100
    }
}

启动Keepalived:

systemctl start keepalived
systemctl enable keepalived
6. 配置HAProxy

编辑/etc/haproxy/haproxy.cfg

frontend http-in
    bind *:80
    default_backend servers

backend servers
    balance roundrobin
    server node1 192.168.1.1:80 check
    server node2 192.168.1.2:80 check
    server node3 192.168.1.3:80 check

启动HAProxy:

systemctl start haproxy
systemctl enable haproxy

三、Python编程实现高可用性配置

1. 安装Python环境

确保每台服务器上都安装了Python 3.x:

yum install python3 -y
2. 编写监控脚本

使用Python编写一个简单的监控脚本,用于检查Nginx和HAProxy的状态,并在服务异常时进行重启。

#!/usr/bin/env python3
import subprocess
import time

def check_service(service):
    result = subprocess.run(['systemctl', 'is-active', service], stdout=subprocess.PIPE)
    return result.stdout.decode('utf-8').strip()

def restart_service(service):
    subprocess.run(['systemctl', 'restart', service])

services = ['nginx', 'haproxy']

while True:
    for service in services:
        if check_service(service) != 'active':
            print(f"{service} is not running, restarting...")
            restart_service(service)
    time.sleep(60)

将脚本保存为monitor.py,并设置为可执行:

chmod +x monitor.py

使用Cronjob或Systemd定时任务来运行此脚本。

3. 配置Systemd定时任务

创建一个Systemd服务文件/etc/systemd/system/monitor.service

[Unit]
Description=Monitor and restart services

[Service]
ExecStart=/path/to/monitor.py
Restart=always
User=root

[Install]
WantedBy=multi-user.target

启动并启用服务:

systemctl start monitor
systemctl enable monitor

四、测试与验证

  1. 测试Nginx负载均衡:访问虚拟IP 192.168.1.100,确保请求被分发到不同的后端服务器。
  2. 测试Keepalived高可用性:停止node1上的Keepalived服务,确保node2接管虚拟IP。
  3. 测试HAProxy负载均衡:通过HAProxy的监控界面或日志,验证请求是否均匀分发。
  4. 测试Python监控脚本:手动停止Nginx或HAProxy服务,观察监控脚本是否能够自动重启服务。

五、总结

通过本文的步骤,我们成功在CentOS环境下搭建了一个由3台服务器组成的集群,并通过Nginx、Keepalived和HAProxy实现了负载均衡和高可用性。此外,利用Python编程进一步增强了系统的自愈能力,确保服务的高可用性。这种方法不仅提高了系统的可靠性,还为后续的扩展和维护提供了坚实的基础。

希望这篇文章能为你搭建高可用性集群提供有价值的参考。如果有任何问题或需要进一步的优化,欢迎交流和探讨!