こんにちは。KOUKIです。k8s学習中のWebエンジニアです。
KubernetesでMicroservicesについて学びましょう。
本記事では、Volumesについて記事にしました。
尚、今回も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 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$ kubectl get all NAME READY STATUS RESTARTS AGE pod/api-gateway-85944f447b-9gvqw 1/1 Running 3 23h pod/position-simulator-54c465565f-rfb42 1/1 Running 5 46h pod/position-tracker-7964b5474f-lnhq4 1/1 Running 5 46h pod/queue-68799ffccd-zgszt 1/1 Running 5 46h pod/webapp-6d4bd8866f-t9bgd 1/1 Running 6 22h NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/fleetman-api-gateway NodePort 10.107.219.59 <none> 8080:30020/TCP 23h service/fleetman-position-tracker ClusterIP 10.102.113.245 <none> 8080/TCP 46h service/fleetman-queue NodePort 10.103.48.65 <none> 8161:30010/TCP,61616:32330/TCP 46h service/fleetman-webapp NodePort 10.110.235.91 <none> 80:30080/TCP 46h service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/api-gateway 1/1 1 1 23h deployment.apps/position-simulator 1/1 1 1 46h deployment.apps/position-tracker 1/1 1 1 46h deployment.apps/queue 1/1 1 1 46h deployment.apps/webapp 1/1 1 1 22h NAME DESIRED CURRENT READY AGE replicaset.apps/api-gateway-85944f447b 1 1 1 23h replicaset.apps/position-simulator-54c465565f 1 1 1 46h replicaset.apps/position-tracker-7964b5474f 1 1 1 46h replicaset.apps/queue-68799ffccd 1 1 1 46h replicaset.apps/webapp-6d4bd8866f 1 1 1 22h |
シチュエーション
新しいバージョン(V1->V2)をリリースするとします。
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# workloads.yml apiVersion: apps/v1 kind: Deployment metadata: name: queue spec: selector: matchLabels: app: queue replicas: 1 template: metadata: labels: app: queue spec: containers: - name: queue image: richardchesterwood/k8s-fleetman-queue:release2<meta charset="utf-8"> # new version --- apiVersion: apps/v1 kind: Deployment metadata: name: position-simulator spec: selector: matchLabels: app: position-simulator replicas: 1 template: metadata: labels: app: position-simulator spec: containers: - name: position-simulator image: richardchesterwood/k8s-fleetman-position-simulator:release2<meta charset="utf-8"> # new version env: - name: SPRING_PROFILES_ACTIVE value: production-microservice --- apiVersion: apps/v1 kind: Deployment metadata: name: position-tracker spec: selector: matchLabels: app: position-tracker replicas: 1 template: metadata: labels: app: position-tracker spec: containers: - name: position-tracker image: richardchesterwood/k8s-fleetman-position-tracker:release2 # new version env: - name: SPRING_PROFILES_ACTIVE value: production-microservice --- apiVersion: apps/v1 kind: Deployment metadata: name: api-gateway spec: selector: matchLabels: app: api-gateway replicas: 1 template: metadata: labels: app: api-gateway spec: containers: - name: api-gateway image: richardchesterwood/k8s-fleetman-api-gateway:release2<meta charset="utf-8"> # new version env: - name: SPRING_PROFILES_ACTIVE value: production-microservice --- apiVersion: apps/v1 kind: Deployment metadata: name: webapp spec: selector: matchLabels: app: webapp replicas: 1 template: metadata: labels: app: webapp spec: containers: - name: webapp image: richardchesterwood/k8s-fleetman-webapp-angular:release2<meta charset="utf-8"> # new version env: - name: SPRING_PROFILES_ACTIVE value: production-microservice |
applyします。
1 2 |
# apply kubectl apply -f workloads.yml |
ブラウザから「http://192.168.99.103:30080/」にアクセスします。

Version 2(R2)になりましたね。
ダウンタイムなしで変更してくれます。
現在のダイアグラムは以下のようになっています。

Position Trackerで画面上に表示されるデータを取得していますが、現状だとDBに保存していないので、アプリを消すとデータも消えます。
そのため、DBを追加しましょう。
Mongo DB
下記のダイアグラムのように、Mongo DBを追加しましょう。

