DEV Community

Bruyo Emuavobor
Bruyo Emuavobor

Posted on

# Containerization and Container Orchestration

Project Review

Develop a microservices-based weather application. The implementation involves creating two microservices; one for fetching weather data and another for diplaying it. The primary objectives include containerizing these microservices using Docker, deploying them to a Kubernetes cluster, and accessing them through Nginx.

Phase 1: Basic Frontend Application with Docker and Kubernetes

Hypothetical Use Case

We are deploying a simple static website (HTML and CSS) for a company's landing page. The goal is to containerize this application using Docker, deploy it to a Kubernetes cluster, and access it through Nginx.

Task

  1. Set up the project
  • Create a new project directory.
'mkdir my-weather-app'
Enter fullscreen mode Exit fullscreen mode

app-dir

  • Inside the directory, create HTML file ('index.html') and a CSS file ('styles.css').
'touch index.html'
Enter fullscreen mode Exit fullscreen mode

index-file

  • Copy and paste the code snippet below.

updated-index-file.JPG)

'touch styles.css'
Enter fullscreen mode Exit fullscreen mode

css-file.JPG

  • Copy and paste the code snippet below.

updated-css-file.JPG

  1. Initialize Git.
  • Initialize a Git repository in the project directory.
'git init'
Enter fullscreen mode Exit fullscreen mode

git-init.JPG

  1. Git Commit.
  • Add and commit the initial code to the Git repository.
'git add .'
Enter fullscreen mode Exit fullscreen mode

add-file.JPG

'git commit -m "my first commit"'
Enter fullscreen mode Exit fullscreen mode

commit-file.JPG

  1. Dockerize the application
  • Create a 'Dockerfile' specfying Nginx as the base image.
'nano Dockerfile'
Enter fullscreen mode Exit fullscreen mode
  • Copy and paste the code snippet below into your Dockerfile.

  • Copy your HTML and CSS files into the Nginx HTML directory.

'# Use official Nginx image
FROM nginx:latest

# Remove default Nginx static files
RUN rm -rf /usr/share/nginx/html/*

# Copy your HTML and CSS files into Nginx directory
COPY index.html /usr/share/nginx/html/
COPY styles.css /usr/share/nginx/html/

# Expose port 80
EXPOSE 80

# Start Nginx (already default, but explicit is fine)
CMD ["nginx", "-g", "daemon off;"]'
Enter fullscreen mode Exit fullscreen mode

dockerfile.JPG

  • Build the Docker Image.
'docker build -t my-weather-app .'
Enter fullscreen mode Exit fullscreen mode

docker-build.JPG

  • Run the container.
'docker run -p 8080:80 my-weather-app'
Enter fullscreen mode Exit fullscreen mode

run-container.JPG

  • Test in browser.
'http://localhost:8080'
Enter fullscreen mode Exit fullscreen mode

test.JPG

  1. Push to Docker Hub
  • Log in to Docker Hub.

docker-hub.JPG

  • Push the Docker image to Docker Hub.

  • Create a repository named "my-weather-app".

create-repo.JPG

repo.JPG

  • Log in to Docker Hub from the terminal.
'docker login'
Enter fullscreen mode Exit fullscreen mode

  • Tag your image correctly.
'docker images'
Enter fullscreen mode Exit fullscreen mode

docker-image.JPG

'docker tag my-weather-app bruyo/my-weather-app:latest'
Enter fullscreen mode Exit fullscreen mode

tag-image.JPG

  • Push the Image.
'docker push bruyo/my-weather-app:latest'
Enter fullscreen mode Exit fullscreen mode

push.JPG

  1. Set up a Kind Kubernetes Cluster
  • Install Kind (Kubernetes in Docker)
'choco install kind'
Enter fullscreen mode Exit fullscreen mode

kind.JPG

  • Verify installation.
'kind version'
Enter fullscreen mode Exit fullscreen mode

version-kind.JPG

  • Ensure that Docker is running, the docker desktop must be running as well.
'docker ps'
Enter fullscreen mode Exit fullscreen mode

docker-run.JPG

  • Create a Kind cluster.

  • Clean old broken cluster

'kind delete cluster'
Enter fullscreen mode Exit fullscreen mode
'docker system prune -f'
Enter fullscreen mode Exit fullscreen mode
'kind create cluster --image kindest/node:v1.29.2'
Enter fullscreen mode Exit fullscreen mode

kind-cluster.JPG

  • Verify Cluster.
'kubectl get nodes'
Enter fullscreen mode Exit fullscreen mode

verify-cluster.JPG

  • Deploy the Application.
'kubectl create deployment nginx --image=nginx'
Enter fullscreen mode Exit fullscreen mode

dp-nginx.JPG

'kubectl get pods'
Enter fullscreen mode Exit fullscreen mode

get-pod.JPG

  • Expose Service.
'kubectl expose deployment nginx --type=NodePort --port=80'
Enter fullscreen mode Exit fullscreen mode

expose-svc.JPG

  • Get Access URL.
'kubectl get svc'
Enter fullscreen mode Exit fullscreen mode

get-service.JPG

'kubectl port-forward service/nginx 8080:80
Enter fullscreen mode Exit fullscreen mode

'

port.JPG

  • Open browser and open application with the link below:
'http://localhost:8080'
Enter fullscreen mode Exit fullscreen mode

test-1.JPG

  1. Deploy to Kubernetes using YAML file
  • Create a Kubernetes Deployment YAML file specifying the image and desired replicas.
'nano nginx-deployment.yaml'
Enter fullscreen mode Exit fullscreen mode
  • Copy and paste the code snippet below into the yaml file.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-weather
  template:
    metadata:
      labels:
        app: my-weather
    spec:
      containers:
      - name: nginx-container
        image: nginx:latest   # Or your Docker Hub image (e.g. bruyo/my-nginx-app:latest)
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

  • Apply the deployment to the cluster.
'kubectl apply -f nginx-deployment.yaml'
Enter fullscreen mode Exit fullscreen mode

dp-created.JPG

  • Verify Deployment.
'kubectl get deployments'
Enter fullscreen mode Exit fullscreen mode

apply-clp.JPG

  1. Create a Service (ClusterIP)
  • Create a Kubernetes service YAML file specifying the type as ClusterIP.
'nano nginx-service.yaml'
Enter fullscreen mode Exit fullscreen mode
  • Copy and paste the code snippet below into the yaml file.
apiVersion: v1
kind: Service
metadata:
  name: my-nginx-service
spec:
  type: ClusterIP
  selector:
    app: my-weather   # MUST match Deployment labels
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Enter fullscreen mode Exit fullscreen mode

svc-yaml.JPG)

  • Apply the service to the cluster.
'kubectl apply -f nginx-service.yaml'
Enter fullscreen mode Exit fullscreen mode

svc-file.JPG

  • Verify Service.
'kubectl get svc'
Enter fullscreen mode Exit fullscreen mode

verify-svc.JPG

  1. Access the Application
  • Port-forward to the service to access the application locally.

'kubectl port-forward service/my-nginx-service 8080:80'

port-forward.JPG

  • Open your browser and visit the specified port to view the application.
'http://localhost:8080'
Enter fullscreen mode Exit fullscreen mode

test-1.JPG

Top comments (0)