-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallation Guide
More file actions
198 lines (148 loc) · 4.53 KB
/
Copy pathInstallation Guide
File metadata and controls
198 lines (148 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Installation Guide: Automated CI/CD Pipeline for AWS ECS
Prerequisites
Before setting up the CI/CD pipeline, ensure you have the following tools and accounts:
- **AWS Account** with necessary IAM permissions
- **EC2 Instance** (Ubuntu) with Jenkins installed
- **Docker** installed on EC2
- **AWS CLI** configured with credentials
- **GitHub Repository** with application source code
- **AWS ECR (Elastic Container Registry)** for storing Docker images
- **AWS ECS (Elastic Container Service)** cluster set up
- **Application Load Balancer (ALB)** for routing traffic
Step 1: Set Up Jenkins on AWS EC2
1.1 Launch an EC2 Instance
- Choose **Ubuntu 22.04** AMI
- Minimum **t2.medium** instance type (for better performance)
- Enable security group rules:
- Port **22** (SSH)
- Port **8080** (Jenkins UI)
- Port **80, 443** (if needed for web traffic)
1.2 Install Jenkins
```bash
sudo apt update && sudo apt upgrade -y
sudo apt install openjdk-11-jdk -y
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update
sudo apt install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins
```
### 1.3 Retrieve Jenkins Admin Password
```bash
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```
- Access **Jenkins UI**: `http://<EC2-Public-IP>:8080`
- Complete the setup by installing recommended plugins
## Step 2: Configure AWS CLI on Jenkins Server
### 2.1 Install AWS CLI
```bash
sudo apt install awscli -y
```
### 2.2 Configure AWS Credentials
```bash
aws configure
```
Provide:
- AWS Access Key ID
- AWS Secret Access Key
- Default region (e.g., `us-east-1`)
Step 3: Install & Configure Docker on EC2
```bash
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker jenkins
```
Restart Jenkins to apply changes:
```bash
sudo systemctl restart jenkins
```
Step 4: Set Up AWS ECR for Docker Image Storage
4.1 Create ECR Repository
```bash
aws ecr create-repository --repository-name my-app-repo
```
Copy the repository URI for later use.
4.2 Authenticate Docker with AWS ECR
```bash
aws ecr get-login-password | docker login --username AWS --password-stdin <ECR-URI>
```
Step 5: Set Up CI/CD Pipeline in Jenkins
5.1 Install Jenkins Plugins
- Pipeline
- GitHub Integration
- Docker Pipeline
- Amazon EC2 Plugin
5.2 Create a New Pipeline Job
- Go to Jenkins Dashboard > New Item
- Select Pipeline, name it `MyApp-CI-CD`
- Under Pipeline Script, use the following Jenkinsfile:
```groovy
pipeline {
agent any
environment {
AWS_REGION = 'us-east-1'
ECR_REPO_URI = '<ECR-URI>'
IMAGE_TAG = 'latest'
}
stages {
stage('Checkout Code') {
steps {
git 'https://github.com/your-repo.git'
}
}
stage('Build Docker Image') {
steps {
sh 'docker build -t $ECR_REPO_URI:$IMAGE_TAG .'
}
}
stage('Push Image to ECR') {
steps {
sh 'aws ecr get-login-password | docker login --username AWS --password-stdin $ECR_REPO_URI'
sh 'docker push $ECR_REPO_URI:$IMAGE_TAG'
}
}
stage('Deploy to ECS') {
steps {
sh 'aws ecs update-service --cluster my-cluster --service my-service --force-new-deployment'
}
}
}
}
```
Step 6: Deploy Microservice to AWS ECS
6.1 Create an ECS Cluster
```bash
aws ecs create-cluster --cluster-name my-cluster
```
6.2 Register a Task Definition
Create a JSON file (`task-definition.json`):
```json
{
"family": "my-task",
"containerDefinitions": [
{
"name": "my-container",
"image": "<ECR-URI>:latest",
"memory": 512,
"cpu": 256,
"essential": true
}
]
}
```
Register the task:
```bash
aws ecs register-task-definition --cli-input-json file://task-definition.json
```
6.3 Create a Service & Deploy
```bash
aws ecs create-service --cluster my-cluster --service-name my-service --task-definition my-task --desired-count 2 --launch-type FARGATE
```
Step 7: Verify Deployment
- Go to AWS ECS Console > Clusters > my-cluster
- Check running services & tasks
- Use ALB DNS Name to access the deployed service
---
Congratulations! Your CI/CD Pipeline is Live 🚀 Now, every code push to GitHub will trigger the pipeline, build a new Docker image, push it to ECR, and deploy it to ECS automatically! 🎉