在开始部署前,请确保:
sudo yum update -y);pgAdmin官方提供了CentOS的YUM仓库,安装步骤如下:
# 添加pgAdmin官方YUM仓库
sudo rpm -i https://download.pgadmin.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat.repo
# 安装pgAdmin(选择适合CentOS版本的包,如el7/el8)
sudo yum install -y pgadmin4
# 启动pgAdmin服务并设置开机自启
sudo systemctl start pgadmin4
sudo systemctl enable pgadmin4
安装完成后,通过浏览器访问http://<服务器IP>:5050(默认端口5050),若出现pgAdmin登录页面,则说明安装成功。
在每台服务器上重复上述安装步骤,确保所有实例运行相同版本的pgAdmin。安装完成后,每个实例会自动创建默认配置文件(位于/var/lib/pgadmin/config_local.py)。
使用Nginx作为负载均衡器,将客户端请求分发到多个pgAdmin实例,提升可用性和性能。
sudo yum install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
编辑Nginx配置文件(/etc/nginx/nginx.conf),添加以下内容:
http {
upstream pgadmin_cluster {
server <pgadmin1_IP>:5050; # 替换为第一台pgAdmin服务器IP
server <pgadmin2_IP>:5050; # 替换为第二台pgAdmin服务器IP
# 可根据需要添加更多实例
}
server {
listen 80;
server_name pgadmin.yourdomain.com; # 替换为你的域名或IP
location / {
proxy_pass http://pgadmin_cluster;
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;
}
}
}
保存后重启Nginx:
sudo systemctl restart nginx
为确保所有pgAdmin实例的用户、服务器配置一致,需启用pgAdmin配置同步:
# 编辑pgAdmin主配置文件(/etc/pgadmin/config_systemd.py)
sudo vim /etc/pgadmin/config_systemd.py
添加以下内容(替换为你的邮件服务器信息):
# 配置邮件服务器(用于发送账户验证邮件)
MAIL_SERVER = 'smtp.yourdomain.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = 'your_email@yourdomain.com'
MAIL_PASSWORD = 'your_email_password'
# 配置同步存储(使用数据库存储配置)
STORAGE_TYPE = 'sqlalchemy'
SQLALCHEMY_DATABASE_URI = 'postgresql://pgadmin_user:your_password@<PostgreSQL_Server_IP>/pgadmin'
重启pgAdmin服务使配置生效:
sudo systemctl restart pgadmin4
为避免单点故障导致数据丢失,建议将pgAdmin的数据目录挂载到高可用文件系统(如GlusterFS、Ceph):
# 示例:使用GlusterFS挂载(需提前部署GlusterFS集群)
sudo yum install -y glusterfs-client
sudo mkdir -p /mnt/pgadmin_data
sudo mount -t glusterfs <glusterfs_server1_IP>:/pgadmin_volume /mnt/pgadmin_data
# 将pgAdmin数据目录指向挂载点
sudo sed -i 's|/var/lib/pgadmin|/mnt/pgadmin_data|g' /etc/pgadmin/config_systemd.py
sudo systemctl restart pgadmin4
pgAdmin的核心功能是管理PostgreSQL数据库集群,需先配置pgAdmin与PostgreSQL集群的连接:
为避免数据传输被窃取,建议为pgAdmin和PostgreSQL配置SSL:
编辑Nginx配置文件,添加SSL证书:
server {
listen 443 ssl;
server_name pgadmin.yourdomain.com;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
location / {
proxy_pass http://pgadmin_cluster;
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;
}
}
# 重定向HTTP到HTTPS
server {
listen 80;
server_name pgadmin.yourdomain.com;
return 301 https://$host$request_uri;
}
重启Nginx:
sudo systemctl restart nginx
参考PostgreSQL官方文档配置SSL,生成证书并修改postgresql.conf:
ssl = on
ssl_cert_file = '/path/to/server.crt'
ssl_key_file = '/path/to/server.key'
允许Nginx(80/443)和PostgreSQL(5432)端口通过防火墙:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --permanent --zone=public --add-port=5432/tcp
sudo firewall-cmd --reload
allow指令,仅允许可信IP访问;systemctl status pgadmin4查看服务运行状态,通过Nginx的access.log和error.log监控访问情况;/var/lib/pgadmin目录(或高可用存储挂载点),确保数据可恢复;pg_stat_replication视图监控主从复制状态,使用repmgr工具监控节点健康状态。通过以上步骤,即可在CentOS上完成pgAdmin集群部署,实现高可用性和负载均衡,有效管理PostgreSQL数据库集群。