Kubernetes ์ƒํ™œ์—์„œ: HTTP ์„œ๋ฒ„๊ฐ€ ์ŠคํŽ˜์ธ ์‚ฌ๋žŒ๋“ค์„ ์„ ํ˜ธํ•˜์ง€ ์•Š์€ ์ด์œ 

Kubernetes ์ƒํ™œ์—์„œ: HTTP ์„œ๋ฒ„๊ฐ€ ์ŠคํŽ˜์ธ ์‚ฌ๋žŒ๋“ค์„ ์„ ํ˜ธํ•˜์ง€ ์•Š์€ ์ด์œ 

Microsoft ํด๋ผ์šฐ๋“œ(Azure)์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์Šคํƒ์ด ์žˆ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ๋‹ด๋‹น์ž๊ฐ€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ ์œ ๋Ÿฝ์˜ ์ผ๋ถ€ ํด๋ผ์ด์–ธํŠธ์˜ ์ผ๋ถ€ ์š”์ฒญ์ด ์˜ค๋ฅ˜ 400(์ž˜๋ชป๋œ ์š”์ฒญ). ๋ชจ๋“  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ .NET์œผ๋กœ ์ž‘์„ฑ๋˜๊ณ  Kubernetes์— ๋ฐฐํฌ๋ฉ๋‹ˆ๋‹ค...

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ค‘ ํ•˜๋‚˜๋Š” ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์ด ๊ถ๊ทน์ ์œผ๋กœ ํ†ต๊ณผํ•˜๋Š” API์ž…๋‹ˆ๋‹ค. ์ด ํŠธ๋ž˜ํ”ฝ์€ HTTP ์„œ๋ฒ„์—์„œ ์ˆ˜์‹ ๋ฉ๋‹ˆ๋‹ค. ํ™ฉ์กฐ๋กฑ์ด, .NET ํด๋ผ์ด์–ธํŠธ์— ์˜ํ•ด ๊ตฌ์„ฑ๋˜๊ณ  ํฌ๋“œ์—์„œ ํ˜ธ์ŠคํŒ…๋ฉ๋‹ˆ๋‹ค. ๋””๋ฒ„๊น…์˜ ๊ฒฝ์šฐ ๋ฌธ์ œ๋ฅผ ์ง€์†์ ์œผ๋กœ ์žฌํ˜„ํ•˜๋Š” ํŠน์ • ์‚ฌ์šฉ์ž๊ฐ€ ์žˆ๋‹ค๋Š” ์ ์—์„œ ์šด์ด ์ข‹์•˜์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํŠธ๋ž˜ํ”ฝ ์ฒด์ธ์œผ๋กœ ์ธํ•ด ๋ชจ๋“  ๊ฒƒ์ด ๋ณต์žกํ•ด์กŒ์Šต๋‹ˆ๋‹ค.

Kubernetes ์ƒํ™œ์—์„œ: HTTP ์„œ๋ฒ„๊ฐ€ ์ŠคํŽ˜์ธ ์‚ฌ๋žŒ๋“ค์„ ์„ ํ˜ธํ•˜์ง€ ์•Š์€ ์ด์œ 

Ingress์˜ ์˜ค๋ฅ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

{
   "number_fields":{
      "status":400,
      "request_time":0.001,
      "bytes_sent":465,
      "upstream_response_time":0,
      "upstream_retries":0,
      "bytes_received":2328
   },
   "stream":"stdout",
   "string_fields":{
      "ingress":"app",
      "protocol":"HTTP/1.1",
      "request_id":"f9ab8540407208a119463975afda90bc",
      "path":"/api/sign-in",
      "nginx_upstream_status":"400",
      "service":"app",
      "namespace":"production",
      "location":"/front",
      "scheme":"https",
      "method":"POST",
      "nginx_upstream_response_time":"0.000",
      "nginx_upstream_bytes_received":"120",
      "vhost":"api.app.example.com",
      "host":"api.app.example.com",
      "user":"",
      "address":"83.41.81.250",
      "nginx_upstream_addr":"10.240.0.110:80",
      "referrer":"https://api.app.example.com/auth/login?long_encrypted_header",
      "service_port":"http",
      "user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
      "time":"2019-03-06T18:29:16+00:00",
      "content_kind":"cache-headers-not-present",
      "request_query":""
   },
   "timestamp":"2019-03-06 18:29:16",
   "labels":{
      "app":"nginx",
      "pod-template-generation":"6",
      "controller-revision-hash":"1682636041"
   },
   "namespace":"kube-nginx-ingress",
   "nsec":6726612,
   "source":"kubernetes",
   "host":"k8s-node-55555-0",
   "pod_name":"nginx-v2hcb",
   "container_name":"nginx",
   "boolean_fields":{}
}

๋™์‹œ์— Kestrel์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋งํ–ˆ์Šต๋‹ˆ๋‹ค.

