DevOps_Zero_To_Hero repository! This project is designed to guide you through the journey of mastering DevOps practices, starting from a simple Python Flask application and progressively moving through various stages. The project begins with running the application locally, then to Docker, Docker Compose, Kubernetes, and then to the cloud.
- Python 3.9 or higher
- pip (Python package installer)
- Docker
-
Clone the repository files to your local machine.
-
Navigate to the project directory.
-
Install the required Python packages using
pip
:pip3 install -r requirements.txt
-
Run the Flask application:
python3 app.py
-
Open your web browser and go to
http://127.0.0.1:5000
. You should see "Hello, Docker with Python!".
-
Make sure you have Docker installed on your machine.
-
Navigate to the project directory where the
Dockerfile
is located. -
Build the Docker image using the following command:
docker build -t zero-to-hero-img .
You can run the Docker container using the built image. There are two options for port mapping:
-
Default Port (5000:5000)
docker run -d --name zero-to-hero-container -p 5000:5000 zero-to-hero-img
-
Custom Port (e.g., 8083:5000)
docker run -d --name zero-to-hero-container -p 8083:5000 zero-to-hero-img
- If you used the default port mapping (
5000:5000
), open your web browser and go tohttp://127.0.0.1:5000
. - If you used a custom port mapping (e.g.,
8083:5000
), open your web browser and go tohttp://127.0.0.1:8083
.
You should see "Hello, Docker with Python!".
To stop the running container, use:
docker stop zero-to-hero-container
To remove the container, use:
docker rm zero-to-hero-container
To remove the docker image, use:
docker rmi zero-to-hero-img
To further streamline the process of building and running the Docker container, you can use the following Ansible playbook playbook.yml
. This playbook automates stopping and removing any existing containers and images, building a new Docker image, tagging and pushing it to Docker Hub, and finally running the container.
-
Ensure you have Ansible installed on your machine.
-
Save the above playbook to a file named
playbook.yml
. -
Run the playbook using the following command:
ansible-playbook playbook.yml
This playbook simplifies the process of managing your Docker container by automating the repetitive tasks, allowing you to focus on development and deployment.
In addition to using Ansible, you can also automate the process of managing your Docker container using shell scripts. Below are two scripts: one for cleaning up existing Docker containers and images, and another for rebuilding and running the Docker container.
The cleanup.sh
script stops and removes any existing Docker containers and images, delete namespace and stop minikube.
The build.sh
script stops and removes any existing Docker containers and images, builds a new Docker image from the Dockerfile, push the docker image to Docker Registry, start minikube, create namespace and deploy the application.
Make sure the scripts are executable by running the following commands:
chmod +x cleanup.sh
chmod +x build.sh
-
Run the cleanup script to remove any existing containers and images:
./cleanup.sh
-
Run the rebuild script to build the Docker image and run the container:
./build.sh
These scripts provide a straightforward and efficient way to manage your Docker containers and images, ensuring that you can quickly clean up and rebuild your application environment as needed.
In this section, we will deploy the DevOps Zero to Hero
application on Minikube using Kubernetes manifest files located in the k8s
directory. The deployment will consist of a Kubernetes Deployment and a Service that exposes the application.
- Minikube installed on your machine.
- kubectl configured to interact with your Minikube cluster.
-
Start Minikube
First, start Minikube if it isn't already running:
minikube start
-
Navigate to the Kubernetes Manifests Directory
Change your directory to the
k8s
folder where the Kubernetes manifests (deployment.yml
andservice.yml
) are stored.cd k8s
-
Create a Namespace
It's a good practice to deploy your resources into a specific namespace. Create a namespace named
zero-to-hero
:kubectl create namespace zero-to-hero
-
Deploy the Application
Apply the deployment manifest to create the Deployment:
kubectl apply -f deployment.yml -n zero-to-hero
This command will deploy two replicas of the
zero-to-hero
application using the Docker imagetriple3a/zero-to-hero:latest
. -
Expose the Application with a Service
Next, apply the service manifest to create a Service that exposes the application on a NodePort:
kubectl apply -f service.yml -n zero-to-hero
The service will expose the application on a NodePort, allowing you to access it through Minikube's IP address.
-
Access the Application
To access the application, first, retrieve the URL using the following command:
minikube service zero-to-hero-service -n zero-to-hero --url
The command will output a URL that you can open in your web browser. This URL will direct you to the
zero-to-hero
application running inside the Minikube cluster.
k9s
is a powerful terminal-based UI to manage your Kubernetes clusters. It simplifies the management of resources within your cluster, including deployments, services, and more.
-
Start k9s
Launch
k9s
by simply typing the following command in your terminal:k9s
-
Switch to the
zero-to-hero
NamespaceBy default,
k9s
shows resources in thedefault
namespace. To switch to thezero-to-hero
namespace, type::zero-to-hero
This will filter the view to only show resources within the
zero-to-hero
namespace.
To scale your zero-to-hero
deployment:
- Navigate to the
Deployments
view by selectingDeployments
from thek9s
menu or by typingdeploy
in the command bar. - Select the
zero-to-hero-deployment
by navigating to it using the arrow keys. - Press
S
to scale the deployment. You will be prompted to enter the desired number of replicas. - Enter the new replica count and press
Enter
.
If you need to update the Docker image used by your deployment:
- Select the
zero-to-hero-deployment
in theDeployments
view. - Press
E
to edit the deployment manifest directly ink9s
. - Navigate to the
spec
section and update theimage
field to the new Docker image version. - Save and exit the editor.
k9s
will automatically apply the changes to your cluster.
To delete the zero-to-hero
deployment and service:
-
Navigate to the
Deployments
view and selectzero-to-hero-deployment
. -
Press
D
to delete the deployment. -
Confirm the deletion when prompted.
Repeat the process for the
Services
view to delete thezero-to-hero-service
.Alternatively, you can delete all resources in the
zero-to-hero
namespace by selecting the namespace ink9s
and pressingctrl+d
.