Posts Kubernetes network 1
Post
Cancel

Kubernetes network 1

Kubernetes network 1


๐Ÿ’ฟ kubernetes

** Docker

A pod consists of one or more containers that are collocated on the same host, and are configured to share a network stack and other resources such as volumes.

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ์ ์€ ๋„คํŠธ์›Œํฌ์™€ ๋ณผ๋ฅจ๊ณผ ๊ฐ™์€ ํŠน์ • ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณต์œ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋„คํŠธ์›Œํฌ๋ฅผ ๊ณต์œ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋ฌด์—‡์„ ์˜๋ฏธํ• ๊นŒ

image

๋„์ปค์˜ ๊ฒฝ์šฐ docker0๋ผ๋Š” veth0(172.17.0.2)๋ฅผ ์œ„ํ•œ default gateway๋ฅผ ์„ค์ •ํ•œ๋‹ค. ์ด ์ƒํƒœ๋กœ ๋‘๋ฒˆ์งธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋„์šฐ๋ฉด

image

์ด ์ƒํƒœ๊ฐ€ ๋œ๋‹ค. ๋‘ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ†ต์‹ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‘ ์ปจํ…Œ์ด๋„ˆ์˜ ์ฃผ์†Œ๋ฅผ ์•Œ๊ณ ์žˆ๋Š” docker0๋ฅผ ํ†ตํ•ด์„œ ์ˆ˜ํ–‰ํ•œ๋‹ค.

์ด๋ ‡๋“ฏ ๋„์ปค๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ์ƒˆ๋กœ์šด network interface๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋Œ€์‹  docker0 ๊ฐ™์€ ๊ณต์œ  ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•œ๋‹ค.

K8s

image

2๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ƒ์„ฑ๋œ ๋ชจ์Šต์ด๋‹ค. 2๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” 172.17.0.2์˜ ์ฃผ์†Œ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์œผ๋ฉฐ ์ด ์ฃผ์†Œ(๋„คํŠธ์›Œํฌ)๋ฅผ ๊ณต์œ ํ•œ๋‹ค. ์ด 2๊ฐœ์˜ ์ปจํ…Œ์ด๋„ˆ๋Š” port๋กœ ๊ตฌ๋ณ„๋  ์ˆ˜ ์žˆ๋‹ค.(์ฆ‰ ๋‚ด๋ถ€์—์„œ localhost์™€ port hit ๋  ์ˆ˜ ์žˆ์Œ) ์ด ์ƒํ™ฉ์€ single host์—์„œ multiple process๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ„๋‹จํ•œ ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์—์„œ decoupling ๊ณผ ์ปจํ…Œ์ด๋„ˆ ๊ณ ๋ฆฝ์˜ ์žฅ์ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค์—์„œ pause ์ปจํ…Œ์ด๋„ˆ๋Š” Pod ๋‚ด๋ถ€์˜ ์ปจํ…Œ์ด๋„ˆ๋“ค์„ ์œ„ํ•œ ์ผ์ข…์˜ โ€˜๋ถ€๋ชจ ์ปจํ…Œ์ด๋„ˆโ€™ ๋กœ์„œ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

pause ์ปจํ…Œ์ด๋„ˆ๋Š” ์ฃผ๋กœ 2๊ฐ€์ง€ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  1. ์ฒซ ๋ฒˆ์งธ๋กœ, pause ์ปจํ…Œ์ด๋„ˆ๋Š” pod์˜ ์ปจํ…Œ์ด๋„ˆ๋“ค์ด ๋ฆฌ๋ˆ…์Šค namespace๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค.
  2. ๋‘๋ฒˆ์งธ๋กœ, PID (Process ID) namespace ๊ณต์œ ๊ฐ€ ์„ค์ •๋˜์—ˆ์„ ๋•Œ์—๋Š”, Pod์—์„œ PID 1 (์—ญ์ฃผ : Init Process) ๋กœ์„œ์˜ ์—ญํ• ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ข€๋น„ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฑฐ๋‘ฌ๋“ค์ด๋Š” ๊ธฐ๋Šฅ๋„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

k8s Pod Network

