当前位置: 首页 > news >正文

keepalive和nginx高可用集群

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.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.3.86smtp_connect_timeout 30router_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从90advert_int 1 # 每隔1秒发送心跳authentication {auth_type PASSauth_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.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.3.87smtp_connect_timeout 30router_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从90advert_int 1 # 每隔1秒发送心跳authentication {auth_type PASSauth_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安装路径# 尝试重新启动nginxsystemctl restart nginx# 睡眠2秒sleep 2if [ $A -eq 0 ];then#启动失败,将keepalived服务杀死。killall keepalivedfi
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.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.3.86smtp_connect_timeout 30router_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从90advert_int 1 # 每隔1秒发送心跳authentication {auth_type PASSauth_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从90advert_int 1 # 每隔1秒发送心跳authentication {auth_type PASSauth_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.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.3.86smtp_connect_timeout 30router_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从90advert_int 1 # 每隔1秒发送心跳authentication {auth_type PASSauth_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从90advert_int 1 # 每隔1秒发送心跳authentication {auth_type PASSauth_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安装路径# 尝试重新启动nginxsystemctl restart nginx# 睡眠2秒sleep 2if [ $A -eq 0 ];then#启动失败,将keepalived服务杀死。killall keepalivedfi
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

访问测试:
在这里插入图片描述
当其中一台调度器宕机后依旧可以访问

http://www.lryc.cn/news/433962.html

相关文章:

  • 二分查找题总结
  • 仕考网:公务员面试流程介绍
  • (十五)SpringCloudAlibaba-Sentinel持久化到Nacos
  • GitHub图床
  • 记一次高版本view-design的组件迁移到自身项目的低版本
  • QT运行ROS工程
  • 电脑技巧:如何在Win11电脑上调整设置,让屏幕更加护眼?
  • 【数据结构】排序算法篇二
  • python进阶篇-day09-数据结构与算法(非线性结构与排序算法)
  • 线性代数基础
  • LCR 021
  • 【阿雄不会写代码】全国职业院校技能大赛GZ036第四套
  • Vue组件:使用$emit()方法监听子组件事件
  • 数据分析-埋点
  • 【文心智能体】通过工作流使用知识库来实现信息查询输出,一键查看旅游相关信息,让出行多一份信心
  • 服务器监控工具都是监控服务器的哪些性能和指标
  • 不小心删除丢失了所有短信?如何在 iPhone 上查找和恢复误删除的短信
  • 【skyvern 快速上手】一句话让AI帮你实现爬虫+自动化
  • 【C++ Primer Plus习题】14.1
  • 在Ubuntu上运行QtCreator相关程序
  • MybatisPlus 快速入门
  • Java.lang中的String类和StringBuilder类介绍和常用方法
  • notepad++软件介绍(含安装包)
  • chapter13-常用类——(章节小结)——day17
  • RTX AI PC 和工作站上部署多样化 AI 应用支持 Multi-LoRA
  • C++ STL-deque容器入门详解
  • 数据结构之折半查找
  • linux高级学习12
  • leetcode:3174 清除数字 使用栈,时间复杂度O(n)
  • 神经网络卷积操作