HTTP/1.1 400 Bad Request
Connection: close
Date: Wed, 06 Mar 2019 12:34:20 GMT
Server: Kestrel
Content-Length: 0

์ตœ๋Œ€ํ•œ ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๋”๋ผ๋„ Kestrel ์˜ค๋ฅ˜์—๋Š” ๋งค์šฐ ๋งŽ์€ ๋‚ด์šฉ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์กฐ๊ธˆ ์œ ์šฉํ•œ ์ •๋ณด:

{
   "number_fields":{"ThreadId":76},
   "stream":"stdout",
   "string_fields":{
      "EventId":"{"Id"=>17, "Name"=>"ConnectionBadRequest"}",
      "SourceContext":"Microsoft.AspNetCore.Server.Kestrel",
      "ConnectionId":"0HLL2VJSST5KV",
      "@mt":"Connection id "{ConnectionId}" bad request data: "{message}"",
      "@t":"2019-03-07T13:06:48.1449083Z",
      "@x":"Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Malformed request: invalid headers.n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection)n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<ProcessRequestsAsync>d__185`1.MoveNext()",
      "message":"Malformed request: invalid headers."
   },
   "timestamp":"2019-03-07 13:06:48",
   "labels":{
      "pod-template-hash":"2368795483",
      "service":"app"
   },
   "namespace":"production",
   "nsec":145341848,
   "source":"kubernetes",
   "host":"k8s-node-55555-1",
   "pod_name":"app-67bdcf98d7-mhktx",
   "container_name":"app",
   "boolean_fields":{}
}

tcpdump๋งŒ์ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค... ํ•˜์ง€๋งŒ ํŠธ๋ž˜ํ”ฝ ์ฒด์ธ์— ๋Œ€ํ•ด ๋‹ค์‹œ ๋ง์”€๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

Kubernetes ์ƒํ™œ์—์„œ: HTTP ์„œ๋ฒ„๊ฐ€ ์ŠคํŽ˜์ธ ์‚ฌ๋žŒ๋“ค์„ ์„ ํ˜ธํ•˜์ง€ ์•Š์€ ์ด์œ 

์กฐ์‚ฌ

๋ฌผ๋ก  ๊ตํ†ต ์†Œ๋ฆฌ๋ฅผ ๋“ฃ๋Š” ๊ฒƒ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค ํ•ด๋‹น ํŠน์ • ๋…ธ๋“œ์—์„œ, Kubernetes๊ฐ€ ํฌ๋“œ๋ฅผ ๋ฐฐํฌํ•œ ๊ฒฝ์šฐ: ๋คํ”„์˜ ์–‘์€ ์ตœ์†Œํ•œ ๋ฌด์–ธ๊ฐ€๋ฅผ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ์ •๋„์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ๋กœ ๊ทธ๊ฒƒ์„ ์กฐ์‚ฌํ–ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ”„๋ ˆ์ž„์ด ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

GET /back/user HTTP/1.1
Host: api.app.example.com
X-Request-ID: 27ceb14972da8c21a8f92904b3eff1e5
X-Real-IP: 83.41.81.250
X-Forwarded-For: 83.41.81.250
X-Forwarded-Host: api.app.example.com
X-Forwarded-Port: 443
X-Forwarded-Proto: https
X-Original-URI: /front/back/user
X-Scheme: https
X-Original-Forwarded-For: 83.41.81.250
X-Nginx-Geo-Client-Country: Spain
X-Nginx-Geo-Client-City: M.laga
Accept-Encoding: gzip
CF-IPCountry: ES
CF-RAY: 4b345cfd1c4ac691-MAD
CF-Visitor: {"scheme":"https"}
pragma: no-cache
cache-control: no-cache
accept: application/json, text/plain, */*
origin: https://app.example.com
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36
referer: https://app.example.com/auth/login
accept-language: en-US,en;q=0.9,en-GB;q=0.8,pl;q=0.7
cookie: many_encrypted_cookies; .AspNetCore.Identity.Application=something_encrypted; 
CF-Connecting-IP: 83.41.81.250
True-Client-IP: 83.41.81.250
CDN-Loop: cloudflare

HTTP/1.1 400 Bad Request
Connection: close
Date: Wed, 06 Mar 2019 12:34:20 GMT
Server: Kestrel
Content-Length: 0

๋คํ”„๋ฅผ ๋ฉด๋ฐ€ํžˆ ์กฐ์‚ฌํ•œ ๊ฒฐ๊ณผ ๋‹จ์–ด๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. M.laga. ์ŠคํŽ˜์ธ์—๋Š” M.laga๋ผ๋Š” ๋„์‹œ๊ฐ€ ์—†๋‹ค๊ณ  ์ถ”์ธกํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ๋ง๋ผ๊ฐ€). ์ด ์•„์ด๋””์–ด๋ฅผ ํฌ์ฐฉํ•˜์—ฌ Ingress ๊ตฌ์„ฑ์„ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ, ํ•œ ๋‹ฌ ์ „์— (ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์— ๋”ฐ๋ผ) ์‚ฝ์ž…๋œ ๊ตฌ์„ฑ์„ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. "๋ฌดํ•ดํ•œ" ์กฐ๊ฐ:

    ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header X-Nginx-Geo-Client-Country $geoip_country_name;
      proxy_set_header X-Nginx-Geo-Client-City $geoip_city;

์ด ํ—ค๋” ์ „๋‹ฌ์„ ๋น„ํ™œ์„ฑํ™”ํ•œ ํ›„ ๋ชจ๋“  ๊ฒƒ์ด ๊ดœ์ฐฎ์•„์กŒ์Šต๋‹ˆ๋‹ค! (์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž์ฒด์—๋Š” ๋” ์ด์ƒ ์ด๋Ÿฌํ•œ ํ—ค๋”๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ด ๊ณง ๋ถ„๋ช…ํ•ด์กŒ์Šต๋‹ˆ๋‹ค.)

์ด์ œ ๋ฌธ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž ๋” ์ผ๋ฐ˜์ ์œผ๋กœ. ํ…”๋„ท ์š”์ฒญ์„ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ ์‰ฝ๊ฒŒ ์žฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. localhost:80:

GET /back/user HTTP/1.1
Host: api.app.example.com
cache-control: no-cache
accept: application/json, text/plain, */*
origin: https://app.example.com
Cookie: test=Desiree

