こんにちは。KOUKIです。k8s学習中のWebエンジニアです。
KubernetesでMicroservicesのデプロイ方法を学びましょう。
本記事では、ReplicaSetについて解説します。
尚、今回もMinikubeを使いますので、以下の記事でインストール&起動してください。
※ MacおよびChromeで検証します。
<目次>
参考
Udemyの「Kubernetes Hands-On – Deploy Microservices to the AWS Cloud」コースを参考にしています。
解釈は私が勝手に付けているので、本物をみたい場合は受講してみてください。
API OVERVIEWも参考になります。
前回
現在の状況は、以下のようになってます。
1 2 3 4 5 6 7 8 9 10 |
$ kubectl get all NAME READY STATUS RESTARTS AGE pod/queue 1/1 Running 0 17m pod/webapp 1/1 Running 0 17m pod/webapp-release-0-5 1/1 Running 0 17m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/fleetman-queues NodePort 10.107.33.216 <none> 8161:30010/TCP 17m service/fleetman-webapp NodePort 10.111.31.12 <none> 80:30080/TCP 17m service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d |
ReplicaSetについて
ReplicaSetは、KubernetesでPodを管理するためのオブジェクトです。
Podを指定した台数になるように作成し、障害か何かでPodが落ちたときに自動的に復旧作業を行い、指定されたPod数を常に維持します。
障害時にはかなり頼りになる機能です。
ReplicaSetを設定する前
現状がどうなっているのか、まずは確認しましょう。
以下は、fleetman-webappサービスの詳細情報です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ kubectl describe svc fleetman-webapp Name: fleetman-webapp Namespace: default Labels: <none> Annotations: <none> Selector: app=webapp,release=0-5 Type: NodePort IP Families: <none> IP: 10.111.31.12 IPs: <none> Port: http 80/TCP TargetPort: 80/TCP NodePort: http 30080/TCP Endpoints: 172.17.0.4:80 Session Affinity: None External Traffic Policy: Cluster Events: <none> |
Selectorの値が、「app=webapp,release=0-5」になっているので、release=0-5のタグがついたアプリケーションに接続できるはずです。
1 2 3 4 5 6 7 |
# pod webapp-release-0-5 を確認 $ kubectl get pod webapp-release-0-5 NAME READY STATUS RESTARTS AGE webapp-release-0-5 1/1 Running 1 24h # サービススタート minikube service fleetman-webapp |
「minikube service fleetman-webapp」を実行するとアプリケーションをブラウザ上で確認できます。

次に、Pod webapp-release-0-5 を削除してみましょう。
1 2 3 4 5 6 7 8 |
$ kubectl delete pod webapp-release-0-5 pod "webapp-release-0-5" deleted # webapp-release-0-5は消えた $ kubectl get pod NAME READY STATUS RESTARTS AGE queue 1/1 Running 1 24h webapp 1/1 Running 1 24h |
消えたことが確認できたらブラウザをリロードします。

アプリケーションに接続できなくなりましたね。
ReplicaSetを設定してみよう
続いて、公式を参考にReplicaSetを設定してみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# pods.yml apiVersion: apps/v1 kind: ReplicaSet # Replicasの情報 metadata: name: webapp spec: selector: matchLabels: app: webapp # replicaset対象のpodを指定 replicas: 1 # 常に1つのpodが動くように変更 template: # podの情報 metadata: name: webapp # webapp-release-0-5->webappに変更 labels: app: webapp spec: containers: - name: webapp image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5 --- apiVersion: v1 kind: Pod metadata: name: queue labels: app: queue release: "0" spec: containers: - name: queue image: richardchesterwood/k8s-fleetman-queue:release1 |
queue Podは前回設定した内容なので、無視してください。
webapp PodにはReplicaSetをつけました。「replicas: 1」の設定により、常に1つのPodが動作する環境になっています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 変更をapply kubectl apply -f pods.yml # 確認 $ kubectl get all NAME READY STATUS RESTARTS AGE pod/queue 1/1 Running 1 24h pod/webapp 1/1 Running 1 24h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/fleetman-queues NodePort 10.107.33.216 <none> 8161:30010/TCP 24h service/fleetman-webapp NodePort 10.111.31.12 <none> 80:30080/TCP 24h service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d NAME DESIRED CURRENT READY AGE replicaset.apps/webapp 1 1 1 8m18s |
webapp PodがReplicaSetを指定したPodです。replicasetも生成されているようですね。
minikube dashboardからも確認できます。
1 2 |
# dashboardを立ち上げる minikube dashboard |
Serviceの変更
Serviceの設定も変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# services.yml kind: Service apiVersion: v1 metadata: name: fleetman-webapp spec: ports: - name: http port: 80 nodePort: 30080 selector: app: webapp # release: "0-5" <<<<<<<<<<<<<<<<<<<<<<<<<<<<< 不要なのでコメントアウト type: NodePort --- kind: Service apiVersion: v1 metadata: name: fleetman-queues spec: ports: - name: http port: 8161 nodePort: 30010 selector: app: queue type: NodePort |
releaseタグをコメントアウトしただけです。変更を反映させましょう。
1 2 |
# 変更を反映 kubectl apply -f services.yml |
ブラウザをリロードするとアプリケーションが表示されます。

ReplicaSetの挙動確認
下記のコマンドで、webapp Podを削除してみましょう。
1 2 |
# webapp Pod削除 $ kubectl delete pod webapp |
少し時間を置いてから下記のコマンドを実行します。
1 2 3 4 |
$ kubectl get pods NAME READY STATUS RESTARTS AGE queue 1/1 Running 1 24h webapp-ftszm 1/1 Running 0 86s |
新しくwebapp-ftszmが立ち上がりましたね。「-ftszm」についてはReplicasetが勝手につける識別子です。2つ以上のPodになった場合、Podを識別するのに役立ちます。
それよりも驚くべきことは、Podが自動的に復旧したことですね^^
ブラウザをリロードしても問題なく表示されます。

Replicasetの詳細を確認しましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# rsはreplicasetの文字 $ kubectl describe rs webapp Name: webapp Namespace: default Selector: app=webapp Labels: <none> Annotations: <none> Replicas: 1 current / 1 desired Pods Status: 1 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=webapp Containers: webapp: Image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5 Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 4m39s replicaset-controller Created pod: webapp-ftszm |
Eventsに「SuccessfulCreate」と表示されているので、いい感じに機能しているようです。
ReplicaSetを増やしてみよう
現状は、webapp Podは常に1 Pod動くようになっています。
これを、2 Podに増やしてみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# pods.yml apiVersion: apps/v1 kind: ReplicaSet metadata: name: webapp spec: selector: matchLabels: app: webapp replicas: 2 # 常に2つのpodが動くように変更 template: metadata: name: webapp labels: app: webapp spec: containers: - name: webapp image: richardchesterwood/k8s-fleetman-webapp-angular:release0-5 --- |
変更をapplyします。
1 2 3 4 5 6 7 8 9 |
# 変更をapply $ kubectl apply -f pods.yml # 確認 $ kubectl get pods NAME READY STATUS RESTARTS AGE queue 1/1 Running 1 24h webapp-849j4 1/1 Running 0 20s webapp-ftszm 1/1 Running 0 9m18s |
webapp Podが2つ起動しましたね! 成功です!!
2つ以上にするとどちらかが機能不全を起こしてももう一方でカバーできるので、アプリケーションの可用性は担保されます。
次回
次回は、Deploymentについて学びましょう。
コメントを残す
コメントを投稿するにはログインしてください。