본문 바로가기

리눅스

쿠버네티스 레플리카셋(Replica Set)

728x90

쿠버네티스 레플리카셋(Replica Set)

쿠버네티스에서 레플리카셋(ReplicaSet)은 파드(Pod)의 복제본을 관리하는 리소스입니다. 레플리카셋은 지정된 수의 파드 복제본을 유지하도록 보장하며 파드가 비정상적으로 종료되었을 때 새로운 복제본을 생성하여 대체합니다.

 

레플리카셋의 중요한 특징

 

  1. 레플리카 수 유지 : 레플리카셋은 사용자가 지정한 수의 복제본을 유지합니다. 파드가 삭제되거나 종료된 경우 새로운 파드를 생성하여 복제본 수를 유지합니다.
  2. 라벨 셀렉터 사용 : 레플리카셋은 라벨 셀렉터를 사용하여 자신이 관리하는 파드를 식별합니다. 이를 통해 레플리카셋이 복제하는 파드를 선택할 수 있습니다.
  3. 스케일링 : 사용자는 필요에 따라 레플리카셋의 복제본 수를 조정하여 애플리케이션을 확장하거나 축소할 수 있습니다.
  4. 자동 복구 : 레플리카셋은 파드가 비정상적으로 종료되었을 때 해당 파드를 다시 시작하여 클러스터의 안정성을 유지합니다.

nginx 레플리카셋(--replicas=3) 배포

Usage:

kubectl create deployment NAME --image=image -- [COMMAND] [args...] [options]
  • nginx 이미지 배포
kubectl create deployment node-app --image=anti1346/node-app:latest --replicas=3 --port=3000
$ kubectl create deployment node-app --image=anti1346/node-app:latest --replicas=3 --port=3000
deployment.apps/node-app created
  • nginx 파드 라벨 확인
kubectl get pod --show-labels
$ kubectl get pod --show-labels
NAME                        READY   STATUS    RESTARTS   AGE   LABELS
node-app-66b8b74c89-tbgp5   1/1     Running   0          18s   app=node-app,pod-template-hash=66b8b74c89
node-app-66b8b74c89-tqgsr   1/1     Running   0          18s   app=node-app,pod-template-hash=66b8b74c89
node-app-66b8b74c89-wwbzw   1/1     Running   0          18s   app=node-app,pod-template-hash=66b8b74c89
  • nginx 파드 갯수 확인
kubectl get pod -l app=node-app
$ kubectl get pod -l app=node-app
NAME                        READY   STATUS    RESTARTS   AGE
node-app-66b8b74c89-tbgp5   1/1     Running   0          63s
node-app-66b8b74c89-tqgsr   1/1     Running   0          63s
node-app-66b8b74c89-wwbzw   1/1     Running   0          63s
  • 레플리카켓 정보 확인
kubectl get replicaset -l app=node-app
$ kubectl get replicaset -l app=node-app
NAME                  DESIRED   CURRENT   READY   AGE
node-app-66b8b74c89   3         3         3       99s
  • 레플리카셋 상세 정보
kubectl describe replicaset node-app-66b8b74c89
$ kubectl describe replicaset node-app-66b8b74c89
Name:           node-app-66b8b74c89
Namespace:      default
Selector:       app=node-app,pod-template-hash=66b8b74c89
Labels:         app=node-app
                pod-template-hash=66b8b74c89
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/node-app
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=node-app
           pod-template-hash=66b8b74c89
  Containers:
   node-app:
    Image:        anti1346/node-app:latest
    Port:         3000/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  2m16s  replicaset-controller  Created pod: node-app-66b8b74c89-tbgp5
  Normal  SuccessfulCreate  2m16s  replicaset-controller  Created pod: node-app-66b8b74c89-wwbzw
  Normal  SuccessfulCreate  2m16s  replicaset-controller  Created pod: node-app-66b8b74c89-tqgsr
kubectl exec -it $(kubectl get pods -o jsonpath='{.items[0].metadata.name}') -- /bin/bash
$ kubectl exec -it $(kubectl get pods -o jsonpath='{.items[0].metadata.name}') -- /bin/bash
root@node-app-66b8b74c89-tbgp5:/usr/src/app#
curl http://localhost:3000
root@node-app-66b8b74c89-tbgp5:/usr/src/app# curl http://localhost:3000
Hostname: node-app-66b8b74c89-tbgp5
Internal IP: 10.244.0.83
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:13:47

node-app 레플리카셋(--replicas=5)

Usage:

kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME [options]
  • 레플리카셋 수 조정
kubectl scale deployment node-app --replicas=5
$ kubectl scale deployment node-app --replicas=5
deployment.apps/node-app scaled
  • 레플리카셋 확인
kubectl get pods -l app=node-app
$ kubectl get pods -l app=node-app
NAME                        READY   STATUS    RESTARTS   AGE
node-app-66b8b74c89-49n22   1/1     Running   0          39s
node-app-66b8b74c89-5wwcg   1/1     Running   0          39s
node-app-66b8b74c89-tbgp5   1/1     Running   0          4m21s
node-app-66b8b74c89-tqgsr   1/1     Running   0          4m21s
node-app-66b8b74c89-wwbzw   1/1     Running   0          4m21s
  • 레플리카켓 정보 확인
kubectl get replicaset -l app=node-app
$ kubectl get replicaset -l app=node-app
NAME                  DESIRED   CURRENT   READY   AGE
node-app-66b8b74c89   5         5         5       4m39s
  • 레플리카셋 상세 정보
kubectl describe replicaset node-app-66b8b74c89
$ kubectl describe replicaset node-app-66b8b74c89
Name:           node-app-66b8b74c89
Namespace:      default
Selector:       app=node-app,pod-template-hash=66b8b74c89
Labels:         app=node-app
                pod-template-hash=66b8b74c89
Annotations:    deployment.kubernetes.io/desired-replicas: 5
                deployment.kubernetes.io/max-replicas: 7
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/node-app
Replicas:       5 current / 5 desired
Pods Status:    5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=node-app
           pod-template-hash=66b8b74c89
  Containers:
   node-app:
    Image:        anti1346/node-app:latest
    Port:         3000/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  5m10s  replicaset-controller  Created pod: node-app-66b8b74c89-tbgp5
  Normal  SuccessfulCreate  5m10s  replicaset-controller  Created pod: node-app-66b8b74c89-wwbzw
  Normal  SuccessfulCreate  5m10s  replicaset-controller  Created pod: node-app-66b8b74c89-tqgsr
  Normal  SuccessfulCreate  88s    replicaset-controller  Created pod: node-app-66b8b74c89-5wwcg
  Normal  SuccessfulCreate  88s    replicaset-controller  Created pod: node-app-66b8b74c89-49n22

서비스 생성(NodePort)

Usage:

kubectl create service [flags] [options]
  • 서비스 확인
kubectl get service node-app
$ kubectl get service node-app
Error from server (NotFound): services "node-app" not found
  • 서비스 생성
kubectl create service nodeport node-app --tcp=8080:3000
$ kubectl create service nodeport node-app --tcp=8080:3000
service/node-app created
  • NodePort 확인
kubectl get service node-app
$ kubectl get service node-app
NAME       TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
node-app   NodePort   10.96.109.124   <none>        8080:32529/TCP   26s
kubectl get service -l app=node-app
$ kubectl get service -l app=node-app
NAME       TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
node-app   NodePort   10.96.109.124   <none>        8080:32529/TCP   54s

 

kubectl exec -it $(kubectl get pods -o jsonpath='{.items[0].metadata.name}') -- /bin/bash
$ kubectl exec -it $(kubectl get pods -o jsonpath='{.items[0].metadata.name}') -- /bin/bash
root@node-app-66b8b74c89-49n22:/usr/src/app#
728x90

cURL 명령어로 서비스 확인

  • localhost 아이피 주소로 테스트
curl http://localhost
root@node-app-85fbd7fc8-2fz9v:/usr/src/app# curl http://localhost:3000
Hostname: node-app-85fbd7fc8-2fz9v
Internal IP: 10.244.0.77
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:03:18
  • 클러스터 아이피 주소로 테스트
curl http://10.96.109.124:8080
root@node-app-66b8b74c89-49n22:/usr/src/app# curl http://10.96.109.124:8080
Hostname: node-app-66b8b74c89-49n22
Internal IP: 10.244.0.86
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:19:33

root@node-app-66b8b74c89-49n22:/usr/src/app# curl http://10.96.109.124:8080
Hostname: node-app-66b8b74c89-tbgp5
Internal IP: 10.244.0.83
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:19:35

root@node-app-66b8b74c89-49n22:/usr/src/app# curl http://10.96.109.124:8080
Hostname: node-app-66b8b74c89-49n22
Internal IP: 10.244.0.86
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:19:36

root@node-app-66b8b74c89-49n22:/usr/src/app# curl http://10.96.109.124:8080
Hostname: node-app-66b8b74c89-tqgsr
Internal IP: 10.244.0.82
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:19:36

root@node-app-66b8b74c89-49n22:/usr/src/app# curl http://10.96.109.124:8080
Hostname: node-app-66b8b74c89-49n22
Internal IP: 10.244.0.86
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:19:39
  • 쿠버네티스 호스트 서버 아이피 주소로 테스트
curl http://$(minikube ip):32529
$ curl http://$(minikube ip):32529
Hostname: node-app-66b8b74c89-wwbzw
Internal IP: 10.244.0.84
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:24:26
$ curl http://$(minikube ip):32529
Hostname: node-app-66b8b74c89-tbgp5
Internal IP: 10.244.0.83
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:25:00

$ curl http://$(minikube ip):32529
Hostname: node-app-66b8b74c89-wwbzw
Internal IP: 10.244.0.84
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:25:03

$ curl http://$(minikube ip):32529
Hostname: node-app-66b8b74c89-5wwcg
Internal IP: 10.244.0.85
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:25:06

$ curl http://$(minikube ip):32529
Hostname: node-app-66b8b74c89-49n22
Internal IP: 10.244.0.86
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:25:21

$ curl http://$(minikube ip):32529
Hostname: node-app-66b8b74c89-tqgsr
Internal IP: 10.244.0.82
External IP: 211.239.167.24
Current Time: 2024. 2. 7. 오후 3:26:23

리소스 삭제

Pod 삭제

Usage:

kubectl delete ([-f FILENAME] | [-k DIRECTORY] | TYPE [(NAME | -l label | --all)]) [options]
  • 리소스 확인
kubectl get all -l app=node-app
$ kubectl get all -l app=node-app
NAME                            READY   STATUS    RESTARTS   AGE
pod/node-app-66b8b74c89-49n22   1/1     Running   0          17m
pod/node-app-66b8b74c89-5wwcg   1/1     Running   0          17m
pod/node-app-66b8b74c89-tbgp5   1/1     Running   0          21m
pod/node-app-66b8b74c89-tqgsr   1/1     Running   0          21m
pod/node-app-66b8b74c89-wwbzw   1/1     Running   0          21m

NAME               TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/node-app   NodePort   10.96.109.124   <none>        8080:32529/TCP   14m

NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/node-app   5/5     5            5           21m

NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/node-app-66b8b74c89   5         5         5       21m
  • node-app 파드 삭제
kubectl delete pod node-app-66b8b74c89-49n22
$ kubectl delete pod node-app-66b8b74c89-49n22
pod "node-app-66b8b74c89-49n22" deleted
  • node-app 파드 갯수 확인
kubectl get pod -l app=node-app
$ kubectl get pod -l app=node-app
NAME                        READY   STATUS    RESTARTS   AGE
node-app-66b8b74c89-5wwcg   1/1     Running   0          19m
node-app-66b8b74c89-tbgp5   1/1     Running   0          23m
node-app-66b8b74c89-tqgsr   1/1     Running   0          23m
node-app-66b8b74c89-wwbzw   1/1     Running   0          23m
node-app-66b8b74c89-zcv4l   1/1     Running   0          59s

Deployment 삭제

kubectl delete deployment node-app

Service 삭제

kubectl delete service node-app

 

728x90