[hello world 오픈세미나]varnish로 웹서버성능...

28
Varnish로 웹서버 성능 향상하기 윤신주 스포츠서비스 개발팀 2013.07

Upload: naver-d2

Post on 29-Nov-2014

11.973 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

Varnish로����������� ������������������  웹서버����������� ������������������  성능����������� ������������������  향상하기����������� ������������������  

윤신주����������� ������������������  

스포츠서비스����������� ������������������  개발팀����������� ������������������  ����������� ������������������  ����������� ������������������  

2013.07����������� ������������������  

����������� ������������������  ����������� ������������������  

����������� ������������������  

Page 2: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

목차����������� ������������������  

1.����������� ������������������  ����������� ������������������  Varnish����������� ������������������  소개����������� ������������������  

2.����������� ������������������  ����������� ������������������  Varnish����������� ������������������  VCL����������� ������������������  이해����������� ������������������  

3.����������� ������������������  ����������� ������������������  Varnish����������� ������������������  Grace����������� ������������������  mode����������� ������������������  ����������� ������������������  

4.����������� ������������������  ����������� ������������������  Varnish����������� ������������������  튜닝����������� ������������������  

5.����������� ������������������  ����������� ������������������  Varnish����������� ������������������  적용사례����������� ������������������  

6.����������� ������������������  ����������� ������������������  Epilogue����������� ������������������  

Page 3: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

1.����������� ������������������  Varnish����������� ������������������  소개����������� ������������������  

Page 4: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

4����������� ������������������  /����������� ������������������  varnish����������� ������������������  

1.1����������� ������������������  ����������� ������������������  Varnish는����������� ������������������  ?����������� ������������������  

웹����������� ������������������  어플리케이션의����������� ������������������  성능을����������� ������������������  극대화����������� ������������������  시켜줄����������� ������������������  수����������� ������������������  있는����������� ������������������  Cache����������� ������������������  서버!����������� ������������������  

Page 5: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

5����������� ������������������  /����������� ������������������  varnish����������� ������������������  

1.2����������� ������������������  ����������� ������������������  어떤����������� ������������������  Cache����������� ������������������  솔루션들을����������� ������������������  사용하고����������� ������������������  있나요?����������� ������������������  ����������� ������������������  

Local����������� ������������������  

Reverse����������� ������������������  Proxy����������� ������������������  

Distributed����������� ������������������  

Page 6: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

6����������� ������������������  /����������� ������������������  varnish����������� ������������������  

1.3����������� ������������������  ����������� ������������������  간단한����������� ������������������  소개를����������� ������������������  해볼까요?����������� ������������������  

2005년����������� ������������������  노르웨이����������� ������������������  신문사인����������� ������������������  Verdens����������� ������������������  Gang(VG)에서����������� ������������������  캐시����������� ������������������  솔루션을����������� ������������������  찾던����������� ������������������  중����������� ������������������  개발����������� ������������������  ����������� ������������������  

FreeBSD����������� ������������������  licence����������� ������������������  ����������� ������������������  

2013-06-17,����������� ������������������  3.0.4����������� ������������������  릴리즈����������� ������������������  

Page 7: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

7����������� ������������������  /����������� ������������������  varnish����������� ������������������  

1.4����������� ������������������  ����������� ������������������  웹����������� ������������������  서버����������� ������������������  앞단에����������� ������������������  설치한다는����������� ������������������  것은?����������� ������������������  

Page 8: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

8����������� ������������������  /����������� ������������������  varnish����������� ������������������  

1.5����������� ������������������  ����������� ������������������  Varnish로����������� ������������������  무엇을����������� ������������������  Cache����������� ������������������  할수����������� ������������������  있을까요?����������� ������������������  

Page 9: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

2.����������� ������������������  Varnish����������� ������������������  VCL����������� ������������������  이해����������� ������������������  

Page 10: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

10����������� ������������������  /����������� ������������������  varnish����������� ������������������  

2.1����������� ������������������  ����������� ������������������  VCL����������� ������������������  ­–����������� ������������������  request����������� ������������������  flow����������� ������������������  

Page 11: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

11����������� ������������������  /����������� ������������������  varnish����������� ������������������  

2.2����������� ������������������  ����������� ������������������  VCL����������� ������������������  ­–����������� ������������������  backend����������� ������������������  설정����������� ������������������  

