Kuberneties minikube project(django-todo)

Before we begin with the Project, we need to make sure we have the prerequisites installed:

  1. EC2 ( AMI- Ubuntu, Type- t2.medium )

You can Install all this by doing the below steps one by one. and these steps are for Ubuntu AMI.

Clone the source code

The first step is to clone the source code for the app. You can do this by using this command https://github.com/Hemantjangir53/django-todo-cicd.git

Containerize the Application using Docker

  • Write a Dockerfile with the following code:
FROM python:3
WORKDIR /data
RUN pip install django==3.2  # Python framework 
COPY . .
RUN python manage.py migrate
EXPOSE 8000
CMD ["python","manage.py","runserver","0.0.0.0:8000"]

Building docker-Image

docker build . -t node-todo-cicd

Run docker-Image

docker run -d -p 8000:8000 node-todo-cicd:latest

open port 8000

curl command

Now show that your app. run inside the k8s cluster. curl hit request on terminal, that anything (app) is running or not

curl -L http://<ip_add>:8000

Write pod.yml file

apiVersion: v1
kind: Pod
metadata:
  name: todo-pod
spec:
  containers:
  - name: todo-app
    image: hemantjangir/django-todo-cicd:latest
    ports:
    - containerPort: 8000

kubectl apply -f pod.yml

kubectl get pods

if i want to run this app. inside the minikube cluster then follow some staps:

kubectl get pods -o wide (show ip_ add)

minikube ssh

cuel -L http://<ip_add>:5000

Write Deployment.yml file

Let's Create a Deployment File For our Application. Use the following code for the Deployment.yml file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-deployment
  labels:
    app: todo-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: todo-app
  template:
    metadata:
      labels:
        app: todo-app
    spec:
      containers:
      - name: todo-app
        image: hemantjangir/django-todo-cicd:latest
        ports:
        - containerPort: 8000

kubectl apply -f Deployment.yml

kubectl get deployment

show all details of pods

kubectl get pods -o wide

here we try of run the k8s container then it show the error. because our app. not expose the out side the container.

Write Service.yml file

UsingClusterIP:-

apiVersion: v1
kind: Service
metadata:
  name: todo-service

spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000

UsingNodePort:-

apiVersion: v1
kind: Service
metadata:
  name: todo-service
spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 8000
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007

kubectl apply -f Service.yml

kubectl get svc

apiVersion: v1
kind: Service
metadata:
  name: todo-service

spec:
  type: NodePort
  selector:
    app: todo-app
  ports:
    - port: 80
      targetPort: 8000
      nodePort: 30007

Expose the app

expose our service:

minikube service todo-service --url (todo-service is a service name)

curl -L http://<ip_add_pod>:30007

Here we app is running in the CLI terminal

minikube ip ---> show ip add. of minikube(static)

<minikube_ip>:<30007>

Host ip Allocation

set the hostname inside the dir. sudo vim /etc/hosts

192.168.49.2todo-app.com

curl -Ltodo-app.com:30007

Now application running in the CLI terminal using host name.

Delete All Deployments:

To delete all deployments across all namespaces, you can use:

kubectl delete deployments --all

If you want to delete resources from a specific namespace, you can specify the namespace using the --namespace flag followed by the namespace name. For example:

kubectl delete deployments --all --namespace=<name_space>