관리 메뉴

피터의 개발이야기

[kubernetes] ReplicaSet 기본 명령어 본문

Kubernetes/기초공부

[kubernetes] ReplicaSet 기본 명령어

기록하는 백앤드개발자 2024. 1. 15. 14:58
반응형

[kubernetes] 쿠버네티스 목차

ㅁ 들어가며

코어 리소스 중 ReplicaSet에 관한 kubectl 명령어 정리

 

ㅁ ReplicaSet 설명보기

$ kubectl explain replicaset
GROUP:      apps
KIND:       ReplicaSet
VERSION:    v1

DESCRIPTION:
    ReplicaSet ensures that a specified number of pod replicas are running at
    any given time.
    
FIELDS:
  apiVersion    <string>
    APIVersion defines the versioned schema of this representation of an object.
    Servers should convert recognized schemas to the latest internal value, and
    may reject unrecognized values. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

  kind  <string>
    Kind is a string value representing the REST resource this object
    represents. Servers may infer this from the endpoint the client submits
    requests to. Cannot be updated. In CamelCase. More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

  metadata      <ObjectMeta>
    If the Labels of a ReplicaSet are empty, they are defaulted to be the same
    as the Pod(s) that the ReplicaSet manages. Standard object`s metadata. More
    info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

  spec  <ReplicaSetSpec>
    Spec defines the specification of the desired behavior of the ReplicaSet.
    More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

  status        <ReplicaSetStatus>
    Status is the most recently observed status of the ReplicaSet. This data may
    be out of date by some window of time. Populated by the system. Read-only.
    More info:
    https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

 

 

ㅁ ReplicaSet 조회

# Replicaset 조회
$ kubectl get replicasets.apps

# 쇼컷 조회
$ k get rs

# 상세 조회
$ kubectl get rs new-replica-set -o wide

 ㅇ new-replica-set의 경우  Pod의 갯수는 4개이고 현재 4개로 구성되어져 있다. 

 ㅇ Pod의 Image와 Selector를 확인 할 수 있다.

 

ㅁ Pod 하나 삭제해 보기

$ k delete po new-replica-set-gwd6d
pod "new-replica-set-gwd6d" deleted

 ㅇ Pod 하나를 삭제를 하여도 ReplicaSet이 다시 Pod를 생성한다.

 

ㅁ ReplicaSet 생성

ㅇ replicaset-1.yaml 생성

$ cat replicaset-definition-1.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-1
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        
        
$ kubectl create -f /root/replicaset-definition-1.yaml
replicaset.apps/replicaset-1 created

 

ㅁ ReplicaSet 생성 전 오류 분석

# dry run을 통한 yaml 오류점검
$ kubectl apply -f replicaset-definition-2.yaml --dry-run=server
The ReplicaSet "replicaset-2" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"tier":"nginx"}: `selector` does not match template `labels`


$ cat replicaset-definition-2.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: replicaset-2
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend   <====== 셀렉터의 라벨이 template의 라벨가 다르다.
  template:
    metadata:
      labels:
        tier: nginx
    spec:
      containers:
      - name: nginx
        image: nginx

 ㅇ 셀렉터의 라벨이 template의 라벨가 달라서 발생한 오류이다.

 ㅇ --dry-run=server로 yaml의 오류를 점검할 수 있다.

 

ㅁ ReplicaSet 삭제

$ kubectl delete rs replicaset-1 replicaset-2
replicaset.apps "replicaset-1" deleted
replicaset.apps "replicaset-2" deleted

 

ㅁ ReplicaSet scale 변경

# scale를 이용한 방법
$ kubectl scale replicaset new-replica-set --replicas=5
replicaset.apps/new-replica-set scaled

# edit를 이용한 방법
$ kubectl edit rs new-replica-set 
~~~~~~~~~~~~~~~~
spec:
  replicas: 5 <==== 이 부분을 5로 수정한다.
  selector:
    matchLabels:

 

ㅁ 함께 보면 좋은 사이트

kubernetes ReplicaSet 공식문서

 

반응형
Comments