backend backend1 { #벡엔드 서버 설정 .host = “222.xxx.xxx.xxx"; .port = "80"; .connect_timeout = 2s; .between_bytes_timeout = 2s; .saintmode_threshold = 20; .first_byte_timeout = 10s; .max_connections = 1024; .probe = { # health 체크 Probe 설정 .url = "/healthCheck.html"; .timeout = 5s; .interval = 3s; .window = 5; .threshold = 2; } } director directorA round-robin { # 벡엔드 로드 밸런싱 {.backend = backend1;} {.backend = backend2;} {.backend = backend3;} {.backend = backend4;} }

Page 12: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

12����������� ������������������  /����������� ������������������  varnish����������� ������������������  

2.3����������� ������������������  ����������� ������������������  VCL����������� ������������������  ­–����������� ������������������  vcl_recv����������� ������������������  

sub vcl_recv{ # Set Client Ip remove req.http.X-Forwarded-For; set req.http.X-Forwarded-For = client.ip; # cache 하지 않는 URL 설정 if( req.url ~ "/.*\.json" || req.url ~ "/request.jsp" ){ set req.backend = backend1; return (pipe); } # backend server 설정 set req.backend = directorA; set req.grace = 30m; # cache 대상 URL 설정 if (req.url ~ "/index.jsp" || req.url ~ “home.jsp“ ){ return (lookup); } return (pipe); }

Page 13: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

13����������� ������������������  /����������� ������������������  varnish����������� ������������������  

2.4����������� ������������������  ����������� ������������������  VCL����������� ������������������  ­–����������� ������������������  vcl_fetch����������� ������������������  

sub vcl_fetch { remove beresp.http.Vary; # 캐시 하지 않는 경우 if( beresp.http.X-Cacheable == "false" || beresp.http.Set-Cookie ){ set beresp.ttl = 0s; set beresp.grace = 0s; return (hit_for_pass); } # error 응답이면 restart if( beresp.status >= 500 ){ return (restart); } # Object TTL 설정 set beresp.grace = 30m; set beresp.ttl = 1m ; if( req.url ~ "^/(index\.jsp" ){ set beresp.ttl = 5m ; } }

Page 14: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

14����������� ������������������  /����������� ������������������  varnish����������� ������������������  

2.5����������� ������������������  ����������� ������������������  VCL����������� ������������������  ­–����������� ������������������  vcl_hash����������� ������������������  

sub vcl_hash { # URL로 해시 hash_data( req.url ); # IOS vs Android if ( req.http.user-agent ~ "iPhone" || req.http.user-agent ~ "iPad" ) { hash_data( "iOS" ); } else { hash_data( "Android" ); } # PC vs Mobile if ( req.url ~ "/viewer.jsp" ){ if ( req.http.user-agent ~ "(?i)mobile" ){ hash_data( "mobile" ); }else{ hash_data( "pc" ); } } return( hash ) ; }

Page 15: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

15����������� ������������������  /����������� ������������������  varnish����������� ������������������  

2.6����������� ������������������  ����������� ������������������  VCL����������� ������������������  ­–����������� ������������������  vcl_deliver,����������� ������������������  vcl_pipe,����������� ������������������  vcl_error����������� ������������������  

sub vcl_deliver{ if (obj.hits > 0) { # cache에서 hit한 경우 set resp.http.X-Varnish-Cache = "HIT"; } else { set resp.http.X-Varnish-Cache = "MISS"; } set resp.http.connection = "close"; # keep-alive off } sub vcl_pipe{ set bereq.http.connection = "close"; # keep-alive off } sub vcl_error { # 세번 restart 시도 if ( req.restarts < 3 ) { return(restart); } # error 페이지로 redirect set obj.http.location = "http://domain_name/error.jsp"; set obj.status = 302; return( deliver ) ; }

Page 16: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

3.����������� ������������������  Varnish����������� ������������������  grace����������� ������������������  mode����������� ������������������  

Page 17: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

17����������� ������������������  /����������� ������������������  varnish����������� ������������������  

3.1����������� ������������������  ����������� ������������������  Varnish����������� ������������������  grace����������� ������������������  mode����������� ������������������  

grace����������� ������������������  object를����������� ������������������  사용하는����������� ������������������  경우����������� ������������������  •  선택된����������� ������������������  Backend����������� ������������������  서버가����������� ������������������  Unhealthy����������� ������������������  한����������� ������������������  경우����������� ������������������  •  이미����������� ������������������  다른����������� ������������������  thread가����������� ������������������  Backend로����������� ������������������  요청을����������� ������������������  보내고����������� ������������������  응답을����������� ������������������  기다리고����������� ������������������  있는경우����������� ������������������  ����������� ������������������  

grace����������� ������������������  object����������� ������������������  TTL����������� ������������������  설정����������� ������������������  sub vcl_recv { if (req.backend.healthy) { set req.grace = 30s; } else { set req.grace = 1h; } } sub vcl_fetch { set beresp.grace = 1h; } ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  

Varnish����������� ������������������   Backend����������� ������������������  

Waitinglist����������� ������������������  

If����������� ������������������  exist����������� ������������������  grace����������� ������������������  obj?����������� ������������������  ����������� ������������������  

Page 18: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

4.����������� ������������������  Varnish����������� ������������������  튜닝����������� ������������������  

Page 19: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

19����������� ������������������  /����������� ������������������  varnish����������� ������������������  

4.1����������� ������������������  ����������� ������������������  Varnish����������� ������������������  튜닝����������� ������������������  

Varsnish����������� ������������������  시작����������� ������������������  스크립트����������� ������������������  …/varnish/sbin/varnishd -f /xxx/xxx/default.vcl -s malloc,4G -a 0.0.0.0:80 -T localhost:6082 -p sess_workspace=262144 -p thread_pool_min=800 -p thread_pool_max=4000 -p listen_depth=10240 -u root -g root ����������� ������������������  ����������� ������������������  

커널����������� ������������������  파라미터����������� ������������������  net.ipv4.tcp_wmem net.core.somaxconn net.core.netdev_max_backlog net.ipv4.tcp_max_tw_buckets fs.file-max Open Files Limit /etc/security/limits.conf /proc/pid/limits ����������� ������������������  ����������� ������������������  

Page 20: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

5.����������� ������������������  Varnish����������� ������������������  적용사례����������� ������������������  

Page 21: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

21����������� ������������������  /����������� ������������������  varnish����������� ������������������  

5.1����������� ������������������  ����������� ������������������  서비스����������� ������������������  적용����������� ������������������  ����������� ������������������  ����������� ������������������  

Page 22: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

22����������� ������������������  /����������� ������������������  varnish����������� ������������������  

5.2����������� ������������������  ����������� ������������������  이슈����������� ������������������  (1)����������� ������������������  

Client����������� ������������������  IP����������� ������������������  

-����������� ������������������  문제����������� ������������������  

1) 동영상����������� ������������������  재생,����������� ������������������  생중계에����������� ������������������  ����������� ������������������  해외����������� ������������������  IP����������� ������������������  제한으로����������� ������������������  WAS에����������� ������������������  Client����������� ������������������  IP����������� ������������������  ����������� ������������������  검사가����������� ������������������  필요����������� ������������������  

2) Client����������� ������������������  IP가����������� ������������������  아닌����������� ������������������  ����������� ������������������  Varnish����������� ������������������  서버����������� ������������������  IP가����������� ������������������  Backend����������� ������������������  웹서버로����������� ������������������  전달됨����������� ������������������  

-����������� ������������������  해결����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  Varnish에서����������� ������������������  http����������� ������������������  request����������� ������������������  header에����������� ������������������  Client����������� ������������������  IP를����������� ������������������  세팅하고����������� ������������������  Apache����������� ������������������  ����������� ������������������  rpaf����������� ������������������  module����������� ������������������  로����������� ������������������  Client����������� ������������������  IP����������� ������������������  변환����������� ������������������  

����������� ������������������  

����������� ������������������  

Page 23: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

23����������� ������������������  /����������� ������������������  varnish����������� ������������������  

5.3����������� ������������������  ����������� ������������������  이슈����������� ������������������  (2)����������� ������������������  

User-Agent별����������� ������������������  UI가����������� ������������������  다름����������� ������������������  

-����������� ������������������  문제����������� ������������������  

1)  IOS/Android����������� ������������������  에����������� ������������������  따라����������� ������������������  모바일����������� ������������������  페이지의����������� ������������������  meta태그인����������� ������������������  viewpoint와����������� ������������������  body����������� ������������������  tag의����������� ������������������  class����������� ������������������  속성값이����������� ������������������  변경됨����������� ������������������  

2)  IOS/Android에����������� ������������������  따라����������� ������������������  페이지����������� ������������������  네비게이션����������� ������������������  UI가����������� ������������������  상이함����������� ������������������  

-����������� ������������������  해결����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  vcl_hash����������� ������������������  함수에서����������� ������������������  User-Agent를����������� ������������������  검사하여����������� ������������������  IOS/Android����������� ������������������  ����������� ������������������  분기����������� ������������������  처리����������� ������������������  

����������� ������������������  

����������� ������������������  

����������� ������������������  

Page 24: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

24����������� ������������������  /����������� ������������������  varnish����������� ������������������  

5.4����������� ������������������  ����������� ������������������  이슈����������� ������������������  (3)����������� ������������������  

비정상적인����������� ������������������  페이지,����������� ������������������  ����������� ������������������  HTTP����������� ������������������  200����������� ������������������  response����������� ������������������  code가����������� ������������������  전달됨����������� ������������������  

-����������� ������������������  문제����������� ������������������  

1) 기사����������� ������������������  페이지는����������� ������������������  본문을����������� ������������������  읽어오지����������� ������������������  못하는����������� ������������������  경우,����������� ������������������  에러����������� ������������������  메세지를����������� ������������������  담고����������� ������������������  200����������� ������������������  status를����������� ������������������  코드를����������� ������������������  보냄����������� ������������������  

2) 1분����������� ������������������  동안����������� ������������������  에러����������� ������������������  페이지가����������� ������������������  캐슁될����������� ������������������  수����������� ������������������  있는����������� ������������������  문제가����������� ������������������  존재����������� ������������������  ����������� ������������������  

