Kustomize Basics
k8s/
βββ base/
β βββ kustomization.yaml
β βββ nginx-depl.yaml
β βββ service.yaml
β βββ redis-depl.yaml
βββ overlays/
βββ dev/
β βββ kustomization.yaml
β βββ config-map.yaml
βββ stg/
β βββ kustomization.yaml
β βββ config-map.yaml
βββ prod/
βββ kustomization.yaml
βββ config-map.yaml
Kustomize λ Environment λ³ λ€λ₯Έ μ€μ κ°μ μ£ΌκΈ° μν΄ μ¬μ©λλ ν΄μ΄λ€. κΈ°λ³Έμ μΌλ‘ base μ κΈ°λ³Έκ°μ΄ ν¬ν¨λ yaml νμΌλ€μ λ£μ΄λκ³ overlay μμ Environment λ³ κ°λ€μ μΆκ°ν΄μ£Όλ μμ΄λ€.
Helm μ κ²½μ° go template νμμΌλ‘ μ€μ κ°λ€μ μ£Όμ νμ§λ§ Kustomize λ μμ yaml νμΌλ‘ ꡬμ±λκΈ° λλ¬Έμ ν¨μ¬ μ½κΈ° νΈνλ€λ μ₯μ μ΄ μλ€. λ€λ§ Helm κ³Ό λ¬λ¦¬ ν¨ν€μ§ λ§€λμ μν μ νμ§ μλλ€.
# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- nginx-deployment.yaml
- nginx-service.yaml
commonLabels:
company: KodeKloud
μμ κ°μ΄ μ μ©ν νμΌλ€μ μΆκ°ν΄μ€ λ€ μΆκ°ν λ΄μ©μ μλ μ μ΄μ£Όλ©΄ λλ€.
kustomize build k8s/
Kustomize μ κ²½μ° μ€μ κ°μ μ£Όμ ν ν yaml νμΌμ λͺ¨μ΅μ μΆλ ₯νκΈ°λ§ ν λΏ μ€μ λ‘ apply νμ§ μλλ€.
kustomize build k8s/ | kubectl apply -f -
kubectl apply -k k8s/
μμ κ°μ λͺ λ Ήμ΄λ₯Ό μ¬μ©νλ©΄ μ€μ κ°μ΄ μ μ©λ Object λ€μ κ·Έλλ‘ apply ν μ μλ€.
k8s/
βββ kustomization.yaml
βββ api/
β βββ kustomization.yaml
βββ db/
β βββ kustomization.yaml
βββ cache/
β βββ kustomization.yaml
βββ kafka/
βββ kustomization.yaml
# k8s/kustomization.yaml
resources:
- api/
- db/
- cache/
- kafka/
μ¬λ¬ 리μμ€λ₯Ό ν λ²μ κ΄λ¦¬ν κ²½μ° κ° λ¦¬μμ€ λ§λ€ kustomization.yaml μ μ μΈν΄ μ€ λ€ μ΅μμ kustomization.yaml μμ μ¬μ©ν μ μλλ‘ μ€μ ν΄μ€ μ μλ€.
Transformers
Common Transformers
commonLabels:
org: OrgLabel
namespace: lab
namePrefix: Org-
nameSuffix: -dev
commonAnnotations:
branch: master
μμ κ°μ Transformer λ€μ νμ©ν΄ yaml νμΌμ μ‘°μν μ μλ€.
- commonLabels
- Resource μ Label μ μΆκ°νλ€.
- namePrefix/Suffix
- Resource μ΄λ¦ μ λλ λ€μ λ¨μ΄λ₯Ό μΆκ°νλ€.
- namespace
- Resource κ° μν namespace λ₯Ό μ§μ νλ€.
- commonAnnotations
- Resource μ Annotation μ μΆκ°νλ€.
Image Transformers
images:
- name: nginx
newName: haproxy
newTag: 2.4
nginx
λΌλ μ΄λ―Έμ§λ₯Ό μ¬μ©νλ Resource λ₯Ό haproxy:2.4
λ‘ λ³κ²½νλ€.
Patches
patches:
- target:
kind: Deployment
name: api-deployment
patch: |-
- op: replace
path: /metadata/name
value: web-deployment
Patch λ₯Ό μ¬μ©νλ©΄ μ€μ μ€μ κ°μ μ‘°μν μ μλ€. μ μμλ api-deployment
λΌλ μ΄λ¦μ κ°μ§ Deployment
Object μ metadata.name
μ web-deployment
λ‘ λ³κ²½νλ μμλ€.
Overlays
bases:
- ../../base
resources:
- grafana-depl.yaml
patch: |-
- op: replace
path: /spec/replicas
value: 2
Overlay μμ base μ μλ 리μμ€λ₯Ό λ³κ²½νκ³ μΆκ°ν΄μΌν κ²½μ° μμ²λΌ μμ±ν μ μλ€.
Components
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Component
resources:
- postgres-depl.yaml
secretGenerator:
- name: postgres-cred
literals:
- password=postgres123
patches:
- deployment-patch.yaml
νΉμ νκ²½μλ§ Component λ₯Ό μΆκ°ν΄μΌν κ²½μ° μμ κ°μ΄ μΆκ°ν μ μλ€.