๋กœ์ผ“๐Ÿพ
article thumbnail

์„ค๋ช…์€ ํ•„์š”์—†๊ณ  ๊ฒฐ๋ก ๋งŒ ์•Œ๊ณ  ์‹ถ์€ ๋ถ„๋“ค์„ ์œ„ํ•œ

$ sed "$(grep -n 'image:' nginx.yaml | grep -Eo '^[^:]+')s/nginx:1.0/nginx:2.0/g" nginx.yaml

๋ณธ๋ฌธ์˜ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•œ ์ƒํ™ฉ ์˜ˆ์‹œ

 

์œ„ ์ด๋ฏธ์ง€๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ปค๋ฐ‹ํ•œ ํ›„ ์ž๋™์œผ๋กœ k8s cluster์— ๋ฐฐํฌ๋˜๊ธฐ ๊นŒ์ง€์˜ ๊ณผ์ •์„ ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ๋„์‹ํ™”ํ•œ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค.

 

๊ฐœ๋ฐœ์ž๊ฐ€ ์šด์˜ ์ค‘์ธ ์„œ๋น„์Šค์— ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ฒ„๊ทธ๋ฅผ ์ˆ˜์ • ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ GitOpsRepo์˜ ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€์˜ ๋ฒ„์ „ ๋˜ํ•œ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

 

๊ทธ๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ ์  ํ‚จ์Šค์—์„œ GitOps Repo์— push๋ฅผ ํ•  ๋•Œ ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“  ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€์˜ ๋ฒ„์ „์œผ๋กœ ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์•ผ argoCD์—์„œ git Hook์„ ์ธ์ง€ํ•˜๊ณ  ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“  ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํŒŒ๋“œ๋ฅผ ์žฌ๋ฐฐํฌํ•ด์ค„ํ…Œ๋‹ˆ๊นŒ์š”.

 

์ด๋Ÿด ๋•Œ "ํŒŒ์ผ์—์„œ ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰ ํ•œ ํ›„ ๊ทธ ๋ฌธ์ž์—ด์˜ ๋ผ์ธ ๋„˜๋ฒ„ ๊ฐ€์ ธ์˜ค๊ธฐ" ๊ฐ€ ํ•„์š”๋กœ ๋ฉ๋‹ˆ๋‹ค.

 

# yaml
images: nginx:1.0

# yaml
images: nginx:2.0

#์œผ๋กœ ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค

์ฒ˜์Œ์—๋Š” ์ด๋ ‡๊ฒŒ ํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.  sed ๋ช…๋ น์–ด๋กœ "image:"๋ผ๋Š” ๋‹จ์–ด๋ฅผ ํ†ต์งธ๋กœ ์ฐพ์€ ํ›„ ๊ทธ ๋ผ์ธ์„ ์•„์˜ˆ ์ƒˆ๋กญ๊ฒŒ ๋ฐ”๊ฟ”์ฃผ์—ˆ์ฃ .

์ฆ‰, ๋ผ์ธ ์ž์ฒด๋ฅผ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค. ๋ญ”๊ฐ€ ์ข‹์•„๋ณด์ด์ง„ ์•Š์Šต๋‹ˆ๋‹ค.. ๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ๋ก  ๋ฒ„์ „๊ณผ ์ €์žฅ์†Œ ์ด๋ฆ„, ์ปจํ…Œ์ด๋„ˆ ์ด๋ฏธ์ง€๊นŒ์ง€ ๋™์ ์œผ๋กœ ์ง€์ •ํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ›จ์”ฌ ์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•ด์ง‘๋‹ˆ๋‹ค.

$ sed -i "/image:/c\\        image: nginx:2.0" nginx.yaml

# ์‹ค์ œ๋กœ CICD์—์„œ ์‚ฌ์šฉํ•œ ๋ช…๋ น์–ด
$ sed -i "/image:/c\\        image: $(echo $DOCKER_USER_ID)/$(echo $image_name):$(echo $BUILD_NUMBER)" deployment.yaml

 