K8s ๋„คํŠธ์›Œํฌ ์กฐ๊ฑด

  • ๊ฐ๊ฐ์˜ Pod์€ ์ „์ฒด ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ์œ ๋‹ˆํฌํ•œ ์ž์‹ ๋งŒ์˜ IP๋ฅผ ๊ฐ€์ง„๋‹ค.
  • ๊ฐ™์€ ๋…ธ๋“œ์— ์žˆ๋Š” ๋ชจ๋“  Pod์€ ์„œ๋กœ๊ฐ„์— ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค.
  • ๋‹ค๋ฅธ ๋…ธ๋“œ์— ์žˆ๋Š” ๋ชจ๋“  Pod์€ ๋ณ„๋„์˜ NAT ์—†์ด ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•œ๋‹ค.

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” ์ด์ œ ๋…ธ๋“œ์˜ ๊ณตํ†ต Gateway๋ฅผ ํ†ตํ•ด pod์—์„œ ๋ฐœ์ƒ๋œ packet์˜ ๋ชฉ์ ์ง€๋ฅผ ํŠน์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋…ธ๋“œ๋Š” ์„œ๋กœ ๋…๋ฆฝ๋œ ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์—์„œ ์ž‘๋™ํ•˜๋ฏ€๋กœ docker0๊ฐ€ ์ƒ์„ฑํ•œ ๋„คํŠธ์›Œํฌ ํ™˜๊ฒฝ์ด ๊ฒน์น  ์ˆ˜ ์žˆ๋Š” ์—ฌ์ง€๊ฐ€ ์ถฉ๋ถ„ํžˆ ์žˆ๋‹ค. ๊ทธ ๊ฒฝ์šฐ๊ฐ€ ์•„๋ž˜์˜ ๊ทธ๋ฆผ์ด๋‹ค.

image

์ด ๊ฒฝ์šฐ ๋ชฉ์ ์ง€๋กœ ์ •ํ•ด์ง„ 172.17.0.2๋กœ ์„ค์ •๋œ ๋„คํŠธ์›Œํฌ๊ฐ€ ๋‹ค์ˆ˜ ์กด์žฌํ•˜๋ฏ€๋กœ bridge์—์„œ ๊ทธ ๋ชฉ์ ์ง€๋ฅผ ํŠน์ •ํ•  ์ˆ˜๊ฐ€ ์—†๋‹ค๋Š” ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธด๋‹ค.

์ฆ‰ one node typically has no idea what private address space was assigned to a bridge on another node

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด 2๊ฐ€์ง€ ํ•ด๊ฒฐ๋ฒ•์ด ์กด์žฌํ•œ๋‹ค.

  1. ๊ฐ ๋…ธ๋“œ์˜ bridge์— ๋Œ€ํ•œ ์ „์ฒด ์ฃผ์†Œ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜๊ณ  ๊ตฌ์ถ•๋œ ๋…ธ๋“œ์˜ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ด๋‹น ๊ณต๊ฐ„์˜ ๋ธŒ๋ฆฌ์ง€ ์ฃผ์†Œ๋ฅผ ํ• ๋‹น
    • ์ฆ‰ node์˜ ์ฃผ์†Œ๋ฅผ ํ™œ์šฉํ•ด์„œ Uniqueํ•œ ์ฃผ์†Œ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜์ž
  2. Routing rule์„ node์˜ ๊ณตํ†ต Gateway์— ์ถ”๊ฐ€ํ•˜์—ฌ subnet-mask๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒจํ‚ท์˜ ๋ชฉ์ ์ง€์— ํ•ด๋‹นํ•˜๋Š” bridge์˜ ์ฃผ์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์ž

2๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ด ๋ฐ‘์˜ ๊ทธ๋ฆผ์ด๋‹ค.

image

์ฟ ๋ฒ„๋„คํ‹ฐ์Šค๋Š” birdge๋กœ cbr(custom bridge)์„ docker ๋Œ€์‹  ์‚ฌ์šฉํ•œ๋‹ค. ๊ทธ ์—ญํ• ์€ ์ž˜ ๋ชจ๋ฅธ๋‹ค.


๐Ÿ’ฟ CNI(Common Network Interface)

