本文将基于 corosync、pacemaker、pcs、nfs 和 nginx 快速搭建高可用集群。同时使用的操作系统为 CentOS8.1。
本文主要讲解步骤,关于高可用的相关知识将在以后的文章中详细说明,现在如果还不了解高可用集群的,可以先自行查询搜索引擎,然会再查看本文。
简易网络拓扑如下:
注:
- 本文使用了3台设备用作高可用集群,如果需要使用2台或偶数设备设备作高可用集群,那么需要提供 qdev(仲裁设备,没有也能用,本文不涉及这方面内容),目前 corosync 只支持 net 模式,同时该设备也需要使用corosync提供,所以干脆就使用了三台设备。
- 由于环境限制,无法提供 stonith 设备,所以在使用 pcs 的使用需要手动禁用stonith功能。
- CentOS 8.0 还没有HighAvailability的repo,HA的软件库需要升级到 CentOS 8.1。
- 本文中的样例将不配置php的web页面,关于后端服务器可自行通过pcs配置。
软件安装和基础环境配置
注意,以下所有的配置将在三台主机上完成相同操作。
- 首先安装基础软件
dnf install corosync pacemaker pcs nginx
- 配置nginx的监听地址
# file: /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
# 这里指定的是监听所有IP,不过最重要的就是监听VIP地址,后面会通过RA来实现。
listen *:80 default_server;
server_name _;
# 这里指定的路径默认不存在,同样会通过RA来挂在该目录的。
root /var/www/web;
}
}
注意:不要启动 nginx, 待会将以资源的形式启动nginx,现在启动nginx将会发生冲突。这里只需要配置以下防火墙就可以了。
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
# 这里还需要设置允许 nginx 访问 nfs。
setsebool httpd_use_nfs on -P
- 配置和启动pcsd
pcsd(server) 将为 pcs(client) 提供API接口,用于实现控制,同时将通过 PAM 来实现用户认证,注意认证的用户为 hacluster。
# file:/etc/sysconfig/pcsd
PCSD_DEBUG=false # 关闭debug模式
PCSD_DISABLE_GUI=true # 禁用图形化(也就是web)
# 启动服务
systemctl start pcsd
systemctl enable pcsd
# 配置防火墙
firewall-cmd --add-service=high-availability --permanent
firewall-cmd --reload
# 注: 这里解放的端口同时包含了 corosync 和 pacemaker 所使用的端口。
# 为 hacluster 设置密码
echo '123456' | passwd --stdin hacluster
# 为了方便操作,可以将三台服务器都设置相同的密码
- 配置hosts
这里必须使三台服务器都能解析到,最好使用/etc/hosts
,这也是 redhat 推荐的做法。
# file:/etc/hosts
……
10.0.0.101 server1.somata.net
10.0.0.102 server2.somata.net
10.0.0.103 server3.somata.net
- 配置NFS文件共享
注:此步骤在其中一台主机完成即可。
首先创建一个文件用于共享使用。
mkdir /var/nfs
然后编辑配置文件:
# file: /etc/exports
/var/nfs 10.0.0.0/8(ro)
启动服务,并且开放防火墙
# 启动服务
systemctl start nfs-server
systemctl enable nfs-server
# 配置防火墙
firewall-cmd --add-service=nfs --permanent
firewall-cmd --reload
注意:不要启动和配置 pacemaker 和 corosync,这些将通过 pcs 来进行管理。
使用 pcs 开始搭建集群
注意:pcs的所有配置都将在命令行完成,无需编辑任何文件。
同时以下这些操作在其中一台配置就可以了,pcs 会通过 pcsd 服务器自动同步至其他服务器。
- 首先认证所有用户,否则无法创建集群
pcs host auth server1.somata.net server2.somata.net server3.somata.net
Username: hacluster
Password:
server1.somata.net: Authorized
server3.somata.net: Authorized
server2.somata.net: Authorized
- 创建集群,这里我由于使用了多网卡(多个IP地址),所以这里需要指定IP地址,否则,pcs将无法获得一个确切地址(当然不指定也能使用,只不过会警告而已)
pcs cluster setup web-server server1.somata.net addr=10.0.0.101 server2.somata.net addr=10.0.0.102 server3.somata.net addr=10.0.0.103
# 由于没有 stonith 设备,注意需要设置禁用stonith设备。
pcs property set stonith-enabled=false
# 然后启动所有节点
pcs cluster start --all
Destroying cluster on hosts: 'server1.somata.net', 'server2.somata.net', 'server3.somata.net'...
server3.somata.net: Successfully destroyed cluster
server2.somata.net: Successfully destroyed cluster
server1.somata.net: Successfully destroyed cluster
Requesting remove 'pcsd settings' from 'server1.somata.net', 'server2.somata.net', 'server3.somata.net'
server3.somata.net: successful removal of the file 'pcsd settings'
server1.somata.net: successful removal of the file 'pcsd settings'
server2.somata.net: successful removal of the file 'pcsd settings'
Sending 'corosync authkey', 'pacemaker authkey' to 'server1.somata.net', 'server2.somata.net', 'server3.somata.net'
server3.somata.net: successful distribution of the file 'corosync authkey'
server3.somata.net: successful distribution of the file 'pacemaker authkey'
server1.somata.net: successful distribution of the file 'corosync authkey'
server1.somata.net: successful distribution of the file 'pacemaker authkey'
server2.somata.net: successful distribution of the file 'corosync authkey'
server2.somata.net: successful distribution of the file 'pacemaker authkey'
Sending 'corosync.conf' to 'server1.somata.net', 'server2.somata.net', 'server3.somata.net'
server1.somata.net: successful distribution of the file 'corosync.conf'
server3.somata.net: successful distribution of the file 'corosync.conf'
server2.somata.net: successful distribution of the file 'corosync.conf'
Cluster has been successfully set up.server2.somata.net: Starting Cluster...
server1.somata.net: Starting Cluster...
server3.somata.net: Starting Cluster...
- 创建资源,pcs支持多种标准的RA,例如:lsb、ocf、service 和 systemd,但是还是推荐使用 ocf 标准的 RA,他支持更加多的检测机制,而像 systemd 仅仅能实现开启、关闭服务和检测进程的正常,无法检测服务是否正常。
# 首先创建 VIP 的资源
pcs resource create vip ocf:heartbeat:IPaddr2 ip=10.0.0.250 cidr_netmask=24
# 然后创建 NFS 的资源
pcs resource create web_file ocf:heartbeat:Filesystem device=10.0.0.101:/var/nfs directory=/var/www/web fstype=nfs
# 最后创建 nginx 的资源
pcs resource create web ocf:heartbeat:nginx op monitor interval=30s
# 注:这里的 nginx 资源与前几个资源有冲突,需要指定不同的 interval,以消除冲突。
- 创建资源关系
# 因为这些资源都需要在同一台服务器上运行,所以需要打包到同一个组中。
pcs resource group add web_cluster vip web_file web
# 同时这里需要配置优先级,以保证nginx不会启动出错
pcs constraint order vip then web
pcs constraint order web_file then web
Adding vip web (kind: Mandatory) (Options: first-action=start then-action=start)
Adding web_file web (kind: Mandatory) (Options: first-action=start then-action=start)
- 更改资源粘性
pcs constraint location vip prefers server1.somata.net=50 server2.somata.net=100 server3.somata.net=150
pcs constraint location web_file prefers server1.somata.net=50 server2.somata.net=100 server3.somata.net=150
pcs constraint location web prefers server1.somata.net=50 server2.somata.net=100 server3.somata.net=150
那么到此为止,CentOS8 快速搭建web高可用集群 就完成了。
简答的测试
首先测试一下 nginx 能否正常执行:
- 编写一个简单的测试文件:
# file:/var/nfs/index.html
<h1>hello,world!!</h1>
- 使用浏览器查看网页是否正常:
- 查询 web 服务在那一主机,同时关闭那一台主机:
没有意外,会在 server3.somata.net,这是应为前面配置过了资源粘性,优先运行再其上。
pcs status
然后我们将server3测试高可用性能。
poweroff
这里我们再次查看,就会发现已经开始转移资源了。
现在测试也是可以正常使用的,这里就不放图了。
本文经「原本」原创认证,作者乾坤盘,访问yuanben.io查询【358DCM5D】获取授权信息。
CentOS8.2没有pcs包
CentOS8.2是有pcs的软件包,pcs在HighAvailability软件库中,默认未启用,你需要手动启动该库,库配置文件路径为/etc/yum.repos.d/CentOS-HA.repo。