keepalived 和 nginx 高可用集群搭建
主备模式
zyj86主机和zyj87主机安装nginx和keepalived
yum install nginx keepalived -y
systemctl enable --now nginx.service keepalived.service
主调度器配置
编辑zyj86主机(主)配置文件
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.3.86
smtp_connect_timeout 30
router_id zyj86 # 访问到主机,本机的hostname,需要修改
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" # 检测脚本位置
interval 2 #(检测脚本执行的间隔)
weight 2 # 权重
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP,需要修改
interface ens160 #网卡名字,使用ifconfig查看,需要修改
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小,一般主100从90
advert_int 1 # 每隔1秒发送心跳
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.3.188 #VRRP虚拟地址,也可以绑定多个虚拟ip
}
track_script {
chk_nginx
}
}
重启keepalived
systemctl restart keepalived.service
从调度器配置
编辑zyj87主机(从)配置文件
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.3.87
smtp_connect_timeout 30
router_id zyj87 # 访问到主机,本机的hostname,需要修改
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" # 检测脚本位置
interval 2 #(检测脚本执行的间隔)
weight 2 # 权重
}
vrrp_instance VI_1 {
state BACKUP # 备份服务器上将 MASTER 改为 BACKUP,需要修改
interface ens160 #网卡名字,使用ifconfig查看,需要修改
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 99 # 主、备机取不同的优先级,主机值较大,备份机值较小,一般主100从90
advert_int 1 # 每隔1秒发送心跳
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.3.188 #VRRP虚拟地址,也可以绑定多个虚拟ip
}
track_script {
chk_nginx
}
}
重启keepalived
systemctl restart keepalived.service
zyj86 zyj87主机编写Nginx状态检测脚本
vi /etc/keepalived/nginx_check.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
# 这里需要替换成自己的nginx安装路径
# 尝试重新启动nginx
systemctl restart nginx
# 睡眠2秒
sleep 2
if [ $A -eq 0 ];then
#启动失败,将keepalived服务杀死。
killall keepalived
fi
fi
给脚本添加执行权限
chmod +x /etc/keepalived/nginx_check.sh
修改zyj86和zyj87主机的nginx配置文件
vi /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
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;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
#访问主机列表
upstream myserver {
server 192.168.3.88 weight=1;
server 192.168.3.89 weight=1;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://myserver;
}
}
}
重启nginx
systemctl restart nginx
后台服务器配置
yum install httpd -y
zyj88主机
echo 88 > /var/www/html/index.html
zyj89主机
echo 89 > /var/www/html/index.html
启动服务
systemctl enable --now httpd
访问测试:
当其中一台调度器宕机后依旧可以访问
互为主备模式
zyj86主机和zyj87主机安装nginx和keepalived
yum install nginx keepalived -y
systemctl enable --now nginx.service keepalived.service
第一台调度器配置
编辑zyj86主机配置文件
vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.3.86
smtp_connect_timeout 30
router_id zyj86 # 访问到主机,本机的hostname,需要修改
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" # 检测脚本位置
interval 2 #(检测脚本执行的间隔)
weight 2 # 权重
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP,需要修改
interface ens160 #网卡名字,使用ifconfig查看,需要修改
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小,一般主100从90
advert_int 1 # 每隔1秒发送心跳
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.3.188 # VRRP虚拟地址,也可以绑定多个虚拟ip
}
track_script {
chk_nginx
}
}
vrrp_instance VI_2 {
state BACKUP # 备份服务器上将 MASTER 改为 BACKUP,需要修改
interface ens160 #网卡名字,使用ifconfig查看,需要修改
virtual_router_id 52 # 主、备机的 virtual_router_id 必须相同
priority 79 # 主、备机取不同的优先级,主机值较大,备份机值较小,一般主100从90
advert_int 1 # 每隔1秒发送心跳
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.3.199 #VRRP虚拟地址,也可以绑定多个虚拟ip
}
track_script {
chk_nginx
}
}
重启keepalived
systemctl restart keepalived.service
第二台调度器配置
编辑zyj87主机配置文件
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.3.86
smtp_connect_timeout 30
router_id zyj86 # 访问到主机,本机的hostname,需要修改
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" # 检测脚本位置
interval 2 #(检测脚本执行的间隔)
weight 2 # 权重
}
vrrp_instance VI_1 {
state BACKUP # 备份服务器上将 MASTER 改为 BACKUP,需要修改
interface ens160 #网卡名字,使用ifconfig查看,需要修改
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 99 # 主、备机取不同的优先级,主机值较大,备份机值较小,一般主100从90
advert_int 1 # 每隔1秒发送心跳
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.3.188 # VRRP虚拟地址,也可以绑定多个虚拟ip
}
track_script {
chk_nginx
}
}
vrrp_instance VI_2 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP,需要修改
interface ens160 #网卡名字,使用ifconfig查看,需要修改
virtual_router_id 52 # 主、备机的 virtual_router_id 必须相同
priority 80 # 主、备机取不同的优先级,主机值较大,备份机值较小,一般主100从90
advert_int 1 # 每隔1秒发送心跳
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.3.199 #VRRP虚拟地址,也可以绑定多个虚拟ip
}
track_script {
chk_nginx
}
}
重启keepalived
systemctl restart keepalived.service
zyj86 zyj87主机编写Nginx状态检测脚本
vi /etc/keepalived/nginx_check.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
# 这里需要替换成自己的nginx安装路径
# 尝试重新启动nginx
systemctl restart nginx
# 睡眠2秒
sleep 2
if [ $A -eq 0 ];then
#启动失败,将keepalived服务杀死。
killall keepalived
fi
fi
给脚本添加执行权限
chmod +x /etc/keepalived/nginx_check.sh
修改zyj86和zyj87主机的nginx配置文件
vi /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
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;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
#访问主机列表
upstream myserver {
server 192.168.3.88 weight=1;
server 192.168.3.89 weight=1;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://myserver;
}
}
}
重启nginx
systemctl restart nginx
后台服务器配置
yum install httpd -y
zyj88主机
echo 88 > /var/www/html/index.html
zyj89主机
echo 89 > /var/www/html/index.html
启动服务
systemctl enable --now httpd
访问测试:
当其中一台调度器宕机后依旧可以访问