ReplicaSet
ReplicaSet は指定した数の Pod を維持するワークロードです。ReplicaSet リソースを作成すると、ReplicaSet の他に指定した数の Pod が作成されます。Pod が指定した数を下回った場合には、数を維持できるように Pod を再作成します。

ReplicaSet のマニフェストには、レプリカ数や展開する Pod の内容およびレプリカを識別するための条件などを指定します。
- spec.replicas: Pod の数を定義します
- spec.template:Pod の内容を定義します
- spec.selector:カウントする Pod の条件を定義します
ReplicationController
同様の挙動をするリソースとして ReplicationController があります。これは ReplicaSet の前世代にあたるリソースとなっており、今後はリタイア予定のため利用は非推奨となっています。
ReplicaSet のマニフェスト
ReplicaSet は次のようなマニフェストで展開できます。
apiVersion: apps/v1 kind: ReplicaSet metadata: name: sample-rs labels: app: nginx-rs spec: replicas: 3 selector: matchLabels: app: nginx-rs template: metadata: labels: app: nginx-rs spec: containers: - name: nginx-rs-contaier image: nginx resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 80
マニフェストを展開すると次のように ReplicaSet および指定した数の Pod が作成されます。ReplicaSet によって自動で作成される Pod の末尾には、既定でランダムな文字列が付与され、[ReplicaSet 名]-[ランダム文字列] となります。
$ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 3 3 3 60s $ kubectl get pod NAME READY STATUS RESTARTS AGE sample-rs-b45dv 1/1 Running 0 85s sample-rs-dfll7 1/1 Running 0 85s sample-rs-dx2gf 1/1 Running 0 85s
詳細を確認すると次のようになります。
$ kubectl describe rs sample-rs Name: sample-rs Namespace: default Selector: app=nginx-rs Labels: app=nginx-rs Annotations: <none> Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=nginx-rs Containers: nginx-rs-contaier: Image: nginx Port: 80/TCP Host Port: 0/TCP Requests: cpu: 100m memory: 100Mi Environment: <none> Mounts: <none> Volumes: <none> Events: <none>
レプリカの管理
ReplicaSet では Pod に付与されたラベルに基づいてレプリカ数を保持します。ReplicaSet 以外によって作成された Pod に対象のラベルが付与されていた場合、その Pod もカウントしてしまうため想定した数の Pod が展開されない場合があります。そのため、ReplicaSet に付与するラベルを別のリソースで使用したり、ReplicaSet で作られた Pod のラベルを上書きしたりしないようにします。
スケーリング
spec.replicas の値を更新することによってスケーリングできます。
$ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 3 3 3 12h # spec.replicas を 5 に変更 $ kubectl apply -f sample-rs.yaml replicaset.apps/sample-rs configured $ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 5 5 5 12h
また、scale コマンドで --replicas
を指定することでもスケーリングできます。
$ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 5 5 5 9s $ kubectl scale rs/sample-rs --replicas=6 replicaset.apps/sample-rs scaled $ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 6 6 6 74s
ReplicaSet の削除
ReplicaSet の削除も delete コマンドで実行できます。また、--cascade=orphan
オプションを付与すると、ReplicaSet に作成された Pod に影響を与えずに ReplicaSet を削除できます。
$ kubectl delete -f sample-rs.yaml --cascade=orphan replicaset.apps "sample-rs" deleted $ kubectl get rs No resources found in default namespace. $ kubectl get pod NAME READY STATUS RESTARTS AGE sample-rs-924dk 1/1 Running 0 5m46s sample-rs-b45dv 1/1 Running 0 12h sample-rs-dfll7 1/1 Running 0 12h sample-rs-dx2gf 1/1 Running 0 12h sample-rs-jgtn2 1/1 Running 0 5m46s
セルフヒーリング
稼働中の Pod に障害などが発生し、指定したレプリカ数を維持できなくなった場合には自動的に Pod が再作成されます。
$ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 6 6 6 54s $ kubectl get po NAME READY STATUS RESTARTS AGE sample-rs-4txpk 1/1 Running 0 3s sample-rs-744tp 1/1 Running 0 61s sample-rs-c69fw 1/1 Running 0 61s sample-rs-lvvtd 1/1 Running 0 61s sample-rs-nnfdh 1/1 Running 0 61s sample-rs-w84bl 1/1 Running 0 61s # Pod を 1 つ削除する $ kubectl delete po sample-rs-4txpk pod "sample-rs-4txpk" deleted $ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 6 6 5 3m32s # 自動的に Pod 再作成 $ kubectl get rs NAME DESIRED CURRENT READY AGE sample-rs 6 6 6 3m37s $ kubectl get po NAME READY STATUS RESTARTS AGE sample-rs-744tp 1/1 Running 0 3m57s sample-rs-c69fw 1/1 Running 0 3m57s sample-rs-lvvtd 1/1 Running 0 3m57s sample-rs-nnfdh 1/1 Running 0 3m57s sample-rs-tc9sn 1/1 Running 0 27s sample-rs-w84bl 1/1 Running 0 3m57s
増減の履歴は describe コマンドの Events から確認できます。
$ kubectl get rs <略> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 9m32s replicaset-controller Created pod: sample-rs-lvvtd Normal SuccessfulCreate 9m32s replicaset-controller Created pod: sample-rs-744tp Normal SuccessfulCreate 9m32s replicaset-controller Created pod: sample-rs-c69fw Normal SuccessfulCreate 9m32s replicaset-controller Created pod: sample-rs-w84bl Normal SuccessfulCreate 9m32s replicaset-controller Created pod: sample-rs-nnfdh Normal SuccessfulCreate 8m34s replicaset-controller Created pod: sample-rs-4txpk Normal SuccessfulCreate 6m2s replicaset-controller Created pod: sample-rs-tc9sn
コメント