From 9a9ab01dcf72fc4bce2229ca32b82c2fb9370085 Mon Sep 17 00:00:00 2001 From: Linux User Date: Thu, 5 Mar 2026 10:54:12 +0000 Subject: [PATCH] Exemples del curs --- .../exemples-inicials/app-configurada.yaml | 21 +++ exemples/{ => exemples-inicials}/deploy.yaml | 0 .../exemples-inicials/nginx-clusterip.yaml | 11 ++ .../exemples-inicials/nginx-deployment.yaml | 19 +++ exemples/exemples-inicials/nginx-ingress.yaml | 20 +++ exemples/{ => exemples-inicials}/pod.yaml | 0 .../exemples-inicials/redis-deployment.yaml | 24 +++ exemples/exemples-inicials/redis-pod.yaml | 15 ++ exemples/exemples-inicials/redis-pvc.yaml | 10 ++ .../redis-stateful-replica.yaml | 40 +++++ .../exemples-inicials/redis-stateful.yaml | 30 ++++ exemples/{ => exemples-inicials}/service.yaml | 0 exemples/maria-db/mariadb-deployment.yaml | 29 ++++ exemples/maria-db/mariadb-pvc.yaml | 10 ++ .../mariadb-replication.yaml | 157 ++++++++++++++++++ exemples/practica-final/01-secret.yaml | 7 + exemples/practica-final/02-pvcs.yaml | 21 +++ exemples/practica-final/03-mariadb.yaml | 44 +++++ exemples/practica-final/04-wordpress.yaml | 46 +++++ exemples/practica-final/05-ingress.yaml | 16 ++ 20 files changed, 520 insertions(+) create mode 100644 exemples/exemples-inicials/app-configurada.yaml rename exemples/{ => exemples-inicials}/deploy.yaml (100%) create mode 100644 exemples/exemples-inicials/nginx-clusterip.yaml create mode 100644 exemples/exemples-inicials/nginx-deployment.yaml create mode 100644 exemples/exemples-inicials/nginx-ingress.yaml rename exemples/{ => exemples-inicials}/pod.yaml (100%) create mode 100644 exemples/exemples-inicials/redis-deployment.yaml create mode 100644 exemples/exemples-inicials/redis-pod.yaml create mode 100644 exemples/exemples-inicials/redis-pvc.yaml create mode 100644 exemples/exemples-inicials/redis-stateful-replica.yaml create mode 100644 exemples/exemples-inicials/redis-stateful.yaml rename exemples/{ => exemples-inicials}/service.yaml (100%) create mode 100644 exemples/maria-db/mariadb-deployment.yaml create mode 100644 exemples/maria-db/mariadb-pvc.yaml create mode 100644 exemples/mariadb-replication/mariadb-replication.yaml create mode 100644 exemples/practica-final/01-secret.yaml create mode 100644 exemples/practica-final/02-pvcs.yaml create mode 100644 exemples/practica-final/03-mariadb.yaml create mode 100644 exemples/practica-final/04-wordpress.yaml create mode 100644 exemples/practica-final/05-ingress.yaml diff --git a/exemples/exemples-inicials/app-configurada.yaml b/exemples/exemples-inicials/app-configurada.yaml new file mode 100644 index 0000000..d4b8753 --- /dev/null +++ b/exemples/exemples-inicials/app-configurada.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: Pod +metadata: + name: pod-amb-configuracio +spec: + containers: + - name: el-meu-contenidor + image: alpine + # Aquesta comanda imprimeix les variables i es queda adormida perquè el Pod no mori + command: ["/bin/sh", "-c", "echo El missatge és: $MISSATGE_APP i la clau és: $CLAU_DB; sleep 3600"] + env: + - name: MISSATGE_APP + valueFrom: + configMapKeyRef: + name: configuracio-web + key: MISSATGE + - name: CLAU_DB + valueFrom: + secretKeyRef: + name: credencials-db + key: PASSWORD diff --git a/exemples/deploy.yaml b/exemples/exemples-inicials/deploy.yaml similarity index 100% rename from exemples/deploy.yaml rename to exemples/exemples-inicials/deploy.yaml diff --git a/exemples/exemples-inicials/nginx-clusterip.yaml b/exemples/exemples-inicials/nginx-clusterip.yaml new file mode 100644 index 0000000..b2a970b --- /dev/null +++ b/exemples/exemples-inicials/nginx-clusterip.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: servei-web-intern +spec: + type: ClusterIP + selector: + app: el-meu-web + ports: + - port: 80 + targetPort: 80 diff --git a/exemples/exemples-inicials/nginx-deployment.yaml b/exemples/exemples-inicials/nginx-deployment.yaml new file mode 100644 index 0000000..dcb7fe6 --- /dev/null +++ b/exemples/exemples-inicials/nginx-deployment.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: el-meu-web-deployment +spec: + replicas: 3 + selector: + matchLabels: + app: el-meu-web # <--- Això ha de coincidir amb el selector del teu Service + template: + metadata: + labels: + app: el-meu-web # <--- Aquesta és l'etiqueta que busca el Service + spec: + containers: + - name: nginx + image: nginx:latest + ports: + - containerPort: 80 diff --git a/exemples/exemples-inicials/nginx-ingress.yaml b/exemples/exemples-inicials/nginx-ingress.yaml new file mode 100644 index 0000000..806a048 --- /dev/null +++ b/exemples/exemples-inicials/nginx-ingress.yaml @@ -0,0 +1,20 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: ingress-web + annotations: + # Per Traefik v2, pots provar d'eliminar-la o deixar-la (sol ignorar-la si no toca). + traefik.ingress.kubernetes.io/router.entrypoints: web +spec: + ingressClassName: traefik # <--- AFEGEIX AIXÒ (si uses k3s/traefik) + rules: + - host: app.local + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: servei-web-intern + port: + number: 80 diff --git a/exemples/pod.yaml b/exemples/exemples-inicials/pod.yaml similarity index 100% rename from exemples/pod.yaml rename to exemples/exemples-inicials/pod.yaml diff --git a/exemples/exemples-inicials/redis-deployment.yaml b/exemples/exemples-inicials/redis-deployment.yaml new file mode 100644 index 0000000..dc1b023 --- /dev/null +++ b/exemples/exemples-inicials/redis-deployment.yaml @@ -0,0 +1,24 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: redis-deployment +spec: + replicas: 9 # <--- Aquí defineixes el nombre de rèpliques + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + containers: + - name: redis + image: redis:alpine + volumeMounts: + - name: emmagatzematge-redis + mountPath: /data + volumes: + - name: emmagatzematge-redis + persistentVolumeClaim: + claimName: redis-dades-pvc # Totes les rèpliques comparteixen aquest PVC diff --git a/exemples/exemples-inicials/redis-pod.yaml b/exemples/exemples-inicials/redis-pod.yaml new file mode 100644 index 0000000..76adfc5 --- /dev/null +++ b/exemples/exemples-inicials/redis-pod.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Pod +metadata: + name: redis-persistent +spec: + containers: + - name: redis + image: redis:alpine + volumeMounts: + - name: emmagatzematge-redis + mountPath: /data + volumes: + - name: emmagatzematge-redis + persistentVolumeClaim: + claimName: redis-dades-pvc diff --git a/exemples/exemples-inicials/redis-pvc.yaml b/exemples/exemples-inicials/redis-pvc.yaml new file mode 100644 index 0000000..1bd8693 --- /dev/null +++ b/exemples/exemples-inicials/redis-pvc.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: redis-dades-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi diff --git a/exemples/exemples-inicials/redis-stateful-replica.yaml b/exemples/exemples-inicials/redis-stateful-replica.yaml new file mode 100644 index 0000000..332b7fa --- /dev/null +++ b/exemples/exemples-inicials/redis-stateful-replica.yaml @@ -0,0 +1,40 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: redis +spec: + serviceName: "redis" + replicas: 3 + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + containers: + - name: redis + image: redis:alpine + command: ["sh", "-c"] + # Aquest script és un exemple molt bàsic: + # Si soc el pod 0, soc el mestre. + # Si no, em connecto al pod 0 (redis-0.redis) com a rèplica. + args: + - | + if [ "$(hostname)" = "redis-0" ]; then + redis-server + else + redis-server --replicaof redis-0.redis 6379 + fi + volumeMounts: + - name: dades + mountPath: /data + volumeClaimTemplates: + - metadata: + name: dades + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 1Gi diff --git a/exemples/exemples-inicials/redis-stateful.yaml b/exemples/exemples-inicials/redis-stateful.yaml new file mode 100644 index 0000000..dad6107 --- /dev/null +++ b/exemples/exemples-inicials/redis-stateful.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: redis-statefulset +spec: + serviceName: "redis" + replicas: 3 # <--- 3 rèpliques, cadascuna amb el seu propi disc + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + containers: + - name: redis + image: redis:alpine + volumeMounts: + - name: emmagatzematge-redis + mountPath: /data + # En lloc de 'volumes', fem servir això per crear un disc per a cada rèplica: + volumeClaimTemplates: + - metadata: + name: emmagatzematge-redis + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 1Gi diff --git a/exemples/service.yaml b/exemples/exemples-inicials/service.yaml similarity index 100% rename from exemples/service.yaml rename to exemples/exemples-inicials/service.yaml diff --git a/exemples/maria-db/mariadb-deployment.yaml b/exemples/maria-db/mariadb-deployment.yaml new file mode 100644 index 0000000..57c3b3c --- /dev/null +++ b/exemples/maria-db/mariadb-deployment.yaml @@ -0,0 +1,29 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mariadb-deployment +spec: + replicas: 3 + selector: + matchLabels: + app: mariadb + template: + metadata: + labels: + app: mariadb + spec: + containers: + - name: mariadb + image: mariadb:lts-ubi + env: + - name: MARIADB_ROOT_PASSWORD + value: "supersecret" # Contrasenya de root + ports: + - containerPort: 3306 + volumeMounts: + - name: mariadb-storage + mountPath: /var/lib/mysql + volumes: + - name: mariadb-storage + persistentVolumeClaim: + claimName: mariadb-pvc diff --git a/exemples/maria-db/mariadb-pvc.yaml b/exemples/maria-db/mariadb-pvc.yaml new file mode 100644 index 0000000..f55c31b --- /dev/null +++ b/exemples/maria-db/mariadb-pvc.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: mariadb-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 2Gi diff --git a/exemples/mariadb-replication/mariadb-replication.yaml b/exemples/mariadb-replication/mariadb-replication.yaml new file mode 100644 index 0000000..59836c8 --- /dev/null +++ b/exemples/mariadb-replication/mariadb-replication.yaml @@ -0,0 +1,157 @@ +# ConfigMap holding information about configuration files for primary/secondary and dockerinit +apiVersion: v1 +kind: ConfigMap +metadata: + name: mariadb-configmap +data: + + primary.cnf: | + [mariadb] + log-bin # enable binary logging + log-basename=my-mariadb # used to be independent of hostname changes (otherwise is in datadir/mysql) + + replica.cnf: | + [mariadb] + log-basename=my-mariadb # used to be independent of hostname changes (otherwise is in datadir/mysql) + + primary.sql: | + CREATE USER 'repluser'@'%' IDENTIFIED BY 'replsecret'; + GRANT REPLICATION REPLICA ON *.* TO 'repluser'@'%'; + CREATE DATABASE primary_db; + + secondary.sql: | + # We have to know name of sts (`mariadb-sts`) and + # service `mariadb-service` in advance as an FQDN. + # No need to use master_port + CHANGE MASTER TO + MASTER_HOST='mariadb-sts-0.mariadb-service.default.svc.cluster.local', + MASTER_USER='repluser', + MASTER_PASSWORD='replsecret', + MASTER_CONNECT_RETRY=10; + +# Secret holds information about root password +--- +apiVersion: v1 +kind: Secret +metadata: + name: mariadb-secret +type: Opaque +data: + mariadb-root-password: c2VjcmV0 # echo -n 'secret'|base64 + +# Headless service +--- +apiVersion: v1 +kind: Service +metadata: + name: mariadb-service + labels: + app: mariadb +spec: + ports: + - port: 3306 + name: mariadb-port + clusterIP: None + selector: + app: mariadb + +# Statefulset +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mariadb-sts +spec: + serviceName: "mariadb-service" + replicas: 3 + selector: + matchLabels: + app: mariadb + template: + metadata: + labels: + app: mariadb + spec: + initContainers: + - name: init-mariadb + image: mariadb + imagePullPolicy: Always + command: + - bash + - "-c" + - | + set -ex + echo 'Starting init-mariadb'; + # Check config map to directory that already exists + # (but must be used as a volume for main container) + ls /mnt/config-map + # Statefulset has sticky identity, number should be last + [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 + ordinal=${BASH_REMATCH[1]} + # Copy appropriate conf.d files from config-map to + # mariadb-config volume (emptyDir) depending on pod number + if [[ $ordinal -eq 0 ]]; then + # This file holds SQL for connecting to primary + cp /mnt/config-map/primary.cnf /etc/mysql/conf.d/server-id.cnf + # Create the users needed for replication on primary on a volume + # initdb (emptyDir) + cp /mnt/config-map/primary.sql /docker-entrypoint-initdb.d + else + # This file holds SQL for connecting to secondary + cp /mnt/config-map/replica.cnf /etc/mysql/conf.d/server-id.cnf + # On replicas use secondary configuration on initdb volume + cp /mnt/config-map/secondary.sql /docker-entrypoint-initdb.d + fi + # Add an offset to avoid reserved server-id=0 value. + 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 + restartPolicy: Always + containers: + - name: mariadb + image: mariadb + ports: + - containerPort: 3306 + name: mariadb-port + env: + # Using Secrets + - name: MARIADB_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: mariadb-secret + key: mariadb-root-password + - name: MYSQL_INITDB_SKIP_TZINFO + value: "1" + # Mount volume from persistent volume claim + 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 + #defaultMode: 0544 + - name: mariadb-config + emptyDir: {} + - name: initdb + emptyDir: {} + + volumeClaimTemplates: + - metadata: + name: datadir + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 300M diff --git a/exemples/practica-final/01-secret.yaml b/exemples/practica-final/01-secret.yaml new file mode 100644 index 0000000..eead3e7 --- /dev/null +++ b/exemples/practica-final/01-secret.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Secret +metadata: + name: credencials-bd +type: Opaque +data: + password: c3VwZXJzZWNyZXQ= # "supersecret" en base64 diff --git a/exemples/practica-final/02-pvcs.yaml b/exemples/practica-final/02-pvcs.yaml new file mode 100644 index 0000000..b068309 --- /dev/null +++ b/exemples/practica-final/02-pvcs.yaml @@ -0,0 +1,21 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: mariadb-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 2Gi +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: wordpress-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 2Gi diff --git a/exemples/practica-final/03-mariadb.yaml b/exemples/practica-final/03-mariadb.yaml new file mode 100644 index 0000000..820fb9c --- /dev/null +++ b/exemples/practica-final/03-mariadb.yaml @@ -0,0 +1,44 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: mariadb-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: mariadb + template: + metadata: + labels: + app: mariadb + spec: + containers: + - name: mariadb + image: mariadb:lts-ubi + env: + - name: MARIADB_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: credencials-bd + key: password + ports: + - containerPort: 3306 + volumeMounts: + - name: dades-bd + mountPath: /var/lib/mysql + volumes: + - name: dades-bd + persistentVolumeClaim: + claimName: mariadb-pvc +--- +apiVersion: v1 +kind: Service +metadata: + name: servei-mariadb +spec: + selector: + app: mariadb + ports: + - protocol: TCP + port: 3306 + targetPort: 3306 diff --git a/exemples/practica-final/04-wordpress.yaml b/exemples/practica-final/04-wordpress.yaml new file mode 100644 index 0000000..d1b0fc8 --- /dev/null +++ b/exemples/practica-final/04-wordpress.yaml @@ -0,0 +1,46 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: wordpress-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: wordpress + template: + metadata: + labels: + app: wordpress + spec: + containers: + - name: wordpress + image: wordpress:latest + env: + - name: WORDPRESS_DB_HOST + value: "servei-mariadb" # Apunta al nom del Service de MariaDB + - name: WORDPRESS_DB_PASSWORD + valueFrom: + secretKeyRef: + name: credencials-bd + key: password + ports: + - containerPort: 80 + volumeMounts: + - name: dades-web + mountPath: /var/www/html + volumes: + - name: dades-web + persistentVolumeClaim: + claimName: wordpress-pvc +--- +apiVersion: v1 +kind: Service +metadata: + name: servei-wordpress +spec: + selector: + app: wordpress + ports: + - protocol: TCP + port: 80 + targetPort: 80 diff --git a/exemples/practica-final/05-ingress.yaml b/exemples/practica-final/05-ingress.yaml new file mode 100644 index 0000000..b194dfb --- /dev/null +++ b/exemples/practica-final/05-ingress.yaml @@ -0,0 +1,16 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: wordpress-ingress +spec: + rules: + - host: elmeublog.local + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: servei-wordpress + port: + number: 80