์ง€๊ธˆ ๊นŒ์ง€ ๋ฆฌ๋ˆ…์Šค ๋ธŒ๋ฆฟ์ง€๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ ๊ฐ„์˜ ํ†ต์‹ ์„ ๊ฐ€๋Šฅ์ผ€ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ดค๋‹ค. ํ•˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ๋„ˆ๋ฌด ๋‹ค์–‘ํ•˜๋ฉฐ ๋ณต์žกํ•˜๋ฉฐ ๊ทธ๋ฆฌ๊ณ  ๋ณ„๋„์˜ ๋กœ์ง์„ ์š”๊ตฌํ•œ๋‹ค๋Š” ๊ฒƒ์ด ๋‹จ์ ์ด๋‹ค.

๊ทธ๋ž˜์„œ ํƒ„์ƒํ•œ๊ฒƒ์ด CNI์ธ๋ฐ CNi๋ž€ Pod๊ฐ„์˜ ํ†ต์‹ ์— ๊ทœ์•ฝ(CNI)์„ ์ •ํ•˜๊ณ  ๊ทธ ๊ทœ์•ฝ์— ๋”ฐ๋ผ ๋งŒ๋“  ๊ตฌํ˜„์ฒด๋ฅผ CNI ํ”Œ๋Ÿฌ๊ทธ ์ธ์ด๋ผ๊ณ  ํ•œ๋‹ค.

๊ฐ๊ฐ์˜ ๋…ธ๋“œ๋Š” kubelet์„ ํ†ตํ•ด ๋…ธ๋“œ๋ฅผ ์ œ์–ดํ•œ๋‹ค. kubelet์€ โ€“cni-conf-dir=/etc/cni/net.d๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ฝ์–ด ์–ด๋–ค CNI๋ฅผ ์‚ฌ์šฉํ•  ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค. ๋˜ํ•œ โ€“cni-bin-dir=/opt/cni/bin๋ฅผ ํ†ตํ•ด CNI ํ”Œ๋Ÿฌ๊ทธ์ธ์˜ ๋ฐ”์ด๋„ˆ๋ฆฌ ์œ„์น˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

Weave Net

CNI ํ”Œ๋Ÿฌ๊ทธ์ธ ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

image

์ง€๊ธˆ๊นŒ์ง€๋Š” IP๋ฅผ ์ ๋‹นํ•œ ๊ฐ’์œผ๋กœ ์ˆ˜๋™์œผ๋กœ ํ• ๋‹นํ–ˆ์ง€๋งŒ ์‹ค์ œ k8s ํ™˜๊ฒฝ์—์„œ๋Š” ์ ์ ˆํ•œ IP๋ฅผ ํ• ๋‹นํ•˜๊ณ  ๊ด€๋ฆฌํ•ด์ฃผ๋Š” IPAM(IP Adress Management)๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด ๋˜ํ•œ CNI๊ฐ€ ์ฑ…์ž„์ง€๊ณ  ์žˆ๋‹ค. Weave ์—์„œ๋Š” 10.32.0.0/12์˜ ๋Œ€์—ญ IP๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Weave์—์„œ๋Š” ๋จผ์ € ๊ฐ ๋…ธ๋“œ์— ์ ๋‹นํ•œ Subnet์„ ํ• ๋‹นํ•˜๊ณ  Bridge์™€ Pod์ด ํ•ด๋‹นํ•˜๋Š” Subnet์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์–ด๋–ค Subnet์„ ์‚ฌ์šฉํ• ์ง€๋Š” Weave์— ์˜ํ•ด ์ž๋™์œผ๋กœ ์ •ํ•ด์ง€์ง€๋งŒ ์„ค์ •์„ ํ†ตํ•ด ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ’ฟ K8s Service

