Prechádzať zdrojové kódy

Intentado que funcione en Openshift

Celestino Rey 1 rok pred
rodič
commit
ed56c4f5f4

+ 7 - 0
Dockerfile.k8s

@@ -48,6 +48,13 @@ COPY --from=builder /app/requirements.txt .
 RUN pip install --upgrade pip
 RUN pip install --no-cache /wheels/*
 
+# copia fichero de configuración de nginx
+
+COPY ./nginx/nginx.conf /etc/nginx/
+
+# enlace los logs de nginx a stdout
+RUN ln -sf /dev/stdout /var/log/nginx/access.log && ln -sf /dev/stderr /var/log/nginx/error.log
+
 # copy entrypoint.sh
 COPY ./entrypoint.sh .
 

+ 57 - 20
K8S/Makefile.OC

@@ -1,10 +1,25 @@
+export ARQUITECTURA := $(shell lscpu |grep itectur | tr -d ' '| cut -f2 -d':')
 export REGISTRY=registry.reymota.es
 
-export IMG_VERSION = 0.70.42
-export IMG_NGINX_VERSION = 2.3
+export IMG_VERSION = 0.90.20
 
 # limpia todo
-all: imagen clean install
+all: ayuda
+
+todo: imagen clean install
+
+ayuda:
+	@echo  "make imagen -> crea la imagen del contenedor con la aplicación y la sube al registry"
+	@echo  "make ns -> crea el namespace"
+	@echo  "make app -> crea el deployment de la aplicación"
+	@echo  "make borraapp -> borra el deployment de la aplicación"
+	@echo  "make claims -> crea las pvc"
+	@echo  "make borraclaims -> crea las pvc ¡ojo! lo que haya en los volumenes se pierde"
+	@echo  "make volumes -> crea los pv en entornos donde no hay creación dinámica de los volumenes"
+	@echo  "make borravolumes -> borra los pv en entornos donde no hay creación dinámica de los volumenes"
+	@echo  "make install -> crea los confimaps, servicios y deployments de todo"
+	@echo  "make clean -> borra los confimaps, servicios y deployments de todo"
+	@echo  "make todo -> crea todo"
 
 imagen:
 	cd ../; make
@@ -12,18 +27,11 @@ imagen:
 ns:
 	-oc create -f namespace.yaml
 
-borrans:
-	-oc delete -f namespace.yaml
-
-claims:
-	-oc create -f pvc-jugaralpadel.yaml
-	-oc create -f pvc-static.yaml
-	-oc create -f pvc-postgresql.yaml
+app:
+	-envsubst < jugaralpadel-deployment.yaml |oc create -f -
 
-borraclaims:
-	-oc delete -f pvc-jugaralpadel.yaml
-	-oc delete -f pvc-static.yaml
-	-oc delete -f pvc-postgresql.yaml
+borraapp:
+	-envsubst < jugaralpadel-deployment.yaml |oc delete -f -
 
 install:
 	-oc create -f reg-secret.yaml
@@ -34,14 +42,25 @@ install:
 	-oc create -f db-service.yaml
 
 	-envsubst < jugaralpadel-deployment.yaml |oc create -f -
-	-envsubst < nginx-deployment.yaml |oc create -f -
-	-oc create -f nginx-service.yaml
-	-oc create -f jugaralpadel-ingress.yaml
+	-oc expose service/jugaralpadel
 
+claims:
+	-oc create -f pvc-jugaralpadel.yaml
+	-oc create -f pvc-static.yaml
+	-oc create -f pvc-postgresql.yaml
+
+borraclaims:
+	-oc delete -f pvc-jugaralpadel.yaml
+	-oc delete -f pvc-static.yaml
+	-oc delete -f pvc-postgresql.yaml
+
+volumes:
+	-oc create -f pv-local-jugaralpadel.yaml
+
+borravolumes:
+	-oc delete -f pv-local-jugaralpadel.yaml
 
 clean:
-	-envsubst < nginx-deployment.yaml |oc delete -f -
-	-oc delete -f nginx-service.yaml
 	-envsubst < jugaralpadel-deployment.yaml |oc delete -f -
 
 	-oc delete -f db-deployment.yaml
@@ -51,5 +70,23 @@ clean:
 	-oc delete -f env-prod-db-configmap.yaml 
 
 	-oc delete -f reg-secret.yaml
-	-oc delete -f namespace.yaml
+
+verimg:
+	docker run -it  ${REGISTRY}/jugaralpadel-${ARQUITECTURA}:${IMG_VERSION} bash
+
+backup:
+	oc --kubeconfig /home/creylopez/.kube/config -n jugaralpadel exec -ti deployment.apps/db -- /usr/lib/postgresql/15/bin/pg_dump --username=creylopez --dbname=jugaralpadel > jugaralpadel-$(IMG_VERSION).sql
+   
+fleet:
+	-cp namespace.yaml ../Fleet/15-namespace.yaml
+	-cp reg-secret.yaml ../Fleet/20-reg-secret.yaml
+	-cp env-prod-configmap.yaml  ../Fleet/25-env-prod-configmap.yaml
+	-cp env-prod-db-configmap.yaml  ../Fleet/30-env-prod-db-configmap.yaml
+	-cp pvc-jugaralpadel.yaml ../Fleet/40-pvc-jugaralpadel.yaml
+	-cp pvc-static.yaml ../Fleet/45-pvc-static.yaml
+	-cp pvc-postgresql.yaml ../Fleet/50-pvc-postgresql.yaml
+	-cp db-deployment.yaml ../Fleet/55-db-deployment.yaml
+	-cp db-service.yaml ../Fleet/60-db-service.yaml
+	-envsubst < jugaralpadel-deployment.yaml > ../Fleet/70-jugaralpadel-deployment.yaml
+	-cp jugaralpadel-ingress.yaml ../Fleet/80-jugaralpadel-ingress.yaml
 

+ 2 - 0
K8S/creaCredencialesRegistry.sh

@@ -0,0 +1,2 @@
+# Añadir lo que salga a la directiva .dockerconfigjson en el fichero de secretos reg-secret.yaml
+cat /home/kubeadmin/.docker/config.json |base64 -w 0

+ 1 - 1
K8S/jugaralpadel-deployment.yaml

@@ -37,7 +37,7 @@ spec:
           - --bind
           - 0.0.0.0:8000
         name: jugaralpadel
-        image: $REGISTRY/jugaralpadel-$ARQUITECTURA:$IMG_VERSION
+        image: $REGISTRY/jugaralpadel:$IMG_VERSION
         env:
         - name: IMG_VERSION
           value: "$IMG_VERSION"

+ 3 - 0
K8S/pvc-jugaralpadel.yaml

@@ -12,6 +12,7 @@ spec:
   resources:
     requests:
       storage: 100Mi
+  storageClassName: lvms-vg1
 status: {}
 ---
 apiVersion: v1
@@ -28,6 +29,7 @@ spec:
   resources:
     requests:
       storage: 50Mi
+  storageClassName: lvms-vg1
 status: {}
 ---
 apiVersion: v1
@@ -44,4 +46,5 @@ spec:
   resources:
     requests:
       storage: 53Mi
+  storageClassName: lvms-vg1
 status: {}

+ 1 - 0
K8S/pvc-postgresql.yaml

@@ -11,3 +11,4 @@ spec:
   resources:
     requests:
       storage: 150Mi
+  storageClassName: lvms-vg1

+ 1 - 0
K8S/pvc-static.yaml

@@ -11,3 +11,4 @@ spec:
   resources:
     requests:
       storage: 70Mi
+  storageClassName: lvms-vg1

+ 3 - 3
Makefile

@@ -1,8 +1,8 @@
 install:
 
-	echo "Creando imagen con version '${IMG_VERSION}' para la arquitectura '${ARQUITECTURA}' en el registry '${REGISTRY}'"
+	echo "Creando imagen con version '${IMG_VERSION}' en el registry '${REGISTRY}'"
 
-	docker build --no-cache -t ${REGISTRY}/jugaralpadel-${ARQUITECTURA}:${IMG_VERSION} -f Dockerfile.k8s .
-	docker push ${REGISTRY}/jugaralpadel-${ARQUITECTURA}:${IMG_VERSION}
+	docker build --no-cache -t ${REGISTRY}/jugaralpadel:${IMG_VERSION} -f Dockerfile.k8s .
+	docker push ${REGISTRY}/jugaralpadel:${IMG_VERSION}
 
 

+ 3 - 5
entrypoint.sh

@@ -1,7 +1,5 @@
 #!/bin/bash
 
-# Este es para K8S
-
 if [ "$DATABASE" = "postgres" ]
 then
     echo "Waiting for postgres..."
@@ -16,7 +14,7 @@ else
 fi
 
 python manage.py collectstatic --noinput
-#python manage.py flush --no-input
-#python manage.py migrate
 
-exec "$@"
+nginx -g 'daemon off;' &
+
+python manage.py runserver 0.0.0.0:8000 --noreload

+ 0 - 32
nginx/default.conf

@@ -1,32 +0,0 @@
-upstream django_project {
-    server localhost:8000;
-}
-
-error_log /var/log/nginx/error.log;
-
-server {
-    listen       8080;
-
-    access_log /var/log/nginx/access.log;
-
-    location / {
-        proxy_pass http://django_project;
-        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
-        proxy_set_header Host $host;
-        proxy_redirect off;
-        client_max_body_size 100M;
-    }
-
-    location /static/ {
-        alias /app/staticfiles/;
-    }
-
-    location /media/ {
-        alias /app/mediafiles/;
-    }
-
-    error_page   500 502 503 504  /50x.html;
-    location = /50x.html {
-        root   /usr/share/nginx/html;
-    }
-}

+ 45 - 0
nginx/nginx.conf

@@ -0,0 +1,45 @@
+error_log  /var/log/nginx/error.log notice;
+
+events {
+    worker_connections 1024;
+}
+
+http {
+    include       /etc/nginx/mime.types;
+    default_type  application/octet-stream;
+
+    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
+                      '$status $body_bytes_sent "$http_referer" '
+                      '"$http_user_agent" "$http_x_forwarded_for"';
+
+    access_log   /var/log/nginx/access.log main;
+
+    sendfile        on;
+    #tcp_nopush     on;
+
+    keepalive_timeout  65;
+
+    #gzip  on;
+
+    server {
+
+        listen 80;
+
+        location / {
+            proxy_pass http://jugaralpadel:8000;
+            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+            proxy_set_header Host $http_host;
+            proxy_redirect off;
+            client_max_body_size 100M;
+        }
+
+        location /static/ {
+            alias /app/src/staticfiles/;
+        }
+
+        location /media/ {
+            alias /app/src/mediafiles/;
+        }
+
+    }
+}

+ 0 - 25
src/config/entrypoint.sh

@@ -1,25 +0,0 @@
-#!/bin/bash
-RUN_PORT="8000"
-DATABASE=postgres
-SQL_HOST=postgresql
-SQL_PORT=5432
-
-if [ "$DATABASE" = "postgres" ]
-then
-    echo "Waiting for postgres..."
-
-    while ! nc -z $SQL_HOST $SQL_PORT; do
-      sleep 0.1
-    done
-
-    echo "PostgreSQL started"
-
-	/opt/venv/bin/python manage.py migrate --no-input
-	/opt/venv/bin/python manage.py collectstatic --no-input
-
-	/opt/venv/bin/gunicorn gestion_reservas.wsgi:application --bind "0.0.0.0:${RUN_PORT}" --daemon
-
-	nginx -g 'daemon off;'
-else
-    echo "la base de datos no es postgres: '$DATABASE'"
-fi

+ 7 - 6
src/eventos/views.py

@@ -52,12 +52,12 @@ def reservar_evento(request, evento_id):
         cc_email = [settings.ADMIN_EMAIL]
 
         texto = f'Hola {request.user.nombre}, \n\n' \
-                    f'Te has inscrito correctamente en el evento "{evento.nombre}".\n' \
-                    f'Detalles del evento:\n' \
-                    f'Nombre: {evento.nombre}\n' \
-                    f'Descripción: {evento.descripcion}\n' \
-                    f'Fecha: {evento.fecha.strftime("%d/%b/%y")}  a las {evento.hora}\n' \
-                    f'\n¡Gracias por inscribirte!\n\n'
+                f'Te has inscrito correctamente en el evento "{evento.nombre}".\n' \
+                f'Detalles del evento:\n' \
+                f'Nombre: {evento.nombre}\n' \
+                f'Descripción: {evento.descripcion}\n' \
+                f'Fecha: {evento.fecha.strftime("%d/%b/%y")}  a las {evento.hora}\n' \
+                f'\n¡Gracias por inscribirte!\n\n'
 
         html_content = render_to_string('emails/confirmacion_reserva.html', {'evento': evento, 'usuario': request.user})
 
@@ -390,6 +390,7 @@ def estadisticas_por_usuario(request):
         'desde': filtro_fecha,
     })
 
+
 @login_required
 def eventos_por_usuario(request, usuario_id):
     usuario = get_object_or_404(User, pk=usuario_id)

+ 2 - 2
src/gestion_reservas/settings.py

@@ -15,7 +15,7 @@ import os
 import logging
 
 APP_NAME = "jugaralpadel.es"
-APP_VERSION = os.environ.get("APP_VERSION", "15.0.3")  # Valor por defecto si no está definido
+APP_VERSION = os.environ.get("APP_VERSION", "15.0.5")  # Valor por defecto si no está definido
 IMG_VERSION = os.environ.get("IMG_VERSION", "0.0.0-dev")  # Valor por defecto si no está definido
 
 MES_COMIENZO_TEMPORADA = int(os.getenv('MES_COMIENZO_TEMPORADA', '9'))  # valor por defecto: septiembre
@@ -153,7 +153,7 @@ THOUSAND_SEPARATOR = '.'
 
 STATIC_URL = 'static/'
 STATIC_ROOT = BASE_DIR / "staticfiles"
-STATICFILES_DIRS = [BASE_DIR / "eventos/static",]
+# STATICFILES_DIRS = [BASE_DIR / "eventos/static",]
 
 # Mediafiles
 MEDIA_ROOT = BASE_DIR / "mediafiles"