nginx+keepalived双主热备

环境准备:windows 2016安装vm虚拟机

三个ip必须互通,在同一段

Server01 192.168.88.128

Server02 192.168.88.135

Vip:192.168.88.230

客户端测试:192.168.88.1  windows 2016的浏览器

1,两台都设置时间同

 yum -y install ntp ntpdate

 ntpdate 0.asia.pool.ntp.org

2,两台关闭selinux和防火墙

sed  -i  s#SELINUX=enforcing#SELINUX=disabled#   /etc/selinux/config

setenforce 0  ##临时关闭

 

systemctl stop firewalld

systemctl disable firewalld

3,同步hosts  设置密钥登陆

[root@server01 ~]# ssh-keygen

[root@server01 ~]# ssh-copy-id  root@192.168.88.135

[root@server01 ~]# scp /etc/hosts root@192.168.88.135:/etc/hosts

 

4,安装软件:两台

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

 

yum install nginx keepalived pcre-devel  -y

 systemctl restart nginx

 systemctl enable nginx

5,测试:

 

 

 

6,修改配置keepalived

主192.168.88.128

[root@server01 keepalived]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

        router_id server01 ##标识节点的字符串,通常为hostname

}

vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh" ##执行脚本位置

    interval 2 ##检测时间间隔

    weight -20 ## 如果条件成立则权重减20(-20)

}

## 定义虚拟路由 VI_1为自定义标识。

vrrp_instance VI_1 {

state MASTER   ## 主节点为MASTER,备份节点为BACKUP

    ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth6)

interface ens33  

virtual_router_id 147  ## 虚拟路由ID号

mcast_src_ip 192.168.88.128  ## 本机ip地址

     priority 100  ##优先级配置(0-254的值)

     Nopreempt  ##

     advert_int 1 ## 组播信息发送间隔,俩个节点必须配置一致,默认1s

     authentication {  

        auth_type PASS

        auth_pass xjd ## 真实生产环境下对密码进行匹配

     }

    track_script {

        chk_nginx

    }

    virtual_ipaddress {

        192.168.88.230 ## 虚拟ip(vip),可以指定多个

    }

}

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 

备192.168.88.135

[root@server02 keepalived]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

        router_id server02 ##标识节点的字符串,通常为hostname

}

vrrp_script chk_nginx {

    script "/etc/keepalived/nginx_check.sh" ##执行脚本位置

    interval 2 ##检测时间间隔

    weight -20 ## 如果条件成立则权重减20(-20)

}

## 定义虚拟路由 VI_1为自定义标识。

vrrp_instance VI_1 {

state BACKUP   ## 主节点为MASTER,备份节点为BACKUP

    ## 绑定虚拟IP的网络接口(网卡),与本机IP地址所在的网络接口相同(我这里是eth6)

interface ens33  

virtual_router_id 147  ## 虚拟路由ID号

mcast_src_ip 192.168.88.138  ## 本机ip地址

     priority 90  ##优先级配置(0-254的值)

     Nopreempt  ##

     advert_int 1 ## 组播信息发送间隔,俩个节点必须配置一致,默认1s

     authentication {  

        auth_type PASS

        auth_pass xjd ## 真实生产环境下对密码进行匹配

     }

    track_script {

        chk_nginx

    }

    virtual_ipaddress {

        192.168.88.230 ## 虚拟ip(vip),可以指定多个

    }

}

 

7,配置nginx检测脚本,两台路径都必须一样

[root@server01 keepalived]# cat /etc/keepalived/nginx_check.sh

 

#!/bin/bash

A=`ps -C nginx --no-header | wc -l`

if [ $A -eq 0 ];then

        killall keepalived

fi

Systemctl start keepalived

8,测试停止nginx 查看vip  页面测试

 

vip到server02上面去了 秒级切换

 

当主服务器的nginx恢复了再启动keepalived

 

 

Nginx+keepalived前端+php后端负载均衡

 

前面配置好前端双主热备

 

一,环境准备,最好是用四台服务器要好点,我这里是配置代理自己和另外一台服务器

主Server01 192.168.88.128

备Server02 192.168.88.135

 

Vip:192.168.88.230

域名www.test.com 解析到vip 192.168.88.230

 

1,主服务器nginx配置

[root@server01 conf.d]# cd /etc/nginx/conf.d/

[root@server01 conf.d]# cp default.conf test.conf

(1)代理default.conf 配置

[root@server01 conf.d]# cat default.conf

upstream myserver{ #定义upstream名字,下面会引用

server 192.168.88.128:8080 max_fails=2 fail_timeout=10s; #指定后端服务器地址默认调度一样

server 192.168.88.135:8080 max_fails=2 fail_timeout=10s; #指定后端服务器地址默认调度一样

#server 192.168.1.120; #指定后端服务器地址

}

server {

listen 80;

server_name www.test.com;

location / {

proxy_pass http://myserver;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

access_log /etc/nginx/logs/access.log;

error_log /etc/nginx/logs/error.log;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

}

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

(2)后端php配置test.conf

[root@server01 conf.d]# cat test.conf

server {

listen 8080;

server_name localhost;

root /usr/share/nginx/html;

index index.php index.html index.htm;

 

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}

 

location ~ \.php$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;

include fastcgi_params;

}

}

#保存退出

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

重起nginx

[root@server01 conf.d]# systemctl restart nginx

 

2,测试:

Server01安装php-fpm并且启动

[root@server01 conf.d]# yum -y install php php-fpm php-mysql

[root@server01 conf.d]# systemctl start php-fpm

到/usr/share/nginx/html 下书写一个index.php

vi index.php

<?php

phpinfo();

?>

 

保存退出

 

首先测试8080端口访问是否正常

 

 

 

测试访问正常

 

然后再访问vip: 一个是php页面主服务器 再次刷新几次就是备用服务器的html页面

 

 

 

二,服务器nginx配置

和上面主服务器nginx配置一模一样

 

然后配置后面配置磁盘共享和mysql主从同步