๊ทธ๋ž˜์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๋ชจ์ƒ‰ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. sed ๋ผ๋Š” ๋ช…๋ น์–ด๋Š” ํ–‰๋ฒˆํ˜ธ๋ฅผ ์ฃผ๊ฒŒ๋˜๋ฉด ๊ทธ ํ–‰๋ฒˆํ˜ธ์—์„œ๋งŒ ์ฐพ๊ฒŒ๋˜๋Š” ๊ธฐ๋Šฅ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ–‰๋ฒˆํ˜ธ๋ฅผ ์ฃผ์–ด ๊ทธ ํ–‰๋ฒˆํ˜ธ์—์„œ๋งŒ ๋ฌธ์ž์—ด ์น˜ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

sed -i "ํ–‰๋ฒˆํ˜ธs/๊ธฐ์กด๋ฌธ์ž์—ด/์ƒˆ๋ฌธ์ž์—ด/" ํŒŒ์ผ๋ช…

 

์‹ค์Šต์€ ์•„๋ž˜ nginx.yaml๋กœ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.0 # ์—ฌ๊ธฐ๋ฅผ ๋ฐ”๊พธ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
        name: nginx
        resources: {}
status: {}

 

์šฐ์„  "image:"์ด๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ฐพ์•„ ๊ทธ ๋‹จ์–ด๊ฐ€ ํฌํ•จ๋˜๋Š” ๋ผ์ธ์˜ ๋„˜๋ฒ„๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.

$ grep -n "image:" nginx.yaml

grep -n "image:" nginx.yaml

ํ•˜์ง€๋งŒ ์›ํ•˜๋˜ ๊ฒƒ์€ "21:      - image: nginx:1.0"์ด ์•„๋‹ˆ๊ณ  "21" ํ•˜๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์€ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค.

$ grep -n 'image:' nginx.yaml | grep -Eo '^[^:]+'

 

21๋งŒ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. ์›ํ•˜๋˜ ๊ฐ’์„ ์ฐพ์•˜์Šต๋‹ˆ๋‹ค!!

$ sed -i "ํ–‰๋ฒˆํ˜ธs/๊ธฐ์กด๋ฌธ์ž์—ด/์ƒˆ๋ฌธ์ž์—ด/" ํŒŒ์ผ๋ช…

 

๊ทธ๋Ÿผ ์œ„ ๋ช…๋ น์–ด๋ฅผ ํ™œ์šฉํ•ด์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ช…๋ น์–ด๋ฅผ ์ˆ˜์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

$ sed "$(grep -n 'image:' nginx.yaml | grep -Eo '^[^:]+')s/nginx:1.0/nginx:2.0/g" nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:2.0 #์ž˜ ๋ฐ”๋€Œ์—ˆ์Šต๋‹ˆ๋‹ค.
        name: nginx
        resources: {}
status: {}

 

์‚ฌ์‹ค ์œ„ ๋ช…๋ น์–ด๋„ ๊นจ๋—ํ•ด๋ณด์ด์ง€ ์•Š์ง€๋งŒ ํ™•์‹คํžˆ ๋งจ์ฒ˜์Œ ์ง  ์ฝ”๋“œ๋ณด๋‹ค๋Š” ํ›จ์”ฌ ์•ˆ์ •์„ฑ์ด ์žˆ๋Š” ๋Š๋‚Œ์ž…๋‹ˆ๋‹ค. ์ฒ˜์Œ ๋ช…๋ น์–ด๋Š” "image:"๋ผ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ „์—ญ์—์„œ ์ฐพ๊ณ , ์ฐพ์€ ๋‹จ์–ด์˜ ๋ผ์ธ์„ ์ง€์›Œ๋ฒ„๋ฆฌ๊ณ  ๋‹ค์‹œ ์ƒ์„ฑํ•˜๋Š” ๊ฑฐ๋‹ˆ๊น ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์ค‘์š”ํ•œ yaml์—์„œ๋Š” ์น˜ํ™˜๋˜๋Š” ๊ณผ์ •์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฆ‰, ๋”ฑ ๋ฒ„์ „๋งŒ ์ฐพ์•„ ๋ฐ”๊พธ๊ฒŒ ๋˜๋‹ˆ๊น ํ›จ์”ฌ ์•ˆ์ •์„ฑ์ด ์žˆ์–ด๋ณด์ž…๋‹ˆ๋‹ค!!

 

profile on loading

Loading...