Coder Social home page Coder Social logo

djangok8spg's Introduction

Django

Create project

django-admin startproject djangok8spgpg

cd djangok8spg

Change database connection settings before building the docker image

  • edit settings.py:
    • add "import os" at the top file with the other imports
    • replace the "DATABASES" config block with this block
DATABASES = {
   'default': {
      'ENGINE': 'django.db.backends.postgresql',
      'NAME': os.getenv('DATABASE_NAME'),
      'USER': os.getenv('DATABASE_USER'),
      'PASSWORD': os.getenv('DATABASE_PASSWORD'),
      'HOST': os.getenv('DATABASE_HOST'),
      'PORT': '5432',
   }
}

Create dockerfile

  • create dockerfile with this content
  • exposes that port 8000 will be used to accept incoming container connections, and runs gunicorn with 3 workers and listening on port 8000.

Build image:

docker build -t djangok8spg .
docker images

Deploying Postgres via ConfigMap with a PersistentVolume

Stateful set deploy To ensure data persistence, use a persistent volume (PV) and persistent volume claims (PVC).

Apply ConfigMap

  • Create a ConfigMap
cat <<EOF > postgres-config.yaml


EOF
  • Apply config map
kubectl apply -f postgres-config.yaml

Create and Apply Persistent Storage Volume and Persistent Volume Claim

Persistent volume (PV)

A durable volume that will remain even if the pod is deleted and stores data.

persistent volume claim (PVC)

How users request and consume PV resources with parameters such as size of your storage disk, access modes, and storage class.

  • Create manifest
cat <<EOF > postgres-pvc-pv-pg.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv-volume  # Sets PV's name
  labels:
    type: local  # Sets PV's type to local
    app: django-pg-pvc
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi # Sets PV Volume
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim  # Sets name of PVC
  labels:
    app: django-pg-pvc
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany  # Sets read and write access
  resources:
    requests:
      storage: 5Gi  # Sets volume size
EOF
  • Apply manifest
kubectl apply -f postgres-pvc-pv-pg.yaml

Postgress deployment

  • Create manifest
cat <<EOF > postgres-pvc-pg-deployment.yaml


EOF
  • Apply manifest
kubectl apply -f postgres-pvc-pg-deployment.yaml

Postgress Service

  • Create manifest
cat <<EOF > postgres-pvc-pg-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: db
  labels:
    name: postgres-service
    app: django-pg-pvc
spec:
  type: NodePort  
  ports:
  - port: 5432
    targetPort: 5432
  selector:
    name: postgres-pod
    app: django-pg-pvc

EOF
  • Apply manifest
kubectl apply -f postgres-pvc-pg-service.yaml

Creating the Database Schema

python3 manage.py createsuperuser

after creating the superuser, hit CTRL+D to quit the container and kill it.

Minikube

K8s cluster on a single host

Install and deploy

https://minikube.sigs.k8s.io/docs/start/

Install

choco install minikube

Start

minikube start

Deploy Docker Registry

Enable Add On

minikube addons enable registry

https://minikube.sigs.k8s.io/docs/drivers/docker

  • confirm
kubectl get service --namespace kube-system

kubectl port-forward --namespace kube-system service/registry 5000:80

Run docker registry

docker run --rm -it --network=host alpine ash -c "apk add socat && socat TCP-LISTEN:5000,reuseaddr,fork TCP:host.docker.internal:5000"

Docker Tag

docker tag host.domain.com localhost:5000/ host.domain.com
docker tag djangok8spg localhost:5000/djangok8spg

Docker Push Image to Registry

docker push localhost:5000/host.domain.com
docker push localhost:5000/djangok8spg

Test to see if registry is running

curl --location http://localhost:5000/v2
curl http://localhost:5000/v2/_catalog
  • After the image is pushed, refer to it by localhost:5000/{name} in kubectl specs With image available to Kubernetes on local Docker registry

Deploy Django




  • Deploy
kubectl apply -f django-pvc-pg-deployment.yaml
  • Verify
kubectl get deploy django-pg-pvc

# NAME         READY   UP-TO-DATE   AVAILABLE   AGE
# django-app   3/3     3            3           12s
  • use the selector: app and the get the pods associated with that value
kubectl get pods -l app=django-pg-pvc

K8s Services

  • Deploy the service "django-pvc-pg-service.yaml"
kubectl apply -f django-pvc-pg-service.yaml
  • use the selector: "app" and the get all things associated with that value
kubectl get all -l app=django-pg-pvc
  • setup port forwarding
kubectl port-forward service/django-service 8000:8000

K8s Ingress

A reverse proxy that enables external access into other Kubernetes resources.

  • Install ingress controller
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/do/deploy.yaml
  • Confirm the pods have started:
kubectl get pods -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx --watch
  • Confirm that the Load Balancer was successfully created
kubectl get svc --namespace=ingress-nginx
  • Unless deploying to DNS use /etc/hosts
vim /etc/hosts
# add an entry for our domain name
127.0.0.1 localhost
  • Deploy ingress
kubectl apply -f ingress.yaml

https://github.com/kubernetes/ingress-nginx

Once built - Run these on subsequent starts

# port forwarding for docker registry to work 
kubectl port-forward --namespace kube-system service/registry 5000:80
# port forwarding for django deployment/service/ http://localhost:8000/ to work 
kubectl port-forward service/django-service 8000:8000
# to start the minikube dashboard in browser
minikube dashboard

command history

kubectl get service --namespace kube-system
curl http://localhost:5000/v2/_catalog
kubectl get deploy django-app
kubectl get pods -l app=django
kubectl get all -l app=django

K8s clean up

kubectl delete all --all

Helm

Package manager for K8s. Packages all manifests "the app" needs into a single item called a chart

Consists of 3 key parts:

a) Metadata

b) Values

c) Templates

Metadata

Chart.yaml

  • Validate the chart is good
helm show all ./chart

Values

Templates

Install Helm

choco install kubernetes-helm

Helm Upgrade

Instead of using install and uninstall use upgrade.

It versions all of your installs on top of each other. It knows when stuff has changed, to install the changes. If you've modified things, to modify in place

helm upgrade --atomic --install $HELM_CHART_NAME $CHART_LOCATION

#Eg
helm upgrade --atomic --install host-website ./chart

--atomic

  • Deploy all resources as a single unit and if it doesnt work it rolls it back.

Delete

kubectl delete -n default configmap postgres-config
kubectl delete -n default deployment postgres-deploy
kubectl delete -n default service db

djangok8spg's People

Watchers

Jay Lavine avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.