지니데비 기록 자세히보기

데브옵스/Orchestration

[Kubernetes] 11. Imperative(명령적) vs Declarative(선언적) + 필수암기

지니데비 2024. 11. 5. 17:52
728x90

Imperative(명령형) 관리
## Create objects
kubectl run --image=nginx nginx
kubectl run hazelcast --image=hazelcast/hazelcast --labels="app=hazelcast,env=prod"
kubectl create deployment --image=nginx nginx
kubectl expose deployment nginx --port 80


## Update object
kubectl edit deployment nginx
kubectl scale deployment nginx --replicas=5
kubectl set image deployment nginx nginx=nginx:1.18


kubectl create -f nginx.yaml # 기존 pod 있으면 생성되지 않음
kubectl replace -f nginx.yaml # 기존 pod 있으면 대체되지 않음
kubectl replace --force -f nginx.yaml # 기존 pod 있으면 삭제하고 대체함
kubectl delete -f nginx.yaml
Declarative(선언형) 관리
## Create objects
kubectl apply -f nginx.yaml
kubectl apply -f /path/to/config-files # 정의된 objects 한번에 생성


## Update objects
kubectl apply -f nginx.yaml # 기존 pod 있어도 상관 없음
필수암기 command
# POD
kubectl run nginx --image=nginx
kubectl run nginx --image=nginx --dry-run=client -o yaml

# Deployment
kubectl create deployment --image=nginx nginx
kubectl create deployment --image=nginx nginx --dry-run=client -o yaml
kubectl create deployment nginx --image=nginx --replicas=4
kubectl scale deployment nginx --replicas=6
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml

# Service
kubectl expose pod redis --port=6379 --name-redis-service --dry-run=client -o yaml
kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml # Pod의 기존 라벨로 selector 자동 지정 BUT, NodePort 지정 불가 (★추천★)
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml # NodePort 지정 가능 BUT, selector 자동 지정 불가

# Recommend
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml > pod-definition.yaml
kubectl apply -f pod-definition.yaml

정리하면,

특징 명령적 (Imperative) 선언적 (Declarative)
작동 방식 명령 실행 시 바로 적용 YAML 파일로 상태 정의 및 적용
상태 관리 명령어 히스토리로 추적 필요 파일로 모든 상태를 기록
버전 관리 추적이 어려움 Git 등으로 쉽게 관리 가능
적합한 용도 일회성 작업, 즉각적인 생성 CI/CD, 자동화, GitOps
유지보수 복잡한 상태 추적 어려움 전체 구성 관리에 용이

 

✅ 배포 환경이나 팀 협업에서는 선언적 방식이 유리하고, 빠른 테스트나 일회성 작업에선 명령적 방식이 편리하다.

728x90
반응형

'데브옵스 > Orchestration' 카테고리의 다른 글

[Kubernetes] 13. Scheduling  (0) 2024.11.06
[Kubernetes] 12. Imperative(명령형) 실습  (0) 2024.11.06
[Kubernetes] 10. Namespace, ResourceQuota  (0) 2024.11.05
[Kubernetes] 9. Service  (0) 2024.11.05
[Kubernetes] 8. Deployment  (0) 2024.11.05