Kubernetes

[Kubernetes] Kustomize Patch로 리소스 커스터마이징 하기

sooondubu 2025. 9. 30. 15:17

 

 

Kustomize Patch로 리소스 커스터마이징 하기


개념

Kustomize Patch란?

쿠버네티스 리소스(yaml)를 수정하기 위해 덮어 쓰는 Kustomize 의 기능

 

Transformer VS Patch

구분 Patch Transformer
목표 특정 리소스의 세부 내용을 정밀하게 수정 여러 리소스에 걸쳐 공통적인 속성을 일괄적으로 변경
적용 범위 개별적 (Targeted): kind, name 등으로 대상을 지정 광범위 (Broad): kustomization.yaml에 포함된 모든 리소스
사용 방식 별도의 패치 파일을 작성하거나 kustomization.yaml에 인라인으로 정의 kustomization.yaml 내의 commonLabels, images 등과 같은 내장 필드 사용
주요 용도 레플리카 수, 이미지 태그, 환경 변수 등 특정 설정값 변경 공통 라벨/어노테이션 추가, 이름 접두사/접미사 적용, 네임스페이스 설정

패치 유형

Kustomize Patch를 적용하는 방법은 2가지가 있다.

JSON 6902 패치

  • RFC 6902 JSON 패치 표준을 따르는 방식.
  • target으로 대상을 지정하고, op(연산), path(수정할 필드의 YAML 경로), value(새 값)를 명시적으로 기술.
# kustomization.yaml
patches:
  - target:
      kind: Deployment
      name: nginx-deployment
    patch: |-
      - op: replace
        path: /spec/replicas
        value: 3
  • target: 패치를 적용할 리소스 종류
    • kind: 리소스 종류 (Deployment, Service, 등)
    • name: 리소스 이름 (metadate.name)
  • patch: 패치 내용을 작성하는 곳
    • op: 수행할 연산
    • path: 수정할 필드 위치를 지정 (/spec/replicas → spec.replicas)
    • value: 연산으로 바꿀 값

Strategic Merge 패치

  • 표준 쿠버네티스 설정 파일과 유사한 형태로 작성하여 가독성이 높음.
  • 원본 리소스에서 변경하려는 부분과 식별을 위한 kind, metadata 정보만 남긴 부분적인 YAML 파일을 패치로 사용.
# kustomization.yaml
patches:
  - patch: |-
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: nginx-deployment
      spec:
        replicas: 3
  • patchesStrategicMerge에 변경할 쿠버네티스 리소스를 작성, 원하는 필드만 변경 가능하다
  • Kustomize가 kind와 metadate.name을 통해 어떤 리소스에 적용할 지 식별한다. 즉 kind와 metadate.name은 필수!

테스트

원본

# nginx-depl.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      component: nginx
  template:
    metadata:
      labels:
        component: nginx
    spec:
      containers:
        - name: nginx
          image: nginx

Kustomize 결과

# guru ~/k8s-test/kustomize-patch-test $ k kustomize .
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    version: v1
  name: nginx-deployment
spec:
  replicas: 3       # 변경 !!
  selector:
    matchLabels:
      component: nginx
  template:
    metadata:
      labels:
        component: nginx
    spec:
      containers:
      - image: nginx
        name: nginx

연산 종류

1. replace

# kustomization.yaml
patches:
  - target:
      kind: Deployment
      name: nginx-deployment
    patch: |-
      # 레플리카 수 변경
      - op: replace
        path: /spec/replicas
        value: 3
      # 컨테이너 이미지 버전 변경
      - op: replace
        path: /spec/template/spec/containers/0/image
        value: nginx:1.25
# guru ~/k8s-test/kustomize-patch-test $ k kustomize .
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    version: v1
  name: nginx-deployment
spec:
  replicas: 3    # 변경 !!
  selector:
    matchLabels:
      component: nginx
  template:
    metadata:
      labels:
        component: nginx
    spec:
      containers:
      - image: nginx:1.25    # 변경 !!
        name: nginx

2. add

# kustomization.yaml
patches:
  - target:
      kind: Deployment
      name: nginx-deployment
    patch: |-
      # Deployment 레이블 추가
      - op: add
        path: /metadata/labels/region
        value: us-east-1
# guru ~/k8s-test/kustomize-patch-test $ k kustomize .
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    region: us-east-1    # 변경 !!
    version: v1
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: nginx
  template:
    metadata:
      labels:
        component: nginx
    spec:
      containers:
      - image: nginx
        name: nginx

3. remove

# kustomization.yaml
patches:
  - target:
      kind: Deployment
      name: nginx-deployment
    patch: |-
      # version 레이블 제거
      - op: remove
        path: /metadata/labels/version
# guru ~/k8s-test/kustomize-patch-test $ k kustomize .
apiVersion: apps/v1
kind: Deployment
metadata:
  labels: {}         # 변경 !!
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: nginx
  template:
    metadata:
      labels:
        component: nginx
    spec:
      containers:
      - image: nginx
        name: nginx