devops project
devops project
# Create VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
vpc_config {
subnet_ids = [aws_subnet.private_subnet.id]
}
}
user_data = <<-EOF
#!/bin/bash
sudo apt update
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
EOF
tags = {
Name = "my-instance-${count.index + 1}"
}
}
provisioner "remote-exec" {
connection {
type = "ssh"
host = aws_instance.my_instances.*.public_ip[count.index]
user = "ubuntu" # Change to your desired username
private_key = file("~/.ssh/your_private_key.pem") # Change to your private
key path
}
inline = [
"echo 'YOUR_PUBLIC_KEY' >> ~/.ssh/authorized_keys" # Change to your public
key
]
}
}
Make sure to replace placeholders like region, availability zone, AMI, IAM role ARNs,
and public/private key paths with your actual configurations. Additionally, ensure
that you have the necessary IAM roles and policies attached to your AWS account for
EKS cluster creation and EC2 instance provisioning.
Setting up Infrastructure Using IAC |
Part-2
In this document, we'll cover the step-by-step process to create and configure
infrastructure using Infrastructure as Code (IAC) tools such as Ansible to configure
VMs to install Jenkins, SonarQube Server, Nexus, and set up a Jenkins slave.
1. Jenkins Installation
---
- name: Install Jenkins
hosts: jenkins_vm
become: true
tasks:
- name: Add Jenkins Repository
apt_repository:
repo: deb https://pkg.jenkins.io/debian-stable binary/
state: present
update_cache: yes
tasks:
- name: Install OpenJDK 11
apt:
name: openjdk-11-jdk
state: present
tasks:
- name: Install OpenJDK 8
apt:
name: openjdk-8-jdk
state: present
tasks:
- name: Install Java
apt:
name: default-jre
state: present
Overview:
• A private Git repository provides a secure and centralized location to store and
manage your application source code.
Steps:
1. Choose a Git Hosting Service: Select a Git hosting service provider such as
GitHub, GitLab, or Bitbucket.
2. Create a New Repository: Log in to your chosen Git hosting service and
create a new repository.
3. Set Repository Visibility: Ensure that the repository is set to private to restrict
access to authorized users only.
Overview:
• Configuring permissions and roles ensures that only authorized users have access to
the repository and defines their level of access.
Steps:
1. Define Access Control Policies: Determine who should have access to the
repository and what actions they can perform (e.g., read, write, admin).
Overview:
• Pushing the application source code to the Git repository makes it accessible to the
development team and enables version control.
Steps:
1. Clone the Repository: Clone the empty repository to your local machine
using the Git command-line interface or a Git client.
2. Add Application Source Code: Add your application source code files to the
local repository directory.
3. Commit Changes: Commit the added files to the local repository with a
descriptive commit message.
4. Push Changes to Remote Repository: Push the committed changes from the
local repository to the remote repository on the Git hosting service.
Overview:
• Branching in Git allows for parallel development, experimentation, and isolation of
changes. Creating the required branches sets up the foundation for managing
different stages of development.
Steps:
1. Create Master Branch: The master branch serves as the main branch and
typically represents the production-ready version of the code.
3. Create Feature Branches: Create feature branches for each new feature or
bug fix. Feature branches allow developers to work on changes independently.
By following these steps, you can establish a robust source code management
system that ensures secure storage, controlled access, version control, and effective
collaboration within your development team. This lays the foundation for successful
software development and delivery in the DevOps environment.
Creating CI/CD Pipelines with Jenkins
In this document, we'll outline the steps to create CI/CD pipelines with Jenkins,
including configuring Jenkins with necessary plugins and tools, setting up webhooks,
and defining stages in the pipeline.
Plugins Installation
• Install Jenkins on your server.
• Navigate to the Jenkins dashboard and go to "Manage Jenkins" > "Manage Plugins".
• Install the following plugins:
o Kubernetes Plugin: Enables Jenkins to dynamically provision Kubernetes
clusters as build agents.
o SonarQube Scanner Plugin: Integrates Jenkins with SonarQube for code
analysis.
o Nexus Platform Plugin: Provides integration with Nexus Repository Manager
for artifact management.
o Docker Pipeline Plugin: Allows Jenkins to build, tag, and push Docker images.
o OWASP ZAP Plugin: Integrates OWASP ZAP for security scanning.
o Pipeline Plugin: Enables Jenkins to define pipelines using Jenkinsfile.
o Trivy Plugin: Integrates Trivy for filesystem and Docker image vulnerability
scanning.
o etc.
Tools Configuration
• Configure Jenkins to connect to Kubernetes cluster:
• Configure SonarQube:
• Configure Nexus:
o Go to "Manage Jenkins" > "Configure System".
o Add Nexus repository manager configuration and credentials.
Webhook Setup
• Set up a webhook in your Git repository to trigger Jenkins pipeline upon commits to
the master branch.
Pipeline Definition
• Create a Jenkinsfile in your project repository with the following stages:
i. Compile: Compile the application code.
ii. Test: Run unit and integration tests.
iii. Filesystem Scan with Trivy: Perform filesystem scanning using Trivy.
iv. SonarQube Analysis: Run SonarQube analysis for code quality checks.
v. Dependency Check: Check for any vulnerabilities in dependencies.
vi. Build Application: Build the application.
vii. Publish Application Artifact on Nexus: Publish the built artifact to Nexus
repository.
viii. Build & Tag Docker Image: Build and tag Docker image.
ix. Scan Docker Image Using Trivy: Scan Docker image for vulnerabilities using
Trivy.
x. Push Docker Image to Docker Hub Repo: Push Docker image to Docker Hub
repository.
xi. Update Docker Image in YAML Manifest Files: Update Kubernetes YAML
manifest files with the new Docker image.
xii. Deploy the Application to Kubernetes: Deploy the application to Kubernetes
cluster.
xiii. Sleep for 60 sec: Pause pipeline execution for 60 seconds.
xiv. Verify Deployment is Done: Check if the deployment is successful.
xv. Run OWASP ZAP Scan: Perform security scanning using OWASP ZAP.
Sample Pipeline
pipeline {
agent any
environment {
// Define environment variables
NEXUS_URL = 'https://nexus.example.com'
DOCKER_REGISTRY = 'docker.io/yourusername'
IMAGE_NAME = 'yourimage'
K8S_MANIFEST = 'path/to/kubernetes/manifest.yaml'
}
stages {
stage('Compile & Test') {
steps {
// Execute compilation and testing commands
sh 'mvn compile'
sh 'mvn test'
}
}
stage('SonarQube Analysis') {
steps {
// Execute SonarQube analysis
withSonarQubeEnv('SonarQube') {
sh 'mvn sonar:sonar'
}
}
}
stage('Dependency Check') {
steps {
// Execute dependency check
sh 'mvn dependency:check'
}
}
stage('Verify Deployment') {
steps {
// Sleep for 60 seconds and verify deployment
sleep time: 60, unit: 'SECONDS'
sh 'kubectl get pods'
}
}
post {
always {
// Clean up or perform post-build actions
}
}
}
Conclusion
By following the steps outlined above, you can set up a robust CI/CD pipeline with
Jenkins, integrating various tools and plugins to automate the software delivery
process. This pipeline enables efficient testing, code analysis, artifact management,
Docker image creation, deployment to Kubernetes, and security scanning, ensuring
the quality and security of your application throughout the development lifecycle.
Managing Security on EKS, Jenkins
Server, Sonar Server, Nexus Server
Security is a critical aspect of any DevOps setup. In this document, we'll outline how
to manage security on various components of your DevOps environment, including
Elastic Kubernetes Service (EKS), Jenkins server, SonarQube server, and Nexus server.
1. EKS Security
Network Security
• Utilize network policies to control traffic between pods within the cluster.
• Implement security groups and network ACLs to restrict access to EKS API server.
Secrets Management
• Use Kubernetes secrets to store sensitive information like passwords and API keys.
• Encrypt secrets at rest using Kubernetes native encryption mechanisms.
Data Protection
• Encrypt sensitive data stored in the SonarQube database and file system.
• Implement regular database backups to prevent data loss.
Vulnerability Scanning
• Periodically scan SonarQube server for vulnerabilities using security scanning tools.
• Apply security patches and updates promptly to mitigate known vulnerabilities.
Data Protection
• Encrypt sensitive data stored in Nexus repositories.
• Implement backup and recovery procedures to protect against data loss.
Conclusion
By implementing robust security measures on EKS, Jenkins server, SonarQube server,
and Nexus server, you can mitigate security risks and ensure the integrity and
confidentiality of your DevOps environment. Regular monitoring, updates, and audits
are essential to maintaining a secure infrastructure and protecting against emerging
threats.
Monitoring Setup with Grafana
Monitoring is a critical aspect of any DevOps setup as it provides insights into the
performance and health of deployed applications and infrastructure. Grafana is a
popular open-source monitoring and visualization tool that allows you to create
customizable dashboards for real-time monitoring. In this section, we'll discuss how
to set up Grafana and configure it to monitor the deployed application and worker
node usage.
Setting Up Grafana
Conclusion
By following these steps, you can set up Grafana to effectively monitor your
deployed application and worker node usage in your EKS cluster. Grafana's flexibility
and visualization capabilities enable you to create comprehensive dashboards
tailored to your monitoring needs, helping you identify issues quickly and ensure the
smooth operation of your DevOps environment.