LoginSignup
1
2
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

⑮1日10分で理解するコンテナ技術入門 - ReplicaSetとは -

Last updated at Posted at 2024-06-27

前回は、Serviceについて説明しました。今回はPodの上位リソースであるReplicaSetを紹介したいと思います。

ReplicaSetとは

ReplicaSetは、特定の数のPodのレプリカを常に維持するためのものです。これによって、クラスタ内で一定数のPodが常に稼働し続けることを保証します。

ReplicaSetが提供する機能はいたってシンプルで、「あらかじめ指定したPod数のPodを動作させ続ける機能」です。Podが異常終了した場合などは新たにPodを作成してノードに配置します。

ReplicaSetを作成してみる

ReplicaSetはマニフェストファイルから作成します。一応、kubectl creat --helpで確認しましたが、以下のようにReplicaSetには対応していないようでした。

$ kubectl create --help
Create a resource from a file or from stdin.

 JSON and YAML formats are accepted.

Examples:
  # Create a pod using the data in pod.json
  kubectl create -f ./pod.json
  
  # Create a pod based on the JSON passed into stdin
  cat pod.json | kubectl create -f -
  
  # Edit the data in registry.yaml in JSON then create the resource using the edited data
  kubectl create -f registry.yaml --edit -o json

Available Commands:
  clusterrole           Create a cluster role
  clusterrolebinding    Create a cluster role binding for a particular cluster role
  configmap             Create a config map from a local file, directory or literal value
  cronjob               Create a cron job with the specified name
  deployment            Create a deployment with the specified name
  ingress               Create an ingress with the specified name
  job                   Create a job with the specified name
  namespace             Create a namespace with the specified name
  poddisruptionbudget   Create a pod disruption budget with the specified name
  priorityclass         Create a priority class with the specified name
  quota                 Create a quota with the specified name
  role                  Create a role with single rule
  rolebinding           Create a role binding for a particular role or cluster role
  secret                Create a secret using a specified subcommand
  service               Create a service using a specified subcommand
  serviceaccount        Create a service account with the specified name
  token                 Request a service account token

Options:
   (省略)

マニフェストファイルから作成する

では、ReplicaSetのマニフェストファイルを作成します。以下のようなマニフェストファイルを作成します。

nginx-rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

kubectl applyコマンドで適用します。

$ kubectl apply -f nginx-rs.yaml
replicaset.apps/my-replicaset created

作成できたかどうか、kubectl getコマンドで確認します。

$ kubectl get rs
 NAME              DESIRED   CURRENT   READY   AGE
my-replicaset     3         3         3       8m18s

作成できていることが分かります。

ReplicaSetの挙動を確かめてみる

ReplicaSet内のPodを削除してみて、ちゃんと起動しなおすかを見てみます。
まず、Pod名を確認します。

$ kubectl get po
NAME                    READY   STATUS    RESTARTS   AGE
my-replicaset-9gvf8     1/1     Running   0          24h
my-replicaset-fxpkq     1/1     Running   0          24h
my-replicaset-tcr8b     1/1     Running   0          24h

この中から適当にPodを削除します。

$ kubectl delete po my-replicaset-9gvf8
pod "my-replicaset-9gvf8" deleted

もう一度Pod一覧を確認してみます。

$ kubectl get po
NAME                    READY   STATUS    RESTARTS   AGE
my-replicaset-fxpkq     1/1     Running   0          24h
my-replicaset-krn7j     1/1     Running   0          6s
my-replicaset-tcr8b     1/1     Running   0          24h

新たにmy-replicaset-krn7jというPodが立ち上がったことが分かります。このようにReplicaSetは、作成時に指定したPod数が常時起動するように再スケジューリングを行ってくれます。

まとめ

今回はReplicaSetについて紹介しました。ReplicaSetは重要なリソースですが、これ単体で作成することはほとんどなく、基本的にはより上位のコンポーネントであるDeploymentを使います。次回はこのDeploymentについて説明したいと思います。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2