DevOps

CKA 예제 리마인더 - 2. ReplicaController, ReplicaSet

Vince_rf 2024. 10. 24. 01:33

ReplicationController vs ReplicaSet

ReplicationController yaml 예시

apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
  labels:
    app: myapp
    tier: frontend
spec:
  template:

    metadata:
      name: myapp
      tier: frontend
    spec:
      containers:
      - name: myapp-container
        image: nginx

  replicas: 3


kubectl get replicationcontroller

ReplicaSet yaml 예시

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    tier: frontend
spec:
  template:

    metadata:
      name: myapp-pod
      labels:
        app: my-app
        tier: frontend
    spec:
      containers:
      - name: myapp-container
        image: nginx

  replicas: 3
  selector: 
    matchLabels:
      tier: frontend


kubectl get replicaset

 

 


replicas 수량 변경

 

yaml파일의 replicas 수량 변경 후

kubectl replace -f <yaml>

or

kubectl scale --replicas=6 -f <yaml>
kubectl scale --replicas=6 replicaset myapp-replicaset


!!!!!!!!! pod는 po듯이 kubectl 명령어 입력시 replicaset은 rs로 사용가능하다
ex) kubectl get rs



실행되고 있는 replicaset의 image를 변경하세요 ( yaml 파일 없음 )

나의 답안 )

kubectl get replicaset <replicaset-name> -o yaml > new-replica-set.yaml

yaml파일에 들어가서 image를 변경 후

kubectl delete replicaset <replicaset-name> ( or 실행중인 Pod를 delete하면 replicas 수량에 맞춰 다시 재시작된다 )

kubectl apply replicaset -f new-replica-set.yaml


Solution )

kubectl edit replicaset new-replica-set -> image 변경




실행되고 있는 replicaset의 replicas 수량을 변경하세요

나의 답안 ) 

kubectl scale --replicas=<수량> replicaset <replicaset-name>

or

yaml 파일에서 replicas 수량 변경 후 
kubectl apply replicaset -f <yaml-file>

or

kubectl edit replicaset <replicaset-name> 후에 replicas 수량 변경

Solution ) 나의 답안과 일치


replicaset yaml파일에서 잘못된 부분을 고치세요

나의 답안 ) replicaset의 apiVersion은 apps/v1이므로 기존의 v1이라고 되어있는 부분 수정

apiVersion: v1 -> apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    tier: frontend
spec:
  template:

    metadata:
      name: myapp-pod
      labels:
        app: my-app
        tier: frontend
    spec:
      containers:
      - name: myapp-container
        image: nginx

  replicas: 3
  selector: 
    matchLabels:
      tier: frontend



Solution )

실행해보면 apiVersion이 kind와 매치되지 않는다는 에러가 발생
replicaset의 apiVersion을 쉽게 찾으려면
kubectl explain replicaset


'DevOps' 카테고리의 다른 글

Kubernetes - Service ( Selector 예시 )  (0) 2024.10.26
Kubernetes - Service ( port, targetPort, NodePort )  (1) 2024.10.26
CKA 예제 리마인더 - 1. Pod  (0) 2024.10.24
Kubernetes - kube-proxy  (0) 2024.10.19
Kubernetes - kubelet  (1) 2024.10.19