前回は、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のマニフェストファイルを作成します。以下のようなマニフェストファイルを作成します。
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について説明したいと思います。