haproxy+keepalived+nginx+php-fpm

19508 단어 nginxkeepalivedhaproxy
수중 에 세 대의 기계 가 있 는데 지도자 가 높 은 사용 가능 한 사 이 트 를 만들어 달라 고 요구 하 는데 한 대의 데이터 베 이 스 를 만 드 는 것 은 틀림없이 맞 을 것 이다. 나머지 두 대의 기 계 는 웹 을 만 들 수 밖 에 없다.
 
       nginx + keepalived 를 사용 하려 고 했 는데 keepalived 를 높 게 사용 하 는 것 은 문제 가 없 었 습 니 다. 한 사람 이 따라 가 려 고 했 지만 기계 에서 완전히 standby 를 사용 하여 자원 이 낭비 되 었 습 니 다.그래서 부하 균형 군집 을 생각 했다.nginx 가 부하 균형 을 맞 추고 웹 을 하 는 것 을 감안 하여 관리 가 번 거 로 울 까 봐 부하 균형 은 haproxy 로 만 들 었 고 haproxy 의 건강 검 사 는 매우 잘 되 었 다.한편, nginx 의 건강 검 사 는 실제 적 으로 가짜 이 므 로 고장 전 이 를 해 야 합 니 다. timeout 이 기한 이 지나 면 데이터 가 나 뉘 어 통 하지 않 는 것 을 발견 한 후에 고장 전 이 를 하면 웹 페이지 의 간헐 적 로드 가 느 릴 수 있 기 때 문 입 니 다.
   본 고의 장점 은 서버 가 평소에 도 메 인 서버 의 데 이 터 를 분담 할 수 있 고 어떤 기계 가 다운 되 어도 웹 서비스 가 정상적으로 접근 할 수 있다 는 것 이다.
 
환경: CentOS 6.4 x8664
VIP          192.168.122.10
web01     192.168.122.11
web02     192.168.122.12
 
1. 설치 소프트웨어

  
  
  
  
  1. yum install haproxy keepalived nginx php-fpm php-gd php-mysql php-xml php-cli -y   
  2. chkconfig keepalived on  
  3. chkconfig haproxy on  
  4. chkconfig nginx on  
  5. chkconfig php-fpm on  
  6. chkconfig iptables off #  

2. 시스템 과 커 널 설정

  
  
  
  
  1. sed -i ‘s/enforcing/disabled/g' /etc/sysconfig/selinux 

  
  
  
  
  1. sysctl -w "net.ipv4.ip_nonlocal_bind = 1
  2. echo "net.ipv4.ip_nonlocal_bind = 1>>/etc/sysctl.conf 

위의 이 커 널 매개 변 수 는 서버 가 존재 하지 않 는 주소 에 감청 할 수 있 도록 합 니 다.
 
3. keepalived 설정 (주종 설정 이 약간 다 름)
홈 서버 웹 01 설정
 

  
  
  
  
  1. global_defs {  
  2.    notification_email {  
  3.      root@localhost  
  4.    }  
  5.    notification_email_from keepalived@localhost  
  6.    smtp_server 127.0.0.1  
  7.    smtp_connect_timeout 30  
  8.    router_id web01  
  9. }  
  10.   
  11. vrrp_instance VI_1 {  
  12.     state MASTER  
  13.     interface eth0  
  14.     virtual_router_id 51  
  15.     priority 100  
  16.     advert_int 1  
  17.     preempt   
  18.     authentication {  
  19.         auth_type PASS  
  20.         auth_pass 1111  
  21.     }  
  22.     virtual_ipaddress {  
  23.         192.168.122.10 label eth0:1  
  24.     }  
  25. }  

 
서버 에서 설정

  
  
  
  
  1. global_defs {  
  2.    notification_email {  
  3.      root@localhost  
  4.    }  
  5.    notification_email_from keepalived02@localhost  
  6.    smtp_server 127.0.0.1  
  7.    smtp_connect_timeout 30  
  8.    router_id web02  
  9. }  
  10.   
  11. vrrp_instance VI_1 {  
  12.     state BACKUP  
  13.     interface eth0  
  14.     virtual_router_id 51  
  15.     priority 90  
  16.     advert_int 1  
  17.     #preempt   
  18.     authentication {  
  19.         auth_type PASS  
  20.         auth_pass 1111  
  21.     }  
  22.     virtual_ipaddress {  
  23.         192.168.122.10 label eth0:1  
  24.     }  
  25. }  

 
4. haproxy 설정 (서버 설정 이 똑 같 고 감청 주소 192.168.12.10: 80)
 

  
  
  
  
  1. global  
  2.     log         127.0.0.1 local2  
  3.     chroot      /var/lib/haproxy  
  4.     pidfile     /var/run/haproxy.pid  
  5.     maxconn     4000  
  6.     user        haproxy  
  7.     group       haproxy  
  8.     daemon  
  9.   
  10. defaults  
  11.     mode                    http  
  12.     log                     global  
  13.     option                  httplog  
  14.     option                  dontlognull  
  15.     option http-server-close  
  16.     option forwardfor       except 127.0.0.0/8  
  17.     option                  redispatch  
  18.     retries                 3  
  19.     timeout http-request    10s  
  20.     timeout queue           1m  
  21.     timeout connect         10s  
  22.     timeout client          1m  
  23.     timeout server          1m  
  24.     timeout http-keep-alive 10s  
  25.     timeout check           10s  
  26.     maxconn                 3000  
  27.  
  28. listen status 
  29.     bind *:10086 
  30.     stats uri /haproxy-status  
  31.     stats auth admin:123456 
  32.     stats hide-version 
  33.   
  34.   
  35. frontend  haproxy-nlb   
  36.     bind    192.168.122.10:80  
  37.     default_backend             nginx-web  
  38.   
  39.   
  40. backend nginx-web  
  41.     option  httpchk HEAD /check.txt HTTP/1.0  
  42.     balance roundrobin  
  43.     server  web01 192.168.122.11:80 weight 3 check inter 5s rise 2 fall 3  
  44.     server  web02 192.168.122.12:80 weight 3 check inter 5s rise 2 fall 3  

 