... ๋ฐ˜ํ™˜ 401 Unauthorized, ์˜ˆ์ƒ๋Œ€๋กœ. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ์š”?

GET /back/user HTTP/1.1
Host: api.app.example.com
cache-control: no-cache
accept: application/json, text/plain, */*
origin: https://app.example.com
Cookie: test=Dรฉsirรฉe

?

๋Œ์•„์˜ฌ ๊ฒƒ์ด๋‹ค 400 Bad request โ€” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ๊ทธ์— ์ด๋ฏธ ์ต์ˆ™ํ•œ ์˜ค๋ฅ˜๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

{
   "@t":"2019-03-31T12:59:54.3746446Z",
   "@mt":"Connection id "{ConnectionId}" bad request data: "{message}"",
   "@x":"Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Malformed request: invalid headers.n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection.TryParseRequest(ReadResult result, Boolean& endConnection)n   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<ProcessRequestsAsync>d__185`1.MoveNext()",
   "ConnectionId":"0HLLLR1J974L9",
   "message":"Malformed request: invalid headers.",
   "EventId":{
      "Id":17,
      "Name":"ConnectionBadRequest"
   },
   "SourceContext":"Microsoft.AspNetCore.Server.Kestrel",
   "ThreadId":71
}

๊ฒฐ๊ณผ

ํŠนํžˆ ํ™ฉ์กฐ๋กฑ์ด ~ ํ•  ์ˆ˜ ์—†๋‹ค. ์ƒ๋‹นํžˆ ๋งŽ์€ ์ˆ˜์˜ ๋„์‹œ ์ด๋ฆ„์— ํฌํ•จ๋œ UTF-8์˜ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž๋กœ HTTP ํ—ค๋”๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ ์ถ”๊ฐ€ ์š”์†Œ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ Kestrel ๊ตฌํ˜„์„ ๋ณ€๊ฒฝํ•  ๊ณ„ํš์ด ์—†๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ AspNetCore ์ž์ฒด์˜ ๋ฌธ์ œ(โ„– 4318, โ„– 7707) ๊ทธ๋“ค์€ ์ด๊ฒƒ์ด ๋„์›€์ด ๋˜์ง€ ์•Š์„ ๊ฒƒ์ด๋ผ๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค...

์š”์•ฝํ•˜์ž๋ฉด, ์ด ๋…ธํŠธ๋Š” ๋” ์ด์ƒ Kestrel ๋˜๋Š” UTF-8(2019๋…„?!)์˜ ํŠน์ • ๋ฌธ์ œ์— ๊ด€ํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์‚ฌ์‹ค์— ๊ด€ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋งˆ์Œ์ฑ™๊น€๊ณผ ๊พธ์ค€ํ•œ ๊ณต๋ถ€ ๋ฌธ์ œ๋ฅผ ์ฐพ๋Š” ๋™์•ˆ ์ทจํ•˜๋Š” ๋ชจ๋“  ๋‹จ๊ณ„๋Š” ์กฐ๋งŒ๊ฐ„ ๊ฒฐ์‹ค์„ ๋งบ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ํ–‰์šด์„ ๋นŒ์–ด์š”!

PS

๋ธ”๋กœ๊ทธ์—์„œ๋„ ์ฝ์–ด๋ณด์„ธ์š”.

์ถœ์ฒ˜ : habr.com

์ฝ”๋ฉ˜ํŠธ๋ฅผ ์ถ”๊ฐ€