This repository contains the code and configuration for my graduation project from the ITI (Information Technology Institute). The project focuses on setting up a Kubernetes-based deployment pipeline using Jenkins, Minikube, Terraform, Docker, and Kubernetes.
The goal of this project is to automate the deployment of a Node.js application and MySQL database on a local Kubernetes cluster. The deployment pipeline is orchestrated using Jenkins, with the infrastructure provisioned using Terraform. Here's an overview of the project's main components and tasks:
-
Setting up the Kubernetes Cluster:
- Utilized Ansible to install and configure Minikube, providing a local Kubernetes instance.
- Configured two namespaces: "tools" and "dev" using Terraform, each serving a specific purpose in the pipeline.
-
Namespace and Pod Configuration:
- Installed Jenkins and Nexus pods in the "tools" namespace using Terraform. Jenkins serves as the automation engine, while Nexus acts as the container registry.
- Configured the "dev" namespace to run two pods: one for the Node.js application and another for the MySQL database.
-
Jenkins Pipeline Job:
- Created a Jenkins pipeline job responsible for automating the deployment process.
- The pipeline performs the following steps:
- Checks out the source code from the provided GitHub repository: https://github.com/mahmoud254/jenkins_nodejs_example.git.
- Builds the Node.js application using the provided Dockerfile.
- Creates a Docker image for the application.
- Uploads the Docker image to the Nexus repository for storage.
-
Docker Container Deployment:
- Developed another Jenkins pipeline job to deploy the Docker container on the desired environment.
- The pipeline retrieves the Docker image from Nexus and deploys it to the specified environment on Minikube.
-
Configuration Management with Secrets:
- Implemented secure handling of microservice configurations using Kubernetes secrets.
- Stored sensitive configuration information in secrets and updated the microservice pods to access the configurations securely.
The first step of the project includes an Ansible playbook to automate the installation and configuration of Minikube, allowing you to set up a local Kubernetes cluster effortlessly. The playbook performs the following steps:
-
Update Apt Cache:
- The playbook updates the apt cache on the target system using the
apt
module. This ensures that the system has the latest package information.
- The playbook updates the apt cache on the target system using the
-
Install Dependencies:
- Necessary dependencies, such as
apt-transport-https
,ca-certificates
,curl
, andsoftware-properties-common
, are installed using theapt
module. These packages are required for subsequent steps.
- Necessary dependencies, such as
-
Add Docker GPG Key:
- The playbook uses the
apt_key
module to add the GPG key for the Docker repository. This key is necessary to authenticate and verify the packages during installation.
- The playbook uses the
-
Add Docker Repository:
- The
apt_repository
module is used to add the Docker repository to the system's package sources. This step ensures that Docker can be installed from the official Docker repository.
- The
-
Install Docker:
- The playbook installs Docker using the
apt
module. The packagedocker-ce
is installed to set up the Docker engine on the target system.
- The playbook installs Docker using the
-
Install Minikube Dependencies:
- Required dependencies for Minikube, such as
conntrack
,ebtables
, andsocat
, are installed using theapt
module. These dependencies are necessary for Minikube to function properly.
- Required dependencies for Minikube, such as
-
Download Minikube:
- The playbook uses the
get_url
module to download the latest Minikube binary for Linux from the official Google Cloud Storage. The downloaded binary is saved as/usr/local/bin/minikube
and given executable permissions (0755
).
- The playbook uses the
-
Start Minikube Cluster:
- The playbook starts the Minikube cluster using the
command
module. The commandminikube start --driver=docker --force
starts Minikube with the Docker driver, ensuring a consistent and reliable Kubernetes environment.
- The playbook starts the Minikube cluster using the
-
Set KUBECONFIG Environment Variable:
- The playbook uses the
lineinfile
module to add theKUBECONFIG
environment variable to the user's.bashrc
file. This environment variable points to the Kubernetes configuration file ($HOME/.kube/config
), allowing easy interaction with the Minikube cluster.
- The playbook uses the
Please ensure that the target system meets the necessary requirements and has Ansible installed before executing the playbook.
To run the playbook, use the following command:
ansible-playbook playbook.yml
Add -i inventory
with your inventory file and playbook.yml
with the path to the Ansible playbook file in case you would run it against a remote server other than the localhost.
- minikube start
-
Kubernetes Ingress addons are additional components or features that can be installed in a Kubernetes cluster to enhance the functionality of the Ingress resource. Ingress is an API object in Kubernetes that manages external access to services within a cluster.
-
Ingress addons provide advanced routing and traffic management capabilities for Kubernetes clusters. They enable features such as load balancing, SSL termination, path-based routing, and more. One popular Ingress addons are:
-
Nginx Ingress Controller: This addon uses the Nginx web server as a reverse proxy to handle incoming traffic and route it to the appropriate services based on the defined rules. It supports various load-balancing algorithms, SSL/TLS termination, and other advanced features.
-
minikube addons enable ingress
- Map custom hostnames to the IP addresses of services running in Minikube. This allows us to access services using user-friendly names in your local environment.
- use http://jenkins.local.com/ in the browser to open jenkins.
- kubectl get pods -n tools
- kubectl exec -n tools -it jenkins-745954bdbf-shsn9 -- cat /var/jenkins_home/secrets/initialAdminPassword
- Plugins extend Jenkins with additional features to support many different needs.
Write http://nexus.local.com/ in the browser to open nexus
- kubectl get pods -n tools
- kubectl exec -it nexus-6b5bd9f4ff-l7tmp -n tools -- cat /nexus-data/admin.password
kubectl get pods -n tools
kubectl exec -it mysql-7cccd7f54d-pd6qn -n dev -- mysql -u root -p
CREATE USER 'sqluser'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'sqluser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
use http://nodejs.local.com/ in the browser to open the app.
This repository was built with ❤️ by:
- Nada Marei - Mahmoud Abdelwahab - Mohamed Sharif