Pod์˜ IP๋Š” ํ†ต์‹ ํ•˜๊ธฐ์œ„ํ•œ ์—”๋“œํฌ์ธํŠธ๋กœ ์ ์ ˆํ•˜์ง€ ์•Š๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‹ค์ˆ˜์˜ Pod๋ฅผ Deployment๋กœ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ Pod๋Š” App์˜ ๋ถ€ํ•˜ ์ •๋„์— ๋”ฐ๋ผ ์Šค์ผ€์ผ์ด ๋ฐ”๋€๋‹ค. ์ฆ‰ IP๋Š” ์ž„์‹œ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์˜๊ตฌ์ ์ธ ์—”๋“œํฌ์ธํŠธ๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด Service๋ฅผ ์—”๋“œํฌ์ธํŠธ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.Pod๋Š” ํŠน์ • ๋ผ๋ฒจ์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๊ทธ ๋ผ๋ฒจ์„ ์„œ๋น„์Šค๊ฐ€ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋‹ค. ๊ทธ๋ž˜์„œ ์™ธ๋ถ€ ํŒจํ‚ท Pod์˜ IP๊ฐ€ ๋ฐ”๋€Œ๋”๋ผ๋„ Service์˜ label๋ฅผ ํ†ตํ•ด Pod์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ธฐ๋Šฅ

  1. LoadBalancer: ๋ถ€ํ•˜๋ถ„์‚ฐ ์žฅ์น˜
    • ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ์™ธ๋ถ€ ๋ถ€ํ•˜ ๋ถ„์‚ฐ ๊ธฐ๋Šฅ
    • ๋‚ด๋ถ€ ์‚ฌ์šฉ์ž๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋‚ด๋ถ€ ๋ถ€ํ•˜ ๋ถ„์‚ฐ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.
  2. Cluser IP : ์„œ๋น„์Šค๋Š” ์ƒ์„ฑ์‹œ ์ด๋ฆ„๊ณผ IP์ฃผ์†Œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ด IP๋ฅผ ํ†ตํ•ด Pod์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ๋ฐ ์ด ๋•Œ IP๋ฅผ Cluster IP ๋ผ๊ณ  ํ•จ
    • ๋ถˆ๋ณ€์„ฑ
  3. Node Port: ํด๋ผ์ด์–ธํŠธ IP ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ ๋…ธ๋“œ์˜ Port๋กœ ์ ‘์†๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.
  4. External Name
    • ExternalName์€ ์™ธ๋ถ€ ์„œ๋น„์Šค๋ฅผ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ๋‚ด๋ถ€์—์„œ ํ˜ธ์ถœํ•˜๊ณ ์žํ• ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ๋‚ด์˜ Pod๋“ค์€ ํด๋Ÿฌ์Šคํ„ฐ IP๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํด๋Ÿฌ์Šคํ„ฐ IP ๋Œ€์—ญ ๋ฐ–์˜ ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•˜๊ณ ์ž ํ•˜๋ฉด, NAT ์„ค์ •๋“ฑ ๋ณต์žกํ•œ ์„ค์ •์ด ํ•„์š”ํ•˜๋‹ค. ์ด๋•Œ ์„œ๋น„์Šค๋ฅผ External ํƒ€์ž…์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์ฃผ์†Œ๋ฅผ DNS๋กœ ์„ค์ •ํ•˜๋ฉด ์ด ์„œ๋น„์Šค๋Š” ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์„ DNS๋กœ ํฌ์›Œ๋”ฉํ•จ

Kube Proxy

ํด๋ผ์ด์–ธํŠธ๋Š” Service Port๋กœ inbound ์—ฐ๊ฒฐ์„ ํ•˜๊ณ  proxy์—์„œ ์„œ๋ฒ„๋กœ outbound ์—ฐ๊ฒฐ์„ ์‹œํ–‰ํ•จ. ์ด๋Ÿฐ ์ข…๋ฅ˜์˜ proxy ์„œ๋ฒ„๋“ค์€ ๋ชจ๋‘ user space์—์„œ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ํŒจํ‚ท๋“ค์€ user space๋ฅผ ์ง€๋‚˜ kernal space๋ฅผ ๊ฑฐ์ณ proxy๋œ๋‹ค. ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์œ„ํ•ด์„œ๋Š” interface device๊ฐ€ ํ•„์š”ํ•œ๋ฐ kube proxy ๋˜ํ•œ user space proxy๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ interface device๋Š” host์— ์กด์žฌํ•˜๋Š” ethernet interface์ด๊ฑฐ๋‚˜ Pod๋‚ด์— ์กด์žฌํ•˜๋Š” ๊ฐ€์ƒ ethernet interface ๋‘๊ฐœ ๋ฟ์ด๋‹ค.