5. nginx 설정 (감청 주소 가 다른 것 을 제외 하고 모두 같 음)
홈 서버 감청 192.168.122.11: 80
서버 감청 192.168.12.12: 80

  
  
  
  
  1. user  nginx; 
  2. worker_processes  2; 
  3. worker_rlimit_nofile 65535; 
  4.  
  5.  
  6. error_log  /var/log/nginx/error.log warn; 
  7. pid        /var/run/nginx.pid; 
  8.  
  9. google_perftools_profiles /tmp/tcmalloc; 
  10.  
  11. events { 
  12.     use epoll; 
  13.     worker_connections  2048; 
  14.  
  15.  
  16. http { 
  17.     include       /etc/nginx/mime.types; 
  18.     include       /etc/nginx/naxsi_core.rules; 
  19.     default_type  application/octet-stream; 
  20.  
  21.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  22.                       '$status $body_bytes_sent "$http_referer" ' 
  23.                       '"$http_user_agent" "$http_x_forwarded_for"'; 
  24.  
  25.     #access_log  /var/log/nginx/access.log  main; 
  26.  
  27.     sendfile        on; 
  28.     server_tokens   off; 
  29.  
  30.     #tcp_nopush     on; 
  31.  
  32.     keepalive_timeout  65; 
  33.  
  34.     gzip  on; 
  35.     gzip_static on; 
  36.     gzip_disable "msie6"; 
  37.     gzip_http_version 1.1; 
  38.     gzip_vary on; 
  39.     gzip_comp_level 6; 
  40.     gzip_proxied any; 
  41.     gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x$ 
  42.     gzip_buffers 16 8k; 
  43.  
  44.     client_max_body_size 20m; 
  45.     client_body_buffer_size 128k; 
  46. server { 
  47.     listen       192.168.122.11:8080; 
  48.     server_name  localhost; 
  49.     root        /usr/share/nginx/html; 
  50.     index       index.html index.htm index.php; 
  51.  
  52.     #charset koi8-r; 
  53.     access_log  /var/log/nginx/host.access.log  main; 
  54.  
  55.     location / { 
  56.         include /etc/nginx/naxsi_conf ; 
  57.  
  58.         if (!-e $request_filename) { 
  59.             rewrite ^/(.*)$ /index.php?q=$1 last; 
  60.         } 
  61.     } 
  62.  
  63.     #error_page  404              /404.html; 
  64.  
  65.     # redirect server error pages to the static page /50x.html 
  66.     # 
  67.     error_page   500 502 503 504  /50x.html; 
  68.   
  69.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  70.     # 
  71.     location ~ \.php$ { 
  72.         #try_files $uri = 404
  73.         fastcgi_pass   127.0.0.1:9000; 
  74.         #fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock; 
  75.         fastcgi_index  index.php; 
  76.         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
  77.         fastcgi_buffer_size 128k; 
  78.         fastcgi_buffers 256 16k; 
  79.         fastcgi_busy_buffers_size 256k; 
  80.         fastcgi_temp_file_write_size 256k; 
  81.         fastcgi_read_timeout 240; 
  82.         include        fastcgi_params; 
  83.     } 
  84.  
  85.     if ($fastcgi_script_name ~ \..*\/.*php) { 
  86.         return 403; 
  87.     } 
  88.  
  89.     # deny access to hiden file . (filename begin with ".") 
  90.     location ~ /\. { 
  91.         access_log off; 
  92.         log_not_found off;  
  93.         deny all; 
  94.     } 
  95.  
  96.     # deny access to bakup file .(any filename end with "~" ) 
  97.     location ~ ~$ {  
  98.         access_log off;  
  99.         log_not_found off;  
  100.         deny all;  
  101.         } 
  102.  
  103.     # cache image file 
  104.     location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|swf)$ { 
  105.         expires           1d; 
  106.     } 
  107.  
  108.     # don't log robots  and favion 
  109.     location = /robots.txt { access_log off; log_not_found off; }  
  110.     location = /favicon.ico { access_log off; log_not_found off; }  
  111.  
  112.     # deny access to .htaccess files, if Apache's document root 
  113.     # concurs with nginx's one 
  114.     # 
  115.     location ~ /\.ht { 
  116.         deny  all; 
  117.     } 
  118.  } 

6. php - fpm 설정

  
  
  
  
  1. sed -i ‘s/apache/nginx/g’ /etc/php-fpm.d/www.conf 

 
7. 기타
웹 01 에 검색 페이지 추가

  
  
  
  
  1. echo web01 >/usr/share/nginx/html/check.txt 

웹 02 에 검색 페이지 추가

  
  
  
  
  1. echo web02 >/usr/share/nginx/html/check.txt 

 
8. 기계 두 대 재 부팅
9. 검증
폴 링 이기 때문에 부하 균형 을 쉽게 감지 할 수 있 습 니 다. (브 라 우 저 를 사용 하지 않 는 것 이 좋 습 니 다. 브 라 우 저 에 캐 시 가 있 습 니 다)

  
  
  
  
  1. $ for n in {1..10};do curl http://192.168.122.10/check.txt;done 
  2. web02 
  3. web01 
  4. web02 
  5. web01 
  6. web02 
  7. web01 
  8. web02 
  9. web01 
  10. web02 
  11. web01 

 
OK, 큰 성 과 를 거 두 었 습 니 다!

좋은 웹페이지 즐겨찾기