负载均衡Nginx+KeepAlived
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了负载均衡Nginx+KeepAlived相关的知识,希望对你有一定的参考价值。
192.168.1.150 VOIP 虚拟IP192.168.1.151 CNSHANK01 负载均衡1
192.168.1.152 CNSHANK02 负载均衡2
系统:Centos7.4
一、nginx配置(两台nginx配置一模一样,主页设置不同):
- 安装:
yum -y install nginx - 配置:
vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 12800;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
limit_conn_zone $binary_remote_addr zone=addr:10m; #防IP***
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types; #协助部分浏览器(如firefox)识别网页文件的类型
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
client_body_buffer_size 1k; #指定客户端请求主体缓冲区大小
client_header_buffer_size 1k; #指定来自客户端请求头的headerbuffer大小
large_client_header_buffers 2 1k; #客户端请求中较大的消息头指定的缓存最大数量和大小
server_tokens off; #禁止在错误页面上显示nginx版本号
client_body_timeout 20; #设置客户端请求主体读取超时时间
client_header_timeout 20; #设置客户端请求头读取超时时间
gzip on;
upstream dynamic { #负载服务器名dynamic
ip hash; #采用ip_hash方式
server 192.168.1.154:80;
server 192.168.1.155:80;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name blog.dollarphp.com;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
limit_conn addr 100; #指定每个ip最多只允许建立100个连接
limit_rate 500k; #每个ip最大带宽是500k
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
index index.html index.htm;
autoindex on;
autoindex_localtime on;
}
location ~* ^.+.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
root html;
}
location ~* ^.+.(?![js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma])$ {
proxy_pass http://dynamic;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
-
设置配置主页测试文件
cd /usr/share/nginx/html
cp ./index.html ./index.html.bak
echo “CNSHANK01-192.168.1.151”>./index.html #在主机192.1681.151上
echo “CNSHANK02-192.168.1.152”>./index.html #在主机192.1681.152上 - 启动
systemctl start nginx
systemctl enable nginx
二、keepalived配置:
I、主服务器配置(192.168.1.151):
- 安装:
yum -y install keepalived -
配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/check.sh"
interval 1
weight -15
}
vrrp_instance VI_1 { # 实例,一个vip一个实例
state MASTER #标识为主服务
interface ens33
virtual_router_id 51 #与从机保持一致即可
priority 100 #权重 ,高于从机
advert_int 1
authentication { # 认证授权,设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 123456
}
virtual_ipaddress { #虚拟ip地址
192.168.1.150
}
track_script { #nginx监控服务
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
} -
添加服务检查脚本(check.sh):
vim /etc/keepalived/check.sh
#!/bin/bash
if [ "pgrep nginx | wc -l
" == "0" ] ; then
exit 1
else
exit 0
fi - 通知脚本(notify.sh):
vim /etc/keepalived/notify.sh
#!/bin/bash
if [ "$1" == "master" ] ; then
echo "date ‘+%F %T‘
:切换到主机模式" >> /var/log/keepalived/keepalived.log
elif [ "$1" == "backup" ] ; then
/usr/bin/systemctl start nginx
echo "date ‘+%F %T‘
:切换到备机模式" >> /var/log/keepalived/keepalived.log
else
/usr/bin/systemctl start nginx
echo "date ‘+%F %T‘
:宕机" >> /var/log/keepalived/keepalived.log
fi -
给脚本添加可执行权限:
chmod +x /etc/keepalived/check.sh
chmod +x /etc/keepalived/notify.sh -
创建日志目录:
mkdir /var/log/keepalived - 启动:
systemctl start keepalived
II、备服务器配置(192.168.1.152):
-
安装:
yum -y install keepalived -
配置:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
}
vrrp_script chk_nginx {
script "/etc/keepalived/check.sh"
interval 1
weight -15
}
vrrp_instance VI_1 {
state BACKUP #BACKUP模式
interface ens33
virtual_router_id 51 #与主机一致
priority 99 #比主机小
advert_int 1
authentication { #与主机一致
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.1.150
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
} - 添加服务检查脚本(check.sh):
vim /etc/keepalived/check.sh
#!/bin/bash
if [ "pgrep nginx | wc -l
" == "0" ] ; then
exit 1
else
exit 0
fi - 通知脚本(notify.sh):
vim /etc/keepalived/notify.sh
#!/bin/bash
if [ "$1" == "master" ] ; then
echo "date ‘+%F %T‘
:切换到主机模式" >> /var/log/keepalived/keepalived.log
elif [ "$1" == "backup" ] ; then
/usr/bin/systemctl start nginx
echo "date ‘+%F %T‘
:切换到备机模式" >> /var/log/keepalived/keepalived.log
else
/usr/bin/systemctl start nginx
echo "date ‘+%F %T‘
:宕机" >> /var/log/keepalived/keepalived.log
fi - 给脚本添加可执行权限:
chmod +x /etc/keepalived/check.sh
chmod +x /etc/keepalived/notify.sh - 创建日志目录:
mkdir /var/log/keepalived - 启动:
systemctl start keepalived
三、测试:
I、正常状态下
II、停止主服务器:192.168.1.151后测试
- 停止从服务器nginx服务,结果可以看到访问虚拟IP时候会从主服务器切到从服务器
systemctl stop nginx
-
查看日志:
tail /var/log/keepalived/keepalived.log
主从机日志都显示切到备机模式测试完成
以上是关于负载均衡Nginx+KeepAlived的主要内容,如果未能解决你的问题,请参考以下文章