在CentOS Minimal上實現負載均衡可以通過多種方法,以下是使用Nginx和HAProxy兩種常見工具的配置示例。
首先,安裝Nginx。由于Nginx軟件包在CentOS系統默認倉庫里面沒有,需要安裝epel倉庫:
sudo dnf install epel-release -y
sudo dnf install nginx -y
允許訪問Nginx的HTTP和HTTPS服務:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
編輯Nginx配置文件,通常位于 /etc/nginx/nginx.conf 或 /etc/nginx/conf.d/default.conf。在 http 塊中添加以下內容:
upstream backend {
server 192.168.1.41:80;
server 192.168.1.42:80;
}
server {
listen 80;
server_name yourdomain.com;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
保存配置文件并退出編輯器,然后測試配置文件是否正確:
sudo nginx -t
如果沒有錯誤,重新加載Nginx服務:
sudo systemctl restart nginx
使用yum安裝HAProxy:
sudo yum install haproxy -y
編輯HAProxy配置文件,通常位于 /etc/haproxy/haproxy.cfg。添加以下內容:
global
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
frontend http-in
bind *:80
default_backend servers
backend servers
balance roundrobin
server server1 192.168.1.2:80 check
server server2 192.168.1.3:80 check
保存并退出文件后,啟動HAProxy服務:
sudo systemctl start haproxy
sudo systemctl enable haproxy
通過以上步驟,你可以在CentOS Minimal上實現基本的負載均衡。根據具體需求,可以選擇不同的負載均衡策略和配置方法。