[Kubernetes] 入門ノート ReplicaSet編

Kubernetes

こんにちは、aiiro(@aiiro29)です。

Kubernetesで起動させるPodが一つだけでは足りないので、同じ構成のPodを複数起動したい場合にどうすれば良いか?

また、Podが思いがけず停止してしまった場合、自動で再起動させることはできないか?

こうした起動させるPodの数を管理するという用途で用いられるのがReplicaSetです。

KubernetesにはDeploymentという概念も存在しているのですが、DeploymentはReplicaSetとかなり似ているようなので、先にReplicaSetについてまとめることにしました。

ReplicaSet
A ReplicaSet's purpose is to maintain a stable set of replica Pods running at any given time. As such, it is often used to guarantee the availability of a speci...
ReplicaSet is the next-generation Replication Controller.
The only difference between a ReplicaSet and a Replication Controller right now is
the selector support.

ReplicaSetは次世代型のReplication Controllerと説明されていて、Replication Controllerとの違いはselectorサポートがあるかどうかのようです。

そのため、ReplicationControllerの説明もあわせて確認することにします。

ReplicationController
Note: A Deployment that configures a ReplicaSet is now the recommended way to set up replication. A ReplicationController ensures that a specified number of pod...

ReplicaSet(ReplicationController)は実行中のPodの数が指定した数を維持する

ReplicaSetを使用しておくと、Podが予期せずに終了した場合でも自動的に再生成され、nodeに配置されるようになります。

そのため、安定してPodを運用したい環境では、Podの数が一つであってもReplicaSetを使った方が良いと思います。

スポンサーリンク

ReplicaSetの操作

ReplicaSetを使用してPodを生成

ReplicaSetもPodと同様にYAMLファイルで構成を管理することができるので、まずはYAMLファイルを作成します。

下記の例ではreplicasでPodを3つ起動するように指定しています。

replicaset.yaml


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

作成したYAMLファイルを使用して、ReplicaSetでPodを生成します。


kubectl create -f replicaset.yaml

ReplicaSetの動作確認

次のコマンドで結果を確認します。


kubectl get replicasets

--------------------------------------------
NAME      DESIRED   CURRENT   READY     AGE
nginx     3         3         0         34s

また下記のようにkubectl describeを実行することで、詳細な情報を確認できます。


kubectl describe replicasets/nginx

--------------------------------------------
Name:         nginx
Namespace:    default
Selector:     tier=sample-nginx
Labels:       app=nginx
              tier=sample-nginx
Annotations:  
Replicas:     3 current / 3 desired
Pods Status:  0 Running / 3 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
           tier=sample-nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  10s   replicaset-controller  Created pod: nginx-svs8h
  Normal  SuccessfulCreate  10s   replicaset-controller  Created pod: nginx-nslx2
  Normal  SuccessfulCreate  10s   replicaset-controller  Created pod: nginx-f4z6w

期待通り3つのPodが実行されていることが確認できます。

ReplicaSetの削除

ReplicaSetとPodを削除するには次のコマンドを使用します。


kubectl delete -f replicaset.yaml

まとめ

ReplicaSetを利用することで、

  • 同じ構成のPodを複数起動することが可能になる
  • 起動中のPodの数を常に一定数に保つ

といったメリットが得られます。

以上、ReplicaSetについてまとめました。