-����������� ������������������  해결����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  X-cacheable����������� ������������������  header����������� ������������������  값을����������� ������������������  세팅하고����������� ������������������  varnish����������� ������������������  vcl_fetch����������� ������������������  에서����������� ������������������  예외처리����������� ������������������  

����������� ������������������  

����������� ������������������  

Page 25: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

25����������� ������������������  /����������� ������������������  varnish����������� ������������������  

5.5����������� ������������������  ����������� ������������������  이슈����������� ������������������  (4)����������� ������������������  

Keep����������� ������������������  Alive����������� ������������������  

-����������� ������������������  문제����������� ������������������  

1) Varnish����������� ������������������  는����������� ������������������  HTTP����������� ������������������  1.1����������� ������������������  SPEC를����������� ������������������  따라����������� ������������������  default로����������� ������������������  Keep-Alive����������� ������������������  로����������� ������������������  connection을����������� ������������������  유지한다.����������� ������������������  

-����������� ������������������  해결����������� ������������������  

����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  ����������� ������������������  vcl에서����������� ������������������  Header에����������� ������������������  connection����������� ������������������  :����������� ������������������  close로����������� ������������������  세팅����������� ������������������  

����������� ������������������  

����������� ������������������  

Page 26: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

26����������� ������������������  /����������� ������������������  varnish����������� ������������������  

