• keepalived的vip实现nginx节点的主备


    nginx

    wget http://nginx.org/download/nginx-1.18.0.tar.gz
    tar zxvf nginx-1.18.0.tar.gz
    
    yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel libnl3-devel
    
    cd nginx-1.18.0
    mkdir -p /usr/local/nginx
    #需要使用https,在编译时启用--with-http_ssl_module
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-stream
    make && make install
    
    ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
    #验证
    nginx -V
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    配置文件1

    vi /usr/local/nginx/conf/nginx.conf
    
    #工作进程数量,一般设置为CPU核数的整数倍
    worker_processes 4;
    #一个worker进程所能打开文件的最大数量
    worker_rlimit_nofile 40000;
    
    events {
    #设置每一个worker进程可以并发处理的最大连接数,该值不能超过worker_rlimit_nofile
        worker_connections  8192;
    }
    
    stream {
        upstream rancher_servers_http {
            least_conn;
            server ip1:80 max_fails=3 fail_timeout=5s;
            server ip2:80 max_fails=3 fail_timeout=5s;
            server ip3:80 max_fails=3 fail_timeout=5s;
        }
        server {
            listen     80;
            proxy_pass rancher_servers_http;
        }
    
        upstream rancher_servers_https {
            least_conn;
            server ip1:443 max_fails=3 fail_timeout=5s;
            server ip2:443 max_fails=3 fail_timeout=5s;
            server ip3:443 max_fails=3 fail_timeout=5s;
        }
    
        server {
            listen     443;
            proxy_pass rancher_servers_https;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    keepalived

    双节点

    wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
    yum install -y heartbeat
    
    systemctl enable keepalived
    
    • 1
    • 2
    • 3
    • 4

    主节点

    cat /etc/keepalived/keepalived.conf
    
    #Master
    ! Configuration File for keepalived
    global_defs {
      notification_email {
        root@localhost
        }
         
    notification_email_from keepalived@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id DRBD_HA_MASTER
    }
         
    vrrp_script chk_nfs {
            script "/etc/keepalived/check_nginx.sh"
            interval 5
        }
        vrrp_instance VI_1 {
            state MASTER
            interface enp0s3
            virtual_router_id 101
            priority 100
            advert_int 1
            authentication {
                auth_type PASS
                auth_pass 1111
            }
            track_script {
                chk_nfs
            }
    
        notify_master /etc/keepalived/notify_master.sh
        notify_stop /etc/keepalived/notify_stop.sh
    
        virtual_ipaddress {
            192.168.101.200/24
        }
    }
    
    
    #检测:
    chmod 744 /etc/keepalived/*.sh
    
    cat /etc/keepalived/check_nginx.sh
    
    #!/bin/bash
    # /usr/bin/systemctl status nfs &>/dev/null
    a= `ps -C nginx --no-heading | wc -l`
    
    if [ $a -eq "0" ];then
       
       nginx -c /usr/local/nginx/conf/nginx.conf
       sleep 3
       b= `ps -C nginx --no-heading | wc -l`
       if [ $b -eq "0" ];then
          systemctl stop keepalived
       fi
    fi
    
    # if [ $? -ne 0 ];then  
    #    /usr/bin/systemctl restart nfs
    #    sleep 3
    #    /usr/bin/systemctl status nfs &>/dev/null
    #    if [ $? -ne 0 ];then    
    #        umount /dev/drbd0
    #        drbdadm secondary r0
    #        systemctl stop keepalived
    #    fi
    # fi
    
    
    cat /etc/keepalived/notify_master.sh
    #!/bin/sh
    nginx -c /usr/local/nginx/conf/nginx.conf
    
    
    cat /etc/keepalived/notify_stop.sh 
    #!/bin/sh
    systemctl stop keepalived
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    备节点

    cat /etc/keepalived/keepalived.conf
    
    #slave
    ! Configuration File for keepalived
    global_defs {
      notification_email {
        root@localhost
        }
         
    notification_email_from keepalived@localhost
    smtp_server 127.0.0.1
    smtp_connect_timeout 30
    router_id DRBD_HA_BACKUP
    }
         
    vrrp_instance VI_1 {
        state BACKUP
        interface enp0s3
        virtual_router_id 101
        priority 90
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        
        notify_master /etc/keepalived/notify_master.sh
        notify_backup /etc/keepalived/notify_backup.sh
    
        virtual_ipaddress {
            192.168.101.200/24
        }
    }
    
    
    #检测:
    chmod 744 /etc/keepalived/*.sh
    
    cat /etc/keepalived/check_nginx.sh
    #!/bin/bash
    a= `ps -C nginx --no-heading | wc -l`
    
    if [ $a -eq "0" ];then
       
       nginx -c /usr/local/nginx/conf/nginx.conf
       sleep 3
       b= `ps -C nginx --no-heading | wc -l`
       if [ $b -eq "0" ];then
          systemctl stop keepalived
       fi
    fi
    
    
    cat /etc/keepalived/notify_master.sh
    #!/bin/sh
    nginx -c /usr/local/nginx/conf/nginx.conf
    
    
    cat /etc/keepalived/notify_backup.sh 
    #!/bin/sh
    systemctl stop keepalived
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    黄金投资面对K线图有哪些好用的交易策略?
    这个方法可以实现自动抠图,快来get
    使用 L293D 电机驱动器 IC 和 Arduino 控制直流电机
    字符串常用方法
    火车头本地文档批量翻译工具
    Pyinstaller打包pikepdf失败的问题排查
    第3章 数据结构中树的概念
    mybatispagehelp嵌套分页处理
    服务器中了locked勒索病毒怎么处理,locked勒索病毒解密,数据恢复
    时间戳 太平洋夏令时间和本地时间相互转换及自定义夏令时
  • 原文地址:https://blog.csdn.net/hy19930118/article/details/133457375