nginx+keepalived搭建主从负载均衡并迅速切换

        大家都听说过主从服务器或者负载均衡之类的专业术语,作为衡量一个中高级运维工程师的标准,集群和负载是运维工程师必须掌握的技术,然而在一家小公司是根本不会体会到运维的重要性的。首先从理论上讲一下,当用户量和访问量达到一定级别的时候,服务器达到负载,浏览器或者客户端响应比较慢的时候除了优化代码外,还可以搭建一个负载均衡的系统,给每个服务器分发相应的请求来满足平台的性能。

       首先准备三台 linux 服务器,一台安装nginx做反向代理,另外两台分别为主、从服务器,按步骤来:

1、先去安装 nginx :

       到 https://nginx.org/download/ 去下载脚本安装,或者直接运行命令下载:

wget https://nginx.org/download/nginx-1.12.2.tar.gz

       我下载的是 nginx-1.12.2 这个版本,后面的 nginx 都是这个版本的。

然后解压进入目录编译安装:

tar -zxvf nginx-1.12.2.tar.gz
cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--with-http_stub_status_module \
--with-http_ssl_module \
--http-scgi-temp-path=/var/temp/nginx/scgi
make && make install

启动nginx:

cd /usr/local/nginx/sbin/
./nginx

启动成功!

接下来搭建负载均衡:

先简单介绍一下负载均衡:

负载均衡 建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。 
负载均衡,英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至其他服务器。 
nginx负载均衡服务器: 172.31.8.217            nginx负载均衡备用服务器:172.31.8.218
应用服务器1:172.26.255.56
应用服务器2:172.26.255.57

配置nginx.conf

cd /usr/local/nginx/conf/
vi nginx.conf

upstream mytomcat{
    server 172.26.255.56:80 weight=1;
    server 172.26.255.57:80 weight=1;
}
server {
    listen       80 default_server;
    #listen       [::]:80 default_server;
    #server_name  _;
    root         /usr/share/nginx/html;
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
    location / {
        proxy_pass http://mytomcat;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

测试一下,这个时候访问 172.31.8.217 就会路由到另外两台服务器上,这样一个简单的负载均衡服务器就搭建好了。

接下来把 keepalived 用上,安装 keeplived 主备都安装:

yum install -y keepalived

master上的keepalived.conf内容如下:

global_defs {
    notification_email {
        [email protected]
    }
    notification_email_from [email protected]
    smtp_server localhost
    smtp_connection_timeout 30
    router_id nginx_master        # 设置nginx master的id,在一个网络应该是唯一的
}
vrrp_script chk_http_port {
    script "/usr/local/src/check_nginx_pid.sh"    #手动执行下此脚本,以确保此脚本能够正常执行
    interval 2                          #(检测脚本执行的间隔,单位是秒)
    weight 2
}
vrrp_instance VI_1 {
    state MASTER            # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface eth0            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
    virtual_router_id 66        # 虚拟路由编号,主从要一直
    priority 100            # 优先级,数值越大,获取处理请求的优先级越高
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        chk_http_port            #(调用检测脚本)
    }
    virtual_ipaddress {
        47.105.66.202            # 定义虚拟ip(VIP),可多设,每行一个
    }
}

backup上的keepalived.conf内容如下:

global_defs {
    notification_email {
        [email protected]
    }
    notification_email_from [email protected]
    smtp_server localhost
    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 eth0                      # 当前进行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 {
        47.105.66.202                   # 定义虚拟ip(VIP),可多设,每行一个
    }
}

nginx检测脚本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重启失败
        exit 1
    else
        exit 0
    fi
else
    exit 0
fi

启动keepalived:

service keepalived start

访问VIP,然后去看 keepalived 的日志 ,当我们把 master 上的 keepalived 停掉(模拟宕机),再来看下 keepalived 日志 ,通过VIP可以正常访问服务,前端请求感受不到后端nginx的切换。这样就完成了一个负载均衡、主从热备的高可用的服务器架构了。

你可能感兴趣的:(linux,运维,负载均衡)