5.6����������� ������������������  ����������� ������������������  이슈����������� ������������������  (5)����������� ������������������  

Varnish����������� ������������������  서버를����������� ������������������  어디에����������� ������������������  두어야����������� ������������������  할까?����������� ������������������  

•  Varnish����������� ������������������  ­–����������� ������������������  Apache����������� ������������������  ­–����������� ������������������  Tomcat����������� ������������������  ����������� ������������������  (����������� ������������������  성능,����������� ������������������  확장����������� ������������������  유연성����������� ������������������  )����������� ������������������  •  Apache����������� ������������������  ­–����������� ������������������  Varnish����������� ������������������  ­–����������� ������������������  Tomcat����������� ������������������  

����������� ������������������  

캐시����������� ������������������  대상����������� ������������������  URL����������� ������������������  리스트는����������� ������������������  어떻게����������� ������������������  관리할까?����������� ������������������  

•  Black����������� ������������������  List����������� ������������������  ����������� ������������������  •  White����������� ������������������  List����������� ������������������  (����������� ������������������  안정성����������� ������������������  )����������� ������������������  

캐시����������� ������������������  TTL과����������� ������������������  ����������� ������������������  메모리����������� ������������������  크기는����������� ������������������  ?����������� ������������������  

•  ttl����������� ������������������  =����������� ������������������  1m����������� ������������������  ����������� ������������������  

•  grace.ttl����������� ������������������  =����������� ������������������  2h����������� ������������������  ����������� ������������������  •  -s����������� ������������������  malloc,512M����������� ������������������  

����������� ������������������  

그����������� ������������������  외����������� ������������������  결정해야����������� ������������������  했던����������� ������������������  것들����������� ������������������  

Page 27: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

6.����������� ������������������  Epilogue����������� ������������������  

Page 28: [Hello world 오픈세미나]varnish로 웹서버성능 향상시키기

Thank����������� ������������������  you.����������� ������������������