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>
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-image
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>
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>
- 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>
- 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.yaml
3. Verify that the deployment is created:
kubectl get deployments
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.yaml
6. Verify that the service is created:
kubectl get services
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"}}'
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:
- How to become a devops freelancer - July 15, 2023
- DevOps Support Services Market in India - July 15, 2023
- 5 Key Considerations Before Embarking on An App Development Project - July 14, 2023