````markdown name=installing-jenkins-on-aws-ec2.md
In this guide, we'll go through the detailed steps to set up Jenkins on an AWS EC2 instance, install necessary plugins, and configure Jenkins for deploying applications to a Kubernetes cluster.
1️⃣ Install Jenkins on AWS EC2
Step 1: Update Your System
First, let's make sure your system is up to date. Run the following command to update your package list and upgrade your installed packages:
sh
sudo apt update && sudo apt upgrade -y
✅ This ensures your system is up-to-date.
Step 2: Install Java (Required for Jenkins)
Jenkins requires Java to run. Install OpenJDK 11 by running:
sh
sudo apt install openjdk-11-jdk -y
Verify the Java installation:
sh
java -version
✅ Expected output:
plaintext
openjdk version "11.0.xx"
Step 3: Install Jenkins
Add the Jenkins repository:
sh
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /etc/apt/trusted.gpg.d/jenkins.asc > /dev/null
Add the Jenkins package source:
sh
echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
Update the package list and install Jenkins:
sh
sudo apt update
sudo apt install jenkins -y
Step 4: Start and Enable Jenkins
Start the Jenkins service:
sh
sudo systemctl start jenkins
Enable Jenkins to start on boot:
sh
sudo systemctl enable jenkins
Check the status of Jenkins:
sh
sudo systemctl status jenkins
✅ You should see "active (running)".
Step 5: Access Jenkins Dashboard
Find your EC2 public IP:
sh
curl ifconfig.me
Now, open your browser and go to:
plaintext
http://<your-ec2-public-ip>:8080
Jenkins should be running! 🎉
Step 6: Unlock Jenkins
Find the admin password:
sh
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy and paste it into the Jenkins setup page. Click "Install suggested plugins" and create an admin user.
✅ Now you have Jenkins running! 🎯
2️⃣ Install Kubernetes and Docker Plugins in Jenkins
Jenkins needs plugins to communicate with Kubernetes.
Step 1: Install Docker & Kubernetes Plugins
Go to Jenkins Dashboard → Manage Jenkins → Manage Plugins. Search for and install the following plugins:
- Docker
- Kubernetes
- Pipeline
- GitHub
After installing the plugins, restart Jenkins.
3️⃣ Configure Jenkins to Deploy to Kubernetes
Jenkins will automate deployments to your Kubernetes cluster.
Step 1: Create a Service Account in Kubernetes
On your EC2 instance, run:
sh
kubectl create serviceaccount jenkins
kubectl create clusterrolebinding jenkins --clusterrole=cluster-admin --serviceaccount=default:jenkins
Step 2: Get Kubernetes API Token
Run the following command to get the Kubernetes API token:
sh
kubectl get secret $(kubectl get serviceaccount jenkins -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decode
Copy this token.
Step 3: Configure Jenkins Credentials
Go to Jenkins Dashboard → Manage Jenkins → Manage Credentials. Under Global Credentials, add:
- Type: Kubernetes Configuration (Secret Text)
- Secret: Paste the token from Step 2
- ID: k8s-api-token
4️⃣ Create a Jenkins Pipeline to Deploy to Kubernetes
Go to Jenkins Dashboard → New Item → Pipeline. Name it K8s-Deployment
and choose Pipeline.
In the Pipeline Script section, paste the following script:
groovy
pipeline {
agent any
environment {
KUBECONFIG = credentials('k8s-api-token')
}
stages {
stage('Checkout Code') {
steps {
git 'https://github.com/YOUR_GITHUB_REPO.git' // Replace with your repo
}
}
stage('Build Docker Image') {
steps {
sh 'docker build -t myapp:latest .'
}
}
stage('Push Image to Docker Hub') {
steps {
withDockerRegistry([credentialsId: 'docker-hub', url: 'https://index.docker.io/v1/']) {
sh 'docker tag myapp:latest YOUR_DOCKER_USERNAME/myapp:latest'
sh 'docker push YOUR_DOCKER_USERNAME/myapp:latest'
}
}
}
stage('Deploy to Kubernetes') {
steps {
sh 'kubectl apply -f deployment.yaml'
}
}
}
}
✅ Replace:
-
YOUR_GITHUB_REPO
with your GitHub repository -
YOUR_DOCKER_USERNAME
with your Docker Hub username
5️⃣ Create Kubernetes Deployment Manifest
Create a file named deployment.yaml
:
sh
nano deployment.yaml
Paste the following content:
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: YOUR_DOCKER_USERNAME/myapp:latest
ports:
- containerPort: 80
✅ Replace YOUR_DOCKER_USERNAME/myapp:latest
with your Docker Hub image.
Apply the deployment:
sh
kubectl apply -f deployment.yaml
✅ Your app is now running on Kubernetes! 🎉
🎯 Final Test
Run the following commands to verify the deployment:
sh
kubectl get pods
kubectl get deployments
✅ You should see your myapp
deployment running.
Congratulations! You've successfully set up Jenkins on AWS EC2 and configured it to deploy to a Kubernetes cluster. Happy CI/CD! 🚀
`
Top comments (0)