Keepalived+Nginx 高可用集群(主从模式)
一、两台群集服务器都需要安装nginx和keepalived,先添加nginx yum 源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
yum clean all yum makecache yum install -y nginx
二、防火墙设置
systemctl status firewalld //查看防火墙状态 systemctl start/stop firewalld //防火墙启停 firewall-cmd --list-ports //查看开放的端口 firewall-cmd --zone=public --query-port=80/tcp //查询80端口是否开放 firewall-cmd --zone=public --add-port=80/tcp --permanent //开放80端口 firewall-cmd --zone=public --remove-port=80/tcp --permanent //移除80端口 firewall-cmd --reload //重新加载 如果开放了执行上放开80端口还不行,那就再执行iptables -F
setenforce 0 vim /etc/selinux/config 将seLIUNX永久关闭 getenforce #查看seliunx状态如果出现disabled或者permissive示示已关闭
三、配置Nginx站配置文件
vim /etc/nginx/conf.d/default.conf #将默认配置的监听端口改成8080
cd /etc/nginx/conf.d/ vim it.conf server { listen 80; #指定监听端口80 server_name it.founderpcb.com; #指定当前网的访问的域名 location / { #指定代码位置 proxy_pass https://pcbsrvdhcp114.founderpcb.com; #指定首页文件 } }
systemctl start nginx 启动nginx systemctl stop nginx 停止nginx systemctl restart nginx 重启nginx nginx -s reload 重新加nginx配置 nginx -V 查看版本
四、两台服务器都要安装Keepalived
安装主服务器
yum install -y keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务 interval 2 #检测时间间隔 weight -20 #如果条件成立的话,则权重 -20 } # 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称 vrrp_instance VI_1 { state MASTER #来决定主从 interface ens192 # 绑定虚拟 IP 的网络接口,根据自己的机器填写 virtual_router_id 121 # 虚拟路由的 ID 号, 两个节点设置必须一样 mcast_src_ip 172.21.57.17 #填写本机ip priority 100 # 节点优先级,主要比从节点优先级高 nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题 advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s authentication { auth_type PASS auth_pass 1111 } # 将track_script块加入instance 配置块 track_script { chk_nginx #执行Nginx监控的服务 } virtual_ipaddress { 172.21.57.15 #虚拟ip } }
安装副服务器
yum install -y keepalived
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" #运行脚本,脚本内容下面有,就是起到一个nginx宕机以后,自动开启服务 interval 2 #检测时间间隔 weight -20 #如果条件成立的话,则权重 -20 } # 定义虚拟路由,VI_1 为虚拟路由的标示符,自己定义名称 vrrp_instance VI_1 { state BACKUP #来决定主从 interface ens192 # 绑定虚拟 IP 的网络接口,根据自己的机器填写 virtual_router_id 121 # 虚拟路由的 ID 号, 两个节点设置必须一样 mcast_src_ip 172.21.57.18 #填写本机ip priority 100 # 节点优先级,主要比从节点优先级高 nopreempt # 优先级高的设置 nopreempt 解决异常恢复后再次抢占的问题 advert_int 1 # 组播信息发送间隔,两个节点设置必须一样,默认 1s authentication { auth_type PASS auth_pass 1111 } # 将 track_script 块加入 instance 配置块 track_script { chk_nginx #执行 Nginx 监控的服务 } virtual_ipaddress { 172.21.57.15 # 虚拟ip } }
五、Nginx+keepalived实现高可用脚本:
vim /etc/keepalived/nginx_check.sh #编辑脚本文件当检测nginx服务停止后立即关闭keepalived
#!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then systemctl kill keepalived fi
chmod 777 /etc/keepalived/nginx_check.sh #授权脚本
systemctl start keepalived #启动keepalived服务 systemctl restart keepalived #重启keepalived服务 systemctl stop keepalived #停止keepalived服务
六、验证高可用是否生效
systemctl stop nginx #将主服务器的nginx服务停止掉 systemctl status keepalived #查询keepalived服务状态也已经停止掉了
群集IP地址已经正常漂移到另一台备份服务器上去了
标签:架构,nginx,--,Keepalived,etc,keepalived,Nginx,systemctl,80 From: https://www.cnblogs.com/fzhelpdesk/p/17502776.html