From 50ce324cb28530ae20ae3704a630b4f1803109fd Mon Sep 17 00:00:00 2001 From: IntenseWebs Date: Sat, 10 Feb 2024 16:27:30 -0600 Subject: [PATCH] Add Kubernetes, Major Updates to postgres-python3-django --- kubernetes.txt | 13 ++ ...gres-pgadmin-python3-django-gunicorn-nginx | 143 ++++++++++++++++++ ...3-virtualenv-django-gunicorn-nginx-pgadmin | 107 ------------- 3 files changed, 156 insertions(+), 107 deletions(-) create mode 100644 kubernetes.txt create mode 100644 postgres-pgadmin-python3-django-gunicorn-nginx delete mode 100644 python3-virtualenv-django-gunicorn-nginx-pgadmin diff --git a/kubernetes.txt b/kubernetes.txt new file mode 100644 index 0000000..d8bf9cc --- /dev/null +++ b/kubernetes.txt @@ -0,0 +1,13 @@ +https://www.youtube.com/watch?v=yn95Cv7Xr7Q +kind, minicube, k3s + +kind create cluster +kns +kctx +kubectl apply -f app.yaml +kubectl get pods +kns +kubectl get svc +docker ps +docker inspect cd45b1645d41 | grep 172 +docker inspect cd45b1645d41 | grep 192 diff --git a/postgres-pgadmin-python3-django-gunicorn-nginx b/postgres-pgadmin-python3-django-gunicorn-nginx new file mode 100644 index 0000000..c3baa5c --- /dev/null +++ b/postgres-pgadmin-python3-django-gunicorn-nginx @@ -0,0 +1,143 @@ +https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04 +https://tonyteaches.tech/django-nginx-uwsgi-tutorial/ +https://www.datadoghq.com/blog/nginx-502-bad-gateway-errors-gunicorn/#nginx-cant-access-the-socket +OLD Writings (2008): Virtual MSCS Server w/ VMWare ESX 3.5 Server, Microsoft Server Cluster Services (MSCS), SQL Server 2005 Cluster, same physical box,Windows 2008 Server AD Domain Controller VM's, Group Policy, Lowest User Access Privileges Service Accounts for Audit Compliance +sudo apt install python3-pip python3-dev python3-venv libpq-dev postgresql postgresql-contrib nginx curl +sudo apt install virtualenv python3-virtualenv + +su - postgres +psql +# ONLY DROP IF NEEDED TO FIX: DROP DATABASE MYDATABASE; +CREATE USER MYDBUSERNAME WITH PASSWORD 'MYDBPASSWORD' CREATEDB; +CREATE DATABASE MYDATABASE WITH OWNER MYDBUSERNAME; +ALTER ROLE MYDBUSERNAME SET client_encoding TO 'utf8'; +ALTER ROLE MYDBUSERNAME SET default_transaction_isolation TO 'read committed'; +ALTER ROLE MYDBUSERNAME SET timezone TO 'UTC'; +GRANT ALL PRIVILEGES ON DATABASE MYDATABASE TO MYDBUSERNAME; +GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO MYDBUSERNAME; +GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO MYDBUSERNAME; +grant postgres to MYDBUSERNAME +\q + +sudo adduser MYUSERNAME +sudo usermod -aG sudo MYUSERNAME +su - MYUSERNAME + +# sudo ufw allow 5432 +# sudo vi /etc/postgresql/15/main/postgresql.conf +listen_addresses = 'localhost, 192.168.1.3, 192.168.1.6, 192.168.1.7, 192.168.1.66, 192.168.1.123, 192.168.1.126, 192.168.1.127, 192.168.1.222' + +# sudo vi /etc/postgresql/15/main/pg_hba.conf +local MYDATABASE MYDBUSERNAME scram-sha-256 +host MYDATABASE MYDBUSERNAME 192.168.1.0/24 scram-sha-256 + +# TEST DATABASE CONNECTION +psql "postgres://MYDBUSERNAME@192.168.1.6/MYDATABASE" + +mkdir ~/env +python3 -m venv /home/MYUSERNAME/env/MYPROJECTDIR +ls ~/env/MYPROJECTDIR/bin +source ~/env/MYPROJECTDIR/bin/activate +which python +pip install Django gunicorn psycopg2-binary +django-admin startproject iwebcity +cd iwebcity +vi ~/iwebcity/iwebcity/settings.py +ALLOWED_HOSTS = ['.iweb.city', '.localhost', '127.0.0.1', '[::1]', '192.168.1.6'] +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'MYDATABASE', + 'USER': 'MYDBUSERNAME', + 'PASSWORD': 'MYDBPASSWORD', + 'HOST': 'localhost', + 'PORT': '', + } +} +STATIC_URL = '/static/' +import os +STATIC_ROOT = os.path.join(BASE_DIR, 'static/') + +~/iwebcity/manage.py makemigrations +~/iwebcity/manage.py migrate +~/iwebcity/manage.py createsuperuser +~/iwebcity/manage.py collectstatic + +# sudo ufw allow 8001 +# TEST DJANGO PYTHON INCLUDED WEBSERVER ONLY - REBOOT TO APPLY ALL FIREWALL AND POSTGRES CHANGES +~/iwebcity/manage.py runserver 0.0.0.0:8001 +gunicorn --bind 0.0.0.0:8001 iwebcity.wsgi +deactivate +_______________________________________________________________ + +vi /etc/systemd/system/gunicorn.socket +[Unit] +Description=gunicorn socket +[Socket] +ListenStream=/run/gunicorn.sock +[Install] +WantedBy=sockets.target + +vi /etc/systemd/system/gunicorn.service +Unit] +Description=gunicorn daemon +Requires=gunicorn.socket +After=network.target + +[Service] +User=serv +Group=nginx +WorkingDirectory=/home/serv/iwebcity +ExecStart=/home/serv/env/MYPROJECTDIR/bin/gunicorn \ + --access-logfile - \ + --workers 3 \ + --bind unix:/run/gunicorn.sock \ + iwebcity.wsgi:application + +[Install] +WantedBy=multi-user.target +_______________________________________________________________ +sudo systemctl daemon-reload +sudo systemctl restart gunicorn +systemctl start gunicorn.socket +systemctl enable gunicorn.socket +systemctl enable gunicorn.service +systemctl status gunicorn.socket +curl --unix-socket /run/gunicorn.sock localhost +sudo journalctl -u gunicorn +sudo tail -F /var/log/nginx/error.log +file /run/gunicorn.sock +namei -l /run/gunicorn.sock +The following logs may be helpful: + Check the Nginx process logs: sudo journalctl -u nginx + Check the Nginx access logs: sudo less /var/log/nginx/access.log + Check the Nginx error logs: sudo less /var/log/nginx/error.log + Check the Gunicorn application logs: sudo journalctl -u gunicorn + Check the Gunicorn socket logs: sudo journalctl -u gunicorn.socket +_______________________________________________________________ +#NGINX + +vi dj.iweb.city.conf +upstream django { + server unix:///home/iw/iwebcity/iwebcity.sock; +} +# configuration of the server +server { + listen 8001; + server_name django.iweb.city www.django.iweb.city dj.iweb.city; + charset utf-8; + # max upload size + client_max_body_size 75M; + # Django media and static files + location /media { + alias /home/iw/iwebcity/media; + } + location /static { + alias /home/iw/iwebcity/static; + } + # Send all non-media requests to the Django server. + location / { + uwsgi_pass django; + include /home/iw/iwebcity/uwsgi_params; + } +} diff --git a/python3-virtualenv-django-gunicorn-nginx-pgadmin b/python3-virtualenv-django-gunicorn-nginx-pgadmin deleted file mode 100644 index b27cf01..0000000 --- a/python3-virtualenv-django-gunicorn-nginx-pgadmin +++ /dev/null @@ -1,107 +0,0 @@ -https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04 -# sudo apt install python3 python3-pip -# NORMAL USER - INSTALL PYTHON VIRTUAL ENVIRONMENT & OPEN FIREWALL PORTS - -sudo apt-get install python3-venv - -mkdir ~/env -python3 -m venv /home/iw/env/firstenv -ls ~/env/firstenv/bin -source ~/env/firstenv/bin/activate -which python -pip install Django gunicorn -# pip install uwsgi --use-pep517 -django-admin startproject iwebcity -cd iwebcity -~/iwebcity/manage.py makemigrations -python ~/iwebcity/manage.py migrate -~/iwebcity/manage.py createsuperuser - -vi ~/iwebcity/iwebcity/settings.py -ALLOWED_HOSTS = ['.iweb.city', '.localhost', '127.0.0.1', '[::1]', '192.168.1.6'] - - -# TEST DJANGO PYTHON INCLUDED WEBSERVER ONLY -python manage.py runserver 0.0.0.0:8000 - -~/iwebcity/manage.py makemigrations -python ~/iwebcity/manage.py migrate -~/iwebcity/manage.py collectstatic - -cd ~/iwebcity -vi test.py -def application(env, start_response): - start_response('200 OK', [('Content-Type','text/html')]) - return [b"Hello World!"] - -_________________________________________________________________________ - -source ~/env/firstenv/bin/activate -cd ~/iwebcity -# ~/iwebcity/manage.py runserver 0.0.0.0:8001 - -gunicorn --bind 0.0.0.0:8001 iwebcity.wsgi - -vi /etc/systemd/system/gunicorn.socket -[Unit] -Description=gunicorn socket - -[Socket] -ListenStream=/run/gunicorn.sock -SocketUser=www-data - -[Install] -WantedBy=sockets.target - -vi /etc/systemd/system/gunicorn.service -[Unit] -Description=gunicorn daemon -Requires=gunicorn.socket -After=network.target - -[Service] -User=iw -Group=www-data -WorkingDirectory=/home/iw/iwebcity -ExecStart=/home/iw/iwebcity/env/bin/gunicorn \ - --access-logfile - \ - --workers 3 \ - --bind unix:/run/gunicorn.sock \ - iwebcity.wsgi:application - -[Install] -WantedBy=multi-user.target - -systemctl start gunicorn.socket -systemctl enable gunicorn.socket -systemctl status gunicorn.socket -file /run/gunicorn.sock - - -_______________________________________________________________ -#NGINX - -vi dj.iweb.city.conf -upstream django { - server unix:///home/iw/iwebcity/iwebcity.sock; -} -# configuration of the server -server { - listen 8001; - server_name django.iweb.city www.django.iweb.city dj.iweb.city; - charset utf-8; - # max upload size - client_max_body_size 75M; - # Django media and static files - location /media { - alias /home/iw/iwebcity/media; - } - location /static { - alias /home/iw/iwebcity/static; - } - # Send all non-media requests to the Django server. - location / { - uwsgi_pass django; - include /home/iw/iwebcity/uwsgi_params; - } -}