当前位置:Gxlcms > PHP教程 > nginx+keepalived高可用负载均衡

nginx+keepalived高可用负载均衡

时间:2021-07-01 10:21:17 帮助过:47人阅读

话就不多说了,nginx安装与配置,还有负载均衡呢,可以看我写的另一篇文章《nginx负载均衡实战》,还有关于负载均衡呢,大家可以看一下我写的另外两篇文章,一个是《lvs+keepalived负载均衡》,另一个是《haproxy+keepalived负载均衡》,三种负载均衡的区别呢,可以看一下我转载的一篇文章《软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比》,下面直接进入配置步骤:

1.系统环境

[plain] view plaincopy

  1. 系统版本:CentOS release 5.9 (Final) x86 32位
  2. nginx版本: 1.2.8
  3. keepalived版本: 1.2.4
  4. 主keepalived:192.168.207.130
  5. 从keepalived:192.168.207.131
  6. VIP:192.168.207.140
  7. WEB_1:192.168.207.129 80端口
  8. WEB_2:192.168.207.130 8080端口
  9. WEB_3:192.168.207.131 8080端口

2.自定义nginx配置文件


在192.168.207.130和192.168.207.131上操作

[plain] view plaincopy

  1. useradd nginx
  2. vi /usr/local/nginx/conf/nginx.conf


内容如下:

[plain] view plaincopy

  1. #运行用户
  2. user nginx nginx;
  3. #启动进程
  4. worker_processes 2;
  5. #全局错误日志及PID文件
  6. error_log logs/error.log notice;
  7. pid logs/nginx.pid;
  8. #工作模式及每个进程连接数上限
  9. events {
  10. use epoll;
  11. worker_connections 1024; #所以nginx支持的总连接数就等于worker_processes * worker_connections
  12. }
  13. #设定http服务器,利用它的反向代理功能提供负载均衡支持
  14. http {
  15. #设定mime类型
  16. include mime.types; #这个是说nginx支持哪些多媒体类型,可以到conf/mime.types查看支持哪些多媒体
  17. default_type application/octet-stream; #默认的数据类型
  18. #设定日志格式
  19. log_format main '$remote_addr - $remote_user [$time_local] '
  20. '"$request" $status $bytes_sent '
  21. '"$http_referer" "$http_user_agent" '
  22. '"$gzip_ratio"';
  23. log_format download '$remote_addr - $remote_user [$time_local] '
  24. '"$request" $status $bytes_sent '
  25. '"$http_referer" "$http_user_agent" '
  26. '"$http_range" "$sent_http_content_range"';
  27. #设定请求缓冲
  28. client_header_buffer_size 1k;
  29. large_client_header_buffers 4 4k;
  30. #开启gzip模块
  31. #gzip on;
  32. #gzip_min_length 1100;
  33. #gzip_buffers 4 8k;
  34. #gzip_types text/plain;
  35. #output_buffers 1 32k;
  36. #postpone_output 1460;
  37. #设定access log
  38. access_log logs/access.log main;
  39. client_header_timeout 3m;
  40. client_body_timeout 3m;
  41. send_timeout 3m;
  42. sendfile on;
  43. tcp_nopush on;
  44. tcp_nodelay on;
  45. keepalive_timeout 65;
  46. #设定负载均衡的服务器列表
  47. upstream mysvr {
  48. #weigth参数表示权值,权值越高被分配到的几率越大
  49. server 192.168.207.129:80 weight=5;
  50. server 192.168.207.130:8080 weight=5;
  51. server 192.168.207.131:8080 weight=5;
  52. }
  53. server { #这个是设置web服务的,监听8080端口
  54. listen 8080;
  55. server_name 192.168.207.131; #这个根据系统ip变化
  56. index index.html index.htm;
  57. root /var/www/html;
  58. #error_page 500 502 503 504 /50x.html;
  59. #location = /50x.html {
  60. # root html;
  61. #}
  62. }
  63. #设定虚拟主机
  64. server {
  65. listen 80;
  66. server_name 192.168.207.140; #这里是VIP
  67. #charset gb2312;
  68. #设定本虚拟主机的访问日志
  69. access_log logs/three.web.access.log main;
  70. #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
  71. #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
  72. #location ~ ^/(img|js|css)/{
  73. # root /data3/Html;
  74. # expires 24h;
  75. #}
  76. #对 "/" 启用负载均衡
  77. location / {
  78. proxy_pass http://mysvr; #以这种格式来使用后端的web服务器
  79. proxy_redirect off;
  80. proxy_set_header Host $host;
  81. proxy_set_header X-Real-IP $remote_addr;
  82. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  83. client_max_body_size 10m;
  84. client_body_buffer_size 128k;
  85. proxy_connect_timeout 90;
  86. proxy_send_timeout 90;
  87. proxy_read_timeout 90;
  88. proxy_buffer_size 4k;
  89. proxy_buffers 4 32k;
  90. proxy_busy_buffers_size 64k;
  91. proxy_temp_file_write_size 64k;
  92. }
  93. #设定查看Nginx状态的地址 ,在安装时要加上--with-http_stub_status_module参数
  94. location /NginxStatus {
  95. stub_status on;
  96. access_log on;
  97. auth_basic "NginxStatus";
  98. auth_basic_user_file conf/htpasswd; #设置访问密码,htpasswd -bc filename username password
  99. }
  100. }
  101. }


