K8s storage:
- Container are ephameral. when container will restart , everthing will be wirte in file system it will be lost.
- K8s Storage sloved two problem
- Presistance: data will survives container restart and pod rescheduling.
- Sharing: multiple container can share data.
Storage building blocks:
Volume:
- scope level is pod
- directly access container in pod
volume
- name: demo
path: /data/mysql
Volume ==> create and name volume ==> POD will use it
PersistentVolume (PV):
- scope level is cluser
- A piece of storage created/provisioned by admin or dynamically
PersistentVolume Claim (PVc):
- scope level is namespace
- User request storage (required by container)
Storage class:
- scope level is cluster
- Define storage class and enable dynamic provisioning
EX: local-path, NFS
CSI Drivers:
- CSI Stands for Container Storage Interface
- scope level is cluser
- plugin like EBS, AZure DIsk, NFS…etc
Ephameral Vloume types:
- these are live and die with the pod
emptyDir
- An Empty dir will create when pod started . Delete dir when pod get deleted/removed
- Use-cases:
- scratch space (every time need new space storage)
- Cache
- Sharing file between sidecar container and main container.
# create empty vloume
volumes:
- name: shared
emptyDir: {}
## Mount volume
volumeMounts:
- name: shared
mountpath: /data
Ram-base variant
volumes:
- name: cache
emptyDir: {}
medium: Memory
sizeLimit: 256Mi
Sample yaml for emptyDir volume:
apiVersion: apps/v1
kind: Deployment
metadata:
name: recreate-deployment-app
labels:
app: apache2
spec:
replicas: 2
selector:
matchLabels:
app: apache2
strategy:
type: Recreate
template:
metadata:
labels:
app: apache2
spec:
initContainers:
- name: download-template
image: alpine:latest
command:
- sh
- -c
- |
apk add --no-cache wget unzip
wget -O /tmp/templatemo_589_lugx_gaming.zip \
https://templatemo.com/tm-zip-files-2020/templatemo_589_lugx_gaming.zip
unzip /tmp/templatemo_589_lugx_gaming.zip \
-d /usr/local/apache2/htdocs/
cp /usr/local/apache2/htdocs/templatemo_589_lugx_gaming/* /usr/local/apache2/htdocs/
rm -rf /usr/local/apache2/htdocs/templatemo_589_lugx_gaming
volumeMounts:
- name: apache2-html
mountPath: /usr/local/apache2/htdocs/
containers:
- name: apache2
image: httpd:2
ports:
- containerPort: 80
volumeMounts:
- name: apache2-html
mountPath: /usr/local/apache2/htdocs/
volumes:
- name: apache2-html
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: nodeport-service-apache2
labels:
app: apache2
spec:
type: NodePort
selector:
app: apache2
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30010
Task:
- create similar way volume emptyDir
- used main image nginx image and create init container download sample template from
https://templatemo.com
- deploy and valiadte endpoint
FROM nginx
COPY static-html-directory /usr/share/nginx/html