🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker and Kubernetes Demo Project

Here’s a step-by-step guide to creating a simple project with a web server and app server using Docker and Kubernetes on Ubuntu, ensuring that the Docker image is accessible from Kubernetes without any errors:

Step 1

1. Create a new directory for your project:

mkdir web-app-project
cd web-app-project



2. Create a file named Dockerfile and open it in a text editor:

Vi Dockerfile
FROM nginx:latest

COPY index.html /usr/share/nginx/html/index.html

3. Create an index.html file in the same directory and add your desired HTML content.

<!DOCTYPE html>
<html>
<head>
    <title>Web Server</title>
</head>
<body>
    <h1>Welcome to the Web Server!</h1>
</body>
</html>
Code language: HTML, XML (xml)

4. Build the Docker image:

docker build -t web-server-image .



Step 2:

Test the Docker Image

1. Run a Docker container using the newly created image:

docker run -d -p 8080:80 web-server-imageCode language: CSS (css)

2. Access the web server by opening your web browser and visiting http://localhost:8080. You should see the web server content you defined in the index.html file.

3. Stop the Docker container:

docker stop <container-id>Code language: HTML, XML (xml)

To push an image to Docker Hub registry, you can follow these steps:

  • Tag the image with the Docker Hub repository name: Before pushing the image, you need to tag it with the Docker Hub repository name. The repository name should follow the format: <username>/<repository-name>
docker tag <image-name> <username>/<repository-name>:<tag>Code language: HTML, XML (xml)
  • Replace <image-name> with the name of the image you built, <username> with your Docker Hub username, <repository-name> with the desired name for your repository, and <tag> with the version or tag you want to assign to the image.
  • Log in to Docker Hub: Authenticate yourself with your Docker Hub account to gain access to your repositories.
docker login
  • Push the image to Docker Hub: Once you’re logged in, you can push the tagged image to Docker Hub.
docker push <username>/<repository-name>:<tag>Code language: HTML, XML (xml)
  • Replace <username>/<repository-name>:<tag> with the same repository name and tag you used in the previous step.
  • The Docker image will be pushed to Docker Hub, and you’ll see progress information during the push process.
  • Verify the pushed image: Go to the Docker Hub website (https://hub.docker.com/) and log in with your Docker Hub account. Navigate to your repository, and you should see the pushed image listed with the specified tag.

Examples

docker tag app-server jami12345/project_1:app-server
docker push jami12345/project_1:app-server

or

During Build Use below steps:

docker build -t jami12345/guestbook-app:latest .
docker push jami12345/guestbook-app:latest

Step 3:

Set Up Kubernetes Cluster

Install and set up a Kubernetes cluster on your Ubuntu machine. You can use Minikube for a local cluster setup or set up a cluster on a cloud provider like AWS, Azure, or GCP. Follow the respective documentation for your chosen method.

Step 4:

Deploy the Web Server and App Server

1. Create a file named web-server-deployment.yaml and define the Kubernetes Deployment for the web server:

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server-deployment
spec:
replicas: 1
selector:
matchLabels:
app: web-server
template:
metadata:
labels:
app: web-server
spec:
containers:
- name: web-server
image: jami12345/project_1:web-server 
ports:
- containerPort: 80



2. Apply the Kubernetes deployment configuration to deploy the web server:

kubectl apply -f web-server-deployment.yamlCode language: CSS (css)

3. Verify that the deployment is created:

kubectl get deploymentsCode language: JavaScript (javascript)

4. Create a file named web-server-service.yaml and define the Kubernetes Service for the web server:

apiVersion: v1
kind: Service
metadata:
name: web-server-service
spec:
selector:
app: web-server
ports:
- protocol: TCP
port: 80
targetPort: 80
type: Nodeport

5. Apply the Kubernetes service configuration to expose the web server:

kubectl apply -f web-server-service.yamlCode language: CSS (css)

6. Verify that the service is created:

kubectl get servicesCode language: JavaScript (javascript)

7. Access the web server:

Look for the service entry and note the assigned PORT number. You can access the Guestbook application by visiting http://<NodeIP>:<PORT> in your web browser.

Note: – To change service type to Node port

kubectl patch service app-server-service -p '{"spec": {"type": "NodePort"}}'Code language: JavaScript (javascript)

or

Step 8:

Access the application

To access the application, you can use port forwarding. Run the following command:

kubectl port-forward service/web-app-service 8080:80

To access the application, you can use port forwarding. Run the following command:

Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x