3.自定义keepalived配置文件

[plain] view plaincopy

  1. vi /etc/keepalived/keepalived.conf


内容如下:

[plain] view plaincopy

  1. global_defs {
  2. notification_email {
  3. root@localhost.localdomain
  4. }
  5. notification_email_from notify@keepalived.com
  6. smtp_server 127.0.0.1
  7. smtp_connect_timeout 30
  8. router_id LVS_DEVEL
  9. }
  10. vrrp_script chk_http_port {
  11. script "/etc/keepalived/check_nginx.sh" ###监控脚本
  12. interval 2 ###监控时间
  13. weight 2 ###目前搞不清楚
  14. }
  15. vrrp_instance VI_1 {
  16. state MASTER ### 设置为 主
  17. interface eth0 ### 监控网卡
  18. virtual_router_id 51 ### 这个两台服务器必须一样
  19. priority 101 ### 权重值 MASTRE 一定要高于 BAUCKUP
  20. authentication {
  21. auth_type PASS
  22. auth_pass 1111
  23. }
  24. track_script {
  25. chk_http_port ### 执行监控的服务
  26. }
  27. virtual_ipaddress {
  28. 192.168.207.140 ### VIP 地址
  29. }
  30. }


4.写自定义脚本

[plain] view plaincopy

  1. vi /etc/keepalived/check_nginx.sh


内容如下:

[plain] view plaincopy

  1. !/bin/bash
  2. A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
  3. if [ $A -eq 0 ];then ## 如果没有进程值得为 零
  4. /usr/local/nginx/sbin/nginx
  5. sleep 3
  6. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  7. /etc/init.d/keepalived stop ## 则结束 keepalived 进程
  8. fi
  9. fi


这里是检查nginx是否启动好,如果没有启动,先启动 nginx,隔了3秒后还没有启动好,则将keepalived进程也关闭,这样从keepalived就能接手过去了,提供高可用性,在这里呢,keepalived服务是提供高可用性,而nginx是提供后端web服务器的负载均衡。
这里还要给脚本加上执行权限,如下

[plain] view plaincopy

  1. 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看到这样的日志

[plain] view plaincopy

  1. Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
  2. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
  3. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
  4. Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
  5. Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 added
  6. Apr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 added
  7. Apr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.


继续访问http://192.168.207.140,依旧可以访问后端的三台web服务器
然后再把原主keepalived打开,可以观察到原从keepalived服务器的日志显示

[plain] view plaincopy

  1. Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140
  2. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
  3. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
  4. Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
  5. Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removed
  6. Apr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 removed
  7. Apr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.


说明有恢复了原来的主从结果。
生产环境中,后端的机器也可能会挂掉,但是呢,这就不用你操心啦,nginx会自动把session分配到好的后端web服务器上的啦

ok,到这里全部结束了,实践亲测,祝君成功

From: http://blog.csdn.net/zmj_88888888/article/details/8825471

以上就介绍了nginx+keepalived 高可用负载均衡,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行