Utimate CI/CD Pipeline
Phase 1: Initial Set UP and Deployment
Step 1: Launch EC2 (Ubuntu 22.04):
Step 2: Clone the Code:
git clone https://github.com/inderharrysingh/ultimate-devops.git
Step 3: Install Docker and Run the App Using a Container:
It will show an error cause you need API key
Step 4: Get the API Key:
Now recreate the Docker image with your api key:
docker build --build-arg TMDB_V3_API_KEY=<your-api-key> -t netflix .
Phase 2: Security
Installing SonarQube and Trivy
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community
sudo apt-get install
wget apt-transport-https gnupg lsb-release wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add - echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update sudo apt-get install trivy
To scan image using trivy
trivy image <imageid>
Integrate SonarQube and Configure:
Phase 3: CI/CD Setup
# Update package list
sudo apt update
# Install required packages
sudo apt install -y fontconfig openjdk-17-jre
# Check Java version
java -version
# Download Jenkins GPG key
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
# Add Jenkins repository
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
# Update package list with Jenkins repository
sudo apt-get update
# Install Jenkins
sudo apt-get install -y jenkins
# Start Jenkins service
sudo systemctl start jenkins
# Enable Jenkins service to start on boot
sudo systemctl enable jenkins
Install Necessary Plugins in Jenkins:
Goto Manage Jenkins →Plugins → Available Plugins
Install below plugins
2. SonarQube Scanner (Install without restart)
3. NodeJs Plugin (Install Without restart)
4. Email Extension Plugin
Goto Manage Jenkins → Tools → Install JDK(17) and NodeJs(16)→ Click on Apply and Save
Create the token
2. After adding sonar token
3. Click on Apply and Save
The Configure System option is used in Jenkins to configure different server
Global Tool Configuration is used to configure different tools that we install using Plugins
We will install a sonar scanner in the tools.
Create a Jenkins webhook
Certainly, here are the instructions without step numbers:
Install Dependency-Check and Docker Tools in Jenkin
Install Dependency-Check Plugin:
Configure Dependency-Check Tool:
Install Docker Tools and Docker Plugins:
Add DockerHub Credentials:
Now, you have installed the Dependency-Check plugin, configured the tool, and added Docker-related plugins along with your DockerHub credentials in Jenkins. You can now proceed with configuring your Jenkins pipeline to include these tools and credentials in your CI/CD process.
pipeline {
agent any
tools {
jdk 'nodejs'
}
environment {
SCANNER_HOME = tool
}
stages {
stage() {
steps {
cleanWs()
}
}
stage() {
steps {
git branch: '', url: 'https://github.com/inderharrysingh/ultimate-devops.git'
}
}
stage() {
steps {
withSonarQubeEnv() {
sh '''
$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Netflix \
-Dsonar.projectKey=Netflix
'''
}
}
}
stage() {
steps {
script {
waitForQualityGate abortPipeline: false, credentialsId: ''
}
}
}
stage() {
steps {
sh ''
}
}
stage() {
steps {
dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit',
odcInstallation: 'dependencyCheckPublisher',
pattern: '**/dependency-check-report.xml'
}
}
stage() {
steps {
sh ''
}
}
stage() {
steps {
script {
withDockerRegistry(credentialsId: '', toolName: '') {
sh '''
docker build --build-arg TMDB_V3_API_KEY=<yourapikey> -t netflix .
docker tag netflix inderharrysingh/netflix:latest
docker push nasi101/netflix:latest
'''
}
}
}
}
stage() {
steps {
sh 'trivy image inderharry/netflix:latest > trivyimage.txt'
}
}
stage() {
steps {
sh 'docker run -d --name netflix -p 8081:80 inderharrysingh/netflix:latest'
}
}
}
}
Phase 4 Monitoring
sudo useradd --system --no-create-home --shell /bin/false prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.47.1/prometheus-2.47.1.linux-amd64.tar.gz
5. Extract Prometheus files, move them, and create directories:
tar -xvf prometheus-2.47.1.linux-amd64.tar.gz
cd prometheus-2.47.1.linux-amd64/
sudo mkdir -p /data /etc/prometheus
sudo mv prometheus promtool /usr/local/bin/
sudo mv consoles/ console_libraries/ /etc/prometheus/
sudo mv prometheus.yml /etc/prometheus/prometheus.yml
7. Set ownership for directories:
sudo chown -R prometheus:prometheus /etc/prometheus/ /data/
8. Create a systemd unit configuration file for Prometheus:
sudo nano /etc/systemd/system/prometheus.service
9. Add the following content to the prometheus.service file:
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/data \
--web.console.templates=/etc/prometheus/consoles \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.listen-address=0.0.0.0:9090 \
--web.enable-lifecycle
[Install]
WantedBy=multi-user.target
Here’s a brief explanation of the key parts in this prometheus.service file:
10. Enable and start Prometheus:
sudo systemctl enable prometheus
11. Verify Prometheus’s status:
sudo systemctl start prometheus
12. You can access Prometheus in a web browser using your server’s IP and port 9090
Installing Node Exporter:
sudo useradd --system --no-create-home --shell /bin/false node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
2. Extract Node Exporter files, move the binary, and clean up:
领英推荐
tar -xvf node_exporter-1.6.1.linux-amd64.tar.gz
sudo mv node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/
rm -rf node_exporter*
3. Create a systemd unit configuration file for Node Exporter:
sudo nano /etc/systemd/system/node_exporter.service
4. Add the following content to the node_exporter.service file:
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
User=node_exporter
Group=node_exporter
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/node_exporter --collector.logind
[Install]
WantedBy=multi-user.target
5. Replace --collector.logind with any additional flags as needed.
6. Enable and start Node Exporter:
sudo systemctl enable node_exporter
Verify the Node Exporter’s status:
sudo systemctl start node_exporter
promtool check config /etc/prometheus/prometheus.yml
curl -X POST https://localhost:9090/-/reload
2. You can access Prometheus targets at https://<your-prometheus-ip>:9090/targets
Grafana
Install Grafana on Ubuntu 22.04 and Set it up to Work with Prometheus
Step 1: Install Dependencies:
First, ensure that all necessary dependencies are installed:
sudo apt-get update
sudo apt-get install -y apt-transport-https software-properties-common
Step 2: Add the GPG Key:
Add the GPG key for Grafana:
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
Step 3: Add Grafana Repository:
Add the repository for Grafana stable releases:
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list
Step 4: Update and Install Grafana:
Update the package list and install Grafana:
sudo apt-get update
sudo apt-get -y install grafana
Step 5: Enable and Start Grafana Service:
To automatically start Grafana after a reboot, enable the service:
sudo systemctl enable grafana-server
Then, start Grafana:
sudo systemctl start grafana-server
Step 6: Check Grafana Status:
Verify the status of the Grafana service to ensure it’s running correctly:
sudo systemctl status grafana-server
Step 7: Access Grafana Web Interface:
Open a web browser and navigate to Grafana using your server’s IP address. The default port for Grafana is 3000. For example:
You’ll be prompted to log in to Grafana. The default username is “admin,” and the default password is also “admin.”
Step 8: Change the Default Password:
When you log in for the first time, Grafana will prompt you to change the default password for security reasons. Follow the prompts to set a new password.
Step 9: Add Prometheus Data Source:
To visualize metrics, you need to add a data source. Follow these steps:
Step 10: Import a Dashboard:
To make it easier to view metrics, you can import a pre-configured dashboard. Follow these steps:
You should now have a Grafana dashboard set up to visualize metrics from Prometheus.
Grafana is a powerful tool for creating visualizations and dashboards, and you can further customize it to suit your specific monitoring needs.
That’s it! You’ve successfully installed and set up Grafana to work with Prometheus for monitoring and visualization.
Phase 6: Kubernetes
In this phase, you’ll set up a Kubernetes cluster with node groups. This will provide a scalable environment to deploy and manage your applications.
Prometheus is a powerful monitoring and alerting toolkit, and you’ll use it to monitor your Kubernetes cluster. Additionally, you’ll install the node exporter using Helm to collect metrics from your cluster nodes.
To begin monitoring your Kubernetes cluster, you’ll install the Prometheus Node Exporter. This component allows you to collect system-level metrics from your cluster nodes. Here are the steps to install the Node Exporter using Helm:
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
2. Create a Kubernetes namespace for the Node Exporter:
kubectl create namespace prometheus-node-exporter
3. Install the Node Exporter using Helm:
helm install prometheus-node-exporter prometheus-community/prometheus-node-exporter - namespace prometheus-node-exporter
4. Add a Job to Scrape Metrics on nodeip:9001/metrics in prometheus.yml:
Update your Prometheus configuration (prometheus.yml) to add a new job for scraping metrics from nodeip:9001/metrics. You can do this by adding the following configuration to your prometheus.yml file:
- job_name: 'Netflix'
metrics_path: '/metrics'
static_configs:
- targets: ['node1Ip:9100']
Replace ‘your-job-name’ with a descriptive name for your job. The static_configs section specifies the targets to scrape metrics from, and in this case, it’s set to nodeip:9001.
Don’t forget to reload or restart Prometheus to apply these changes to your configuration.
To deploy an application with ArgoCD, you can follow these steps, which I’ll outline in Markdown format:
After installing ArgoCD, you need to set up your GitHub repository as a source for your application deployment. This typically involves configuring the connection to your repository and defining the source for your ArgoCD application. The specific steps will depend on your setup and requirements.
4. Create an ArgoCD Application:
Phase 7: Cleanup
Cleanup AWS EC2 Instances:
Terminate AWS EC2 instances that are no longer needed.