keepalived高可用反向代理的nginx

 

环境准备                          

VIP(Virtual IP)为192.168.1.225,用户只需要访问这个IP地址即可获得网页服务

负载均衡主机为192.168.1.221(master) ----》keepalived+nginx

备机为 192.168.1.222(backup) ----》keepalived+nginx

Web服务器A为192.168.1.223(web01) ----》realserver + nginx 

 

192.168.1.221(master)主机配置            

root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install keepalived
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# echo "" > keepalived.conf
[root@localhost src]# cd /etc/keepalived/
[root@localhost keepalived]# cat keepalived.conf
global_defs {
    notification_email {
        1350748936@qq.com
    }
    notification_email_from sns-lvs@gmail.com
    smtp_server smtp.hysec.com
    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 ens33            # 当前进行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 {
        192.168.1.225            # 定义虚拟ip(VIP),可多设,每行一个
    }
}

nginx安装

[root@localhost keepalived]# tar xf nginx-1.12.2.tar.gz 
[root@localhost keepalived]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# yum -y install gcc*  pcre-devel zlib-devel
[root@localhost nginx-1.12.2]# useradd -r -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

#location / {

        #    root   html;

        #    index  index.html index.htm;

        #}

#注释掉

        location / {

        proxy_pass http://192.168.1.223;

        }

192.168.1.222(slave)备机配置                      

[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# vi keepalived.conf 

global_defs {
    notification_email {
        1350748936@qq.com
    }
    notification_email_from sns-lvs@gmail.com
    smtp_server smtp.hysec.com
    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 BACKUP           # 指定keepalived的角色,MASTER为主,BACKUP为备 
    interface ens33            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
    virtual_router_id 66        # 虚拟路由编号,主从要一直
    priority 90            # 优先级,数值越大,获取处理请求的优先级越高 
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
    chk_http_port            #(调用检测脚本)
    }
    virtual_ipaddress {
        192.168.1.225            # 定义虚拟ip(VIP),可多设,每行一个
    }
}
[root@localhost keepalived]# cat /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重启失败
        exit 1
    else
        exit 0
    fi
else
    exit 0
fi
[root@localhost ~]# chmod 775 /usr/local/src/check_nginx_pid.sh

nginx安装

[root@localhost keepalived]# tar xf nginx-1.12.2.tar.gz 
[root@localhost keepalived]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# yum -y install gcc*  pcre-devel zlib-devel
[root@localhost nginx-1.12.2]# useradd -r -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

#location / {

        #    root   html;

        #    index  index.html index.htm;

        #}

#注释掉

        location / {

        proxy_pass http://192.168.1.223;

        }

192.168.1.222这装nginx                    

[root@localhost ~]# tar xf nginx-1.12.2.tar.gz 
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# yum -y install gcc*  pcre-devel zlib-devel
[root@localhost nginx-1.12.2]# useradd -r -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/
[root@localhost nginx]# cd html/
[root@localhost html]# echo "This is test." > index.html
[root@localhost html]# curl localhost
This is test.

测试

[root@localhost ~]# curl 192.168.1.225
This is test.
[root@localhost ~]# curl 192.168.1.225
This is test.
[root@localhost ~]# curl 192.168.1.225
This is test.
[root@localhost ~]# curl 192.168.1.225
This is test.
[root@localhost ~]# curl 192.168.1.225
This is test.
[root@localhost ~]# curl 192.168.1.225
This is test.
posted @ 2019-12-13 10:40  星火撩原  阅读(290)  评论(0编辑  收藏  举报