본문 바로가기

Kubernetes

쿠버네티스 기초 ReplicaSet

Replication Controller와 사용 목적은 비슷하다.

Replication Controller에서는 label selector을 이용할때 A AND B AND C 방식을 사용했지만

ReplicaSet을 이용하면 A AND B AND (x or y) 사용할수 있다.

쉽게 말하자면 Replication Controller의 label selector는 특정 label을 포함하는 pod가 일치 하는지만 보고 

ReplicaSet의 selector는 특정 label이 없거나 해당 값과 관계없이 특정 label 키를 포함하는 pod를 매치 하는지 확인한다.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: Back-end
spec:
  template:

    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: Back-end
    spec:
      containers:
      - name: nginx-container
        image: nginx
  replicas: 3
  selector: 
    matchLabels:
      type: Back-end 

Scale을 늘리고 싶을떄는 첫번쨰 방법은 replicas:3 -> replicas:6로 수정해주고

kubectl replace -f [파일이름].yml 

을 실행해주면 replicas 가 6 으로 바뀌게 된다.

두번쨰 방법은 yml 파일을 수정하지 않고 수정이 가능하다

kubectl scale --replicas=6 -f [파일이름].yml

세번쨰 방법

kubectl scale --replicas=6 [type] [name]
#ex
kubectl scale --replicas-6 replicaset myapp-replicaset

replicaset에서 사용하는 명령어들

kubectl create -f [파일이름].yml
kubectl get replicaset
kubectl delete replicaset [name]
kubectl replace -f [파일이름].yml
kubectl scale --replicas=6 [type] [name]

replicaset 생성 후 확인

KDS-2:yaml kimdaesung$ kubectl create -f replicaset_v1.yml 
replicaset.apps/myapp-replicaset created 
KDS-2:yaml kimdaesung$ kubectl get replicasets.apps 
NAME               DESIRED   CURRENT   READY   AGE
myapp-replicaset   3         3         2       19s

scale 3 에서 6으로 수정 후 확인

KDS-2:yaml kimdaesung$ kubectl scale --replicas=6 -f replicaset_v1.yml 
replicaset.apps/myapp-replicaset scaled
KDS-2:yaml kimdaesung$ kubectl get replicasets.apps 
NAME               DESIRED   CURRENT   READY   AGE
myapp-replicaset   6         6         3       70s

pod을 확인해보면 6개의 replicaset이 실행되고 있는걸 볼수가 있다.

KDS-2:yaml kimdaesung$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
1pod-2container                     2/2     Running   8          4d20h
myapp-replicaset-2ldwd              1/1     Running   0          28s
myapp-replicaset-4v92t              1/1     Running   0          90s
myapp-replicaset-czcf6              1/1     Running   0          90s
myapp-replicaset-jcvlx              1/1     Running   0          28s
myapp-replicaset-jndlr              1/1     Running   0          28s
myapp-replicaset-md9dw              1/1     Running   0          90s
redis-pod                           1/1     Running   4          4d19h
replication-1pod-2container-9ntqv   2/2     Running   4          38h

KDS-2:yaml kimdaesung$ 

scale을 3으로 줄인 다음 확인

KDS-2:yaml kimdaesung$ kubectl scale --replicas=3 -f replicaset_v1.yml 
replicaset.apps/myapp-replicaset scaled
KDS-2:yaml kimdaesung$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
1pod-2container                     2/2     Running   8          4d20h
myapp-replicaset-4v92t              1/1     Running   0          118s
myapp-replicaset-czcf6              1/1     Running   0          118s
myapp-replicaset-md9dw              1/1     Running   0          118s
redis-pod                           1/1     Running   4          4d19h
replication-1pod-2container-9ntqv   2/2     Running   4          38h
KDS-2:yaml kimdaesung$ kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE     IP           NODE   NOMINATED NODE   READINESS GATES
1pod-2container                     2/2     Running   8          4d23h   172.17.0.2   m01    <none>           <none>
myapp-replicaset-4v92t              1/1     Running   0          3h23m   172.17.0.9   m01    <none>           <none>
myapp-replicaset-czcf6              1/1     Running   0          3h23m   172.17.0.8   m01    <none>           <none>
myapp-replicaset-md9dw              1/1     Running   0          3h23m   172.17.0.7   m01    <none>           <none>
redis-pod                           1/1     Running   4          4d23h   172.17.0.4   m01    <none>           <none>
replication-1pod-2container-9ntqv   2/2     Running   4          41h     172.17.0.3   m01    <none>           <none>