服务器负载均衡nginx+keepalived 高可用负载均衡
最近研究服务器负载均衡,稍微总结一下,以后继承补充:空话就不多说了,nginx安装与置配,还有负载均衡呢,可以看我写的另外一篇文章《nginx负载均衡实战》,还有关于负载均衡呢,大家可以看一下我写的另外两篇文章,一个是《lvs+keepalived负载均衡》,另外一个是《haproxy+keepalived负载均衡》,三种负载均衡的别区呢,可以看一下我转载的一篇文章《软件级负载均衡器(LVS/HAProxy/Nginx)的特色简介和比对》,面下直接进入置配骤步:
1.系统环境
系统本版:CentOS release 5.9 (Final) x86 32位
nginx本版: 1.2.8
keepalived本版: 1.2.4
主keepalived:192.168.207.130
从keepalived:192.168.207.131
VIP:192.168.207.140
WEB_1:192.168.207.129 80端口
WEB_2:192.168.207.130 8080端口
WEB_3:192.168.207.131 8080端口
2.自定义nginx置配文件
在192.168.207.130和192.168.207.131上操纵
useradd nginx
vi /usr/local/nginx/conf/nginx.conf
容内如下:
#行运用户
user nginx nginx;
#启动进程
worker_processes 2;
#全局错误志日及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#任务式模及每一个进程连接数限上
events {
use epoll;
worker_connections 1024; #所以nginx持支的总连接数就于等worker_processes * worker_connections
}
#设定http服务器,用利它的向反理代功能供给负载均衡持支
http {
#设定mime类型
include mime.types;#这个是说nginx持支哪些多媒体类型,可以到conf/mime.types看查持支哪些多媒体
default_type application/octet-stream; #默许的数据类型
#设定志日格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#设定求请缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#开启gzip模块
#gzip on;
#gzip_min_length 1100;
#gzip_buffers 4 8k;
#gzip_types text/plain;
#output_buffers 1 32k;
#postpone_output 1460;
#设定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth数参表现权值,权值越高被分配到的概率越大
server 192.168.207.129:80 weight=5;
server 192.168.207.130:8080 weight=5;
server 192.168.207.131:8080 weight=5;
}
server { #这个是设置web服务的,监听8080端口
listen 8080;
server_name 192.168.207.131; #这个根据系统ip化变
index index.html index.htm;
root /var/www/html;
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root html;
#}
}
#设定虚拟主机
server {
listen 80;
server_name 192.168.207.140; #这里是VIP
#charset gb2312;
#设定本虚拟主机的拜访志日
access_log logs/three.web.access.log main;
#如果拜访 /img/*, /js/*, /css/* 资源,则直接取当地文件,不通过squid
#如果这些文件较多,不推荐种这式方,因为通过squid的缓存效果更好
#location ~ ^/(img|js|css)/{
# root /data3/Html;
# expires 24h;
#}
#对 "/" 启用负载均衡
location / {
proxy_pass http://mysvr;#以种这格式来应用端后的web服务器
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#设定看查Nginx状态的地址 ,在安装时要加上--with-http_stub_status_module数参
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd; #设置拜访密码,htpasswd -bc filename username password
}
}
}
每日一道理
航行者把树比作指引方向的路灯,劳动者把树比作遮风挡雨的雨伞,诗人把树比作笔下的精灵,而我却要把树比作教师,它就是为我们遮风挡雨的伞,指明方向的路灯,开打知识殿堂的金钥匙。
3.自定义keepalived置配文件
vi /etc/keepalived/keepalived.conf
容内如下:
global_defs {
notification_email {
root@localhost.localdomain
}
notification_email_from notify@keepalived.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/check_nginx.sh" ###监控脚本
interval 2 ###监控时光
weight 2 ###前目搞不清楚
}
vrrp_instance VI_1 {
state MASTER ### 设置为 主
interface eth0 ### 监控网卡
virtual_router_id 51 ### 这个两台服务器必须一样
priority 101 ### 权重值 MASTRE 一定要高于 BAUCKUP
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.207.140 ### VIP 地址
}
}
4.写自定义脚本
vi /etc/keepalived/check_nginx.sh
容内如下:
!/bin/bash
A=`ps -C nginx --no-header |wc -l` ## 看查是不是有 nginx进程 把值赋给变量A
if [ $A -eq 0 ];then ## 如果没有进程值得为 零
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop ## 则束结 keepalived 进程
fi
fi
这里是查检nginx是不是启动好,如果没有启动,先启动 nginx,隔了3秒后还没有启动好,则将keepalived进程也闭关,这样从keepalived能就手接过去了,供给高可用性,在这里呢,keepalived服务是供给高可用性,而nginx是供给端后web服务器的负载均衡。
这里还要给脚本加上执行权限,如下
chmod +x /etc/keepalived/check_nginx.sh
5.启动服务,并测试
在这里先说一下啊,在WEB_1上,我是应用系统自带的apache昨晚web服务器的,比拟事省,这样呢,我只要启动好主从keepalived就ok了,因为它会用利check_nginx.sh脚本来动自启动nginx。
都启动好了。
拜访http://192.168.207.140以可就轮训拜访端后的三台web服务器容内啦
这里我们把主keepalived服务给关掉,来测试高可用性
然后会在从keepalived服务器上的/var/log/messages看到这样的志日
Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added
Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 added
Apr 19 17:42:45 localhost avahi-daemon: Registering new address record for 192.168.207.140 on eth0.
继承拜访http://192.168.207.140,旧依可以拜访端后的三台web服务器
然后再把原主keepalived开打,可以观察到原从keepalived服务器的志日示显
Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed
Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 removed
Apr 19 17:44:06 localhost avahi-daemon: Withdrawing address record for 192.168.207.140 on eth0.
明说有恢复了本来的主从结果。
出产环境中,端后的器机也可能会挂掉,但是呢,这就不必你费心啦,nginx会动自把session分配到好的端后web服务器上的啦
ok,到这里全体束结了,践实亲测,祝君功成
文章结束给大家分享下程序员的一些笑话语录: 大家喝的是啤酒,这时你入座了。
你给自己倒了杯可乐,这叫低配置。
你给自已倒了杯啤酒,这叫标准配置。
你给自己倒了杯茶水,这茶的颜色还跟啤酒一样,这叫木马。
你给自己倒了杯可乐,还滴了几滴醋,不仅颜色跟啤酒一样,而且不冒热气还有泡泡,这叫超级木马。
你的同事给你倒了杯白酒,这叫推荐配置。
菜过三巡,你就不跟他们客气了。
你向对面的人敬酒,这叫p2p。
你向对面的人敬酒,他回敬你,你又再敬他……,这叫tcp。
你向一桌人挨个敬酒,这叫令牌环。
你说只要是兄弟就干了这杯,这叫广播。
有一个人过来向这桌敬酒,你说不行你先过了我这关,这叫防火墙。
你的小弟们过来敬你酒,这叫一对多。
你是boss,所有人过来敬你酒,这叫服务器。
酒是一样的,可是喝酒的人是不同的。
你越喝脸越红,这叫频繁分配释放资源。
你越喝脸越白,这叫资源不释放。
你已经醉了,却说我还能喝,叫做资源额度不足。
你明明能喝,却说我已经醉了,叫做资源保留。
喝酒喝到最后的结果都一样
你突然跑向厕所,这叫捕获异常。
你在厕所吐了,反而觉得状态不错,这叫清空内存。
你在台面上吐了,觉得很惭愧,这叫程序异常。
你在boss面前吐了,觉得很害怕,这叫系统崩溃。
你吐到了boss身上,只能索性晕倒了,这叫硬件休克。
页:
[1]