django-admin startproject djangok8spgpg
cd djangok8spg
- 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 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.
docker build -t djangok8spg .
docker images
Stateful set deploy To ensure data persistence, use a persistent volume (PV) and persistent volume claims (PVC).
- Create a ConfigMap
cat <<EOF > postgres-config.yaml
EOF
- Apply config map
kubectl apply -f postgres-config.yaml
A durable volume that will remain even if the pod is deleted and stores data.
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
- Create manifest
cat <<EOF > postgres-pvc-pg-deployment.yaml
EOF
- Apply manifest
kubectl apply -f postgres-pvc-pg-deployment.yaml
- 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
python3 manage.py createsuperuser
after creating the superuser, hit CTRL+D to quit the container and kill it.
K8s cluster on a single host
https://minikube.sigs.k8s.io/docs/start/
choco install minikube
minikube start
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
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 host.domain.com localhost:5000/ host.domain.com
docker tag djangok8spg localhost:5000/djangok8spg
docker push localhost:5000/host.domain.com
docker push localhost:5000/djangok8spg
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
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
- 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
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
# 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
kubectl delete all --all
Package manager for K8s. Packages all manifests "the app" needs into a single item called a chart
a) Metadata
b) Values
c) Templates
Chart.yaml
- Validate the chart is good
helm show all ./chart
choco install kubernetes-helm
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.
kubectl delete -n default configmap postgres-config
kubectl delete -n default deployment postgres-deploy
kubectl delete -n default service db