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
- Set up the project
- Create a new project directory.
'mkdir my-weather-app'
- Inside the directory, create HTML file ('index.html') and a CSS file ('styles.css').
'touch index.html'
- Copy and paste the code snippet below.
'touch styles.css'
- Copy and paste the code snippet below.
- Initialize Git.
- Initialize a Git repository in the project directory.
'git init'
- Git Commit.
- Add and commit the initial code to the Git repository.
'git add .'
'git commit -m "my first commit"'
- Dockerize the application
- Create a 'Dockerfile' specfying Nginx as the base image.
'nano Dockerfile'
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;"]'
- Build the Docker Image.
'docker build -t my-weather-app .'
- Run the container.
'docker run -p 8080:80 my-weather-app'
- Test in browser.
'http://localhost:8080'
- Push to Docker Hub
- Log in to Docker Hub.
Push the Docker image to Docker Hub.
Create a repository named "my-weather-app".
- Log in to Docker Hub from the terminal.
'docker login'
- Tag your image correctly.
'docker images'
'docker tag my-weather-app bruyo/my-weather-app:latest'
- Push the Image.
'docker push bruyo/my-weather-app:latest'
- Set up a Kind Kubernetes Cluster
- Install Kind (Kubernetes in Docker)
'choco install kind'
- Verify installation.
'kind version'
- Ensure that Docker is running, the docker desktop must be running as well.
'docker ps'
Create a Kind cluster.
Clean old broken cluster
'kind delete cluster'
'docker system prune -f'
'kind create cluster --image kindest/node:v1.29.2'
- Verify Cluster.
'kubectl get nodes'
- Deploy the Application.
'kubectl create deployment nginx --image=nginx'
'kubectl get pods'
- Expose Service.
'kubectl expose deployment nginx --type=NodePort --port=80'
- Get Access URL.
'kubectl get svc'
'kubectl port-forward service/nginx 8080:80
'
- Open browser and open application with the link below:
'http://localhost:8080'
- Deploy to Kubernetes using YAML file
- Create a Kubernetes Deployment YAML file specifying the image and desired replicas.
'nano nginx-deployment.yaml'
- 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
- Apply the deployment to the cluster.
'kubectl apply -f nginx-deployment.yaml'
- Verify Deployment.
'kubectl get deployments'
- Create a Service (ClusterIP)
- Create a Kubernetes service YAML file specifying the type as ClusterIP.
'nano nginx-service.yaml'
- 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
- Apply the service to the cluster.
'kubectl apply -f nginx-service.yaml'
- Verify Service.
'kubectl get svc'
- Access the Application
- Port-forward to the service to access the application locally.
'kubectl port-forward service/my-nginx-service 8080:80'
- Open your browser and visit the specified port to view the application.
'http://localhost:8080'
Top comments (0)