이 글은 아래 자료를 참고하여 작성되었습니다.
Udemy [Kubernetes For the Absolute Beginners]
https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
1. ReplicationController란?
단일 애플리케이션 Pod가 충돌할 경우를 대비해, 다수의 Pod를 실행하여 고가용성을 제공하는 것이 중요하다. 이를 위해 Replication Controller가 필요하다
주요 기능
- 다중 인스턴스 실행: 여러 Pod 인스턴스를 실행해 애플리케이션의 가용성을 보장한다.
- 특정 개수의 동일한 Pod가 항상 실행되도록 보장한다
- ReplicationController / ReplicaSet이 생성된 시점에 조건을 만족하는 pod이 replicas 만큼 이미 존재한다면 추가적으로 pod을 생성하지 않는다. (초과하여 pod이 존재한다면 replicas의 숫자만큼만 남기고 삭제한다)
- 자동 복구: Pod가 실패하면 자동으로 새로운 Pod를 생성한다.
- 로드 분산: 여러 Pod 간에 로드를 분산시켜 성능을 최적화한다.
- 스케일링: 수요 증가 시 애플리케이션을 확장한다.
2. ReplicationController vs ReplicaSet
ReplicaSet은 기능적으로 ReplicationController와 유사하나, 몇 가지 차이점이 존재한다.
- label selector:
- RC: equality-based 라벨 셀렉터를 사용. matchLabels를 사용하여 특정 라벨 값을 정확히 일치시키는 조건만 사용 가능. 필수항목은 아니며, 정의되지 않으면 template에 정의된 pod의 lable을 디폴트로 사용한다.
- RS: set-based 라벨 셀렉터를 사용. matchLabels와 matchExpressions를 사용하여 다양한 조건을 설정 가능. matchExpressions에서는 In, NotIn, Exists, DoesNotExist 등의 조건 사용 가능. 필수로 정의해야 한다.
- 참고: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
- 호환성:
- RC는 Kubernetes 초기 버전부터 존재했으나, RS는 더 나중에 도입됨
- 대부분의 최신 Kubernetes 클러스터에서는 RC 대신 RS를 사용하는 것이 권장된다
- Deployment와의 통합:
- RC는 Deployment와 통합되지 않으며, 직접 관리해야 한다.
- RS는 Deployment와 함께 사용되며, 이는 RS의 업데이트 및 관리 작업을 단순화한다.
3. ReplicaSet 정의하기
# replicaset-definition.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-replicaset
labels:
app: myapp
spec:
replicas: 3
selector:
matchLabels:
type: front-end
template:
metadata:
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
- metadata:
- name (required): ReplicaSet의 이름을 정의. 클러스터 내에서 고유해야 한다.
- labels: ReplicaSet에 라벨을 추가
- spec:
- replicas: ReplicaSet이 유지해야 할 Pod의 복제본 수를 정의. 기본값은 1
- selector (required): ReplicaSet이 관리할 Pod를 선택하는 기준을 정의. matchLabels 또는 matchExpressions를 사용할 수 있다. 여기서는 type: front-end 라벨을 가진 Pod를 선택하도록 설정됨
- template (required): 새로운 Pod를 생성할 때 사용할 Pod 템플릿을 정의
- template.metadata (required): 생성될 Pod에 추가될 메타데이터 정의
- template.metadata.labels (required): ReplicaSet의 selector와 일치해야 한다.
- template.spec (required): Pod의 스펙을 정의
- template.spec.containers (required): Pod 내의 컨테이너를 정의
# 생성
kubectl create -f <file>
# 목록 보기
kubectl get rs
kubectl get replicaset
# 삭제
# 관리하고 있는 pod들도 모두 함께 삭제된다
kubectl delete replicaset <name>
# 업데이트
# 파일에 정의된 리소스로 기존 리소스를 완전히 교체
# 리소스가 이미 존재하는 경우, 현재 리소스를 완전히 삭제하고 새로운 정의로 대체
# 리소스가 존재하지 않는 경우, 오류 발생
kubectl replace -f <file>
# 업데이트 (definition 파일 수정 x)
kubectl edit replicaset <name>
# 스케일링 (definition 파일 수정 x)
# -f옵션의 경우 file을 input으로 사용할 뿐, 실제 yaml filed의 replicas는 변경되지 않는다.
kubectl scale --replicas=<number> -f <file>
kubectl scale --replicas=<number> replicaset <name>
# help (ReplicaSet에 대한 정보를 얻고 싶을 때)
kubectl explain replicaset
'Kubernetes' 카테고리의 다른 글
9. Kubernetes Overview - Networking (1) | 2024.07.08 |
---|---|
8. Core Concepts - Deployment (0) | 2024.07.04 |
6. Core Concepts - pod (0) | 2024.06.24 |
5. Introduction - Minikube (1) | 2024.06.24 |
4. Introduction - Kubernetes with YAML (1) | 2024.06.24 |