Deploymentの追加
まずは、ファイルを作成します。
1 2 |
# mongo DBのDeploymentを記載 touch mongo-stack.yml |
このファイルに、MongoのDeploymentを配置します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# mongo-stack.yml apiVersion: apps/v1 kind: Deployment metadata: name: mongodb spec: selector: matchLabels: app: mongodb replicas: 1 template: metadata: labels: app: mongodb spec: containers: - name: mongodb image: mongo:3.6.5-jessie |
「mongo:3.6.5-jessie」イメージは、Dobker Hubに公式されている公式のものです。
また、アプリケーションの仕様上、MongoDBを参照するのはバージョン3からなので、position-trackerのバージョンを2->3にします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# workloads.yml --- apiVersion: apps/v1 kind: Deployment metadata: name: position-tracker spec: selector: matchLabels: app: position-tracker replicas: 1 template: metadata: labels: app: position-tracker spec: containers: - name: position-tracker image: richardchesterwood/k8s-fleetman-position-tracker:release3<meta charset="utf-8"> # new version env: - name: SPRING_PROFILES_ACTIVE value: production-microservice |
これらをapplyします。
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 35 36 37 38 39 |
$ kubectl apply -f workloads.yml -f mongo-stack.yml $ kubectl get all NAME READY STATUS RESTARTS AGE pod/api-gateway-6494b87d9c-vb2s5 1/1 Running 0 50m pod/mongodb-64cbcfb9d4-fbzxx 1/1 Running 0 51s pod/position-simulator-784c7d7686-bp29s 1/1 Running 0 50m pod/position-tracker-64b7b5c6c-4dtlm 1/1 Running 0 51s pod/queue-6c49f58687-rm4mv 1/1 Running 0 50m pod/webapp-644f7b68-9nnwl 1/1 Running 0 50m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/fleetman-api-gateway NodePort 10.107.219.59 <none> 8080:30020/TCP 24h service/fleetman-position-tracker ClusterIP 10.102.113.245 <none> 8080/TCP 47h service/fleetman-queue NodePort 10.103.48.65 <none> 8161:30010/TCP,61616:32330/TCP 47h service/fleetman-webapp NodePort 10.110.235.91 <none> 80:30080/TCP 47h service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 47h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/api-gateway 1/1 1 1 24h deployment.apps/mongodb 1/1 1 1 51s deployment.apps/position-simulator 1/1 1 1 47h deployment.apps/position-tracker 1/1 1 1 47h deployment.apps/queue 1/1 1 1 47h deployment.apps/webapp 1/1 1 1 23h NAME DESIRED CURRENT READY AGE replicaset.apps/api-gateway-6494b87d9c 1 1 1 50m replicaset.apps/api-gateway-85944f447b 0 0 0 24h replicaset.apps/mongodb-64cbcfb9d4 1 1 1 51s replicaset.apps/position-simulator-54c465565f 0 0 0 47h replicaset.apps/position-simulator-784c7d7686 1 1 1 50m replicaset.apps/position-tracker-64b7b5c6c 1 1 1 51s replicaset.apps/position-tracker-7964b5474f 0 0 0 47h replicaset.apps/position-tracker-b4575fb4 0 0 0 50m replicaset.apps/queue-68799ffccd 0 0 0 47h replicaset.apps/queue-6c49f58687 1 1 1 50m replicaset.apps/webapp-644f7b68 1 1 1 50m replicaset.apps/webapp-6d4bd8866f 0 0 0 23h |
ログを見たい場合は、以下のコマンドで確認できます。
1 |
kubectl logs -f pod/mongodb-64cbcfb9d4-fbzxx |
Mongo Serviceの追加
続いて、先ほど作成した「mongo-stack.yml」にMongo Serviceを追加しましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# mongo-stack.yml ... --- apiVersion: v1 kind: Service metadata: name: fleetman-mongodb spec: selector: app: mongodb ports: - port: 27017 name: mongoport type: ClusterIP |
変更をapplyします。
1 2 3 4 5 6 7 8 9 10 |
$ kubectl apply -f mongo-stack.yml $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE fleetman-api-gateway ClusterIP 10.107.219.59 <none> 8080/TCP 2d fleetman-mongodb ClusterIP 10.96.197.203 <none> 27017/TCP 3m58s fleetman-position-tracker ClusterIP 10.102.113.245 <none> 8080/TCP 2d23h fleetman-queue NodePort 10.103.48.65 <none> 8161:30010/TCP,61616:32330/TCP 2d23h fleetman-webapp NodePort 10.110.235.91 <none> 80:30080/TCP 2d23h kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d23h |
OKですね。
VolumeのMount
続いて、VolumeのMount方法を学びましょう。
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 |
# mongo-stack.yml apiVersion: apps/v1 kind: Deployment metadata: name: mongodb spec: selector: matchLabels: app: mongodb replicas: 1 template: metadata: labels: app: mongodb spec: containers: - name: mongodb image: mongo:3.6.5-jessie # volume設定 volumeMounts: # 2 - name: mongo-persistent-storage mountPath: /data/db volumes: # 1 - name: mongo-persistent-storage hostPath: path: /mnt/some/directory/structure # もしディレクトリがない場合は作る # https://kubernetes.io/docs/concepts/storage/volumes/ type: DirectoryOrCreate |
DockerコンテナのようにローカルのVolumeとマウントすることができます。
そのために、containersと同じ階層にvolumes(#1)を定義します。これはローカル(virutalbox)のVolumeを指定しています。
続いて、Pod内のマウント先をvolumeMounts(#2)に定義しています。
applyしましょう。
1 2 3 4 5 6 7 8 9 10 11 12 |
$ kubectl apply -f mongo-stack.yml $ kubectl get po NAME READY STATUS RESTARTS AGE api-gateway-6494b87d9c-vb2s5 1/1 Running 2 2d mongodb-5bf6cd7977-xl4bt 1/1 Running 0 30s position-simulator-784c7d7686-bp29s 1/1 Running 2 2d position-tracker-64b7b5c6c-4dtlm 1/1 Running 2 2d queue-6c49f58687-rm4mv 1/1 Running 2 2d webapp-644f7b68-9nnwl 1/1 Running 6 2d |
minikubeにログインします。
login: docker
Password: tcuser

「/mnt/some/directory/structure」のフォルダが作成されているか確認しましょう。

mntフォルダ自体は「/」にあるので、一旦そこまで戻る必要があります。
上記の出力を見る限りでは、問題なく作成できたようですね。
ファイルも確認できます。

PersistentVolume
PersistentVolume設定しましょう。
KubernetesにはPersistentVolumeというシステムがあり、PersistentVolume及びPersistentVolumeClaimのAPIリソースを提供しています。
これは、ストレージが何から提供されているか、どのように消費されているかをユーザーと管理者から抽象化するものです。
1 |
$ touch storage.yml |
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 |
# storage.yml # https://v1-20.docs.kubernetes.io/docs/concepts/storage/persistent-volumes/ apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mongo-pvc spec: # 名前をつけられる key-valueでありPersistentVolumeと一意(要求するため) storageClassName: mylocalstorage accessModes: # 一つのノードに対して読み取り書き取りを要求 - ReadWriteOnce resources: requests: storage: 20Gi # PersistentVolumeに要求する容量 --- apiVersion: v1 kind: PersistentVolume metadata: name: local-storage spec: # 名前をつけられる storageClassName: mylocalstorage capacity: storage: 20Gi # キャパシティ accessModes: # 一つのノードに対して読み取り書き取りを要求 - ReadWriteOnce hostPath: path: "/mnt/some new/directory/structure/" type: DirectoryOrCreate |
applyします。
1 2 3 4 5 6 7 8 9 10 |
kubectl apply -f storage.yml # 確認 $ kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE local-storage 20Gi RWO Retain Bound default/mongo-pvc mylocalstorage 8s $ kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE mongo-pvc Bound local-storage 20Gi RWO mylocalstorage 24s |
Clean Up
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ kubectl delete -f . deployment.apps "mongodb" deleted service "fleetman-mongodb" deleted service "fleetman-webapp" deleted service "fleetman-queue" deleted service "fleetman-position-tracker" deleted service "fleetman-api-gateway" deleted persistentvolumeclaim "mongo-pvc" deleted persistentvolume "local-storage" deleted deployment.apps "queue" deleted deployment.apps "position-simulator" deleted deployment.apps "position-tracker" deleted deployment.apps "api-gateway" deleted deployment.apps "webapp" deleted $ kubectl get all NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d |
次回
次回は、検討中です。
記事まとめ
コード
services.yml
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# services.yml apiVersion: v1 kind: Service metadata: name: fleetman-webapp spec: selector: app: webapp ports: - name: http port: 80 nodePort: 30080 type: NodePort --- apiVersion: v1 kind: Service metadata: name: fleetman-queue spec: selector: app: queue ports: - name: http port: 8161 nodePort: 30010 - name: endpoint port: 61616 type: NodePort --- apiVersion: v1 kind: Service metadata: name: fleetman-position-tracker spec: selector: app: position-tracker ports: - name: http port: 8080 type: ClusterIP --- apiVersion: v1 kind: Service metadata: name: fleetman-api-gateway spec: selector: app: api-gateway ports: - name: http port: 8080 type: ClusterIP |
workloads.yml
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# workloads.yml apiVersion: apps/v1 kind: Deployment metadata: name: queue spec: selector: matchLabels: app: queue replicas: 1 template: metadata: labels: app: queue spec: containers: - name: queue image: richardchesterwood/k8s-fleetman-queue:release2 --- apiVersion: apps/v1 kind: Deployment metadata: name: position-simulator spec: selector: matchLabels: app: position-simulator replicas: 1 template: metadata: labels: app: position-simulator spec: containers: - name: position-simulator image: richardchesterwood/k8s-fleetman-position-simulator:release2 env: - name: SPRING_PROFILES_ACTIVE value: production-microservice --- apiVersion: apps/v1 kind: Deployment metadata: name: position-tracker spec: selector: matchLabels: app: position-tracker replicas: 1 template: metadata: labels: app: position-tracker spec: containers: - name: position-tracker image: richardchesterwood/k8s-fleetman-position-tracker:release3 env: - name: SPRING_PROFILES_ACTIVE value: production-microservice --- apiVersion: apps/v1 kind: Deployment metadata: name: api-gateway spec: selector: matchLabels: app: api-gateway replicas: 1 template: metadata: labels: app: api-gateway spec: containers: - name: api-gateway image: richardchesterwood/k8s-fleetman-api-gateway:release2 env: - name: SPRING_PROFILES_ACTIVE value: production-microservice --- apiVersion: apps/v1 kind: Deployment metadata: name: webapp spec: selector: matchLabels: app: webapp replicas: 1 template: metadata: labels: app: webapp spec: containers: - name: webapp image: richardchesterwood/k8s-fleetman-webapp-angular:release2 env: - name: SPRING_PROFILES_ACTIVE value: production-microservice |
mongo-stack.yml
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 35 36 37 38 39 40 41 |
# mongo-stack.yml apiVersion: apps/v1 kind: Deployment metadata: name: mongodb spec: selector: matchLabels: app: mongodb replicas: 1 template: metadata: labels: app: mongodb spec: containers: - name: mongodb image: mongo:3.6.5-jessie # volume設定 volumeMounts: - name: mongo-persistent-storage mountPath: /data/db volumes: - name: mongo-persistent-storage # https://kubernetes.io/ja/docs/concepts/storage/persistent-volumes/ persistentVolumeClaim: claimName: mongo-pvc --- apiVersion: v1 kind: Service metadata: name: fleetman-mongodb spec: selector: app: mongodb ports: - port: 27017 name: mongoport type: ClusterIP |
storage.yml
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 |
# storage.yml # https://v1-20.docs.kubernetes.io/docs/concepts/storage/persistent-volumes/ apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mongo-pvc spec: # 名前をつけられる key-valueでありPersistentVolumeと一意(要求するため) storageClassName: mylocalstorage accessModes: # 一つのノードに対して読み取り書き取りを要求 - ReadWriteOnce resources: requests: storage: 20Gi # PersistentVolumeに要求する容量 --- apiVersion: v1 kind: PersistentVolume metadata: name: local-storage spec: # 名前をつけられる storageClassName: mylocalstorage capacity: storage: 20Gi # キャパシティ accessModes: # 一つのノードに対して読み取り書き取りを要求 - ReadWriteOnce hostPath: path: "/mnt/some new/directory/structure/" type: DirectoryOrCreate |
コメントを残す
コメントを投稿するにはログインしてください。