nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > keepalived nginx双服务器主备

keepalived+nginx实现双服务器主备方案

作者:Should·L

本文主要介绍了使用keepalived和nginx实现双服务器主备方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、keepalived + nginx 主备方案

1. 架构图

服务器为CentOS 7系统

在这里插入图片描述

2. nginx 部署

两台服务器应用同样的配置,仅HTML文件不一样。

2.1 服务器环境初始化

(1)时间同步

# 安装时间同步工具ntpdata
yum -y install ntp ntpdate

# 同步网络时间
ntpdate cn.pool.ntp.org

(2)创建一个cron任务,设置每天同步服务器时间

# 打开cron作业任务列表的编辑器
crontab -e
# 每天0点同步一次
* 00 * * *  /usr/sbin/ntpdate cn.pool.ntp.org 

(3)关闭防火墙

在 windows 系统中访问 linux 中 nginx,默认不能访问的,因为防火墙问题 (1)关闭防火墙 (2)开放访问的端口号.

iptables和firewalld是两种不同的防火墙,两个都关闭,有哪个关哪个。

systemctl stop iptables    
systemctl stop firewalld

# 两种防火墙,个人比较习惯使用iptables,后面做策略也是使用iptables;没有的话可以安装一个;
yum install iptables-services   #安装这个比较方便管理。

或者,设置开放访问端口号

# 查看开放的端口号
firewall-cmd --list-all

# 设置开放的端口号
firewall-cmd --add-service=http –permanent 
firewall-cmd --add-port=80/tcp --permanent 

# 重启防火墙
firewall-cmd -reload

2.2 安装 nginx

(1)安装依赖包

pcre-8.3.7.tar.gz, openssl-1.0.1t.tar.gz, zlib-1.2.8.tar.gz

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

(2)下载并解压安装包

# 创建一个nginx文件夹
cd /usr/local
mkdir nginx
cd nginx

# 下载tar包
wget http://nginx.org/download/nginx-1.22.1.tar.gz

# 解压
tar -xvf nginx-1.22.1.tar.gz

# 进入nginx-1.22目录
cd nginx-1.22.1

(2)添加 ssl 证书模块

./configure --with-http_stub_status_module --with-http_ssl_module

(3)编译安装

make -j 4 && make install

(4)配置 nginx.conf

