CentOS环境下Nginx实现3台虚拟机负载均衡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS环境下Nginx实现3台虚拟机负载均衡相关的知识,希望对你有一定的参考价值。
参考技术A负载均衡
先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。
测试环境
在VMware里安装了三台。
A服务器IP :192.168.0.219 (主)
B服务器IP :192.168.0.119
C服务器IP :192.168.0.109
部署思路
A服务器做为主服务器,域名直接解析到A服务器(192.168.0.219)上,由A服务器负载均衡到B服务器(192.168.0.119)与C服务器(192.168.0.109)上。
在A服务器上,upstream指令——分配负载
vi /etc/nginx/conf.d/default.conf
upstream 192.168.0.219
server 192.168.0.119:80;
server 192.168.0.109:80;
server
listen 80;
server_name 192.168.0.219;
charset utf8;
location /
proxy_pass
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
保存重启nginx
在B、C服务器上,
vi /etc/nginx/conf.d/default.conf
server
listen 80;
server_name 192.168.0.219;
index index.html;
root /usr/share/nginx/html;
保存重启nginx
测试
当访问的时候,为了区分是转向哪台服务器处理我分别在B、C服务器下写一个不同内容的index.html文件,以作区分。
打开浏览器访问a.com结果,刷新会发现所有的请求均分别被主服务器(192.168.5.149)分配到B服务器(192.168.0.119)与C服务器(192.168.0.109)上,实现了负载均衡效果。
主服务器不能提供服务吗?
以上例子中,我们都是应用到了主服务器负载均衡到其它服务器上,那么主服务器本身能不能也加在服务器列表中,这样就不会白白浪费拿一台服务器纯当做转发功能,而是也参与到提供服务中来。
怎么解决这个问题呢?因为80端口已经用来监听负载均衡的处理,那么本服务器上就不能再使用80端口来处理192.168.0.219的访问请求,得用一个新的。
于是我们在主服务器中编辑/etc/nginx/conf.d/default.conf,添加以下内容
server
listen 8080;
server_name 192.168.0.219;
index index.html;
root /usr/share/nginx/html;
重启nginx
然后,再重新渡负载均衡。
更多Nginx相关教程见以下内容 :
使用Nginx搭建WEB服务器
搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程
CentOS 6.3下Nginx性能调优
CentOS 6.3下配置Nginx加载ngx_pagespeed模块
CentOS 6.4安装配置Nginx+Pcre+php-fpm
Nginx安装配置使用详细笔记
Nginx日志过滤 使用ngx_log_if不记录特定日志
Nginx负载均衡+keepalived高可用
注:环境介绍:VMware环境下4台虚拟机 两台做负载均衡服务器 两台做realserver (模拟物理web服务器)
1 配置keepalived实现nginx负载均衡高可用,keepalived 主要功能是是实现主备负载 均衡服务器之间的高可用,当一台宕机另一台会自动接管.实现热备。
2 在主备负载均衡服务器个配置nginx反向代理服务,并在本地(你的windows电 脑)/etc/hosts 做域名解析。
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id 130
}
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth2
virtual_router_id 130
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.232.80/24 dev eth0 label eth0:1
}
}
virtual_server 192.168.232.80 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
}
lvs01-nginx
[[email protected] ~]# cat /application/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format commonlog ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
include extra/upstream01.conf;
}
[[email protected] ~]# cat /application/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format commonlog ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status
$body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
include extra/upstream01.conf;
}
[[email protected]~]#cat /application/nginx/conf/extra/upstream01.conf
#blog lb by cyt at 20180107
upstream server_pools { server 192.168.232.132:80 weight=5;
server 192.168.232.133:80 weight=5;
}
server {
listen 80;
server_name www.etiantian01.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
lvs02:
[[email protected] ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id 131
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
lvs_sync_daemon_inteface eth2
virtual_router_id 130
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.232.80/24 dev eth0 label eth0:1
}
}
virtual_server 192.168.232.80 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
}
lvs02-nginx
[[email protected] ~]# cat /application/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format commonlog ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
include extra/upstream01.conf;
}
[[email protected]~]#cat /application/nginx/conf/extra/upstream01.conf
#blog lb by cyt at 20180107
upstream server_pools {
server 192.168.232.132:80 weight=5;
server 192.168.232.133:80 weight=5;
}
server {
listen 80;
server_name www.etiantian01.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
web01 和web02部署nginx和抑制ARP和绑定vip (后面两部写脚本执行)
[[email protected] html]# ps -ef | grep nginx
root 1342 1 0 11:20 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 1343 1342 0 11:20 ? 00:00:00 nginx: worker process
root 1346 1298 0 11:29 pts/0 00:00:00 grep nginx
[[email protected] html]# netstat -lnt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
[[email protected] html]# lsof -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 1342 root 6u IPv4 11119 0t0 TCP *:http (LISTEN)
nginx 1343 nginx 6u IPv4 11119 0t0 TCP *:http (LISTEN)
[[email protected] html]#
然后抑制arp和绑定vip我们写如下脚本实现:
#!/bin/bash
# chkconfig: 3 52 23
#created by chenyant 11:43 2013/8/11
. /etc/init.d/functions
VIP=(
192.168.232.80
)
start(){
for ((i=0;i<${#VIP[@]};i++))
do
ifconfig lo:$i ${VIP[$i]} netmask 255.255.255.255 up
route add -host ${VIP[$i]} dev lo
done
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
}
start
服务启动后脚本执行了以后然后就检查:
可以模拟主备负载均衡服务器宕机 然后客户端持续访问看下是否会有影响。
拆除单台webserver 看下访问是否会受影响。
此文的只是模拟环境学习使用,生产环境相对复杂些需要结合实践情况学习。
以上是关于CentOS环境下Nginx实现3台虚拟机负载均衡的主要内容,如果未能解决你的问题,请参考以下文章