mirror of
https://github.com/marcel-dempers/docker-development-youtube-series.git
synced 2025-06-06 17:01:30 +00:00
add pv files
This commit is contained in:
parent
bcf8e55856
commit
7c4a86d7bb
16
kubernetes/persistentvolume/persistentvolume.yaml
Normal file
16
kubernetes/persistentvolume/persistentvolume.yaml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolume
|
||||||
|
metadata:
|
||||||
|
name: example-volume
|
||||||
|
labels:
|
||||||
|
type: local
|
||||||
|
spec:
|
||||||
|
#we use local node storage here!
|
||||||
|
#kubectl get storageclass
|
||||||
|
storageClassName: hostpath
|
||||||
|
capacity:
|
||||||
|
storage: 1Gi
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteOnce
|
||||||
|
hostPath:
|
||||||
|
path: "/mnt/data"
|
11
kubernetes/persistentvolume/persistentvolumeclaim.yaml
Normal file
11
kubernetes/persistentvolume/persistentvolumeclaim.yaml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: PersistentVolumeClaim
|
||||||
|
metadata:
|
||||||
|
name: example-claim
|
||||||
|
spec:
|
||||||
|
storageClassName: hostpath
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteOnce
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 50Mi
|
50
kubernetes/persistentvolume/postgres-no-pv.yaml
Normal file
50
kubernetes/persistentvolume/postgres-no-pv.yaml
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: postgres-config
|
||||||
|
labels:
|
||||||
|
app: postgres
|
||||||
|
data:
|
||||||
|
POSTGRES_DB: postgresdb
|
||||||
|
POSTGRES_USER: admin
|
||||||
|
POSTGRES_PASSWORD: admin123
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: postgres
|
||||||
|
spec:
|
||||||
|
serviceName: postgres
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: postgres
|
||||||
|
replicas: 1
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: postgres
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: postgres
|
||||||
|
image: postgres:10.4
|
||||||
|
imagePullPolicy: "IfNotPresent"
|
||||||
|
ports:
|
||||||
|
- containerPort: 5432
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: postgres-config
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: postgres
|
||||||
|
labels:
|
||||||
|
app: postgres
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: postgres
|
||||||
|
ports:
|
||||||
|
- protocol: TCP
|
||||||
|
name: http
|
||||||
|
port: 5432
|
||||||
|
targetPort: 5432
|
57
kubernetes/persistentvolume/postgres-with-pv.yaml
Normal file
57
kubernetes/persistentvolume/postgres-with-pv.yaml
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: postgres-config
|
||||||
|
labels:
|
||||||
|
app: postgres
|
||||||
|
data:
|
||||||
|
POSTGRES_DB: postgresdb
|
||||||
|
POSTGRES_USER: admin
|
||||||
|
POSTGRES_PASSWORD: admin123
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: postgres
|
||||||
|
spec:
|
||||||
|
serviceName: postgres
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: postgres
|
||||||
|
replicas: 1
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: postgres
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: postgres
|
||||||
|
image: postgres:10.4
|
||||||
|
imagePullPolicy: "IfNotPresent"
|
||||||
|
ports:
|
||||||
|
- containerPort: 5432
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: postgres-config
|
||||||
|
volumeMounts:
|
||||||
|
- name: data
|
||||||
|
mountPath: /var/lib/postgresql/data
|
||||||
|
volumes:
|
||||||
|
- name: data
|
||||||
|
persistentVolumeClaim:
|
||||||
|
claimName: example-claim
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: postgres
|
||||||
|
labels:
|
||||||
|
app: postgres
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: postgres
|
||||||
|
ports:
|
||||||
|
- protocol: TCP
|
||||||
|
name: http
|
||||||
|
port: 5432
|
||||||
|
targetPort: 5432
|
77
kubernetes/persistentvolume/readme.md
Normal file
77
kubernetes/persistentvolume/readme.md
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
# Persisted Volumes Demo
|
||||||
|
|
||||||
|
## Container Storage
|
||||||
|
|
||||||
|
By default containers store their data on the file system like any other process.
|
||||||
|
Container file system is temporary and not persisted during container restarts
|
||||||
|
When container is recreated, so is the file system
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
# run postgres
|
||||||
|
docker run -d --rm -e POSTGRES_DB=postgresdb -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin123 postgres:10.4
|
||||||
|
|
||||||
|
# enter the container
|
||||||
|
docker exec -it <container-id> bash
|
||||||
|
|
||||||
|
# login to postgres
|
||||||
|
psql --username=admin postgresdb
|
||||||
|
|
||||||
|
#create a table
|
||||||
|
CREATE TABLE COMPANY(
|
||||||
|
ID INT PRIMARY KEY NOT NULL,
|
||||||
|
NAME TEXT NOT NULL,
|
||||||
|
AGE INT NOT NULL,
|
||||||
|
ADDRESS CHAR(50),
|
||||||
|
SALARY REAL
|
||||||
|
);
|
||||||
|
|
||||||
|
#show table
|
||||||
|
\dt
|
||||||
|
|
||||||
|
# quit
|
||||||
|
\q
|
||||||
|
```
|
||||||
|
|
||||||
|
Restarting the above container and going back in you will notice `\dt` commands returning no tables.
|
||||||
|
Since data is lost.
|
||||||
|
|
||||||
|
Same can be demonstrated using Kubernetes
|
||||||
|
|
||||||
|
```
|
||||||
|
cd .\kubernetes\persistedvolumes\
|
||||||
|
|
||||||
|
kubectl create ns postgres
|
||||||
|
kubectl apply -n postgres -f ./postgres-no-pv.yaml
|
||||||
|
kubectl -n postgres get pods
|
||||||
|
kubectl -n postgres exec -it postgres-0 bash
|
||||||
|
|
||||||
|
# run the same above mentioned commands to create and list the database table
|
||||||
|
|
||||||
|
kubectl delete po -n postgres postgres-0
|
||||||
|
|
||||||
|
# exec back in and confirm table does not exist.
|
||||||
|
```
|
||||||
|
|
||||||
|
# Persist data Docker
|
||||||
|
|
||||||
|
```
|
||||||
|
docker volume create postges
|
||||||
|
docker run -d --rm -v postges:/var/lib/postgresql/data -e POSTGRES_DB=postgresdb -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin123 postgres:10.4
|
||||||
|
|
||||||
|
# run the same tests as above and notice
|
||||||
|
```
|
||||||
|
|
||||||
|
# Persist data Kubernetes
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
kubectl apply -f persistedvolume.yaml
|
||||||
|
kubectl apply -n postgres -f persistedvolumeclaim.yaml
|
||||||
|
|
||||||
|
kubectl apply -n postgres -f postgres-with-pv.yaml
|
||||||
|
|
||||||
|
kubectl -n postgres get pods
|
||||||
|
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user