Kubernetes 기초 - Pod, Service, Deployment



Kubernetes(K8s)는 컨테이너화된 애플리케이션의 배포, 확장, 관리를 자동화하는 오픈소스 플랫폼입니다.



언제 사용하나요?



  • 여러 컨테이너를 오케스트레이션하고 관리할 때

  • 자동 스케일링, 롤링 업데이트가 필요할 때

  • 서비스 디스커버리, 로드밸런싱이 필요할 때

  • 마이크로서비스 아키텍처 운영



핵심 개념








개념설명
Pod가장 작은 배포 단위. 1개 이상의 컨테이너 묶음
ServicePod에 접근하기 위한 고정 엔드포인트
DeploymentPod의 선언적 배포 및 업데이트 관리
Namespace리소스를 논리적으로 분리하는 가상 클러스터


Pod 정의 (pod.yaml)


apiVersion: v1
kind: Pod
metadata:
name: my-app
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0
ports:
- containerPort: 8080
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
env:
- name: SPRING_PROFILES_ACTIVE
value: "production"


Deployment 정의 (deployment.yaml)


apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3 # Pod 개수
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0
ports:
- containerPort: 8080
readinessProbe: # 준비 상태 체크
httpGet:
path: /health
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
livenessProbe: # 생존 상태 체크
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10


Service 정의 (service.yaml)


apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app # 이 라벨의 Pod에 연결
ports:
- protocol: TCP
port: 80 # Service 포트
targetPort: 8080 # Pod 포트
type: ClusterIP # 클러스터 내부용

---
# 외부 노출용 (LoadBalancer)
apiVersion: v1
kind: Service
metadata:
name: my-app-external
spec:
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
type: LoadBalancer


Service 타입








타입설명
ClusterIP클러스터 내부에서만 접근 (기본값)
NodePort노드 IP:포트로 외부 접근
LoadBalancer클라우드 로드밸런서 생성
ExternalName외부 DNS 이름으로 매핑


kubectl 기본 명령어


# 리소스 생성
kubectl apply -f deployment.yaml

# Pod 목록
kubectl get pods
kubectl get pods -o wide # 상세

# Deployment 목록
kubectl get deployments

# Service 목록
kubectl get services

# 로그 확인
kubectl logs my-app-pod
kubectl logs -f my-app-pod # 실시간

# Pod 접속
kubectl exec -it my-app-pod -- /bin/bash

# 리소스 삭제
kubectl delete -f deployment.yaml
kubectl delete pod my-app-pod

# 스케일링
kubectl scale deployment my-app --replicas=5

# 롤링 업데이트
kubectl set image deployment/my-app my-app=my-app:2.0

# 롤백
kubectl rollout undo deployment/my-app

# 상태 확인
kubectl describe pod my-app-pod
kubectl describe deployment my-app


ConfigMap과 Secret


# ConfigMap 생성
kubectl create configmap app-config
--from-literal=DB_HOST=mysql
--from-literal=DB_PORT=3306

# Secret 생성
kubectl create secret generic db-secret
--from-literal=password=mypassword

# Deployment에서 사용
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: app-config
key: DB_HOST
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-secret
key: password


Namespace 사용


# Namespace 생성
kubectl create namespace production

# 특정 Namespace에 배포
kubectl apply -f deployment.yaml -n production

# Namespace별 조회
kubectl get pods -n production

# 기본 Namespace 변경
kubectl config set-context --current --namespace=production


자주 사용하는 명령어


# 모든 리소스 조회
kubectl get all

# 이벤트 확인 (문제 진단)
kubectl get events --sort-by=.metadata.creationTimestamp

# 리소스 사용량
kubectl top pods
kubectl top nodes

# YAML 출력
kubectl get deployment my-app -o yaml