オートスケールも可能だが今回はやらない。
https://kubernetes.io/ja/docs/tutorials/kubernetes-basics/scale
アプリケーションのスケール
デプロイメントの確認
kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hello-nodejs-deployment 3 3 3 0 63d kubernetes-bootcamp 1 1 1 1 2h
DESIRED: 指定したレプリカの数。
CURRENT: 実行中のレプリカの数。
UP-TO-DATE: 目的の状態になるように更新されたレプリカの数。
AVAILABLE: 利用可能なレプリカの数。
レプリカの数を4に増やす。
kubectl scale deployments/kubernetes-bootcamp --replicas=4
kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE hello-nodejs-deployment 3 3 3 0 63d kubernetes-bootcamp 4 4 4 3 2h kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE kubernetes-bootcamp-5c69669756-72ffk 1/1 Running 0 1m 10.1.0.18 docker-for-desktop kubernetes-bootcamp-5c69669756-fzj9p 1/1 Running 0 1m 10.1.0.19 docker-for-desktop kubernetes-bootcamp-5c69669756-zg8z6 1/1 Running 0 2h 10.1.0.16 docker-for-desktop kubernetes-bootcamp-5c69669756-zlv7l 1/1 Running 0 1m 10.1.0.17 docker-for-desktop
IPアドレスの違う4つのPodが作成される。
kubectl describe deployments/kubernetes-bootcamp
で詳細が確認できる。負荷分散の確認
前回サービスを削除している場合作成し直す。
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
ポート確認
kubectl get services
あるいはkubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}'
curl http://localhost:32088/
Pod名が変わることを確認する。ブラウザでアクセスした場合は更新しても変わらない。
一度閉じたりブラウザを変えることで別のPodへアクセスするようになる。
ログの確認
kubectl logs kubernetes-bootcamp-5c69669756-72ffk
kubectl logs kubernetes-bootcamp-5c69669756-fzj9p
kubectl logs kubernetes-bootcamp-5c69669756-zg8z6
kubectl logs kubernetes-bootcamp-5c69669756-zlv7l
kubectl logs kubernetes-bootcamp-5c69669756-fzj9p
kubectl logs kubernetes-bootcamp-5c69669756-zg8z6
kubectl logs kubernetes-bootcamp-5c69669756-zlv7l
MacBook-Pro:powerpecognition shinichiyamada$ kubectl logs kubernetes-bootcamp-5c69669756-72ffk
Kubernetes Bootcamp App Started At: 2019-04-02T07:51:40.120Z | Running On: kubernetes-bootcamp-5c69669756-72ffk
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 1 | App Uptime: 771.42 seconds | Log Time: 2019-04-02T08:04:31.566Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 2 | App Uptime: 777.357 seconds | Log Time: 2019-04-02T08:04:37.477Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 3 | App Uptime: 817.381 seconds | Log Time: 2019-04-02T08:05:17.501Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 4 | App Uptime: 829.62 seconds | Log Time: 2019-04-02T08:05:29.740Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 5 | App Uptime: 830.671 seconds | Log Time: 2019-04-02T08:05:30.791Z
MacBook-Pro:powerpecognition shinichiyamada$ kubectl logs kubernetes-bootcamp-5c69669756-fzj9p
Kubernetes Bootcamp App Started At: 2019-04-02T07:51:40.046Z | Running On: kubernetes-bootcamp-5c69669756-fzj9p
Running On: kubernetes-bootcamp-5c69669756-fzj9p | Total Requests: 1 | App Uptime: 776.53 seconds | Log Time: 2019-04-02T08:04:36.577Z
Running On: kubernetes-bootcamp-5c69669756-fzj9p | Total Requests: 2 | App Uptime: 859.374 seconds | Log Time: 2019-04-02T08:05:59.420Z
MacBook-Pro:powerpecognition shinichiyamada$ kubectl logs kubernetes-bootcamp-5c69669756-zg8z6
Kubernetes Bootcamp App Started At: 2019-04-02T04:55:54.763Z | Running On: kubernetes-bootcamp-5c69669756-zg8z6
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 1 | App Uptime: 1271.722 seconds | Log Time: 2019-04-02T05:17:06.485Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 2 | App Uptime: 1597.794 seconds | Log Time: 2019-04-02T05:22:32.557Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 3 | App Uptime: 5467.554 seconds | Log Time: 2019-04-02T06:27:02.317Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 4 | App Uptime: 7283.182 seconds | Log Time: 2019-04-02T06:57:17.945Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 5 | App Uptime: 7283.284 seconds | Log Time: 2019-04-02T06:57:18.047Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 6 | App Uptime: 7291.141 seconds | Log Time: 2019-04-02T06:57:25.904Z
MacBook-Pro:powerpecognition shinichiyamada$ kubectl logs kubernetes-bootcamp-5c69669756-zlv7l
Kubernetes Bootcamp App Started At: 2019-04-02T07:51:40.093Z | Running On: kubernetes-bootcamp-5c69669756-zlv7l
Running On: kubernetes-bootcamp-5c69669756-zlv7l | Total Requests: 1 | App Uptime: 673.061 seconds | Log Time: 2019-04-02T08:02:53.154Z
Running On: kubernetes-bootcamp-5c69669756-zlv7l | Total Requests: 2 | App Uptime: 772.984 seconds | Log Time: 2019-04-02T08:04:33.078Z
Running On: kubernetes-bootcamp-5c69669756-zlv7l | Total Requests: 3 | App Uptime: 773.981 seconds | Log Time: 2019-04-02T08:04:34.074Z
Kubernetes Bootcamp App Started At: 2019-04-02T07:51:40.120Z | Running On: kubernetes-bootcamp-5c69669756-72ffk
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 1 | App Uptime: 771.42 seconds | Log Time: 2019-04-02T08:04:31.566Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 2 | App Uptime: 777.357 seconds | Log Time: 2019-04-02T08:04:37.477Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 3 | App Uptime: 817.381 seconds | Log Time: 2019-04-02T08:05:17.501Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 4 | App Uptime: 829.62 seconds | Log Time: 2019-04-02T08:05:29.740Z
Running On: kubernetes-bootcamp-5c69669756-72ffk | Total Requests: 5 | App Uptime: 830.671 seconds | Log Time: 2019-04-02T08:05:30.791Z
MacBook-Pro:powerpecognition shinichiyamada$ kubectl logs kubernetes-bootcamp-5c69669756-fzj9p
Kubernetes Bootcamp App Started At: 2019-04-02T07:51:40.046Z | Running On: kubernetes-bootcamp-5c69669756-fzj9p
Running On: kubernetes-bootcamp-5c69669756-fzj9p | Total Requests: 1 | App Uptime: 776.53 seconds | Log Time: 2019-04-02T08:04:36.577Z
Running On: kubernetes-bootcamp-5c69669756-fzj9p | Total Requests: 2 | App Uptime: 859.374 seconds | Log Time: 2019-04-02T08:05:59.420Z
MacBook-Pro:powerpecognition shinichiyamada$ kubectl logs kubernetes-bootcamp-5c69669756-zg8z6
Kubernetes Bootcamp App Started At: 2019-04-02T04:55:54.763Z | Running On: kubernetes-bootcamp-5c69669756-zg8z6
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 1 | App Uptime: 1271.722 seconds | Log Time: 2019-04-02T05:17:06.485Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 2 | App Uptime: 1597.794 seconds | Log Time: 2019-04-02T05:22:32.557Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 3 | App Uptime: 5467.554 seconds | Log Time: 2019-04-02T06:27:02.317Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 4 | App Uptime: 7283.182 seconds | Log Time: 2019-04-02T06:57:17.945Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 5 | App Uptime: 7283.284 seconds | Log Time: 2019-04-02T06:57:18.047Z
Running On: kubernetes-bootcamp-5c69669756-zg8z6 | Total Requests: 6 | App Uptime: 7291.141 seconds | Log Time: 2019-04-02T06:57:25.904Z
MacBook-Pro:powerpecognition shinichiyamada$ kubectl logs kubernetes-bootcamp-5c69669756-zlv7l
Kubernetes Bootcamp App Started At: 2019-04-02T07:51:40.093Z | Running On: kubernetes-bootcamp-5c69669756-zlv7l
Running On: kubernetes-bootcamp-5c69669756-zlv7l | Total Requests: 1 | App Uptime: 673.061 seconds | Log Time: 2019-04-02T08:02:53.154Z
Running On: kubernetes-bootcamp-5c69669756-zlv7l | Total Requests: 2 | App Uptime: 772.984 seconds | Log Time: 2019-04-02T08:04:33.078Z
Running On: kubernetes-bootcamp-5c69669756-zlv7l | Total Requests: 3 | App Uptime: 773.981 seconds | Log Time: 2019-04-02T08:04:34.074Z
それぞれのPodに違うログが出力されている。
基本的にログは外部のサービス(ストレージ)へ出力することになる。
スケールダウン
kubectl scale deployments/kubernetes-bootcamp --replicas=2
kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
kubernetes-bootcamp-5c69669756-72ffk 1/1 Terminating 0 21m 10.1.0.18 docker-for-desktop
kubernetes-bootcamp-5c69669756-fzj9p 1/1 Terminating 0 21m 10.1.0.19 docker-for-desktop
kubernetes-bootcamp-5c69669756-zg8z6 1/1 Running 0 3h 10.1.0.16 docker-for-desktop
kubernetes-bootcamp-5c69669756-zlv7l 1/1 Running 0 21m 10.1.0.17 docker-for-desktop
Terminating は停止中の意味。ログなどすべて消えてしまう。