keepalived+nginx高可用方案
环境准备
node1(Nginx1):192.168.10.10 # 主负载均衡器
node2(Nginx2):192.168.10.20 # 备负载均衡器
node3(WEB1):192.168.10.30 # 网站1
node4(WEB2):192.168.10.40 # 网址2
VIP:192.168.10.100
web部署
在node3和node4执行下面的脚本:#!/bin/bashyum install net‐tools httpd ‐ysystemctl stop firewalldsetenforce 0echo "<h1>This is RS1</h1>" > /var/www/html/index.html # 修改不同的主页以便测试!systemctl start httpd
nginx 部署
node1和node2节点执行以下脚本:#!/bin/bashsystemctl stop firewalldsetenforce 0yum install nginx ‐ycat > /etc/nginx/conf.d/proxy.conf << EOFupstream websers{server 192.168.10.30;server 192.168.10.40;}server{listen 8080;server_name 192.168.10.10;location / {proxy_pass http://websers;}}EOFnginx ‐s reload
keepalived部署
在node1和node2节点执行以下脚本:#!/bin/bashyum install keepalived ‐ymv /etc/keepalived/keepalived.conf{,.bak}cat > /etc/keepalived/keepalived.conf << EOF! Configuration File for keepalivedglobal_defs {router_id node1 # node2修改}vrrp_instance VI_1 {state MASTER # node2节点BACKUPinterface ens33virtual_router_id 10priority 100 # node2节点小于100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.10.100}}因此我们需要自定义脚本检测nginx服务是否正常[root@node1 ~]# cat /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {router_id node1}# 定义scriptvrrp_script chk_http_port {script "/usr/local/src/check_nginx_pid.sh"interval 1weight ‐2 # 优先级‐2}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 10priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}# 调用script脚本track_script {chk_http_port}virtual_ipaddress {192.168.10.100}}[root@node1 ~]# cat /usr/local/src/check_nginx_pid.sh#!/bin/bashA=`ps ‐C nginx ‐‐no‐header |wc ‐l`if [ $A ‐eq 0 ];then/usr/local/nginx/sbin/nginxif [ `ps ‐C nginx ‐‐no‐header |wc ‐l` ‐eq 0 ];thenexit 1elseexit 0fielseexit 0fi
测试
- 访问http://192.168.10.100:8080
- 停止node1的Keepalived服务,观察VIP是否漂移,访问正常
- 停止node1的nginx服务,观察VIP是否漂移,访问正常
- 如果需要检测后端服务,可以通过Nginx提供策略
keepalived+haproxy+MySQL双主
环境准备
node1(HAProxy1):192.168.10.10
node2(HAProxy2):192.168.10.20
node3(Mysql1):192.168.10.30
node4(Mysql2):192.168.10.40MySQL部署(双主)
