Exemples del curs

This commit is contained in:
Linux User
2026-03-05 10:54:12 +00:00
parent d8780cbe2b
commit 9a9ab01dcf
20 changed files with 520 additions and 0 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: redis-dades-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mariadb-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi

View File

@@ -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

View File

@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: credencials-bd
type: Opaque
data:
password: c3VwZXJzZWNyZXQ= # "supersecret" en base64

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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