[k8s] replicaset

jordy·2023년 1월 1일
0

study-k8s

목록 보기
3/7

REPLICASET

Replicaset 적용

kubectl apply -f test-replicaset.yaml

❯ vim test-replicaset.yaml

❯ kubectl apply -f test-replicaset.yaml
replicaset.apps/test-replicaset created

❯ kubectl get replicaset,pods
NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/test-replicaset   3         3         3       34s

NAME                        READY   STATUS    RESTARTS   AGE
pod/test-replicaset-8555q   1/1     Running   0          34s
pod/test-replicaset-bkwxx   1/1     Running   0          34s
pod/test-replicaset-g4fk7   1/1     Running   0          34s

test-replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: test-replicaset
spec:
  template:
    metadata:
      name: test-replicaset
      labels:
        app: test-replicaset
    spec:
      containers:
      - name: test-replicaset
        image: nginx
        ports:
        - containerPort: 80
  replicas: 3
  selector:
    matchLabels:
      app: test-replicaset

Replicaset 내에 있는 POD 삭제

desired 유지하기 위해 새로운 pod 생성됨을 확인

❯ kubectl delete pod test-replicaset-g4fk7
pod "test-replicaset-g4fk7" deleted

❯ kubectl get replicaset,pods
NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/test-replicaset   3         3         3       57s

NAME                        READY   STATUS    RESTARTS   AGE
pod/test-replicaset-4hqd5   1/1     Running   0          4s
pod/test-replicaset-8555q   1/1     Running   0          57s
pod/test-replicaset-bkwxx   1/1     Running   0          57s

change pod label in replicaset

kubectl edit pod [podname]

❯ kubectl edit pod test-replicaset-bkwxx
pod/test-replicaset-bkwxx edited
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2023-01-01T07:55:41Z"
  generateName: test-replicaset-
  labels:
    app: independent-replicaset ## 수정함

Label 을 수정하면?

replicaset에서 관리하는 pod들은 label로 식별 하기 때문에 label 값 수정시 새로운 pod 생성됨을 확인

❯ kubectl get replicaset,pods
NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/test-replicaset   3         3         3       9m48s

NAME                        READY   STATUS    RESTARTS   AGE
pod/test-replicaset-4hqd5   1/1     Running   0          8m55s
pod/test-replicaset-8555q   1/1     Running   0          9m48s
pod/test-replicaset-bkwxx   1/1     Running   0          9m48s
pod/test-replicaset-l6fjt   1/1     Running   0          83s

파드 개수 조정

kubectl scale replicaset [replicaset name] --replicas=[변경 할 개수]

❯ kubectl scale replicaset test-replicaset --replicas=5
replicaset.apps/test-replicaset scaled
❯
❯
❯ kubectl get replicaset,pods
NAME                              DESIRED   CURRENT   READY   AGE
replicaset.apps/test-replicaset   5         5         3       15m

NAME                        READY   STATUS              RESTARTS   AGE
pod/test-replicaset-4hqd5   1/1     Running             0          14m
pod/test-replicaset-8555q   1/1     Running             0          15m
pod/test-replicaset-bkwxx   1/1     Running             0          15m
pod/test-replicaset-fw2rq   0/1     ContainerCreating   0          4s
pod/test-replicaset-l6fjt   1/1     Running             0          7m26s
pod/test-replicaset-zgt4s   0/1     ContainerCreating   0          4s

실행중인 Pods 유지하며 Replicaset 삭제

kubectl delete replicaset --cascade=false

❯ kubectl delete replicaset test-replicaset --cascade=false
warning: --cascade=false is deprecated (boolean value) and can be replaced with --cascade=orphan.
replicaset.apps "test-replicaset" deleted
❯ kubectl get replicaset,pods
NAME                        READY   STATUS    RESTARTS   AGE
pod/test-replicaset-4hqd5   1/1     Running   0          17m
pod/test-replicaset-8555q   1/1     Running   0          18m
pod/test-replicaset-bkwxx   1/1     Running   0          18m
pod/test-replicaset-fw2rq   1/1     Running   0          2m30s
pod/test-replicaset-l6fjt   1/1     Running   0          9m52s
pod/test-replicaset-zgt4s   1/1     Running   0          2m30s
❯ kubectl get replicaset
No resources found in default namespace.

label로 pod 삭제하기

1. kubectl delete pods,services -l name=myLabel ->

❯ kubectl delete pods -l app=test-replicaset
pod "test-replicaset-5hfqj" deleted
pod "test-replicaset-kd4ww" deleted
pod "test-replicaset-spcfc" deleted

2. kubectl delete pods --selector=<label_name>=<label_value>

❯ kubectl delete pods --selector=app=test-replicaset
pod "test-replicaset-4hqd5" deleted
pod "test-replicaset-8555q" deleted
pod "test-replicaset-fw2rq" deleted
pod "test-replicaset-l6fjt" deleted
pod "test-replicaset-zgt4s" deleted
profile
Hello Worlds!

0개의 댓글