# root html; 改成自己的路径 /usr/local/nginx/html;
# index 后面加一个 index.php 否则无法显示网页内容,报403错误的。
    location / {
       root   /usr/local/nginx/html;
       index index.html index.php index.htm,;
server_tokens off;

(5)校验配置文件是否正确

/usr/local/nginx/sbin/nginx -t 

# 看到如下显示代表正常:
# nginx.conf syntax is ok
# nginx.conf test is successful

(6)启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
重新加载配置命令:
/usr/local/nginx/sbin/nginx -s reload

(7)验证

浏览器页面输入自己的服务器地址出现nginx字样的网页,表示安装成功:

(8)修改环境变量

添加环境变量,使nginx在任意目录都可以执行启动操作;

# 编辑/etc/profile文件在最后添加如下命令:
vim /etc/profile

# 添加命令
PATH=$PATH:/usr/local/nginx/sbin
export PAT

# 保存退出
:wq

# 执行生效命令
source /etc/profile 

所有的服务器部署方法相同

3. keepalived 部署

(1)安装依赖

yum install -y gcc openssl-devel libnl3-devel net-snmp-devel

(2)安装keepalived

yum install  keepalived -y

3.1 主 keepalived 配置文件解析

cd /etc/keepalived/
vim keepalived.conf
# --- ---- keepalived.conf --- ----

! Configuration File for keepalived

# 管理员邮箱(系统故障通知)
global_defs {
 notification_email {
 	acassen@firewall.loc
 	failover@firewall.loc
	sysadmin@firewall.loc
 }

notification_email_from Alexandre.Cassen@firewall.loc   # 邮件发件人邮箱
smtp_server 127.0.0.1	# SMTP服务器地址(邮箱服务器地址)
smtp_connect_timeout 30	#超时时间
router_id NGINX	# 路由器标识,一般不用改,也可改成应用名,主从服务器要一致
}

#一个vrrp_instance就是定义一个虚拟路由器的,实例名称
vrrp_script check_nginx {	 #脚本模块
	script "/etc/keepalived/check_nginx.sh"  #脚本模块
	interval 2  #每2秒钟检测一次脚本
#weight 10    #优先级
}

vrrp_instance VI_1 {
	state MASTER	# 定义初始状态,可以是MASTER或者BACKUP
	interface eth0	#当前使用的网卡名
	virtual_router_id 51	#虚拟路由id号
	priority 100	 #当前服务器优先级,数字越大越优先
	advert_int 1	#检测心跳时间为1秒
	authentication {	#身份验证方式通信认证机制,这里是明文认证还有一种是加密认证
    	auth_type PASS	#密码验证
    	auth_pass 1111	#密码为1111
    	
    	## 如果使用加密认证(所有节点必须一致)
    	# auth_type AH
    	# auth_pass <key>  # 通常为一个长度为8或者16的字符串
	}
virtual_ipaddress {
    192.168.10.100     #VIP地址
	}
track_script{        #添加监控条件
	check_nginx	 #脚本模块后边定义的名称
	}
}

3.2 监控nginx状态

新建一个脚本文件,并打开

vim /etc/keepalived/check_nginx.sh

编写监控脚本

#!/bin/bash
#1、判断 Nginx 是否存活
counter=`ps -C nginx --no-header | wc -l`
if [ $counter -eq 0 ]; then
#2、如果不存活则尝试启动 Nginx
/usr/local/nginx/sbin/nginx
sleep 5
#3、等待 5 秒后再次获取一次 Nginx 状态
counter=`ps -C nginx --no-header | wc -l`
#4、再次进行判断,如 Nginx 还不存活则停止 Keepalived,让地址进行漂移
	if [ $counter -eq 0 ]; then
	systemctl stop keepalived  #停用keepalived服务
	fi
fi

注意,这里的重启nginx命令/usr/local/nginx/sbin/nginx被注释掉,测试时关闭才能看到效果。

3.3 从 keepalived配置

! Configuration File for keepalived

global_defs {
notification_email {
 acassen@firewall.loc
 failover@firewall.loc
 sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX
}

vrrp_script check_nginx {  #这个地方名字不能乱改,系统不认识,
	script "/etc/keepalived/check_nginx.sh"
	interval 2  #每2秒钟检测一次脚本
}

vrrp_instance VI_1 {
	state BACKUP	#改这里
	interface eth0
	virtual_router_id 51
	priority 80		#改这里
	advert_int 1
	authentication {
    	auth_type PASS
    	auth_pass 1111
}
virtual_ipaddress {
    172.28.127.100
}
track_script {
	check_nginx
	}
}

keepalived相关命令

启动:systemctl start keepalived
重启:systemctl restart keepalived
动态持续查看日志:tail -f /var/log/messages

二、nginx 端口转发/反向代理

1. nginx 端口转发

1.1 修改nginx.conf

server {
    listen      19100;  # 监听19180端口
    server_name  192.168.248.10;

    location / {
            # 请求头转发
            proxy_set_header Host $host;
            # 获得真实ip
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # 转发到目标地址
            proxy_pass http://192.168.0.100:60080;
            
            # 还可以写root和index字段,只不过端口默认优先转发,当转发不成功时,才会选择走root字段下的目录和html
            # root /usr/local/nginx/html;
            # index index.html index.php index.htm;
        }	
    # ... ...
}

# 这个60086端口,目前没有服务,访问是被拒绝的!

保存退出

nginx -s reload;
# 输入vip地址访问
192.168.248.10:19180

1.2 分离nginx中的server模块为单独的文件

# 在nginx配置的最后一个中括号上方添加如下命令
include /usr/local/nginx/conf/conf.d/*.conf;

另外在conf.d文件夹下新建conf文件,单独编写server模块。

到这里配置就告一段落了,后面算是补充的 nginx 配置

2. nginx 多端口转发

2.1 配置文件

server {
	listen	8001;
	server_name	208.208.128.122;
	
	location ~/vod/ {
		proxy_pass	http://127.0.0.1:8081;
	}
	
	location ~/edu/ {
		proxy_pass	http://127.0.0.1:8082;
	}
}

2.1 匹配规则

2.3 实现效果

四、nginx 负载均衡

1. 配置说明

1.1 修改 nginx.conf

    # 定义myserver结构体
    upstream myserver {
        server 208.208.128.122:8081;
        server 208.208.128.122:8082;
    }
    
    server {
        listen       80;
        server_name  208.208.128.122;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            # 在这里调用
            #proxy_pass   http://myserver;
            proxy_pass   http://127.0.0.1:8081;
            index  index.html index.htm;
    }

1.2 实现效果

2. 修改负载均衡分配策略

upstream myserver {
    server 208.208.128.122:8081 weight=10;   #  在这儿
    server 208.208.128.122:8082 weight=10;
}
server {
    listen       80;
    server_name  208.208.128.122;
    location / {
        root   html;
        proxy_pass   http://myserver;
        index  index.html index.htm;
}
upstream myserver {
    ip_hash;		#  在这儿
    server 208.208.128.122:8081 ;   
    server 208.208.128.122:8082 ;
}
server {
    listen       80;
    server_name  208.208.128.122;
    location / {
        root   html;
        proxy_pass   http://myserver;
        index  index.html index.htm;
}
    upstream myserver {					
        server 208.208.128.122:8081 ;   
        server 208.208.128.122:8082 ;
        fair; 	#  在这儿
    }
    server {
        listen       80;
        server_name  208.208.128.122;
        location / {
            root   html;
            proxy_pass   http://myserver;
            index  index.html index.htm;
    }

五、架构

1. 动静分离

img

Nginx 动静分离简单来说就是把动态跟静态请求分开,不能理解成只是单纯的把动态页面和 静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用 Nginx 处理静态页面,Tomcat 处理动态页面。动静分离从目前实现角度来讲大致分为两种:

img

2. 高可用集群

keepalived + nginx 模式

img

3. 其他

这个值是表示每个 worker 进程所能建立连接的最大值,所以,一个 nginx 能建立的最大连接 数,应该是 worker_connections * worker_processes。当然,这里说的是最大连接数,对于 HTTP 请 求 本 地 资 源 来 说 , 能 够 支 持 的 最 大 并 发 数 量 是 worker_connections * worker_processes,如果是支持 http1.1 的浏览器每次访问要占两个连接,所以普通的静态访 问最大并发数是: worker_connections * worker_processes /2,而如果是 HTTP 作 为反向代 理来说,最大并发数量应该是 worker_connections * worker_processes/4。因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服 务的连接,会占用两个连接。

到此这篇关于keepalived+nginx实现双服务器主备方案的文章就介绍到这了,更多相关keepalived nginx双服务器主备内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文