nginx+keepalived 高可用兼负载均衡集群
Nginx是一个高性能的web服务器,同时也是一个优秀的反向代理服务器,本文利用两台Dell R720 构建一个高可用兼负载均衡的Linux web集群。
原理
通过nginx分别搭建两个web服务器,监听在本地非80端口;
然后利用nginx构建一个包含两个节点的负载均衡池;
最后通过keepalived实现负载均衡池的高可用。两个节点同时运行时,备用节点可以承载一半的前端流量,一个节点宕机后,负载均衡器通过健康检查,将失效节点踢出集群。
环境:CentOS 6.4 x86_64
软件 |
版本 |
nginx |
1.2.7 |
keepalived |
1.2.7 |
php-fpm |
5.4.13 |
主机名 |
IP地址 |
web01 |
192.168.122.10 |
web02 |
192.168.122.20 |
VIP |
192.168.122.30 |
安装nginx、keepalived和php-fpm等(编译过程略)
yum install nginx keepalived php-fpm php-gd php-xml php-mysql php-pecl-memcached php-pecl-sphinx watchodg -y chkconfig nginx on chkconfig keepalived on chkconfig php-fpm on chkconfig watchdog on |
配置nginx
nginx主配置文件 /etc/nginx/nginx.conf
user nginx; worker_processes 2; worker_rlimit_nofile 65535; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { use epoll; worker_connections 2048; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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; server_tokens off; #tcp_nopush on; keepalive_timeout 65; gzip on; gzip_static on; gzip_disable "msie6"; gzip_http_version 1.1; gzip_vary on; gzip_comp_level 6; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x$ gzip_buffers 16 8k; client_max_body_size 20m; client_body_buffer_size 128k; proxy_hide_header Vary; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; #proxy_temp_file_write_size 128k proxy_temp_path /dev/shm/tmp; proxy_cache_path /dev/shm/proxycache/ levels=1:2 keys_zone=shmcache:10m inactive=2h max_size=500m; include /etc/nginx/conf.d/*.conf; } |
网站主配置文件/etc/nginx/conf.d/default.conf
upstream webservers { ip_hash ; server 192.168.122.10:88 max_fails=3 fail_timeout=3s weight=2 ; server 192.168.122.20:88 max_fails=3 fail_timeout=3s weight=2 ; } server { listen 80 ; server_name localhost ; access_log /var/log/nginx/web01.access.log main; location / { proxy_redirect off; proxy_pass http://webservers ; proxy_cache shmcache; proxy_cache_valid 200 302 1d; proxy_cache_valid 404 1h; proxy_cache_valid any 10m; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_504; } location /status { stub_status on; access_log off; allow 192.168.122.0/24; } } server { listen 192.168.122.10:88; server_name localhost; root /usr/share/nginx/html; index index.html index.htm index.php; #charset koi8-r; location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?q=$1 last; } } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { #try_files $uri = 404; fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_buffer_size 128k; fastcgi_buffers 256 16k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_read_timeout 240; include fastcgi_params; } if ($fastcgi_script_name ~ \..*\/.*php) { return 403; } # deny access to hiden file . (filename begin with ".") location ~ /\. { access_log off; log_not_found off; deny all; } # deny access to bakup file .(any filename end with "~" ) location ~ ~$ { access_log off; log_not_found off; deny all; } # cache image file location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|swf)$ { expires 1d; } # don't log robots and favion location = /robots.txt { access_log off; log_not_found off; } location = /favicon.ico { access_log off; log_not_found off; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; } } |
配置php-fpm
php-fpm全称php fastcgi process manager,用于管理php的fastcgi进程,自从php5.3.3之后集成进了php源码中。默认的php-fpm用户是apache,我们需要修改成nginx,代码:
sed -i 's/apache/nginx/g' /etc/php-fpm.d/www.conf |
配置keepalived
keepalived是一个高可用软件,通过vrrp心跳来检测对方是否存活。
keepalived主配置文件
global_defs { notification_email { root@localhost } notification_email_from keepalived01@web01.test.org smtp_server 127.0.0.1 smtp_connect_timeout 30 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.122.30 label eth0:1 } } |
配置watchdog
watchdog是个健壮可靠的内核模块,即使在高负载的系统中仍然可以存活。它能够执行检测脚本,定时检查nginx和keepalived进程是否存在,如果不存在可以重新开启进程。
watchdog主配置文件/etc/watchdog.conf
min-memory = 1 repair-binary = /etc/watchdog.d/repair.sh test-binary = /etc/watchdog.d/test.sh test-timeout = 5 watchdog-device = /dev/watchdog admin = root interval = 10 logtick = 1 realtime = yes priority = 1 pidfile = /var/run/syslogd.pid pidfile = /var/run/nginx.pid pidfile = /var/run/keepalived.pid pidfile = /var/run/php-fpm/php-fpm.pid |
测试脚本,权限751
#!/usr/bin/env bash #author:purplegrape #desc: shell script to check if keepalived/nginx/php-fpm down. keepalived_proc=`pgrep keepalived |wc -l` nginx_proc=`pgrep nginx|wc -l` php_fpm_porc=`pgrep php-fpm|wc -l` if [$keepalived_proc == 0 ];then exit 1 fi if [$nginx_proc == 0 ];then exit 1 fi if [$php_fpm_proc == 0 ];then exit 1 fi |
修复脚本,权限751
#!/usr/bin/env bash #author:purplegrape #desc: shell script to repair keepalived/nginx/php-fpm. keepalived_proc=`pgrep keepalived |wc -l` nginx_proc=`pgrep nginx|wc -l` php_fpm_proc=`pgrep php-fpm|wc -l` if [$keepalived_proc == 0 ];then wall “keepalived is dead” /etc/init.d/keepalived restart sleep 30 /etc/init.d/nginx restart fi if [$nginx_proc == 0 ];then if [$keepalived_proc != 0 ];then wall “nginx is dead” /etc/init.d/nginx restart fi fi if [ $php_fpm_proc == 0 ]; then wall “php-fpm is dead” /etc/init.d/php-fpm restart fi |
测试
打开浏览器,访问地址http://192.168.122.30
nginx+keepalived 高可用兼负载均衡集群相关推荐
- keepalive+nginx实现负载均衡高可用_高可用、负载均衡 集群部署方案:Keepalived + Nginx + Tomcat...
前言:初期应用较小,一般以单机部署为主,即可满足业务的需求,随着业务的不断扩大,单机部署的模式无法承载这么大的业务量,需要进行服务集群化的部署,本文主要介绍服务器Tomcat多实例部署,搭载Keepa ...
- haproxy + keepalived + mycat 高可用与负载均衡集群配置 centos7
架构如上,但是其实keepalived.haproxy.Mycat都可以多台(比如keepalived.haproxy.Mycat各3台,3台keepalived抢占vip,然后抢到vip的hapro ...
- JMS之——ActiveMQ 高可用与负载均衡集群安装、配置(ZooKeeper + LevelDB + Static discovery)...
一.说明 从 ActiveMQ 5.9 开始, ActiveMQ 的集群实现方式取消了传统的 Master-Slave 方式,增加了基于ZooKeeper + LevelDB 的 Master-Sla ...
- 使用nginx实现动静分离的负载均衡集群
架构图 本次要实现的架构图: 工作中我们希望这样: 静态文件处理:可以使用nginx 或apache 动文件处理: apache ,tomcat 图片文件处理: squid 我们可以使用nginx实现 ...
- 分布式架构高可用架构篇_04_Keepalived+Nginx实现高可用Web负载均衡
一.场景需求 二.Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 ...
- Keepalived + Nginx 实现高可用 Web 负载均衡
在实际项目中,为了保证服务的高可用性,nginx单点是不行的,因为一旦nginx挂了,没有后备节点顶上去,web服务将会处于不可用状态,因此为了确保服务的高可用性,我们需要把nginx也做成类似于集群 ...
- nginx集群_使用Nginx+Tomcat+keepalived 搭建高性能高可用性负载均衡集群
在互联网项目中,往往面临着高用户量.高并发的问题,造成服务器的压力非常大,特别是电商项目,以淘宝天猫为例,近年双十一的成交量屡创新高,可想而知淘宝天猫的服务器面临的并发量有多大,单一服务器肯定承受不住 ...
- 高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群
高性能Linux服务器 第11章 构建高可用的LVS负载均衡集群 libnet软件包<-依赖-heartbeat(包含ldirectord插件(需要perl-MailTools的rpm包)) l ...
- 使用pgpool-ii 搭建postgresql 高可用、负载均衡架构
pgpool有很多功能,其中最重要的我觉得是如下几个:提供连接池(负载均衡模式),复制模式(能通过pgpool分发sql,因此是基于sql语句的分发复制),主备模式(依赖其他的复制,如snoly和流复 ...
最新文章
- UI Bootstrap
- python 写linux mysql_(linux)python之mysql数据库操作环境搭建
- WEB入门之十九 UI
- JS页面跳转的各种形式
- Redis 数据持久化的方案的实现
- r语言mfrow全程_如何使用R完成文章中图片处理小教程
- DeepFaceLab报错,CUDA driver is insufficient 解决方法!
- 使用LaTeX绘制列表(有的地方称作Num Item)
- PostgreSQL Pattern Matching
- html期末作品,走完HTML和CSS,进军期末
- Emacs进阶之选择当前word/line
- SAS安装时出现的问题:Diagram Control
- java一天一只顽猴想去从山脚_六年级上册语文一课一练(附答案)
- git语法大全(值得收藏)
- max pooling 和 average pooling
- 数据安全生命周期管理介绍(一)
- 清北学堂noip2019集训D6——动态规划
- 如何改进项目的经验教训总结会
- python中geometry_python arcgis Geometry
- 启动光盘制作完全手册下载
热门文章
- 将视频转换成音乐 - MP4toMP3!
- 智能马桶盖销售持续走高 京东数据显示四大趋势
- 对话张雪峰:云服务助力饿了么发展 云计算一定是未来
- java手机编程软件ios,赶紧收藏起来!
- Android-设备管理器Device Administration
- JVM——》G1垃圾收集器
- python中with open的用法_python中open和with open有什么区别?
- 【IT企业笔试集】2013年阿里巴巴实习生招聘笔试题目及解答
- php文本框的属性,在PHP中,为文本框设置“name”属性的方法是() 答案:为不同文本框表单元素分别设置不同的“name”属性值...
- 机器学习实战之基于概率论的分类方法:朴素贝叶斯