nginx+keepalived高可用(keepalived抢占模式)

nginx single ip
keepalived源码:https://github.com/acassen/keepalived
nginx源码:https://github.com/nginx/nginx
环境:10.18.1.185(主) 10.18.1.183(备)
1.安装nginx + keep

sudo apt install nginx
sudo apt install keepalived

2.修改配置文件

sudo vi /etc/nginx/nginx.conf

配置文件改为以下内容,其中root代表html文件位置:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections  1024;
}
http {
    include            mime.types;
    default_type       application/octet-stream;
    sendfile           on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   /var/www/html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

修改nginx183和185的返回:
/var/www/html/index.html的内容修改为对应的ip

sudo echo "185"  /var/www/html/index.html

3.修改keepalived的配置信息

sudo vi /etc/keepalived/keepalived.conf

主185的如下:

global_defs {
  router_id nginx_lb01
}

vrrp_script check_ng
{
   script "/etc/keepalived/script/check_nginx.sh"
   interval 1
}

vrrp_instance VI_1 {
    virtual_router_id 51
    state MASTER
    priority 101
    advert_int 1
    interface ens3
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      10.18.1.195
    }
    unicast_src_ip 10.18.1.185
    unicast_peer {
        10.18.1.183
    }
    track_script {
      check_ng
    }
}

备183的如下:

global_defs {
  router_id nginx_lb02
}

vrrp_instance VI_1 {
    virtual_router_id 51
    state BACKUP
    priority 100
    advert_int 1
    interface ens3
    authentication {
      auth_type PASS
      auth_pass 1111
    }
    virtual_ipaddress {
      10.18.1.195
    }
    unicast_src_ip 10.18.1.183
    unicast_peer {
        10.18.1.185
    }
}

主185还需要nginx检测脚本,当nginx挂掉时,自动关闭keepalived:

sudo  mkdir /etc/keepalived/script

4.改变mysql目录权限

sudo chown -R mysql.mysql /usr/local/mysql/
sudo vi  /etc/keepalived/script/check_nginx.sh

脚本如下:

#!/bin/bash
# 
# Define variables 
RETVAL=0
NginxStats=$(netstat -lntup|grep ":80 "|grep -v grep|wc -l)

## $NginxStats = 0 and stop keepalived 
if [ "$NginxStats" -eq 0 ];then
   service keepalived stop
fi

5.重启nginx和keepalived

sudo service nginx restart (或sudo service nginx reload )
sudo service keepalived restart

看到如下输出:

● keepalived.service - Keepalive Daemon (LVS and VRRP)
   Loaded: loaded (/lib/systemd/system/keepalived.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2023-02-27 16:45:57 CST; 4s ago
  Process: 16369 ExecStart=/usr/sbin/keepalived $DAEMON_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 16371 (keepalived)
    Tasks: 3 (limit: 4915)
   CGroup: /system.slice/keepalived.service
           ├─16371 /usr/sbin/keepalived
           ├─16375 /usr/sbin/keepalived
           └─16377 /usr/sbin/keepalived

2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Registering Kernel netlink command channel
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Registering gratuitous ARP shared channel
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Opening file '/etc/keepalived/keepalived.conf'.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Unsafe permissions found for script '/etc/keepalived/script/check_nginx.sh'.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: SECURITY VIOLATION - scripts are being executed but script_security not enabled. There are insecure scripts.
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: Using LinkWatch kernel netlink reflector...
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: VRRP_Script(check_ng) succeeded
2月 27 16:45:57 sder_plt_185 Keepalived_vrrp[16377]: VRRP_Instance(VI_1) Transition to MASTER STATE
2月 27 16:45:58 sder_plt_185 Keepalived_vrrp[16377]: VRRP_Instance(VI_1) Entering MASTER STATE

输出如下:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2023-02-27 16:45:50 CST; 40s ago
     Docs: man:nginx(8)
  Process: 16017 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 16286 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 16278 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 16288 (nginx)
    Tasks: 33 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ├─16288 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─16290 nginx: worker process
           ├─16292 nginx: worker process
           ├─16293 nginx: worker process
           ├─16294 nginx: worker process
           ├─16295 nginx: worker process
           ├─16298 nginx: worker process
           ├─16299 nginx: worker process
           ├─16300 nginx: worker process
           ├─16301 nginx: worker process
           ├─16302 nginx: worker process
           ├─16303 nginx: worker process

6.测试185nginx挂掉,183启用
先找一台其它机器:

curl 10.18.1.195

显示如下:

185

关闭185主的nginx服务:
sudo service nginx stop
再次执行,结果如下:

183

看到185的keepalived挂掉,inactive (dead)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,123评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,031评论 2 384
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,723评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,357评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,412评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,760评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,904评论 3 405
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,672评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,118评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,456评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,599评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,264评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,857评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,731评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,956评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,286评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,465评论 2 348

推荐阅读更多精彩内容