ReplicaSet を使ってみる (kubernetes)
kubernetes
Published: 2020-12-28

やったこと

ReplicaSet を使ってみます。

確認環境

$ k version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:18:23Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.3", GitCommit:"1e11e4a2108024935ecfcb2912226cedeafd99df", GitTreeState:"clean", BuildDate:"2020-10-14T12:41:49Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"linux/amd64"}

調査

sample2.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replica-first
spec:
  replicas: 2
  selector:
    matchLabels:
      app: sample-pod2
  template:
    metadata:
      labels:
        app: sample-pod2
    spec:
      containers:
        - name: nginx-container
          image: nginx:1.17

マニフェストの適用

$ k apply -f sample2.yaml
replicaset.apps/my-replica-first created
$ k get replicasets -o wide
NAME               DESIRED   CURRENT   READY   AGE     CONTAINERS        IMAGES       SELECTOR
my-replica-first   2         2         2       4m50s   nginx-container   nginx:1.17   app=sample-pod2
$ k get pods
NAME                     READY   STATUS    RESTARTS   AGE
my-replica-first-9tmnq   1/1     Running   0          8m13s
my-replica-first-cqmff   1/1     Running   0          8m13s

Pod を1つ落として、セルフヒーリング機能を確かめる

$ k delete pod my-replica-first-9tmnq
pod "my-replica-first-9tmnq" deleted
$ k get pods
NAME                     READY   STATUS    RESTARTS   AGE
my-replica-first-86tk2   1/1     Running   0          10s
my-replica-first-cqmff   1/1     Running   0          8m36s
$ k describe rs my-replica-first
(省略)
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  10m   replicaset-controller  Created pod: my-replica-first-cqmff
  Normal  SuccessfulCreate  10m   replicaset-controller  Created pod: my-replica-first-9tmnq
  Normal  SuccessfulCreate  101s  replicaset-controller  Created pod: my-replica-first-86tk2

scale コマンドを使ってみる

$ k scale rs my-replica-first --replicas=3
replicaset.apps/my-replica-first scaled
$ k get pods
NAME                     READY   STATUS    RESTARTS   AGE
my-replica-first-7flv6   1/1     Running   0          8s
my-replica-first-86tk2   1/1     Running   0          5m29s
my-replica-first-cqmff   1/1     Running   0          13m

削除

$ k delete rs my-replica-first
replicaset.apps "my-replica-first" deleted