2 Star 1 Fork 0

zhrun8899 / learning-notes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
keepalived+nginx负载均衡.md 4.85 KB
一键复制 编辑 原始数据 按行查看 历史

keepalived不需要开机启动,假如开机自启的话,如果keepalived比nginx 更快启动的话,脚本检测会把keepalived停掉的,所以没必要,只需要nginx开机启动,启动主机后自行手动的把keepalived服务启动即可。

检测脚本1:

#!/bin/bash
# 如果进程中没有nginx则将keepalived进程kill掉
A=`ps -C nginx --no-header |wc -l`      ## 查看是否有 nginx进程 把值赋给变量A
if` `[ $A -eq ``0` `];then                    ## 如果没有进程值得为 零
       ``service keepalived stop          ## 则结束 keepalived 进程
fi

检测脚本:

需要注意的是,要判断本机nginx是否正常,如果发现nginx不正常,重启之后,等待三秒在校验,任然失败则不尝试,关闭keepalived,发送邮件,其他主机此时接管VIP;

[root@centos-4~]# cat /opt/check_nginx.sh

#!/bin/bash

check=$(ps-C nginx --no-heading | wc -l)

IP=`ipadd | grep eth0 | awk  'NR==2{print $2}'| awk -F '/' '{print $1}'`

if ["${check}" = "0" ]; then

​    /usr/local/nginx/sbin/nginx

​    sleep 2

​    counter=$(ps -C nginx --no-heading|wc -l)

​    if [ "${check}" = "0"]; then

​        /etc/init.d/keepalived stop

​      echo "check $IP nginx is down"| mail -s "check keepalived nginx" *********@qq.com

​    fi

fi

keepalived 配置:/etc/keepalived/keepalived.conf

gilobal_defs {
    notification_email {
        runzh@163.com
    }
    notification_email_from runzh@163.com
    smtp_server smtp.hysec.com
    smtp_connection_timeout 30
    router_id nginx_backup              # 设置nginx backup的id,在一个网络应该是唯一的
}
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh"
    interval 2                          #(检测脚本执行的间隔)
    weight -2
}
vrrp_instance VI_1 {
    state BACKUP                        # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface ens160                      # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
    virtual_router_id 66                # 虚拟路由编号,主从要一直
    priority 99                         # 优先级,数值越大,获取处理请求的优先级越高
    advert_int 1                        # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_http_port                   #(调用检测脚本)
    }
    virtual_ipaddress {
        193.169.1.200                   # 定义虚拟ip(VIP),可多设,每行一个
    }
}

nginx检测脚本 /usr/local/src/check_nginx_pid.sh

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
    /usr/local/nginx/sbin/nginx                #重启nginx
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败
         sleep 2
         if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                killall keepalived
         fi
         exit 1
    else
        exit 0
    fi
else
    exit 0
fi

nginx配置文件 /usr/local/nginx/conf/nginx.conf

重点是:stream 段的配置

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

stream{
        upstream crmapp{
                server 193.169.1.35:8021 weight=1 max_fails=2 fail_timeout=30s;
                server 193.169.1.37:8021 weight=1 max_fails=2 fail_timeout=30s;
        }
server{
                listen 8099;
                proxy_pass crmapp;
        }

}

http {
    include       mime.types;
    default_type  application/octet-stream;

#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  logs/access.log  main;
location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
}
1
https://gitee.com/zhrun8899/learning-notes.git
git@gitee.com:zhrun8899/learning-notes.git
zhrun8899
learning-notes
learning-notes
master

搜索帮助