一、系统架构:

角色                                内网ip                    外网ip
keepalived+nginx           192.168.8.81         192.168.8.201
keepalived+nginx           192.168.8.82         192.168.8.201
web1                              192.168.8.83          -
web2                              192.168.8.84          -

二、nginx配置(两台nginx配置一模一样):

  1. 安装:
    yum  -y  install  nginx
  2. 配置:
    vim /etc/nginx/nginx.conf
    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /run/nginx.pid;
    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;
    include /etc/nginx/conf.d/*.conf;
    upstream dynamic {
        server 192.168.8.83:80;
        server 192.168.8.84:80;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  blog.dollarphp.com;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
            index index.html index.htm;
            autoindex on;
            autoindex_localtime on;
        }
        location ~* ^.+.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
            root html;
        }
        location ~* ^.+.(?![js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma])$ {
            proxy_pass http://dynamic;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    }
  3. 启动:
    systemctl  start  nginx

    三、keepalived配置:

    I、主服务器配置(192.168.8.81):

  4. 安装:
    yum  -y  install  keepalived
  5. 配置:
    vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    }
    vrrp_script chk_nginx {
    script "/etc/keepalived/check.sh"
    interval 1
    weight -15
    }
    vrrp_instance VI_1 {  #  实例,一个vip一个实例
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.8.201
    }
    track_script {
        chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
    }
  6. 添加服务检查脚本(check.sh):
    vim /etc/keepalived/check.sh
    #!/bin/bash
    # 心跳检测:如果服务还在运行返回0,否则返回1
    if [ "`pgrep  nginx  |  wc  -l`" == "0" ] ; then
    exit 1
    else
    exit 0
    fi
  7. 通知脚本(notify.sh):
    vim /etc/keepalived/notify.sh
    #!/bin/bash
    # 通知脚本:具体操作
    if [ "$1" == "master" ] ; then
    echo "`date '+%F %T'`:切换到主机模式" >> /var/log/keepalived/keepalived.log
    elif [ "$1" == "backup" ] ; then
    /usr/bin/systemctl start nginx
    echo "`date '+%F %T'`:切换到备机模式" >> /var/log/keepalived/keepalived.log
    else
    /usr/bin/systemctl start nginx
    echo "`date '+%F %T'`:宕机" >> /var/log/keepalived/keepalived.log
    fi
  8. 给脚本添加可执行权限:
    chmod  +x  /etc/keepalived/check.sh
    chmod  +x  /etc/keepalived/notify.sh
  9. 创建日志目录:
    mkdir  /var/log/keepalived
  10. 启动:
    systemctl  start  keepalived

    II、备服务器配置(192.168.8.82):

  11. 安装:
    yum  -y  install  keepalived
  12. 配置:
    vim /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived
    global_defs {
    }
    vrrp_script chk_nginx {
    script "/etc/keepalived/check.sh"
    interval 1
    weight -15
    }
    vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.8.201
    }
    track_script {
        chk_nginx
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
    }
  13. 添加服务检查脚本(check.sh):
    vim /etc/keepalived/check.sh
    #!/bin/bash
    # 心跳检测:如果服务还在运行返回0,否则返回1
    if [ "`pgrep  nginx  |  wc  -l`" == "0" ] ; then
    exit 1
    else
    exit 0
    fi
  14. 通知脚本(notify.sh):
    vim /etc/keepalived/notify.sh
    #!/bin/bash
    # 通知脚本:具体操作
    if [ "$1" == "master" ] ; then
    echo "`date '+%F %T'`:切换到主机模式" >> /var/log/keepalived/keepalived.log
    elif [ "$1" == "backup" ] ; then
    /usr/bin/systemctl start nginx
    echo "`date '+%F %T'`:切换到备机模式" >> /var/log/keepalived/keepalived.log
    else
    /usr/bin/systemctl start nginx
    echo "`date '+%F %T'`:宕机" >> /var/log/keepalived/keepalived.log
    fi
  15. 给脚本添加可执行权限:
    chmod  +x  /etc/keepalived/check.sh
    chmod  +x  /etc/keepalived/notify.sh
  16. 创建日志目录:
    mkdir  /var/log/keepalived
  17. 启动:
    systemctl  start  keepalived

    三、测试:

    I、测试负载均衡:

    centos7下keepalived+nginx配置_第1张图片
    centos7下keepalived+nginx配置_第2张图片

    II、测试心跳检测(主服务器:192.168.8.81):

  18. 停止nginx服务:
    systemctl  stop  nginx
  19. 查看日志:
    tail  /var/log/keepalived/keepalived.log

    centos7下keepalived+nginx配置
    centos7下keepalived+nginx配置