K8s์˜ Pod์—์„œ ํ•จ๊ป˜ ๋Œ์•„๊ฐ€๋Š” container๋“ค์„ ์„œ๋กœ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ณต์œ ํ•œ๋‹ค!

2 minute read

K8s์˜ Pod์—์„œ ํ•จ๊ป˜ ๋Œ์•„๊ฐ€๋Š” container๋“ค์„ ์„œ๋กœ ๋„คํŠธ์›Œํฌ๋ฅผ ๊ณต์œ ํ•œ๋‹ค!

๋‘ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•˜๋‚˜์˜ Pod ์œ„์—์„œ ์‹คํ–‰๋˜๋Š” multi container Pod์„ ์ƒ๊ฐํ•ด๋ณด์ž. ๋‘ ์ปจํ…Œ์ด๋„ˆ๋Š” ์–ด๋–ป๊ฒŒ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ์„๊นŒ? Docker Network๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” docker-compose์˜ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•˜๋ฉด, ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์œผ๋กœ ์„œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๊ธด ํ–ˆ๋‹ค. K8s Pod์—์„œ๋„ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์œผ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?

K8s Pod์—์„œ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์œผ๋กœ ํ†ต์‹ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ loalhost:[port]๋กœ ํ†ต์‹ ํ•ด์•ผ ํ•œ๋‹ค. docker-compose์ฒ˜๋Ÿผ ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์œผ๋กœ ํ†ต์‹ ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค. ์‚ฌ์‹ค K8s Pod์€ ๋‚ด๋ถ€์ ์œผ๋กœ ํ•˜๋‚˜์˜ ํ˜ธ์ŠคํŠธ๋กœ ์ทจ๊ธ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋งˆ์น˜ ํ˜ธ์ŠคํŠธ ์ปดํ“จํ„ฐ์—์„œ docker runํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์ด localhost๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ์ด๋Ÿฐ ์ ์„ ์ข€๋” ์‚ดํŽด๋ณด์ž.

Pod ๋‚ด๋ถ€์—์„  ๋™์ผ ํฌํŠธ ์‚ฌ์šฉ ๋ถˆ๊ฐ€

์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ผ ํฌํŠธ๋ฅผ ์“ฐ๋Š” ๋‘ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ•˜๋‚˜์˜ Pod์œผ๋กœ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜๋Š” ์—†๋‹ค.

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: container1
    image: nginx:alpine
  - name: container2
    image: nginx:alpine

์ด ๊ฒฝ์šฐ, container2์—์„œ ์ด๋ฏธ ์ ์œ ํ•˜๊ณ  ์žˆ๋Š” ํฌํŠธ๋ฅผ ์“ฐ๋ ค๊ณ  ํ•œ๋‹ค๊ณ  ์˜ค๋ฅ˜๊ฐ€ ๋‚œ๋‹ค.

Pod ๋‚ด๋ถ€์—์„  localhost๋กœ ํ†ต์‹ 

์ด๋ฒˆ์—๋Š” container2๋ฅผ 6379 ํฌํŠธ๋ฅผ ์“ฐ๋Š” redis๋กœ ๋ฐ”๊ฟ”๋ณด์ž.

apiVersion: v1
kind: Pod
metadata:
  name: multi-container-pod
spec:
  containers:
  - name: container1
    image: nginx:alpine
  - name: container2
    image: redis:alpine # redis๋Š `6379` ํฌํŠธ์—์„œ ์‹คํ–‰๋œ๋‹ค

๊ทธ๋ฆฌ๊ณ  nc ๋ช…๋ น์–ด๋กœ localhost์— ํ•ด๋‹น ํฌํŠธ๊ฐ€ ์—ด๋ ค ์žˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

$ k exec -it multi-container-pod -- nc -vz localhost 80
localhost ([::1]:80) open
$ k exec -it multi-container-pod -- nc -vz localhost 6379
localhost ([::1]:6379) open

80, 6379 ํฌํŠธ๋ฅผ ์—ด๋ผ๊ณ  ๋ช…์‹œํ•˜์ง€๋„ ์•Š์•˜๋Š”๋ฐ, localhost์—์„œ ํ•ด๋‹น ํฌํŠธ๋“ค์ด ์—ด๋ ค ์žˆ๋‹ค.

์ฆ‰, K8s Pod์€ ํ•˜๋‚˜์˜ ํ˜ธ์ŠคํŠธ ๋จธ์‹ ์œผ๋กœ์จ container๋“ค์„ ๋Œ๋ฆฌ๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค!