๐ŸšŒ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ReplicaSet, ReplicationController

๊น€์„ฑ์ธยท2023๋…„ 10์›” 11์ผ
0

[DevOps] ๐ŸณDocker & Kubernetes

๋ชฉ๋ก ๋ณด๊ธฐ
19/62

https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/

ReplicaSet

ReplicaSet์€ replica Pod์ด ์–ธ์ œ๋“ ์ง€ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก ์•ˆ์ •๋œ set์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•จ.
ํŠน์ • ์‹๋ณ„์ž๋ฅผ ๊ฐ€์ง€๋Š” Pod์˜ ์œ ํšจ์„ฑ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•จ.

ReplicaSet ๋™์ž‘

  • ReplicaSet์€ ํ•„๋“œ๋กœ ์ •์˜๋˜์–ด ์žˆ์Œ.
    • ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ Pod๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋ช…์‹œํ•œ selector,
    • ์œ ์ง€ ๋˜์–ด์•ผ ํ•˜๋Š” Pod์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ replica์˜ ๊ฐœ์ˆ˜,
    • replica๊ฐ€ ์œ ์ง€๋˜์–ด์•ผ ํ•  ๊ฐœ์ˆ˜์˜ ๊ธฐ์ค€์„ ๋งŒ์กฑํ•˜๊ธฐ ์œ„ํ•ด, ์ƒˆ๋กœ์šด Pod์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ช…์‹œํ•˜๋Š” ํƒฌํ”Œ๋ฆฟ(Template)
  • ReplicaSet์€ ์š”๊ตฌ๋˜๋Š” ๊ฐœ์ˆ˜์˜ Pod๋ฅผ ์ƒ์„ฑ/์‚ญ์ œํ•˜๋Š” ๊ฒƒ์„ ๋งŒ์กฑํ•˜๋Š”๋ฐ ๋ชฉ์ ์„ ๋‘ .
  • ์ƒˆ๋กœ์šด Pod๋ฅผ ReplicaSet์ด ์ƒ์„ฑํ• ๋•Œ Podํƒฌํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • ReplicaSet์€ ๋ณธ์ธ์˜ Pods๋‚ด๋ถ€์— ์กด์žฌํ•˜๋Š”, ํ˜„์žฌ ๊ฐ์ฒด๊ฐ€ ์–ด๋–ค ๋ฆฌ์†Œ์Šค์— ์˜ํ•ด ์†Œ์œ ๋˜๋Š”์ง€ ๋ช…์‹œํ•˜๋Š” metadata.ownerReferences ํ•„๋“œ๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์Œ.
    • Pod ๋‚ด๋ถ€์— metadata.ownerReferences ํ•„๋“œ๊ฐ€ ์กด์žฌํ•˜๊ณ  ๊ฑฐ๊ธฐ์— ReplicaSet์ด ์—ฐ๊ฒฐ๋˜์–ด์žˆ์Œ.
    • ํ•ด๋‹น ํ•„๋“œ๋ฅผ ํ†ตํ•ด์„œ ReplicaSet์€ Pod์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ณ  ์œ ์ง€ํ•œ๋‹ค.
  • ReplicaSet์€ select๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํš๋“ ๊ฐ€๋Šฅํ•œ ์ƒˆ๋กœ์šด Pod๋ฅผ ์‹๋ณ„ํ•œ๋‹ค.
    • ownerReference๊ฐ€ ์—†๊ฑฐ๋‚˜ Controller๊ฐ€ ์•„๋‹ˆ๊ฑฐ๋‚˜, ReplicaSet์˜ Selector์— ์ผ์น˜ํ•˜๊ฒŒ ๋˜๋ฉด ์ฆ‰์‹œ ReplicaSet์— ์†ํ•˜๊ฒŒ ๋œ๋‹ค.
    • Controller : apiserver์™€ ์›ํ•˜๋Š” ์ƒํƒœ๋กœ ์ „ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ํด๋Ÿฌ์Šคํ„ฐ์˜ ๊ณต์œ ๋œ ์ƒํƒœ๋ฅผ ๊ฐ์‹œํ•˜๋Š” ์ œ์–ด ๋ฃจํ”„

      ReplicaSet vs ReplicationController

      • ๋ ˆํ”Œ๋ฆฌ์นด์…‹์ด ๋” ํ’๋ถ€ํ•œ ํ‘œํ˜„์‹ ํฌ๋“œ ์…€๋ ‰ํ„ฐ๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
      • ๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ: ํŠน์ • Label์„ ํฌํ•จํ•˜๋Š” ํฌ๋“œ๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ

ReplicaSet ์‚ฌ์šฉ

  • ReplicaSet์€ pod replica์˜ ํŠน์ • ์ˆ˜๊ฐ€ ์œ ์ง€๋˜๋Š” ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค.
  • ํ•˜์ง€๋งŒ, Deployment๋Š” Replica๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ƒ์œ„ ๋ ˆ๋ฒจ์ด๊ณ , Pod์— ์‚ฌ์šฉ ํŠน์ง•์„ ์„ ์–ธ์ ์œผ๋กœ ์ˆ˜์ •์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค.
  • ๊ทธ๋ž˜์„œ ReplicaSet์„ ์ง์ ‘ ์“ฐ๊ธฐ๋ณด๋‹ค Deployment๋ฅผ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•จ
    • ์ปค์Šคํ…€ ์—…๋ฐ์ดํŠธ ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜์ด๋‚˜ ์—…๋ฐ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ๊ดœ์ฐฎ์Œ.
  • Deployment๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ReplicaSet ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
    (์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ŠคํŽ™ ์„น์…˜์—์„œ ์ •์˜)
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3

spec ํ•„๋“œ์— label selector๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์–‘ํ•œ selector ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

spec:
  matchLabels:
    tier: frontend
spec:
  selector:
    matchExpressions:
    - key: tier
      operator: In
      values:
      - frontend
spec:
  selector:
    matchLabels:
      key: value
    matchExpressions:
      - {key: keyName, operator: In, valeus: [valueName]} #[] : value๋ฆฌ์ŠคํŠธ

์‚ฌ์šฉ

kubectl create -f <replicaSet.yaml> : ๋ ˆํ”Œ๋ฆฌ์นด์…‹ ์ƒ์„ฑ
kubectl get rs : ๋ ˆํ”Œ๋ฆฌ์นด์…‹ ์กฐํšŒ
kubectl describe rs <Replicaset ์ด๋ฆ„> : ๋ ˆํ”Œ๋ฆฌ์นด์…‹ ์ƒ์„ธ ์กฐํšŒ
kubectl delete rs <Replicaset ์ด๋ฆ„> : ๋ ˆํ”Œ๋ฆฌ์นด์…‹ ์‚ญ์ œ

  • ์Šค์ผ€์ผ๋ง : replicas์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ pod์˜ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ฆผ

ReplicationController

https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/

Replication์„ ๋„ˆ๋ฌด ๋งŽ์ด ๋ฐœ์ƒ์‹œํ‚ค๋ฉด ์˜ค๋ฒ„ํ—ค๋“œ ๋ฐœ์ƒ, ์›Œ์ปค๋…ธ๋“œ๊ฐ€ ๋ณต๊ตฌ๋  ์‹œ๊ฐ„์„ ๋ถ€์—ฌํ•ด์„œ ๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ ๊ฒŒํ•˜๊ธฐ๋„ ํ•จ.

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx

  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
  • replicas: ์œ ์ง€ํ•ด์•ผํ•  ํฌ๋“œ์˜ ๊ฐœ์ˆ˜
  • selector: ๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ํฌ๋“œ ๋ฒ”์œ„๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” Label Selector
  • template: ํฌ๋“œ ํƒฌํ”Œ๋ฆฟ
    • pod ๋””์Šคํฌ๋ฆฝํ„ฐ ํ˜•ํƒœ์™€ ๋™์ผํ•˜๊ฒŒ ์ž‘์„ฑ
    • labels์— key:value์™€ selector์˜ key:value๋ฅผ ํ†ต์ผํ•˜์—ฌ ์ž‘์„ฑ

ReplicationController ์‹คํ–‰

kubectl create -f <replication.yaml>

๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ ํ™•์ธ

kubectl get rc
kubectl describe rc <ReplicationController ์ด๋ฆ„>

  • Pod๋ฅผ ์ž„์˜๋กœ ์ •์ง€์‹œ์ผœ ํ™•์ธํ•˜๋ฉด
    kubectl delete pod <ํŒŒ๋“œ๋„ค์ž„>
    kubectl get pod : AGE๊ฐ€ ๋ฐ”๋€Œ๋Š”๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.

๋ช…๋ น์–ด๋กœ ์„ค์ •ํŒŒ์ผ ์ ‘๊ทผ

kubectl edit rc <ReplicationController ์ด๋ฆ„>

๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ ์‚ญ์ œ

kubectl delete rc <replicationController ์ด๋ฆ„> : ํ•˜์œ„ Pod๋“ค๋„ ๋ชจ๋‘ ์‚ญ์ œ๋จ
kubectl delete rc <replicationController ์ด๋ฆ„> --cascade=false : ํฌ๋“œ๋Š” ๊ณ„์† ์‹คํ–‰์œ ์ง€ํ•˜๊ณ  ๋ ˆํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํŠธ๋กค๋Ÿฌ๋งŒ ์‚ญ์ œ

profile
๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฟˆ์ธ 25์‚ด ๋Œ€ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€