Skip to content

Fumnanya92/Containerization-and-Container-Orchestration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Containerization-and-Container-Orchestration

Basic Frontend Application with Docker and Kubernetes

1. Hypothetical Use Case

You are developing 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.

2. Tasks

Task 1: Set up your project

  1. Create a new project directory:

    mkdir my-landing-page
    cd my-landing-page
  2. Inside the directory, create an HTML file ('index.html') and a CSS file ('styles.css'):

    • index.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=device-width, initial-scale=1.0">
          <title>Company Landing Page</title>
          <link rel="stylesheet" href="styles.css">
      </head>
      <body>
          <header>
              <h1>Welcome to Our Company</h1>
          </header>
          <main>
              <p>This is our landing page.</p>
          </main>
          <footer>
              <p>&copy; 2024 Our Company</p>
          </footer>
      </body>
      </html>
    • styles.css

      body {
          font-family: Arial, sans-serif;
          margin: 0;
          padding: 0;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          height: 100vh;
      }
      
      header, footer {
          background-color: #333;
          color: white;
          text-align: center;
          padding: 1em 0;
      }
      
      main {
          flex-grow: 1;
          display: flex;
          justify-content: center;
          align-items: center;
      }
      
      p {
          font-size: 1.2em;
      }

Task 2: Initialize Git

  1. Initialize a Git and clone repository in your project directory:
    git init
    git clone 

image

Task 3: Git Commit

  1. Add and commit your initial code to the Git repository:
    git add .
    git commit -m "Initial commit with HTML and CSS files"

image

Task 4: Dockerize the application

  1. Create a Dockerfile specifying Nginx as the base image:
    • Dockerfile
# Use the official NGINX base image
FROM nginx:latest

# Set the working directory in the container
WORKDIR  /usr/share/nginx/html/

# Copy the local HTML file to the NGINX default public directory
COPY landingPage/* /usr/share/nginx/html/

# Expose port 80 to allow external access
EXPOSE 80

# No need for CMD as NGINX image comes with a default CMD to start the server

image

Task 5: Push to Docker Hub

check if docker is installed image

To install docker

apt install docker.io

image

  1. Log in to Docker Hub:

    docker login
  2. Build your Docker image:

    docker build -t dockerfile .

image

  1. Push your Docker image to Docker Hub:
    docker push your-dockerhub-username/frontend:latest

image

Task 6: Set up a Kind Kubernetes Cluster

  1. Instal minikube in Docker: This command refreshes the Debian-based system
sudo apt-get update

Download and Install minikube

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb

image

If that doesn't work use the command below

sudo dpkg -i minikube_latest_amd64.deb

image

Using the docker driver based on user configuration The "docker" driver should not be used with root privileges. exit root user return into working directory cd /home/ubuntu/Landing-page/Containerization-and-Container-Orchestration

Add your user to the docker group:

sudo usermod -aG docker $USER

Apply the new group membership:

newgrp docker

Verify Docker version:

docker version

image

Start Minikube:

minikube start --driver=docker

verify

docker ps -a

image

use the command below to download the kubernetes commandline(kubectl) too to interact with kubernetes cluster

sudo snap install kubectl --classic

to verify

kubectl get nodes

Task 7: Deploy to Kubernetes

  1. Create a Kubernetes Deployment YAML file specifying the image and desired replicas:

    • deployment.yaml

apiVersion: apps/v1 kind: Deployment metadata: name: my-landing-page-deployment spec: replicas: 2 selector: matchLabels: app: my-landing-page template: metadata: labels: app: my-landing-page spec: containers:

  • name: nginx image: fumnanya92/frontend:latest ports:
    • containerPort: 80

image

  1. Apply the deployment to your cluster:
    kubectl apply -f deployment.yaml

Task 8: Create a Service (ClusterIP)

  1. Create a Kubernetes Service YAML file specifying the type as ClusterIP:

    • service.yaml

apiVersion: v1 kind: Service metadata: name: my-landing-page-service spec: selector: app: my-landing-page ports:

  • protocol: TCP port: 80 targetPort: 80 type: NodePort

image

  1. Apply the service to your cluster:
    kubectl apply -f service.yaml

verify Deployment

kubectl get deployments

kubectl get services

image

image

Task 9: Access the Application

  1. Port-forward to the service to access the application locally:

    kubectl port-forward service/my-landing-page-service 8080:80

    docker run -p 8080:80 [dockerfile]

  2. Open your browser and visit http://Host-IP:8080 to view your simple frontend application.

About

Basic Frontend Application with Docker and Kubernetes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published