๋‹ค๋งŒ ์ด๊ฒƒ๋“ค์€ ๊ฐ€์ƒ์ด๋ฏ€๋กœ ํฌํŠธ๋ฅผ ์—ด๊ณ  ์ปค๋„ฅ์…˜์„ ๋งบ๋Š” ๋“ฑ์˜ ํ–‰์œ„๋Š” ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ ์ž์ฒด๋ฅผ ์ด์šฉํ•˜์ง€๋Š” ๋ชปํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋ฆฌ๋ˆ…์Šค ์ปค๋„ ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜์ธ netfilter์™€ user space์— ์กด์žฌํ•˜๋Š” interface์ธ iptables๋ผ๋Š” ๋…€์„๋“ค์„ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•ฉ๋‹ˆ๋‹ค. netfilter๋Š” ๊ฐ„๋‹จํžˆ ๋งํ•˜์ž๋ฉด Rule-Based ํŒจํ‚ท ์ฒ˜๋ฆฌ ์—”์ง„์ด๋‹ค. ์ฆ‰ ๋ชจ๋“  ์˜ค๊ณ  ๊ฐ€๋Š” ํŒจํ‚ท์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

๊ทธ๋ ‡๋‹ค. netfilter๋ž€ kernal space์— ์กด์žฌํ•˜๋Š” kube proxy์ž…๋‹ˆ๋‹ค.

์•„๋ž˜ ๋„ํ‘œ๋Š” kube-proxy๊ฐ€ user space proxy๋กœ ์‹คํ–‰๋ ๋•Œ netfilter์˜ ์—ญํ• ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

image

  1. kube-proxy๊ฐ€ localhost interface์—์„œ service์˜ ์š”์ฒญ์„ ๋ฐ›์•„๋‚ด๊ธฐ ์œ„ํ•ด 10400 ํฌํŠธ(์˜ˆ์ œ ๊ธฐ์ค€)๋ฅผ ์—ฝ๋‹ˆ๋‹ค.
  2. netfilter๋กœ ํ•˜์—ฌ๊ธˆ service IP๋กœ ๋“ค์–ด์˜ค๋Š” ํŒจํ‚ท์„ kube-proxy ์ž์‹ ์—๊ฒŒ ๋ผ์šฐํŒ… ๋˜๋„๋ก ์„ค์ •์„ ํ•ฉ๋‹ˆ๋‹ค.
  3. kube-proxy๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ์‹ค์ œ server Pod์˜ IP:Port๋กœ ์š”์ฒญ์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ์ œ์—์„œ๋Š” 10.0.2.2:8080)

์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด service IP 10.3.241.152:80๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ๋งˆ๋ฒ•์ฒ˜๋Ÿผ ์‹ค์ œ server Pod๊ฐ€ ์œ„์น˜ํ•œ 10.0.2.2:8080๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

netfilter์˜ ๋Šฅ๋ ฅ์„ ๋ณด์ž๋ฉด, ์ด ๋ชจ๋“  ๊ฒƒ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹จ์ง€ kube-proxy๊ฐ€ ์ž์‹ ์˜ ํฌํŠธ๋ฅผ ์—ด๊ณ  ๋งˆ์Šคํ„ฐ api ์„œ๋ฒ„๋กœ ๋ถ€ํ„ฐ ์ „๋‹ฌ ๋ฐ›์€ service ์ •๋ณด๋ฅผ netfilter์— ์•Œ๋งž๋Š” ๊ทœ์น™์œผ๋กœ ์ž…๋ ฅํ•˜๋Š” ๊ฒƒ ์™ธ์—” ๋‹ค๋ฅธ ๊ฒƒ์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค.

