Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes - Storage: Pod Volume

Back


Container storage


Container Storage Interface


Container Volume



Inject data as file using volume


Pod Volume


Lab: without volume vs with volume

no volume

# demo-non-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: demo-non-volume
spec:
  containers:
    - name: nginx
      image: nginx
    - name: mongo
      image: mongo
# create app
kubectl apply -f demo-non-volume.yaml
# pod/demo-non-volume created

# confirm
kubectl get pod
# NAME              READY   STATUS    RESTARTS   AGE
# demo-non-volume   2/2     Running   0          27s

# insert data
kubectl exec -it demo-non-volume -c mongo -- mongosh
db.telemetry.insertOne({device_id: 1, x: 1, y: 2});
# {
#   acknowledged: true,
#   insertedId: ObjectId('694b4e86dae1c244908de666')
# }

# get data
kubectl exec -it demo-non-volume -c mongo -- mongosh
db.telemetry.find()
# [
#   {
#     _id: ObjectId('694b4e86dae1c244908de666'),
#     device_id: 1,
#     x: 1,
#     y: 2
#   }
# ]

# shutdown db
kubectl exec -it demo-non-volume -c mongo -- mongosh admin --eval "db.shutdownServer()"
# command terminated with exit code 137

# monitor terminal
kubectl get ev -w
# 0s          Normal    Pulling     pod/demo-non-volume   Pulling image "mongo"
# 0s          Normal    Pulled      pod/demo-non-volume   Successfully pulled image "mongo" in 1.263s (1.263s including waiting). Image size: 327004046 bytes.
# 0s          Normal    Created     pod/demo-non-volume   Created container: mongo
# 0s          Normal    Started     pod/demo-non-volume   Started container mongo

kubectl get pod -w
# NAME              READY   STATUS    RESTARTS   AGE
# demo-non-volume   2/2     Running   0          3m32s
# demo-non-volume   1/2     NotReady   0          4m
# demo-non-volume   2/2     Running    1 (3s ago)   4m3s

kubectl describe pod demo-non-volume
# Events:
#   Type    Reason     Age                 From               Message
#   ----    ------     ----                ----               -------
#   Normal  Scheduled  5m11s               default-scheduler  Successfully assigned default/demo-non-volume to docker-desktop
#   Normal  Pulling    5m11s               kubelet            Pulling image "nginx"
#   Normal  Pulled     5m10s               kubelet            Successfully pulled image "nginx" in 1.256s (1.256s including waiting). Image size: 59795293 bytes.
#   Normal  Created    5m9s                kubelet            Created container: nginx
#   Normal  Started    5m9s                kubelet            Started container nginx
#   Normal  Pulled     5m8s                kubelet            Successfully pulled image "mongo" in 797ms (797ms including waiting). Image size: 327004046 bytes.
#   Normal  Pulling    71s (x2 over 5m9s)  kubelet            Pulling image "mongo"
#   Normal  Pulled     70s                 kubelet            Successfully pulled image "mongo" in 1.263s (1.263s including waiting). Image size: 327004046 bytes.
#   Normal  Created    69s (x2 over 5m8s)  kubelet            Created container: mongo
#   Normal  Started    69s (x2 over 5m8s)  kubelet            Started container mongo

# try to read data
kubectl exec -it demo-non-volume -c mongo -- mongosh --eval "db.telemetry.find()"
# none

Pod has 2 containers: nginx + mongo container mongo restart, but nginx is still running, therefore the pod still runs. No data persists after mongo restart -> container without volume cannot persists data.


Pod volume

# demo-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: demo-volume
spec:
  # pod vol
  volumes:
    - name: mongo-vol
      emptyDir: {}
  containers:
    - name: nginx
      image: nginx
    - name: mongo
      image: mongo
      volumeMounts:
        - name: mongo-vol # reference vol
          mountPath: /data/db # path in container
kubectl apply -f demo-volume.yaml
# pod/demo-volume created

kubectl get pod demo-volume
# NAME          READY   STATUS    RESTARTS   AGE
# demo-volume   2/2     Running   0          29s

# insert data
kubectl exec -it demo-volume -c mongo -- mongosh
db.telemetry.insertOne({device_id: 1, x: 1, y: 2});
# {
#   acknowledged: true,
#   insertedId: ObjectId('694b56d088e4cd1e7c8de666')
# }

# get data
kubectl exec -it demo-volume -c mongo -- mongosh
db.telemetry.find()
# [
#   {
#     _id: ObjectId('694b56d088e4cd1e7c8de666'),
#     device_id: 1,
#     x: 1,
#     y: 2
#   }
# ]

# shutdown db
kubectl exec -it demo-volume -c mongo -- mongosh admin --eval "db.shutdownServer()"
# command terminated with exit code 137

# monitor terminal
kubectl get ev -w
# 0s          Normal    Pulling     pod/demo-volume       Pulling image "mongo"
# 0s          Normal    Pulled      pod/demo-volume       Successfully pulled image "mongo" in 1.144s (1.144s including waiting). Image size: 327004046 bytes.
# 0s          Normal    Created     pod/demo-volume       Created container: mongo
# 0s          Normal    Started     pod/demo-volume       Started container mongo

kubectl get pod -w
# NAME          READY   STATUS    RESTARTS   AGE
# demo-volume   2/2     Running   0          3m12s
# demo-volume   1/2     NotReady   0          3m58s
# demo-volume   2/2     Running    1 (3s ago)   4m1s

kubectl describe pod demo-volume
# Events:
#   Type    Reason     Age                  From               Message
#   ----    ------     ----                 ----               -------
#   Normal  Scheduled  4m34s                default-scheduler  Successfully assigned default/demo-volume to docker-desktop
#   Normal  Pulling    4m34s                kubelet            Pulling image "nginx"
#   Normal  Pulled     4m33s                kubelet            Successfully pulled image "nginx" in 1.214s (1.215s including waiting). Image size: 59795293 bytes.
#   Normal  Created    4m32s                kubelet            Created container: nginx
#   Normal  Started    4m32s                kubelet            Started container nginx
#   Normal  Pulled     4m32s                kubelet            Successfully pulled image "mongo" in 764ms (764ms including waiting). Image size: 327004046 bytes.
#   Normal  Pulling    36s (x2 over 4m32s)  kubelet            Pulling image "mongo"
#   Normal  Created    35s (x2 over 4m31s)  kubelet            Created container: mongo
#   Normal  Pulled     35s                  kubelet            Successfully pulled image "mongo" in 1.144s (1.144s including waiting). Image size: 327004046 bytes.
#   Normal  Started    34s (x2 over 4m31s)  kubelet            Started container mongo

# try to read data
kubectl exec -it demo-volume -c mongo -- mongosh --eval "db.telemetry.find()"
# [
#   {
#     _id: ObjectId('694b56d088e4cd1e7c8de666'),
#     device_id: 1,
#     x: 1,
#     y: 2
#   }
# ]