Practica final
This commit is contained in:
@@ -1,16 +1,5 @@
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: PersistentVolumeClaim
|
kind: PersistentVolumeClaim
|
||||||
metadata:
|
|
||||||
name: mariadb-pvc
|
|
||||||
spec:
|
|
||||||
accessModes:
|
|
||||||
- ReadWriteOnce
|
|
||||||
resources:
|
|
||||||
requests:
|
|
||||||
storage: 2Gi
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: PersistentVolumeClaim
|
|
||||||
metadata:
|
metadata:
|
||||||
name: wordpress-pvc
|
name: wordpress-pvc
|
||||||
spec:
|
spec:
|
||||||
|
|||||||
@@ -1,37 +1,29 @@
|
|||||||
apiVersion: apps/v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: mariadb-deployment
|
name: mariadb-configmap
|
||||||
spec:
|
data:
|
||||||
replicas: 1
|
primary.cnf: |
|
||||||
selector:
|
[mariadb]
|
||||||
matchLabels:
|
log-bin
|
||||||
app: mariadb
|
log-basename=my-mariadb
|
||||||
template:
|
|
||||||
metadata:
|
replica.cnf: |
|
||||||
labels:
|
[mariadb]
|
||||||
app: mariadb
|
log-basename=my-mariadb
|
||||||
spec:
|
|
||||||
containers:
|
primary.sql: |
|
||||||
- name: mariadb
|
CREATE USER 'repluser'@'%' IDENTIFIED BY 'replsecret';
|
||||||
image: mariadb:lts-ubi
|
GRANT REPLICATION REPLICA ON *.* TO 'repluser'@'%';
|
||||||
env:
|
CREATE DATABASE IF NOT EXISTS wordpress;
|
||||||
- name: MARIADB_ROOT_PASSWORD
|
|
||||||
valueFrom:
|
secondary.sql: |
|
||||||
secretKeyRef:
|
CHANGE MASTER TO
|
||||||
name: credencials-bd
|
MASTER_HOST='mariadb-sts-0.servei-mariadb.default.svc.cluster.local',
|
||||||
key: password
|
MASTER_USER='repluser',
|
||||||
- name: MARIADB_DATABASE
|
MASTER_PASSWORD='replsecret',
|
||||||
value: "wordpress"
|
MASTER_CONNECT_RETRY=10;
|
||||||
ports:
|
START SLAVE;
|
||||||
- containerPort: 3306
|
|
||||||
volumeMounts:
|
|
||||||
- name: dades-bd
|
|
||||||
mountPath: /var/lib/mysql
|
|
||||||
volumes:
|
|
||||||
- name: dades-bd
|
|
||||||
persistentVolumeClaim:
|
|
||||||
claimName: mariadb-pvc
|
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
@@ -44,3 +36,87 @@ spec:
|
|||||||
- protocol: TCP
|
- protocol: TCP
|
||||||
port: 3306
|
port: 3306
|
||||||
targetPort: 3306
|
targetPort: 3306
|
||||||
|
clusterIP: None
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: mariadb-sts
|
||||||
|
spec:
|
||||||
|
serviceName: "servei-mariadb"
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: mariadb
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: mariadb
|
||||||
|
spec:
|
||||||
|
initContainers:
|
||||||
|
- name: init-mariadb
|
||||||
|
image: mariadb:lts-ubi
|
||||||
|
command:
|
||||||
|
- bash
|
||||||
|
- "-c"
|
||||||
|
- |
|
||||||
|
set -ex
|
||||||
|
echo 'Starting init-mariadb'
|
||||||
|
ls /mnt/config-map
|
||||||
|
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
|
||||||
|
ordinal=${BASH_REMATCH[1]}
|
||||||
|
if [[ $ordinal -eq 0 ]]; then
|
||||||
|
cp /mnt/config-map/primary.cnf /etc/mysql/conf.d/server-id.cnf
|
||||||
|
cp /mnt/config-map/primary.sql /docker-entrypoint-initdb.d
|
||||||
|
else
|
||||||
|
cp /mnt/config-map/replica.cnf /etc/mysql/conf.d/server-id.cnf
|
||||||
|
cp /mnt/config-map/secondary.sql /docker-entrypoint-initdb.d
|
||||||
|
fi
|
||||||
|
echo server-id=$((3000 + ordinal)) >> /etc/mysql/conf.d/server-id.cnf
|
||||||
|
ls /etc/mysql/conf.d/
|
||||||
|
cat /etc/mysql/conf.d/server-id.cnf
|
||||||
|
volumeMounts:
|
||||||
|
- name: mariadb-config-map
|
||||||
|
mountPath: /mnt/config-map
|
||||||
|
- name: mariadb-config
|
||||||
|
mountPath: /etc/mysql/conf.d/
|
||||||
|
- name: initdb
|
||||||
|
mountPath: /docker-entrypoint-initdb.d
|
||||||
|
containers:
|
||||||
|
- name: mariadb
|
||||||
|
image: mariadb:lts-ubi
|
||||||
|
ports:
|
||||||
|
- containerPort: 3306
|
||||||
|
name: mariadb-port
|
||||||
|
env:
|
||||||
|
- name: MARIADB_ROOT_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: credencials-bd
|
||||||
|
key: password
|
||||||
|
- name: MYSQL_INITDB_SKIP_TZINFO
|
||||||
|
value: "1"
|
||||||
|
volumeMounts:
|
||||||
|
- name: datadir
|
||||||
|
mountPath: /var/lib/mysql
|
||||||
|
- name: mariadb-config
|
||||||
|
mountPath: /etc/mysql/conf.d/
|
||||||
|
- name: initdb
|
||||||
|
mountPath: /docker-entrypoint-initdb.d
|
||||||
|
volumes:
|
||||||
|
- name: mariadb-config-map
|
||||||
|
configMap:
|
||||||
|
name: mariadb-configmap
|
||||||
|
- name: mariadb-config
|
||||||
|
emptyDir: {}
|
||||||
|
- name: initdb
|
||||||
|
emptyDir: {}
|
||||||
|
volumeClaimTemplates:
|
||||||
|
- metadata:
|
||||||
|
name: datadir
|
||||||
|
spec:
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteOnce
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 2Gi
|
||||||
@@ -17,7 +17,7 @@ spec:
|
|||||||
image: wordpress:latest
|
image: wordpress:latest
|
||||||
env:
|
env:
|
||||||
- name: WORDPRESS_DB_HOST
|
- name: WORDPRESS_DB_HOST
|
||||||
value: "servei-mariadb" # Apunta al nom del Service de MariaDB
|
value: "mariadb-sts-0.servei-mariadb.default.svc.cluster.local" # Apunta al pod primari de MariaDB
|
||||||
- name: WORDPRESS_DB_PASSWORD
|
- name: WORDPRESS_DB_PASSWORD
|
||||||
valueFrom:
|
valueFrom:
|
||||||
secretKeyRef:
|
secretKeyRef:
|
||||||
|
|||||||
@@ -1,29 +1,41 @@
|
|||||||
# Pràctica final
|
# Pràctica final
|
||||||
|
|
||||||
Carpeta que agrupa els manifests per una pràctica completa (WordPress + MariaDB) amb secrets, PVCs, base de dades i ingress.
|
Carpeta que agrupa els manifests per una pràctica completa de WordPress + MariaDB amb replicació.
|
||||||
|
|
||||||
|
Aquesta pràctica desplega:
|
||||||
|
- MariaDB en un `StatefulSet` de 3 rèpliques amb replicació.
|
||||||
|
- WordPress connectat al node primari de MariaDB.
|
||||||
|
- Un Ingress per exposar l'aplicació a `elmeublog.local`.
|
||||||
|
|
||||||
Prerequisits
|
Prerequisits
|
||||||
- Controlador d'Ingress actiu (traefik, nginx-ingress, etc.).
|
- Controlador d'Ingress actiu (traefik, nginx-ingress, etc.).
|
||||||
- `kubectl` configurat.
|
- `kubectl` configurat al cluster.
|
||||||
- StorageClass per proveir PVCs.
|
- StorageClass disponible per proveir els PVCs.
|
||||||
|
- Opcional: afegir `127.0.0.1 elmeublog.local` al teu `/etc/hosts` si fas servir un cluster local.
|
||||||
|
|
||||||
Ordre d'aplicació recomanat
|
Ordre d'aplicació recomanat
|
||||||
1. `01-secret.yaml` — Crear `Secret` amb credencials.
|
1. `kubectl apply -f exemples/practica-final/01-secret.yaml`
|
||||||
2. `02-pvcs.yaml` — Crear `PersistentVolumeClaim` per MariaDB i WordPress.
|
2. `kubectl apply -f exemples/practica-final/02-pvcs.yaml` # crea el PVC per WordPress
|
||||||
3. `03-mariadb.yaml` — Desplegar MariaDB.
|
3. `kubectl apply -f exemples/practica-final/03-mariadb.yaml`
|
||||||
4. `04-wordpress.yaml` — Desplegar WordPress connectat a MariaDB.
|
4. `kubectl apply -f exemples/practica-final/04-wordpress.yaml`
|
||||||
5. `05-ingress.yaml` — Crear Ingress per exposar l'aplicació.
|
5. `kubectl apply -f exemples/practica-final/05-ingress.yaml`
|
||||||
|
|
||||||
|
Aquest ordre garanteix que el Secret i el volum de WordPress existeixin abans de desplegar MariaDB i WordPress. MariaDB crea els seus volums amb `volumeClaimTemplates` dins del `StatefulSet`.
|
||||||
|
|
||||||
Com aplicar tot junt
|
Com aplicar tot junt
|
||||||
```bash
|
```bash
|
||||||
kubectl apply -f exemples/practica-final/install.yaml
|
kubectl apply -f exemples/practica-final/install.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Accés
|
||||||
|
- Navega a `http://elmeublog.local` després de desplegar l'Ingress.
|
||||||
|
|
||||||
Neteja
|
Neteja
|
||||||
```bash
|
```bash
|
||||||
kubectl delete -f exemples/practica-final/install.yaml
|
kubectl delete -f exemples/practica-final/install.yaml
|
||||||
```
|
```
|
||||||
|
|
||||||
Notes
|
Notes
|
||||||
- Revisa que els secrets (usuaris/contrassenyes) i les connexions (host/port) entre WordPress i MariaDB estiguin correctes abans d'executar la pràctica.
|
- WordPress es connecta directament al pod primari de MariaDB: `mariadb-sts-0.servei-mariadb.default.svc.cluster.local`.
|
||||||
- Si utilitzes hostnames en l'Ingress, afegeix les entrades corresponents al teu `/etc/hosts` o DNS de desenvolupament.
|
- La MariaDB es desplega com a `StatefulSet` de 3 rèpliques amb configuració de primari/secundari.
|
||||||
|
- Si l'Ingress no funciona, revisa que el controlador estigui actiu i que el host `elmeublog.local` estigui resoluble.
|
||||||
|
|||||||
@@ -8,17 +8,6 @@ data:
|
|||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: PersistentVolumeClaim
|
kind: PersistentVolumeClaim
|
||||||
metadata:
|
|
||||||
name: mariadb-pvc
|
|
||||||
spec:
|
|
||||||
accessModes:
|
|
||||||
- ReadWriteOnce
|
|
||||||
resources:
|
|
||||||
requests:
|
|
||||||
storage: 2Gi
|
|
||||||
---
|
|
||||||
apiVersion: v1
|
|
||||||
kind: PersistentVolumeClaim
|
|
||||||
metadata:
|
metadata:
|
||||||
name: wordpress-pvc
|
name: wordpress-pvc
|
||||||
spec:
|
spec:
|
||||||
@@ -28,40 +17,32 @@ spec:
|
|||||||
requests:
|
requests:
|
||||||
storage: 2Gi
|
storage: 2Gi
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: v1
|
||||||
kind: Deployment
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: mariadb-deployment
|
name: mariadb-configmap
|
||||||
spec:
|
data:
|
||||||
replicas: 1
|
primary.cnf: |
|
||||||
selector:
|
[mariadb]
|
||||||
matchLabels:
|
log-bin
|
||||||
app: mariadb
|
log-basename=my-mariadb
|
||||||
template:
|
|
||||||
metadata:
|
replica.cnf: |
|
||||||
labels:
|
[mariadb]
|
||||||
app: mariadb
|
log-basename=my-mariadb
|
||||||
spec:
|
|
||||||
containers:
|
primary.sql: |
|
||||||
- name: mariadb
|
CREATE USER 'repluser'@'%' IDENTIFIED BY 'replsecret';
|
||||||
image: mariadb:lts-ubi
|
GRANT REPLICATION REPLICA ON *.* TO 'repluser'@'%';
|
||||||
env:
|
CREATE DATABASE IF NOT EXISTS wordpress;
|
||||||
- name: MARIADB_ROOT_PASSWORD
|
|
||||||
valueFrom:
|
secondary.sql: |
|
||||||
secretKeyRef:
|
CHANGE MASTER TO
|
||||||
name: credencials-bd
|
MASTER_HOST='mariadb-sts-0.servei-mariadb.default.svc.cluster.local',
|
||||||
key: password
|
MASTER_USER='repluser',
|
||||||
- name: MARIADB_DATABASE
|
MASTER_PASSWORD='replsecret',
|
||||||
value: "wordpress"
|
MASTER_CONNECT_RETRY=10;
|
||||||
ports:
|
START SLAVE;
|
||||||
- containerPort: 3306
|
|
||||||
volumeMounts:
|
|
||||||
- name: dades-bd
|
|
||||||
mountPath: /var/lib/mysql
|
|
||||||
volumes:
|
|
||||||
- name: dades-bd
|
|
||||||
persistentVolumeClaim:
|
|
||||||
claimName: mariadb-pvc
|
|
||||||
---
|
---
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
@@ -74,6 +55,90 @@ spec:
|
|||||||
- protocol: TCP
|
- protocol: TCP
|
||||||
port: 3306
|
port: 3306
|
||||||
targetPort: 3306
|
targetPort: 3306
|
||||||
|
clusterIP: None
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: StatefulSet
|
||||||
|
metadata:
|
||||||
|
name: mariadb-sts
|
||||||
|
spec:
|
||||||
|
serviceName: "servei-mariadb"
|
||||||
|
replicas: 3
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: mariadb
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: mariadb
|
||||||
|
spec:
|
||||||
|
initContainers:
|
||||||
|
- name: init-mariadb
|
||||||
|
image: mariadb:lts-ubi
|
||||||
|
command:
|
||||||
|
- bash
|
||||||
|
- "-c"
|
||||||
|
- |
|
||||||
|
set -ex
|
||||||
|
echo 'Starting init-mariadb'
|
||||||
|
ls /mnt/config-map
|
||||||
|
[[ `hostname` =~ -([0-9]+)$ ]] || exit 1
|
||||||
|
ordinal=${BASH_REMATCH[1]}
|
||||||
|
if [[ $ordinal -eq 0 ]]; then
|
||||||
|
cp /mnt/config-map/primary.cnf /etc/mysql/conf.d/server-id.cnf
|
||||||
|
cp /mnt/config-map/primary.sql /docker-entrypoint-initdb.d
|
||||||
|
else
|
||||||
|
cp /mnt/config-map/replica.cnf /etc/mysql/conf.d/server-id.cnf
|
||||||
|
cp /mnt/config-map/secondary.sql /docker-entrypoint-initdb.d
|
||||||
|
fi
|
||||||
|
echo server-id=$((3000 + ordinal)) >> /etc/mysql/conf.d/server-id.cnf
|
||||||
|
ls /etc/mysql/conf.d/
|
||||||
|
cat /etc/mysql/conf.d/server-id.cnf
|
||||||
|
volumeMounts:
|
||||||
|
- name: mariadb-config-map
|
||||||
|
mountPath: /mnt/config-map
|
||||||
|
- name: mariadb-config
|
||||||
|
mountPath: /etc/mysql/conf.d/
|
||||||
|
- name: initdb
|
||||||
|
mountPath: /docker-entrypoint-initdb.d
|
||||||
|
containers:
|
||||||
|
- name: mariadb
|
||||||
|
image: mariadb:lts-ubi
|
||||||
|
ports:
|
||||||
|
- containerPort: 3306
|
||||||
|
name: mariadb-port
|
||||||
|
env:
|
||||||
|
- name: MARIADB_ROOT_PASSWORD
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: credencials-bd
|
||||||
|
key: password
|
||||||
|
- name: MYSQL_INITDB_SKIP_TZINFO
|
||||||
|
value: "1"
|
||||||
|
volumeMounts:
|
||||||
|
- name: datadir
|
||||||
|
mountPath: /var/lib/mysql
|
||||||
|
- name: mariadb-config
|
||||||
|
mountPath: /etc/mysql/conf.d/
|
||||||
|
- name: initdb
|
||||||
|
mountPath: /docker-entrypoint-initdb.d
|
||||||
|
volumes:
|
||||||
|
- name: mariadb-config-map
|
||||||
|
configMap:
|
||||||
|
name: mariadb-configmap
|
||||||
|
- name: mariadb-config
|
||||||
|
emptyDir: {}
|
||||||
|
- name: initdb
|
||||||
|
emptyDir: {}
|
||||||
|
volumeClaimTemplates:
|
||||||
|
- metadata:
|
||||||
|
name: datadir
|
||||||
|
spec:
|
||||||
|
accessModes:
|
||||||
|
- ReadWriteOnce
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
storage: 2Gi
|
||||||
---
|
---
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
@@ -94,7 +159,7 @@ spec:
|
|||||||
image: wordpress:latest
|
image: wordpress:latest
|
||||||
env:
|
env:
|
||||||
- name: WORDPRESS_DB_HOST
|
- name: WORDPRESS_DB_HOST
|
||||||
value: "servei-mariadb" # Apunta al nom del Service de MariaDB
|
value: "mariadb-sts-0.servei-mariadb.default.svc.cluster.local" # Apunta al pod primari de MariaDB
|
||||||
- name: WORDPRESS_DB_PASSWORD
|
- name: WORDPRESS_DB_PASSWORD
|
||||||
valueFrom:
|
valueFrom:
|
||||||
secretKeyRef:
|
secretKeyRef:
|
||||||
|
|||||||
Reference in New Issue
Block a user