k8s๋Š” userspace์—์„œ proxing์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  ํŒจํ‚ท์„ user space์—์„œ kernal space๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด kubernetes 1.2 kube proxy์—์„œ๋Š” iptables mode๊ฐ€ ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค. ์ด ๋ชจ๋“œ์—์„œ๋Š” kube proxy๊ฐ€ ์ง์ ‘ proxy์˜ ์„ค์ •์„ ํ•˜์ง€ ์•Š๊ณ  ๊ทธ ์—ญํ• ์„ netfilterdp ๋งก๊น๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด service IP๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ณ  ๊ทธ๊ฒƒ์„ ์‹ค์ œ Pod๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์€ ๋ชจ๋‘ netfilter๊ฐ€ ๋‹ด๋‹นํ•˜๊ฒŒ ๋˜์—ˆ๊ณ  kube-proxy๋Š” ๋‹จ์ˆœํžˆ ์ด netfilter์˜ ๊ทœ์น™์„ ์•Œ๋งž๊ฒŒ ์ˆ˜์ •ํ•˜๋Š” ๊ฒƒ์„ ๋‹ด๋‹นํ•  ๋ฟ์ž…๋‹ˆ๋‹ค.

image

__

๐Ÿ’ฟ Kube Proxy์˜ ๋‚ด๊ตฌ์„ฑ

Kube Proxy๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ systemd unit์œผ๋กœ ๋™์ž‘ํ•˜๊ฑฐ๋‚˜ daemonset์œผ๋กœ ์„ค์น˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ฃฝ์–ด๋„ ๋‹ค์‹œ ์‚ด์•„๋‚  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋‹ค๋งŒ kube proxy๋Š” user space ๋ชจ๋“œ์—์„œ ๋™์ž‘ํ• ๋•Œ ๋‹จ์ผ ์ง€์  ์žฅ์• ์ ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ip table ๋ชจ๋“œ์—์„œ ์‹คํ–‰ ํ•  ๋•Œ๋Š” ์•ˆ์ •์ ์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
    • ์™œ๋ƒํ•˜๋ฉด ์ด๋Š” netfilter๋ฅผ ํ†ตํ•ด ๋™์ž‘ํ•˜๊ณ  ์„œ๋ฒ„๊ฐ€ ์‚ด์•„์žˆ๋Š” ํ•œ netfilter๋„ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์žฅ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๐Ÿ’ฟ Kube Proxy์˜ Service ๊ฐ์ง€

image

kube proxy๋Š” ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ์˜ api server์˜ ์ •๋ณด๋ฅผ ์ˆ˜์‹ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•จ ์ด๋ฅผ ํ†ตํ•ด ์ง€์†์ ์œผ๋กœ iptable์„ ์—…๋ฐ์ดํŠธํ•˜์—ฌ netfilter์˜ ๊ทœ์น™์„ ๋ณ€ํ™”์‹œํ‚ต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ์„œ๋น„์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด kube-proxy๋Š” ์•Œ๋ฆผ์„ ๋ฐ›๊ณ  ๊ทธ์— ๋งž๋Š” ๊ทœ์น™์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ service๊ฐ€ ์‚ญ์ œ๋˜๋ฉด ์ด์™€ ๋น„์Šทํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ทœ์น™์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

์„œ๋ฒ„์˜ health-check๋Š” kubelet์„ ํ†ตํ•˜์—ฌ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. kubelet์€ ์„œ๋ฒ„์—์„œ ์„ค์น˜๋˜๋Š” ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ์ปดํฌ๋„ŒํŠธ ์ค‘ ํ•˜๋‚˜๋กœ ์„œ๋ฒ„(๋…ธ๋“œ์˜) Pod๋ฅผ ์ง์ ‘ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์ด kubelet์ด ์„œ๋ฒ„์˜ health check์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌ์‹œ ๋งˆ์Šคํ„ฐ api ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด kube-proxy์—๊ฒŒ ์•Œ๋ ค unhealthy Pod์˜ endpoint๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ฒƒ๋“ค์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ณ ๊ฐ€์šฉ์„ฑ์„ ์œ ์ง€ํ•˜๋Š”๋ฐ ๋‹จ์ ์œผ๋กœ๋Š” Pod์—์„œ ์š”์ฒญํ•œ Request๋งŒ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ netfilter๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€์—์„œ ๋“ค์–ด์˜จ ์š”์ฒญ์— ๋Œ€ํ•ด ์› ์š”์ฒญ์ž์˜ origin IP๊ฐ€ ์ˆ˜์ •๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ 2ํŽธ์—์„œ ๊ณ„์†โ€ฆ

This post is licensed under CC BY 4.0 by the author.

Kubernetes 4

Kubernetes network 2