LoginSignup
2

More than 5 years have passed since last update.

Kubernetes勉強 その3(ReplicaSet)

Posted at

引き続き「Kubernetes完全ガイド」読んでお勉強。

Pod と ReplicaSet と Deployment

Pod はリソースの最小単位であり、ReplicaSetPod を管理するリソース。 ReplicaSetPod が停止すると自動的に再起動したりする。
Deployment は、 Pod に含まれるコンテナをバージョンアップする際に、古いコンテナを徐々に減らし、新しいコンテナを徐々に増やすなどの管理をする。

ReplicaSet

  • ReplicaSetは指定した数のPodを起動し、その数を維持し続けるリソース。
  • Podがなんらかの理由で停止したら、ReplicaSetが再起動してくれる。

ReplicaSetの作成

sample-replicaset.yml というファイルを作って、ReplicaSetを定義する。

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

ReplicaSetを起動する。

$ kubectl apply -f sample-replicaset.yml 

replicaset.apps "sample-rs" created

状態を確認してみる。

$ kubectl get pods

NAME              READY     STATUS    RESTARTS   AGE
sample-rs-54hgh   1/1       Running   0          1m
sample-rs-nbntp   1/1       Running   0          1m
sample-rs-zfpsd   1/1       Running   0          1m

ここで、意図的にpodを停止してみる。上記のpodの中から、 sample-rs-54hgh だけを停止する。

$ kubectl delete pod sample-rs-54hgh

pod "sample-rs-54hgh" deleted

podが停止した。状態を確認する。

$ kubectl get pods

NAME              READY     STATUS        RESTARTS   AGE
sample-rs-54hgh   0/1       Terminating   0          2m
sample-rs-95p4r   1/1       Running       0          3s
sample-rs-nbntp   1/1       Running       0          2m
sample-rs-zfpsd   1/1       Running       0          2m

お、 sample-rs-54hgh のSTATUSがTerminatingになり、入れ替わるように sample-rs-95p4r が起動した。

少し時間をおいて再度状態確認。

$ kubectl get pods

NAME              READY     STATUS    RESTARTS   AGE
sample-rs-95p4r   1/1       Running   0          16s
sample-rs-nbntp   1/1       Running   0          2m
sample-rs-zfpsd   1/1       Running   0          2m

sample-rs-54hgh が完全に姿を消して、もとの3Pod状態に収束した。なるほど。

ReplicaSetを削除する。

$ kubectl delete -f sample-replicaset.yml 

replicaset.apps "sample-rs" deleted

今夜はここまで。

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
2