Pod
Pod
쿠버네티스가 컨테이너를 다루는 기본 단위
Pod is a collection of containers that can run on a host.
- 1개 이상의 컨테이너로 구성된 컨테이너 집합
- 동일 파드 내 컨테이너는 여러 리눅스 네임스페이스를 공유
- 네트워크 네임스페이스 공유 (동일 노드, IP 사용)
- 사용자가 파드를 직접 관리하는 경우는 거의 없음
Manifest
apiVersion: v1
kind: Pod
metadata:
name: hello
spec:
containers:
- name: nginx
image: nginxdemos/hello:plain-text
ports:
- name: http
containerPort: 80
protocol: TCP
- name: debug
image: posquit0/doraemon:latest
Pod 관련 kubectl 명령어
kubectl apply -f multi.yaml
kubectl delete -f multi.yaml
kubectl get pod
kubectl describe pod hello
kubectl exec -it hello -- bash # exec default container
kubectl exec -it hello -c debug -- bash # exec 'debug' container
kubectl logs pod/hello
kubectl logs pod/hello -c debug # select 'debug' container
kubectl logs pod/hello -o wide
kubectl delete pod/hello
kubectl delete pod debug
kubectl api-resources | grep pod
- Cluster 접속 후 healthcheck
minikube ssh curl [IP]
멀티 컨테이너 파드와 사이드카 패턴
Side-car Pattern
메인 컨테이너를 보조하는 컨테이너와 같이 실행하는 구조 \
- Examples
- Filebeat: 로그 에이전트로 파드 로그 수집
- Envoy: 프록시 서버로 서비스메시 구성
- Vault Agent: 기밀 데이터 전달
- Nginx: 설정 리로드 역할 에이전트
ReplicaSet
ReplicaSet
정해진 수의 파드가 항상 실행될 수 있도록 관리
ReplicaSet ensures that a specified number of pod replicas are running at any given time.
- selector를 통해 replica의 개수를 조정
- matchLabels 등
- 기존 실행 중이던 파드에 문제가 생기면 파드를 다시 스케줄링
- 레플리카셋을 직접 관리하는 경우는 거의 없음
Manifest
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: hello
spec:
replicas: 3
selector:
matchLabels:
app: hello
template:
metadata:
name: hello
labels:
app: hello
spec:
containers:
- name: nginx
image: nginxdemos/hello:plain-text
ports:
- name: http
containerPort: 80
protocol: TCP
ReplicaSet 관련 kubectl 명령어
kubectl apply -f replicaset.yaml
kubectl delete -f replicaset.yaml
kubectl edit pod [POD]
kubectl get rs
kubectl api-resources | grep replicasets
Deployment
Deployment
파드의 이미지 버전이 갱신될 때 배포 전략을 설정
Deployment enables declarative updates for Pods and ReplicaSets.
- 파드의 이미지 버전이 갱신될 때 배포 전략을 설정
- Deployment 오브젝트 생성 시, 대응하는 ReplicaSet과 Pod 자동 생성
- 기본적으로 Recreate 전략과 RollingUpdate 전략 지원
- 특수한 목적이 아니라면 Pod, ReplicaSet이 아닌 Deployment로 워크로드 관리
Deployment 배포전략
- 재생성 (Recreate)
기존 replicaset의 pods를 모두 종료 후 새 레플리카셋의 파드를 새로 생성
- Default 설정
- 롤링 업데이트 (Rolling Update)
세부 설정에 따라 기존 replicaset에서 새 replicaset으로 점진적으로 이동
- maxSurge: 업데이트 과정에 spec.replicas 수 기준 최대 새로 추가되는 파드 수
- maxUnavailable: 업데이트 과정에 spec.replicas 수 기준 최대 이용 불가능 파드 수
- e.g. spec.replicas=10 인 경우,
- maxSurge: 1, maxUnavailable: 0
- 새 파드 생성 후 기존 파드 삭제 반복 (노드 최대 개수가 11개 가능)
- 이용 불가능 파드 수가 0 이기 때문에, 항상 사용가능한 pod수는 (10-0)개. 초과로 1개 파드를 더 만들고 하나씩 바꾸어나감.
- maxSurge: 0, maxUnavailable: 1
- 기존 파드 삭제 후 새 파드 생성 반복 (노드 최대 개수가 10개로 고정)
- 이용 불가능 파드 수가 1 이기 때문에, 항상 사용가능한 pod수는 (10-1)개. 기존 파드 1개를 지우고 새로운 파드를 만들어 하나씩 바꾸어나감.
apiVersion: apps/v1
kind: Deployment
metadata:
name: rolling
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
minReadySeconds: 5
revisionHistoryLimit: 5
replicas: 5
selector:
matchLabels:
app: rolling
template:
metadata:
name: rolling
labels:
app: rolling
spec:
containers:
- name: nginx
image: nginxdemos/hello:plain-text
ports:
- name: http
containerPort: 80
protocol: TCP
Deployment 관련 명령어
kubectl apply -f deployment.yaml --record
kubectl delete -f deployment.yaml
kubectl get deploy
kubectl get rs
kubectl get pods
kubectl rollout history deployment rolling
kubectl rollout history -f rolling-update.yaml
kubectl set image deployment rolling nginx=nginxdemos/hello:latest --record
kubectl rollout status deployment rolling
Service
Service
여러 파드에 대한 요청을 분산하는 로드 밸런서(L4) 기능 수행
Service is a named abstraction of software service (for example, mysql) consisting of local port (for example 3306) that the proxy listens on, and the selector that determines which pods will answer requests sent through the proxy.
- 여러 파드에 대해 클러스터 내에서 사용 가능한 고유 도메인 부여
- 파드의 IP는 항상 변할 수 있음에 유의
- 일반적으론 ClusterIP 타입의 SErvice와 함께 Ingress(L7)를 사용하여 외부 트래픽을 처리
서비스의 종류
- ClusterIP(default)
- 외부 트래픽을 받을 수 없음
- Kubernetes cluster는 Pod에 부여되는 Pod IP를 위한 CIDR 대역과, Service에 부여되는 Cluster IP CIDER 대역이 독립적으로 존재
kubectl cluster-info dump | grep -m 1 service-cluster-ip-range "--service-cluster-ip-range=10.96.0.0/12",
- Label Selector를 통해 서비스와 연결할 파드 목록 관리
- Cluster IP로 들어오는 요청에 대하여 파드에 L4 레벨의 로드밸런싱
- 로드 밸런싱을 통해 파드 이름(Server name)이 바뀌는 것을 확인가능
minikube ssh docker@minikube:~$ curl 10.99.142.87:8080 Server address: 10.244.0.41:80 Server name: hello-7cd57cb49f-tzlzw Date: 07/Aug/2025:15:26:57 +0000 URI: / Request ID: 22b4d8a748e2de0d507dc9cb905dcb61 docker@minikube:~$ curl 10.99.142.87:8080 Server address: 10.244.0.42:80 Server name: hello-7cd57cb49f-cljvf Date: 07/Aug/2025:15:26:58 +0000 URI: / Request ID: 0b619599ab9bf19ec29d9ed583d474a6
- Cluster IP 뿐만 아니라 내부 DNS를 통해 서비스 이름을 통한 통신 가능 (docker compose와 유사) ```bash kubectl run -it test --image=posquit0/doraemon bash
root@test:/# curl http://hello:8080 Server address: 10.244.0.41:80 Server name: hello-7cd57cb49f-tzlzw Date: 07/Aug/2025:15:29:27 +0000 URI: / Request ID: 589281aa66c259ca41239b9f2c1b8e0b root@test:/# curl http://hello:8080 Server address: 10.244.0.42:80 Server name: hello-7cd57cb49f-cljvf Date: 07/Aug/2025:15:29:27 +0000 URI: / Request ID: 82476149a7c3e6f4ea16143f614ab1f1 ```
apiVersion: v1
kind: Service
metadata:
name: hello
labels:
app: hello
spec:
type: ClusterIP
ports:
- name: http
protocol: TCP
port: 8080 # cluster port
targetPort: 80 # container port
selector:
app: hello
- NodePort
- 외부 트래픽을 받을 수 있음
- ClusterIP를 래핑하는 구조
- 모든 쿠버네티스 노드의 동일 포트를 개방하여 서비스에 접근하는 방식
apiVersion: v1
kind: Service
metadata:
name: hello
labels:
app: hello
spec:
type: NodePort
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 80
# nodePort: 31000 # use random port available
selector:
app: hello
- LoadBalancer
- 외부 트래픽을 받을 수 있음 (로드 밸런서)
- ClusterIP + 외부 로드밸런서를 동적으로 관리
- Cloud provider와 주로 함께 사용
- ExternalName
- 외부로 나가는 트래픽을 변환
- 도메인을 변환
Deployment 관련 명령어
kubectl apply -f deployment.yaml
kubectl delete -f deployment.yaml
minikube ssh
curl [IP]:8080