Skip to content

Commit

Permalink
feat: volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
benoittouron committed Jun 19, 2024
1 parent ada693c commit 7a94fbc
Show file tree
Hide file tree
Showing 20 changed files with 328 additions and 0 deletions.
4 changes: 4 additions & 0 deletions volumes/assets/solution-step1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Retrouver le mot de passe:

kubectl get secret mysql -o jsonpath='{.data.password}' | base64 -d

8 changes: 8 additions & 0 deletions volumes/assets/solution-step2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Le déploiement possède une image avec un tag non valide
image: nginx:<changeme> #mettre: nginx:1.9.14

Il est possible d'utiliser un outil tel que vim pour remplacer nginx:<changeme> en nginx:1.9.14

Pour appliquer le changement d'un fichier yaml, utiliser kubectl apply -f frontend.yaml

echo 'kubectl apply -f frontend.yaml' > res-step2
12 changes: 12 additions & 0 deletions volumes/assets/volume/mysql-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: mysql
spec:
type: ClusterIP
ports:
- port: 3306
selector:
app: mysql
10 changes: 10 additions & 0 deletions volumes/assets/volume/mysql-volumeclaim.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-volumeclaim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
42 changes: 42 additions & 0 deletions volumes/assets/volume/mysql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
labels:
app: mysql
spec:
strategy:
type: Recreate
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
resources:
requests:
memory: 400Mi
limits:
cpu: 0.1
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-volumeclaim
15 changes: 15 additions & 0 deletions volumes/assets/volume/wordpress-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
labels:
app: wordpress
name: wordpress
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30000
protocol: TCP
selector:
app: wordpress
10 changes: 10 additions & 0 deletions volumes/assets/volume/wordpress-volumeclaim.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: wordpress-volumeclaim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
41 changes: 41 additions & 0 deletions volumes/assets/volume/wordpress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: wordpress
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: mysql:3306
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
resources:
limits:
cpu: 0.1
memory: "50Mi"
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wordpress-volumeclaim
3 changes: 3 additions & 0 deletions volumes/background.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

true
5 changes: 5 additions & 0 deletions volumes/foreground.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

echo "Welcome to the Sfeir School Volume scenario"
echo " Enjoy :-)"

40 changes: 40 additions & 0 deletions volumes/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"title": "Volume",
"description": "Gérer les données dans Kubernetes",
"details": {
"intro": {
"text": "intro.md",
"background": "background.sh",
"foreground": "foreground.sh"
},
"steps": [
{
"title": "Créer un secret pour la base de données",
"text": "step1/step1.md",
"verify": "step1/verify.sh"
},
{
"title": "Déployer un Wordpress complet",
"text": "step2/step2.md",
"verify": "step2/verify.sh"
},
{
"title": "Détruire et relancer des pods",
"text": "step3/step3.md"
},
{
"title": "Nettoyage",
"text": "step4/step4.md",
"verify": "step4/verify.sh"
}
],
"assets": {
"host01": [
{"file": "volume/*", "target": "/root", "chmod": "+w"}
]
}
},
"backend": {
"imageid": "kubernetes-kubeadm-1node"
}
}
5 changes: 5 additions & 0 deletions volumes/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Volumes

Les fichiers sur disque dans un conteneur sont éphémères, ce qui présente des problèmes pour des applications non-triviales lorsqu'elles s'exécutent dans des conteneurs. Premièrement, lorsqu'un conteneur plante, kubelet va le redémarrer mais les fichiers seront perdus - le conteneur démarre avec un état propre. Deuxièmement, lorsque plusieurs conteneurs s'exécutent ensemble dans un `Pod`, il est souvent nécessaire de partager des fichiers entre ces conteneurs. L'abstraction Kubernetes `Volume` résout ces deux problèmes.

Le but de ce TP est de déployer une stack Wordpress complète avec une base de données MySQL.
36 changes: 36 additions & 0 deletions volumes/step1/step1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## 1/ Créer un secret pour votre base de données

Executer la commande suivante:
```
kubectl create secret generic mysql --from-literal=password=$(openssl rand -hex 12)
```{{exec}}
### Question ?
- Quelle est le mot de passe généré ?
Indices pour le retrouver:
1. Afficher le secret en format YAML
```
kubectl get secret mysql -o yaml
```{{exec}}
2. Dans ce yamml, repérer le chemin de l'élément qui contient le mot de passe, et l'extraire avec le paramètre _jsonpath_ de kubectl
```
kubectl get secret mysql -o jsonpath='{.mon.chemin.ici}'
```
3. Attention, la valeur est encodée en base64, il faut donc la décoder:
```
echo 'mon_MoT_de_PaSsE_enCOde==' | base64 -d
```
- Ecrire la valeur de ce mot de passe dans le fichier /tmp/res-step1
> vous pouvez utiliser la commande `echo '..mot_de_passe_trouvé..' > /tmp/res-step1`
3 changes: 3 additions & 0 deletions volumes/step1/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

grep $(kubectl get secret mysql -o jsonpath='{.data.password}' | base64 -d) /tmp/res-step1
61 changes: 61 additions & 0 deletions volumes/step2/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
## 1/ Créer les volumes
Appliquer ces 2 commandes

```
kubectl apply -f volume/mysql-volumeclaim.yaml
kubectl apply -f volume/wordpress-volumeclaim.yaml
```{{exec}}
## 2/ Créer ma base de données MySQL
Les descripteurs permettant de déployer une base de données sont founis dans le TP. Les examiner et les appliquer:
```
cat volume/mysql.yaml
```{{exec}}
```
kubectl apply -f volume/mysql.yaml
```{{exec}}
```
cat volume/mysql-service.yaml
```{{exec}}
```
kubectl apply -f volume/mysql-service.yaml
```{{exec}}
## 3/ Créer l'instance Wordpress et l'exposer
Les descripteurs sont founis dans le TP. Les examiner et les appliquer:
```
cat volume/wordpress.yaml
```{{exec}}
```
kubectl apply -f volume/wordpress.yaml
```{{exec}}
```
cat volume/wordpress-service.yaml
```{{exec}}
```
kubectl apply -f volume/wordpress-service.yaml
```{{exec}}
## 4/ Accédez à votre wordpress
```
curl http://$(kubectl get service wordpress -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
```{{exec}}
### Challenge
Accédez à l'application depuis votre navigateur
- Trouver l'IP
- Se connecter en http
3 changes: 3 additions & 0 deletions volumes/step2/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

true
12 changes: 12 additions & 0 deletions volumes/step3/step3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## 1/ Détruire des pods mysql et wordpress

```
kubectl delete pods -l="app=mysql"
```{{exec}}
```
kubectl delete pods -l="app=wordpress"
```{{exec}}
### Question ?
Que se passe-t-il ? (un peu de patience quand même ;) )
3 changes: 3 additions & 0 deletions volumes/step3/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

true
12 changes: 12 additions & 0 deletions volumes/step4/step4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## 1/ Nettoyer l'environnement

Effacer tous les objets crées.

```
kubectl delete service wordpress
kubectl delete deployment wordpress
kubectl delete pvc wordpress
kubectl delete service mysql
kubectl delete deployment mysql
kubectl delete pvc mysql
```{{exec}}
3 changes: 3 additions & 0 deletions volumes/step4/verify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

true

0 comments on commit 7a94fbc

Please sign in to comment.